90 lines
3.0 KiB
Ruby
90 lines
3.0 KiB
Ruby
|
|
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)
|
|
end
|
|
|
|
get '/cart/delivery_destination' do
|
|
cart = Cart.just_find(anonymous_id, logged_in_user_id)
|
|
cart.delivery_destination.to_json(:except => [:created_at, :email_verification_code, :phone_verification_code])
|
|
end
|
|
|
|
update_delivery_destination = ->() {
|
|
cart = Cart.just_find(anonymous_id, logged_in_user_id)
|
|
allowed_keys = ["name", "address", "place", "postal_code", "phone", "email", "note"]
|
|
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])
|
|
}
|
|
put '/cart/delivery_destination', &update_delivery_destination
|
|
post '/cart/delivery_destination', &update_delivery_destination
|
|
|
|
|
|
def send_order_email(cart)
|
|
Pony.options = {
|
|
:subject => "Nova Narudžba: #{cart.id}",
|
|
:html_body => "Mušterija naručila nešto. <br /> Pogledati https://www.ribica.ba/backoffice/carts/#{cart.id}",
|
|
:body => "Mušterija naručila nešto. Pogledati https://www.ribica.ba/backoffice/carts/#{cart.id}",
|
|
:via => :smtp,
|
|
:via_options => {
|
|
:address => 'smtp.gmail.com',
|
|
:port => '587',
|
|
:enable_starttls_auto => true,
|
|
:user_name => 'ribica.ba@gmail.com',
|
|
:password => 'pumparica*pumpa*sumpa*lumpa',
|
|
:authentication => :plain, # :plain, :login, :cram_md5, no auth by default
|
|
:domain => "localhost.localdomain"
|
|
}
|
|
}
|
|
Pony.mail(:to => "narudzbe@ribica.ba")
|
|
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)
|
|
send_order_email(cart)
|
|
"OK".to_json
|
|
end
|