Files
old-ribica/front-api/controllers/cart.rb

146 lines
4.2 KiB
Ruby
Raw Normal View History

create_the_cart = -> () {
# -1 is a placeholder for user id when we implement users
# auid will still be used in case user is not logged in
Cart.find_or_create(anonymous_id, logged_in_user_id).to_json
}
post '/cart', &create_the_cart
put '/cart', &create_the_cart
get '/cart' do
Cart.just_find(anonymous_id, logged_in_user_id).to_json
end
# gets number of items in cart for every item
get '/cart/item' do
Cart.just_find(anonymous_id, logged_in_user_id).item_in_carts.to_json
end
update_cart_item = ->() {
cart_id = Cart.just_find(anonymous_id, logged_in_user_id).id
item_id = @json_params["item_id"].to_i
count = @json_params["count"].to_i
ItemInCart.update_state(cart_id, item_id, count).to_json
}
put '/cart/item', &update_cart_item
post '/cart/item', &update_cart_item
# gets list of items in cart without count
get '/cart/item/display' do
cart = Cart.just_find(anonymous_id, logged_in_user_id)
item_ids = cart.item_in_carts.map do |x|
x.item_id
end
items = []
items = Item.find(item_ids) if cart.item_in_carts.length > 0
prepare_items_for_mass_display(items)
2015-02-26 06:48:34 +01:00
end
get '/cart/delivery_destination' do
cart = Cart.just_find(anonymous_id, logged_in_user_id)
2015-02-26 06:48:34 +01:00
cart.delivery_destination.to_json(:except => [:created_at, :email_verification_code, :phone_verification_code])
end
2015-02-26 06:48:34 +01:00
update_delivery_destination = ->() {
cart = Cart.just_find(anonymous_id, logged_in_user_id)
2016-01-12 13:26:43 +01:00
allowed_keys = ["name", "address", "place", "postal_code", "phone", "email", "note", "payment_method", "gift",
"recipient_name", "recipient_address", "recipient_place", "recipient_postal_code", "recipient_phone", "recipient_email"]
2015-02-26 06:48:34 +01:00
params = @json_params.reject { |key,_| !allowed_keys.include?(key) }
cart.delivery_destination.update_attributes(params)
cart.delivery_destination.save!
cart.delivery_destination.to_json(:except => [:created_at, :email_verification_code, :phone_verification_code])
2015-02-26 06:48:34 +01:00
}
put '/cart/delivery_destination', &update_delivery_destination
post '/cart/delivery_destination', &update_delivery_destination
2015-09-26 11:58:01 +02:00
def report_to_trello(cart)
2016-01-06 08:40:27 +01:00
Thread.new do
2015-09-26 11:58:01 +02:00
@cart = cart
2015-10-31 08:24:44 +01:00
board = Trello::Board.find(RibicaConfig.TRELLO_BOARD_NAME)
2015-09-26 11:58:01 +02:00
list = board.lists.first
card = Trello::Card.new
card.list_id = list.id
card.name = cart.title
card.pos = "bottom"
card.desc = erb(:cart_trello, :layout => false)
card.save
end
end
2015-09-21 06:31:52 +02:00
2015-09-26 11:58:01 +02:00
def send_order_email(cart)
2016-01-06 08:40:27 +01:00
Thread.new do
2015-09-26 11:58:01 +02:00
client = SendGrid::Client.new(
2016-01-06 08:40:27 +01:00
api_user: RibicaConfig.SENDGRID_API_USER,
2015-10-31 08:24:44 +01:00
api_key: RibicaConfig.SENDGRID_API_KEY
2015-09-21 06:31:52 +02:00
)
2015-09-26 11:58:01 +02:00
email = SendGrid::Mail.new do |m|
2015-10-31 08:24:44 +01:00
m.to = RibicaConfig.BACKOFFICE_ORDER_EMAIL_TO
m.from = RibicaConfig.BACKOFFICE_ORDER_EMAIL_FROM
2015-09-26 11:58:01 +02:00
m.from_name = "Prodavnica Ribica"
m.subject = "Nova Narudžba: #{cart.id}"
2015-10-31 08:24:44 +01:00
m.html = "Mušterija naručila nešto. <br /> Pogledati #{RibicaConfig.ROOT_ADDRESS}/backoffice/carts/#{cart.id}"
2015-09-26 11:58:01 +02:00
end
client.send(email)
2015-09-21 06:31:52 +02:00
end
end
post '/cart/confirmation' do
anonymous = anonymous_id
cart = Cart.just_find(anonymous, logged_in_user_id)
if cart.item_in_carts.length > 0
cart.ordered = true
cart.save!
end
# since there is no more ordered cart this needs to be done
# in order for next call of Cart#just_find to be ready
Cart.find_or_create(anonymous, logged_in_user_id)
2015-09-26 11:58:01 +02:00
report_to_trello(cart)
send_order_email(cart)
2015-03-05 05:24:56 +01:00
"OK".to_json
end
post '/payment/confirmation' do
2016-01-11 08:53:03 +01:00
data = JSON.parse params[:custom]
2016-01-11 08:53:03 +01:00
puts "Data #{data.inspect}"
2016-01-11 08:53:03 +01:00
anonymous = data["anonymous_id_string"]
user = data["user_id"]
user ||= -1
user = user.to_i
cart = Cart.just_find(anonymous, user)
if cart.item_in_carts.length > 0
cart.ordered = true
cart.save!
end
2016-01-11 08:53:03 +01:00
Cart.find_or_create(anonymous, user)
report_to_trello(cart)
send_order_email(cart)
"OK".to_json
end
2016-01-11 08:53:03 +01:00
get '/pikpay/confirmation' do
anonymous = params["anonymous_id_string"]
user = params["user_id"]
user ||= -1
user = user.to_i
2016-01-11 10:52:18 +01:00
2016-01-11 08:53:03 +01:00
cart = Cart.just_find(anonymous, user)
if cart.item_in_carts.length > 0
cart.ordered = true
cart.save!
end
Cart.find_or_create(anonymous, user)
report_to_trello(cart)
send_order_email(cart)
2016-01-13 09:26:28 +01:00
redirect "#{RibicaConfig::ROOT_ADDRESS}/hvala"
2016-01-11 08:53:03 +01:00
end