cart logic now ties cart with user when cart is created with logged in user
This commit is contained in:
@@ -1,36 +1,23 @@
|
||||
|
||||
|
||||
helpers do
|
||||
def anonymous_id
|
||||
auid = cookies[:anonymous_user_id]
|
||||
if auid.nil?
|
||||
auid = AnonymousUser.uid
|
||||
response.set_cookie('anonymous_user_id', :path=> '/', :httponly => true, :value=>auid, :expires=> Time.now + 100.year)
|
||||
end
|
||||
return auid
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
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, -1).to_json
|
||||
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, -1).to_json
|
||||
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, -1).item_in_carts.to_json
|
||||
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, -1).id
|
||||
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
|
||||
@@ -41,7 +28,7 @@ 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, -1)
|
||||
cart = Cart.just_find(anonymous_id, logged_in_user_id)
|
||||
item_ids = cart.item_in_carts.map do |x|
|
||||
x.item_id
|
||||
end
|
||||
@@ -51,12 +38,12 @@ get '/cart/item/display' do
|
||||
end
|
||||
|
||||
get '/cart/delivery_destination' do
|
||||
cart = Cart.just_find(anonymous_id, -1)
|
||||
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, -1)
|
||||
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)
|
||||
@@ -69,13 +56,13 @@ post '/cart/delivery_destination', &update_delivery_destination
|
||||
|
||||
post '/cart/confirmation' do
|
||||
anonymous = anonymous_id
|
||||
cart = Cart.just_find(anonymous, -1)
|
||||
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, -1)
|
||||
Cart.find_or_create(anonymous, logged_in_user_id)
|
||||
"OK".to_json
|
||||
end
|
||||
@@ -1,4 +1,22 @@
|
||||
|
||||
helpers do
|
||||
def logged_in_user_id
|
||||
auth = cookies['ribica_auth']
|
||||
auth ||= -1
|
||||
return auth.to_i
|
||||
end
|
||||
|
||||
def anonymous_id
|
||||
auid = cookies[:anonymous_user_id]
|
||||
if auid.nil?
|
||||
auid = AnonymousUser.uid
|
||||
response.set_cookie('anonymous_user_id', :path=> '/', :httponly => true, :value=>auid, :expires=> Time.now + 100.year)
|
||||
end
|
||||
return auid
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
post '/user/logout' do
|
||||
response.delete_cookie("ribica_auth", :path => "/")
|
||||
end
|
||||
|
||||
@@ -44,6 +44,8 @@ var CheckoutPage = React.createClass({
|
||||
<div className="col-md-4">
|
||||
<RibicaFormError componentName="place" errorMessagesObject={this.state.deliveryDestinationErrors} />
|
||||
<select id="place" name="place" className="form-control" value={this.state.deliveryDestination.get('place')} onChange={this._onFieldChange} >
|
||||
<option value="-12">Izaberite mjesto</option>
|
||||
<option value="-13">-------------------------------</option>
|
||||
<option value=" 71000">Sarajevo</option>
|
||||
<option value=" 71103">Sarajevo, Centar</option>
|
||||
<option value=" 71160">Sarajevo, Novi Grad</option>
|
||||
@@ -58,7 +60,6 @@ var CheckoutPage = React.createClass({
|
||||
<option value=" 88000">Mostar, Sjever</option>
|
||||
<option value=" 88000">Mostar, Zapad</option>
|
||||
<option value="-1">-------------------------------</option>
|
||||
|
||||
<option value="78255">Aleksandrovac</option>
|
||||
<option value="89245">Avtovac</option>
|
||||
<option value="76310">Balatun</option>
|
||||
|
||||
Reference in New Issue
Block a user