Move tab api to seperate job

This commit is contained in:
benji 2015-09-14 17:29:00 +02:00
parent 02b27233cd
commit 251d3b69b7
8 changed files with 31 additions and 42 deletions

View file

@ -15,7 +15,6 @@ class OrdersController < ApplicationController
flash[:success] = "#{@order.to_sentence} ordered. Enjoy it!"
redirect_to root_path
else
@order.g_order_items Product.for_sale
render 'new'
end
end

View file

@ -9,6 +9,10 @@ module ApplicationHelper
"background-color: \#"+ get_color(user) +";"
end
def euro_from_cents(f)
euro(f / 100.0)
end
def euro(f)
number_to_currency(f, unit: '€')
end

15
app/jobs/tab_api_job.rb Normal file
View file

@ -0,0 +1,15 @@
TabApiJob = Struct.new(:order_id) do
def perform(*args)
order = Order.find_by(id: order_id)
if order && !order.transaction_id
body = { transaction: { debtor: order.user.uid, cents: order.price_cents, message: order.to_sentence, id_at_client: order.id } }
headers = { "Authorization" => "Token token=#{Rails.application.secrets.tab_api_key}" }
result = HTTParty.post("https://zeus.ugent.be/tab/transactions", body: body, headers: headers )
order.update_attribute(:transaction_id, JSON.parse(result.body)["id"].to_i)
end
end
def error(job, exception)
Airbrake.notify(exception)
end
end

View file

@ -20,26 +20,16 @@ class Order < ActiveRecord::Base
has_many :products, through: :order_items
before_validation :calculate_price
after_create :tab_api_created
before_save { |o| o.order_items = o.order_items.reject{ |oi| oi.count == 0 } }
after_create { Delayed::Job.enqueue TabApiJob.new(id) }
default_scope -> { where(cancelled: false) }
validates :user, presence: true
validates :order_items, presence: true, in_stock: true
validates :price_cents, presence: true
accepts_nested_attributes_for :order_items, reject_if: proc { |oi| oi[:count].to_i <= 0 }
def price_cents
self.order_items.map{ |oi| oi.count * oi.product.price_cents }.sum
end
def price
self.price_cents / 100.0
end
def price=(_)
write_attribute(:price_cents, price_cents)
end
accepts_nested_attributes_for :order_items
def cancel
return false if cancelled || created_at < 5.minutes.ago
@ -59,35 +49,13 @@ class Order < ActiveRecord::Base
def g_order_items(products)
products.each do |p|
if (oi = self.order_items.select { |t| t.product == p }).size > 0
oi.first.count = [oi.first.product.stock, oi.first.count].min
else
self.order_items.build(product: p)
end
self.order_items.build(product: p)
end
end
private
def calculate_price
self.price_cents = price_cents
end
def tab_api_created
body = { transaction: { debtor: user.uid, cents: price_cents, message: to_sentence } }
tab_api body
end
handle_asynchronously :tab_api_created
def tab_api_cancelled
body = { transaction: { creditor: user.uid, cents: price_cents, message: "Order cancelled" } }
tab_api body
end
handle_asynchronously :tab_api_cancelled
def tab_api body
headers = { "Authorization" => "Token token=#{Rails.application.secrets.tab_api_key}" }
result = HTTParty.post("https://zeus.ugent.be/tab/transactions", body: body, headers: headers )
update_attribute(:transaction_id, JSON.parse(result.body)["id"])
self.price_cents = self.order_items.map{ |oi| oi.count * oi.product.price_cents }.sum
end
end

View file

@ -13,7 +13,7 @@ class OrderItem < ActiveRecord::Base
belongs_to :product
validates :product, presence: true
validates :count, presence: true, numericality: { only_integer: true, greater_than: 0 }
validates :count, presence: true, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
after_create :remove_from_stock

View file

@ -8,6 +8,7 @@
<%= order.to_sentence %>
</td>
<td>
<%= euro(order.price) %>
<% p order %>
<%= euro_from_cents(order.price_cents) %>
</td>
</tr>

View file

@ -0,0 +1,2 @@
Delayed::Worker.destroy_failed_jobs = false
Delayed::Worker.logger = Logger.new(File.join(Rails.root, 'log', 'delayed_job.log'))

View file

@ -15,7 +15,7 @@ development:
omniauth_client_id: tomtest
omniauth_client_secret: blargh
access_token: "token"
tab_api_key: "LNJxGqkM39O21gcJJq6BLQ=="
tab_api_key: "6GNUM/BhsU3If20qXluPIA=="
test:
secret_key_base: 961437e28e7d6055ffaad9cf1f8d614354f57f10cb2d7601c9d6ede72a03b9c9535ad9e63507e3eb31252c4895970a63117493408f2e9a46c7a0c4a5a7836b81