Merge branch 'master' of https://github.com/senaduka/ribica
This commit is contained in:
@@ -27,20 +27,20 @@ post '/cart/item', &update_cart_item
|
|||||||
|
|
||||||
|
|
||||||
# gets list of items in cart without count
|
# gets list of items in cart without count
|
||||||
get '/cart/item/display' do
|
get '/cart/item/display' do
|
||||||
cart = Cart.just_find(anonymous_id, logged_in_user_id)
|
cart = Cart.just_find(anonymous_id, logged_in_user_id)
|
||||||
item_ids = cart.item_in_carts.map do |x|
|
item_ids = cart.item_in_carts.map do |x|
|
||||||
x.item_id
|
x.item_id
|
||||||
end
|
end
|
||||||
items = []
|
items = []
|
||||||
items = Item.find(item_ids) if cart.item_in_carts.length > 0
|
items = Item.find(item_ids) if cart.item_in_carts.length > 0
|
||||||
prepare_items_for_mass_display(items)
|
prepare_items_for_mass_display(items)
|
||||||
end
|
end
|
||||||
|
|
||||||
get '/cart/delivery_destination' do
|
get '/cart/delivery_destination' do
|
||||||
cart = Cart.just_find(anonymous_id, logged_in_user_id)
|
cart = Cart.just_find(anonymous_id, logged_in_user_id)
|
||||||
cart.delivery_destination.to_json(:except => [:created_at, :email_verification_code, :phone_verification_code])
|
cart.delivery_destination.to_json(:except => [:created_at, :email_verification_code, :phone_verification_code])
|
||||||
end
|
end
|
||||||
|
|
||||||
update_delivery_destination = ->() {
|
update_delivery_destination = ->() {
|
||||||
cart = Cart.just_find(anonymous_id, logged_in_user_id)
|
cart = Cart.just_find(anonymous_id, logged_in_user_id)
|
||||||
@@ -54,7 +54,7 @@ put '/cart/delivery_destination', &update_delivery_destination
|
|||||||
post '/cart/delivery_destination', &update_delivery_destination
|
post '/cart/delivery_destination', &update_delivery_destination
|
||||||
|
|
||||||
|
|
||||||
post '/cart/confirmation' do
|
post '/cart/confirmation' do
|
||||||
anonymous = anonymous_id
|
anonymous = anonymous_id
|
||||||
cart = Cart.just_find(anonymous, logged_in_user_id)
|
cart = Cart.just_find(anonymous, logged_in_user_id)
|
||||||
if cart.item_in_carts.length > 0
|
if cart.item_in_carts.length > 0
|
||||||
@@ -65,4 +65,4 @@ post '/cart/confirmation' do
|
|||||||
# in order for next call of Cart#just_find to be ready
|
# in order for next call of Cart#just_find to be ready
|
||||||
Cart.find_or_create(anonymous, logged_in_user_id)
|
Cart.find_or_create(anonymous, logged_in_user_id)
|
||||||
"OK".to_json
|
"OK".to_json
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -23,11 +23,11 @@ ActiveRecord::Schema.define(version: 20150319104317) do
|
|||||||
t.datetime "updated_at", null: false
|
t.datetime "updated_at", null: false
|
||||||
t.string "anonymous_id_string"
|
t.string "anonymous_id_string"
|
||||||
t.integer "delivery_destination_id"
|
t.integer "delivery_destination_id"
|
||||||
t.boolean "confirmed", default: false
|
t.boolean "confirmed"
|
||||||
t.boolean "packed", default: false
|
t.boolean "packed"
|
||||||
t.boolean "canceled_on_check", default: false
|
t.boolean "canceled_on_check"
|
||||||
t.boolean "canceled_on_delivery", default: false
|
t.boolean "canceled_on_delivery"
|
||||||
t.boolean "delivered", default: false
|
t.boolean "delivered"
|
||||||
t.text "internal_note"
|
t.text "internal_note"
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -101,6 +101,7 @@ ActiveRecord::Schema.define(version: 20150319104317) do
|
|||||||
t.datetime "updated_at", null: false
|
t.datetime "updated_at", null: false
|
||||||
t.string "tags"
|
t.string "tags"
|
||||||
t.json "traits"
|
t.json "traits"
|
||||||
|
t.integer "supplier_id"
|
||||||
t.decimal "weight", precision: 5, scale: 3
|
t.decimal "weight", precision: 5, scale: 3
|
||||||
t.integer "special_offer_id"
|
t.integer "special_offer_id"
|
||||||
t.integer "supplier_id"
|
t.integer "supplier_id"
|
||||||
|
|||||||
@@ -1,14 +1,50 @@
|
|||||||
class Cart < ActiveRecord::Base
|
class Cart < ActiveRecord::Base
|
||||||
has_many :item_in_carts, -> { order "created_at" }
|
has_many :item_in_carts, -> { order "created_at" }
|
||||||
belongs_to :delivery_destination
|
belongs_to :delivery_destination
|
||||||
|
|
||||||
def self.find_or_create(anonymous_id, user_id)
|
def self.find_or_create(anonymous_id, user_id)
|
||||||
cart = Cart.where(user_id: user_id).where(ordered: false).first
|
cart = nil
|
||||||
cart ||= Cart.where(anonymous_id_string: anonymous_id).where(ordered: false).first
|
ActiveRecord::Base.transaction do
|
||||||
safe_user_id = (user_id > 0) ? user_id : nil
|
|
||||||
cart ||= Cart.create!(anonymous_id_string: anonymous_id, user_id: safe_user_id, ordered: false )
|
logged_in_user_cart = Cart.where(user_id: user_id).where(ordered: false).first
|
||||||
cart.delivery_destination ||= DeliveryDestination.create_from_last(anonymous_id, safe_user_id);
|
anonymous_user_cart = Cart.where(anonymous_id_string: anonymous_id).where(ordered: false).first
|
||||||
cart.save!
|
|
||||||
|
safe_user_id = (user_id > 0) ? user_id : nil
|
||||||
|
if safe_user_id.nil?
|
||||||
|
cart = anonymous_user_cart || Cart.create!(anonymous_id_string: anonymous_id, user_id: safe_user_id, ordered: false )
|
||||||
|
else
|
||||||
|
cart = logged_in_user_cart || Cart.create!(anonymous_id_string: nil, user_id: safe_user_id, ordered: false )
|
||||||
|
logged_in_user_cart = cart
|
||||||
|
end
|
||||||
|
|
||||||
|
# add the contents of anonymous cart
|
||||||
|
# to the logged in one on login
|
||||||
|
if logged_in_user_cart and anonymous_user_cart
|
||||||
|
logged_in_items = logged_in_user_cart.item_in_carts
|
||||||
|
anonymous_user_cart.item_in_carts.each do |a_item|
|
||||||
|
item_exists = false
|
||||||
|
|
||||||
|
logged_in_items.each do |l_item|
|
||||||
|
if a_item.item_id == l_item.item_id
|
||||||
|
l_item.count += a_item.count
|
||||||
|
l_item.save!
|
||||||
|
a_item.destroy
|
||||||
|
item_exists = true
|
||||||
|
break
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
unless item_exists
|
||||||
|
a_item.cart_id = logged_in_user_cart.id
|
||||||
|
a_item.save!
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
cart.delivery_destination ||= DeliveryDestination.create_from_last(anonymous_id, safe_user_id);
|
||||||
|
cart.save!
|
||||||
|
|
||||||
|
end
|
||||||
return cart
|
return cart
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -17,4 +53,4 @@ class Cart < ActiveRecord::Base
|
|||||||
cart ||= Cart.where(anonymous_id_string: anonymous_id).where(ordered: false).first
|
cart ||= Cart.where(anonymous_id_string: anonymous_id).where(ordered: false).first
|
||||||
return cart
|
return cart
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -2,7 +2,8 @@ var AppDispatcher = require('../dispatcher/appDispatcher');
|
|||||||
var EventEmitter = require('events').EventEmitter;
|
var EventEmitter = require('events').EventEmitter;
|
||||||
var UserConstants = require('../constants/userConstants');
|
var UserConstants = require('../constants/userConstants');
|
||||||
var _ = require('underscore');
|
var _ = require('underscore');
|
||||||
var NavigationActions = require('../actions/navigationActions');
|
var CartActions = require('../actions/cartActions');
|
||||||
|
var InitializationActions = require('../actions/initializationActions');
|
||||||
var _registrationState = {};
|
var _registrationState = {};
|
||||||
var _loginState = {};
|
var _loginState = {};
|
||||||
|
|
||||||
@@ -29,6 +30,8 @@ var handleLoginSuccess = function(user) {
|
|||||||
user: user
|
user: user
|
||||||
};
|
};
|
||||||
|
|
||||||
|
refreshCart();
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
var handleLoginFailure = function(error) {
|
var handleLoginFailure = function(error) {
|
||||||
@@ -40,12 +43,12 @@ var handleLoginFailure = function(error) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
var handleCheckLoginArrived = function(user, error) {
|
var handleCheckLoginArrived = function(user, error) {
|
||||||
if(user) {
|
if (user) {
|
||||||
_loginState = {
|
_loginState = {
|
||||||
loggedIn: true,
|
loggedIn: true,
|
||||||
user: user
|
user: user
|
||||||
};
|
};
|
||||||
} else{
|
} else {
|
||||||
_loginState = {
|
_loginState = {
|
||||||
loggedIn: false
|
loggedIn: false
|
||||||
};
|
};
|
||||||
@@ -57,70 +60,85 @@ var handleLogoutDone = function() {
|
|||||||
_loginState = {
|
_loginState = {
|
||||||
loggedIn: false
|
loggedIn: false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
refreshCart();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
var refreshCart = function() {
|
||||||
|
|
||||||
|
setTimeout(function() {
|
||||||
|
// needed for cart reset
|
||||||
|
InitializationActions.initialize();
|
||||||
|
setTimeout(function() {
|
||||||
|
// reload the items
|
||||||
|
CartActions.load();
|
||||||
|
}, 0);
|
||||||
|
}, 0);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
// Extend SectionStore with EventEmitter to add eventing capabilities
|
// Extend SectionStore with EventEmitter to add eventing capabilities
|
||||||
var UserStore = _.extend({}, EventEmitter.prototype, {
|
var UserStore = _.extend({}, EventEmitter.prototype, {
|
||||||
|
|
||||||
getRegistrationState: function() {
|
getRegistrationState: function() {
|
||||||
//return _categoryDetails;
|
//return _categoryDetails;
|
||||||
return _registrationState;
|
return _registrationState;
|
||||||
},
|
},
|
||||||
getLoginState: function() {
|
getLoginState: function() {
|
||||||
return _loginState;
|
return _loginState;
|
||||||
},
|
},
|
||||||
// Emit Change event
|
// Emit Change event
|
||||||
emitChange: function() {
|
emitChange: function() {
|
||||||
console.log("Emmiting Section change!");
|
console.log("Emmiting Section change!");
|
||||||
this.emit('change');
|
this.emit('change');
|
||||||
},
|
},
|
||||||
|
|
||||||
// Add change listener
|
// Add change listener
|
||||||
addChangeListener: function(callback) {
|
addChangeListener: function(callback) {
|
||||||
this.on('change', callback);
|
this.on('change', callback);
|
||||||
},
|
},
|
||||||
|
|
||||||
// Remove change listener
|
// Remove change listener
|
||||||
removeChangeListener: function(callback) {
|
removeChangeListener: function(callback) {
|
||||||
this.removeListener('change', callback);
|
this.removeListener('change', callback);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
// Register callback with AppDispatcher
|
// Register callback with AppDispatcher
|
||||||
AppDispatcher.register(function(payload) {
|
AppDispatcher.register(function(payload) {
|
||||||
var action = payload.action;
|
var action = payload.action;
|
||||||
var text;
|
var text;
|
||||||
|
|
||||||
switch(action.actionType) {
|
switch (action.actionType) {
|
||||||
|
|
||||||
|
|
||||||
case UserConstants.REGISTRATION_SUCCESS:
|
case UserConstants.REGISTRATION_SUCCESS:
|
||||||
handleRegistrationSuccess(action.user);
|
handleRegistrationSuccess(action.user);
|
||||||
break;
|
break;
|
||||||
case UserConstants.REGISTRATION_FAILURE:
|
case UserConstants.REGISTRATION_FAILURE:
|
||||||
handleRegistrationFailure(action.error);
|
handleRegistrationFailure(action.error);
|
||||||
break;
|
break;
|
||||||
case UserConstants.LOGIN_SUCCESS:
|
case UserConstants.LOGIN_SUCCESS:
|
||||||
handleLoginSuccess(action.user);
|
handleLoginSuccess(action.user);
|
||||||
break;
|
break;
|
||||||
case UserConstants.LOGIN_FAILURE:
|
case UserConstants.LOGIN_FAILURE:
|
||||||
handleLoginFailure(action.error);
|
handleLoginFailure(action.error);
|
||||||
break;
|
break;
|
||||||
case UserConstants.CHECK_LOGIN_ARRIVED:
|
case UserConstants.CHECK_LOGIN_ARRIVED:
|
||||||
handleCheckLoginArrived(action.user, action.error);
|
handleCheckLoginArrived(action.user, action.error);
|
||||||
break;
|
break;
|
||||||
case UserConstants.USER_LOGOUT_DONE:
|
case UserConstants.USER_LOGOUT_DONE:
|
||||||
handleLogoutDone();
|
handleLogoutDone();
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// If action was responded to, emit change event
|
// If action was responded to, emit change event
|
||||||
UserStore.emitChange();
|
UserStore.emitChange();
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
module.exports = UserStore;
|
module.exports = UserStore;
|
||||||
Reference in New Issue
Block a user