Move tab api to seperate job
This commit is contained in:
parent
02b27233cd
commit
251d3b69b7
|
@ -15,7 +15,6 @@ class OrdersController < ApplicationController
|
||||||
flash[:success] = "#{@order.to_sentence} ordered. Enjoy it!"
|
flash[:success] = "#{@order.to_sentence} ordered. Enjoy it!"
|
||||||
redirect_to root_path
|
redirect_to root_path
|
||||||
else
|
else
|
||||||
@order.g_order_items Product.for_sale
|
|
||||||
render 'new'
|
render 'new'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -9,6 +9,10 @@ module ApplicationHelper
|
||||||
"background-color: \#"+ get_color(user) +";"
|
"background-color: \#"+ get_color(user) +";"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def euro_from_cents(f)
|
||||||
|
euro(f / 100.0)
|
||||||
|
end
|
||||||
|
|
||||||
def euro(f)
|
def euro(f)
|
||||||
number_to_currency(f, unit: '€')
|
number_to_currency(f, unit: '€')
|
||||||
end
|
end
|
||||||
|
|
15
app/jobs/tab_api_job.rb
Normal file
15
app/jobs/tab_api_job.rb
Normal 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
|
|
@ -20,26 +20,16 @@ class Order < ActiveRecord::Base
|
||||||
has_many :products, through: :order_items
|
has_many :products, through: :order_items
|
||||||
|
|
||||||
before_validation :calculate_price
|
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) }
|
default_scope -> { where(cancelled: false) }
|
||||||
|
|
||||||
validates :user, presence: true
|
validates :user, presence: true
|
||||||
validates :order_items, presence: true, in_stock: 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 }
|
accepts_nested_attributes_for :order_items
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
def cancel
|
def cancel
|
||||||
return false if cancelled || created_at < 5.minutes.ago
|
return false if cancelled || created_at < 5.minutes.ago
|
||||||
|
@ -59,35 +49,13 @@ class Order < ActiveRecord::Base
|
||||||
|
|
||||||
def g_order_items(products)
|
def g_order_items(products)
|
||||||
products.each do |p|
|
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)
|
self.order_items.build(product: p)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def calculate_price
|
def calculate_price
|
||||||
self.price_cents = price_cents
|
self.price_cents = self.order_items.map{ |oi| oi.count * oi.product.price_cents }.sum
|
||||||
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"])
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -13,7 +13,7 @@ class OrderItem < ActiveRecord::Base
|
||||||
belongs_to :product
|
belongs_to :product
|
||||||
|
|
||||||
validates :product, presence: true
|
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
|
after_create :remove_from_stock
|
||||||
|
|
||||||
|
|
|
@ -8,6 +8,7 @@
|
||||||
<%= order.to_sentence %>
|
<%= order.to_sentence %>
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
<%= euro(order.price) %>
|
<% p order %>
|
||||||
|
<%= euro_from_cents(order.price_cents) %>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
|
|
2
config/initializers/delayed_job_config.rb
Normal file
2
config/initializers/delayed_job_config.rb
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
Delayed::Worker.destroy_failed_jobs = false
|
||||||
|
Delayed::Worker.logger = Logger.new(File.join(Rails.root, 'log', 'delayed_job.log'))
|
|
@ -15,7 +15,7 @@ development:
|
||||||
omniauth_client_id: tomtest
|
omniauth_client_id: tomtest
|
||||||
omniauth_client_secret: blargh
|
omniauth_client_secret: blargh
|
||||||
access_token: "token"
|
access_token: "token"
|
||||||
tab_api_key: "LNJxGqkM39O21gcJJq6BLQ=="
|
tab_api_key: "6GNUM/BhsU3If20qXluPIA=="
|
||||||
|
|
||||||
test:
|
test:
|
||||||
secret_key_base: 961437e28e7d6055ffaad9cf1f8d614354f57f10cb2d7601c9d6ede72a03b9c9535ad9e63507e3eb31252c4895970a63117493408f2e9a46c7a0c4a5a7836b81
|
secret_key_base: 961437e28e7d6055ffaad9cf1f8d614354f57f10cb2d7601c9d6ede72a03b9c9535ad9e63507e3eb31252c4895970a63117493408f2e9a46c7a0c4a5a7836b81
|
||||||
|
|
Loading…
Reference in a new issue