diff --git a/back-office/app/views/carts/_show.html.erb b/back-office/app/views/carts/_show.html.erb index 5352327..f3f66fd 100644 --- a/back-office/app/views/carts/_show.html.erb +++ b/back-office/app/views/carts/_show.html.erb @@ -29,6 +29,16 @@ Bosna i Hercegovina

+<% if dd.gift %> +

Ovo je poklon

+

Ime primaoca: <%= dd.recipient_name %>
+

Adresa primaoca: <%= dd.recipient_address %>
+

Postanski broj primaoca: <%= dd.recipient_postal_code %>
+

Grad primaoca: <%= dd.recipient_place %>
+

Email primaoca: <%= dd.recipient_email %>
+

Telefon primaoca: <%= dd.recipient_phone %>

+<% end %> +

Naručeno: <%= c.confirmed_at.in_time_zone("Europe/Sarajevo").strftime("%A %d.%m.%Y. %H:%M") %>
diff --git a/front-api/controllers/cart.rb b/front-api/controllers/cart.rb index 791de1c..1d71057 100644 --- a/front-api/controllers/cart.rb +++ b/front-api/controllers/cart.rb @@ -44,7 +44,9 @@ end update_delivery_destination = ->() { cart = Cart.just_find(anonymous_id, logged_in_user_id) - allowed_keys = ["name", "address", "place", "postal_code", "phone", "email", "note", "payment_method", "gift"] + 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"] + params = @json_params.reject { |key,_| !allowed_keys.include?(key) } cart.delivery_destination.update_attributes(params) cart.delivery_destination.save! @@ -53,22 +55,6 @@ update_delivery_destination = ->() { put '/cart/delivery_destination', &update_delivery_destination post '/cart/delivery_destination', &update_delivery_destination -get '/cart/recipient_destination' do - cart = Cart.just_find(anonymous_id, logged_in_user_id) - cart.recipient.to_json(:except => [:created_at, :email_verification_code, :phone_verification_code]) -end - -update_recipient_destination = ->() { - cart = Cart.just_find(anonymous_id, logged_in_user_id) - allowed_keys = ["name", "address", "place", "postal_code", "phone", "email"] - params = @json_params.reject { |key,_| !allowed_keys.include?(key) } - cart.recipient_destination.update_attributes(params) - cart.recipient_destination.save! - cart.recipient_destination.to_json(:except => [:created_at, :email_verification_code, :phone_verification_code]) -} -put '/cart/recipient_destination', &update_delivery_destination -post '/cart/recipient_destination', &update_delivery_destination - def report_to_trello(cart) Thread.new do @cart = cart diff --git a/front-api/db/migrate/20160112091903_add_recipient_destination_to_delivery_destination.rb b/front-api/db/migrate/20160112091903_add_recipient_destination_to_delivery_destination.rb new file mode 100644 index 0000000..a85dd88 --- /dev/null +++ b/front-api/db/migrate/20160112091903_add_recipient_destination_to_delivery_destination.rb @@ -0,0 +1,10 @@ +class AddRecipientDestinationToDeliveryDestination < ActiveRecord::Migration + def change + add_column :delivery_destinations, :recipient_name, :string + add_column :delivery_destinations, :recipient_address, :string + add_column :delivery_destinations, :recipient_place, :string + add_column :delivery_destinations, :recipient_postal_code, :string + add_column :delivery_destinations, :recipient_phone, :string + add_column :delivery_destinations, :recipient_email, :string + end +end diff --git a/front-api/db/schema.rb b/front-api/db/schema.rb index 638d080..3695b4b 100644 --- a/front-api/db/schema.rb +++ b/front-api/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20160111105607) do +ActiveRecord::Schema.define(version: 20160112091903) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -22,18 +22,17 @@ ActiveRecord::Schema.define(version: 20160111105607) do create_table "carts", force: :cascade do |t| t.integer "user_id" - t.boolean "ordered", default: false - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + t.boolean "ordered", default: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false t.string "anonymous_id_string" t.integer "delivery_destination_id" - t.boolean "confirmed", default: false - t.boolean "packed", default: false - t.boolean "canceled_on_check", default: false - t.boolean "canceled_on_delivery", default: false - t.boolean "delivered", default: false + t.boolean "confirmed", default: false + t.boolean "packed", default: false + t.boolean "canceled_on_check", default: false + t.boolean "canceled_on_delivery", default: false + t.boolean "delivered", default: false t.text "internal_note" - t.integer "recipient_destination_id" end create_table "categories", force: :cascade do |t| @@ -70,6 +69,12 @@ ActiveRecord::Schema.define(version: 20160111105607) do t.boolean "instant_delivery", default: false t.string "payment_method" t.boolean "gift", default: false + t.string "recipient_name" + t.string "recipient_address" + t.string "recipient_place" + t.string "recipient_postal_code" + t.string "recipient_phone" + t.string "recipient_email" end create_table "delivery_time_estimations", force: :cascade do |t| diff --git a/front-api/models/cart.rb b/front-api/models/cart.rb index 31236bc..44eb33b 100644 --- a/front-api/models/cart.rb +++ b/front-api/models/cart.rb @@ -1,8 +1,6 @@ class Cart < ActiveRecord::Base - has_many :item_in_carts, -> { order "created_at" } - - belongs_to :delivery_destination, :class_name => 'DeliveryDestination', :foreign_key => 'delivery_destination_id' - belongs_to :delivery_destination, :class_name => 'DeliveryDestination', :foreign_key => 'recipient_destination_id' + has_many :item_in_carts, -> { order "created_at" } + belongs_to :delivery_destination def self.find_or_create(anonymous_id, user_id) cart = nil diff --git a/front-api/models/delivery_destination.rb b/front-api/models/delivery_destination.rb index 7e51f0d..f84e23c 100644 --- a/front-api/models/delivery_destination.rb +++ b/front-api/models/delivery_destination.rb @@ -1,12 +1,21 @@ class DeliveryDestination < ActiveRecord::Base - has_many :carts + has_one :cart belongs_to :user def self.create_from_last(anonymous_id, user_id) dd = DeliveryDestination.where(["user_id is not null and user_id = ?", user_id]).order("id desc").first dd ||= DeliveryDestination.where(["anonymous_id_string is not null and anonymous_id_string = ?", anonymous_id]).order("id desc").first dd ||= DeliveryDestination.new({user_id: user_id, anonymous_id_string: anonymous_id }) + dd.payment_method ||= "cash_on_delivery" + dd.gift = false + dd.recipient_name = "" + dd.recipient_phone = "" + dd.recipient_email = "" + dd.recipient_address = "" + dd.recipient_place = "" + dd.recipient_postal_code = "" + attributes = dd.as_json attributes.delete("id") result = DeliveryDestination.create!(attributes) diff --git a/front-ui/app/actions/cartActions.js b/front-ui/app/actions/cartActions.js index aec5b04..c85de07 100644 --- a/front-ui/app/actions/cartActions.js +++ b/front-ui/app/actions/cartActions.js @@ -47,13 +47,6 @@ var CartActions = { value: value }); }, - changeRecipientDestinationProperty: function(property, value) { - AppDispatcher.handleAction({ - actionType: CartConstants.CHANGE_RECIPIENT_DESTINATION_PROPERTY, - propertyName: property, - value: value - }); - }, confirmDelivery: function() { AppDispatcher.handleAction({ actionType: CartConstants.CONFIRM_DELIVERY, diff --git a/front-ui/app/components/cart/checkoutPage.js b/front-ui/app/components/cart/checkoutPage.js index 90a47d4..eb270c4 100644 --- a/front-ui/app/components/cart/checkoutPage.js +++ b/front-ui/app/components/cart/checkoutPage.js @@ -127,30 +127,29 @@ var CheckoutPage = React.createClass({ Podaci o dostavi -

- +
- - + + ime osobe koja prima pošiljku
- +
- - + + adresa na koju će roba biti isporučena
- +
- - {supportedPlaceOptions} @@ -158,21 +157,21 @@ var CheckoutPage = React.createClass({
- +
- +
+387 - +

broj mobitela - mora biti sa jedne od mreža u BiH

- +
- - + + E - mail adresa na koju će vam biti poslano obavještenje o narudžbi
@@ -243,10 +242,6 @@ var CheckoutPage = React.createClass({ } }, - _onFieldRecipientChange: function(event) { - CartActions.changeRecipientDestinationProperty(event.target.name, event.target.value); - }, - _onOrderClick: function (event) { CartActions.changeDeliveryDestinationProperty("payment_method", "cash_on_delivery"); CartActions.confirmDelivery(); diff --git a/front-ui/app/constants/cartConstants.js b/front-ui/app/constants/cartConstants.js index 5c8635d..af879a7 100644 --- a/front-ui/app/constants/cartConstants.js +++ b/front-ui/app/constants/cartConstants.js @@ -6,7 +6,6 @@ module.exports = keyMirror({ CART_DATA_LOADED: null, SAVE_CART_STATE_FOR_ITEM: null, CHANGE_DELIVERY_DESTINATION_PROPERTY: null, - CHANGE_RECIPIENT_DESTINATION_PROPERTY: null, CONFIRM_DELIVERY: null, SET_ITEM_COUNT: null, ADD_N_ITEMS: null, diff --git a/front-ui/app/stores/cartStore.js b/front-ui/app/stores/cartStore.js index d3562c9..2745f36 100644 --- a/front-ui/app/stores/cartStore.js +++ b/front-ui/app/stores/cartStore.js @@ -7,7 +7,6 @@ var ItemInCart = require('../models/itemInCart'); var ItemInCartCollection = require('../models/itemInCartCollection'); var ItemCollection = require('../models/itemCollection'); var DeliveryDestination = require('../models/deliveryDestination'); -var RecipientDestination = require('../models/recipientDestination'); var OrderConfirmation = require('../models/orderConfirmation'); var Place = require('../models/place'); var Validation = require('../utils/validation'); @@ -26,12 +25,6 @@ var _deliveryCosts = new Place({ postalCode: _deliveryDestination.get('place') }); -var _recipientDestination = new RecipientDestination(); -var _recipientDestinationErrors = {}; -var _recipientCosts = new Place({ - postalCode: _recipientDestination.get('place') -}); - var _addressColapsed = false; var supportedPlaces = [{ @@ -2610,9 +2603,12 @@ var collapseAddressIfNeeded = function() { } } -var fetchPlace = function() { +var fetchPlace = function(gift) { + gift = gift === true; + postalCode = gift ? _deliveryDestination.get('recipient_place') : _deliveryDestination.get('place') + _deliveryCosts = new Place({ - postalCode: _deliveryDestination.get('place') + postalCode: postalCode }) _deliveryCosts.fetch({ success: function() { @@ -2704,18 +2700,11 @@ var changeDeliveryDestinationProperty = function(property, value) { if (property === 'place') { fetchPlace(); } - - validateDeliveryDestinationForm(); -}; - -var changeRecipientDestinationProperty = function(property, value) { - _recipientDestination.set(property, value); - - if (property === 'place') { - fetchPlace(); + if (property === 'recipient_place') { + fetchPlace(true); } - validateRecipientDestinationForm(); + validateDeliveryDestinationForm(); }; var confirmOrder = function() { @@ -2744,11 +2733,10 @@ var saveDeliveryDestinationAnd = function(successCallback) { var saveDeliveryDestination = function() { _deliveryDestination.save(null, { - success: function() { - - confirmOrder(); - } - }) + success: function() { + confirmOrder(); + } + }); }; var validateDeliveryDestinationForm = function() { @@ -2779,46 +2767,34 @@ var validateDeliveryDestinationForm = function() { _deliveryDestinationErrors['place'] = "Mjesto mora biti izabrano"; } + if(_deliveryDestination.get('gift')) { + if (Validation.safeString(_deliveryDestination.get('recipient_name')).search(nameRegex) < 0) { + _deliveryDestinationErrors['recipient_name'] = "I prezime i ime su obavezni"; + } + + if (Validation.safeString(_deliveryDestination.get('recipient_address')).search(addressRegex) < 0) { + _deliveryDestinationErrors['recipient_address'] = "Adresa mora biti ispravna"; + } + + if (_deliveryDestination.get('recipient_email') && Validation.safeString(_deliveryDestination.get('recipient_email')).search(emailRegex) < 0) { + _deliveryDestinationErrors['recipient_email'] = "Email mora biti ispravno upisan"; + } + + if (Validation.safeString(_deliveryDestination.get('recipient_phone')).search(phoneRegex) < 0) { + _deliveryDestinationErrors['recipient_phone'] = "Telefon mora biti ispravan"; + } + + if (Validation.safeString(_deliveryDestination.get('recipient_place')).search(placeRegex) < 0) { + _deliveryDestinationErrors['recipient_place'] = "Mjesto mora biti izabrano"; + } + } + var requiredFields = ["name", "email", "place", 'address', 'phone']; - for (var i in requiredFields) { - var value = _deliveryDestination.get(requiredFields[i]); - if (value === undefined || value === null || value === "") { - // if it's required there will be a star there - _deliveryDestinationErrors[requiredFields[i]] = "*"; - } + + if(_deliveryDestination.get('gift')) { + requiredFields = requiredFields.concat(["recipient_name", "recipient_place", 'recipient_address', 'recipient_phone']); } -} - -var validateRecipientDestinationForm = function() { - _recipientDestinationErrors = {}; - - var nameRegex = /.+\s+.+/i; - if (Validation.safeString(_recipientDestinationErrors.get('name')).search(nameRegex) < 0) { - _recipientDestinationErrors['name'] = "I prezime i ime su obavezni"; - } - - var addressRegex = /.+\s+.+/i; - if (Validation.safeString(_recipientDestinationErrors.get('address')).search(addressRegex) < 0) { - _recipientDestinationErrors['address'] = "Adresa mora biti ispravna"; - } - - var emailRegex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/i; - if (Validation.safeString(_recipientDestinationErrors.get('email')).search(emailRegex) < 0) { - _recipientDestinationErrors['email'] = "Email mora biti ispravno upisan"; - } - - var phoneRegex = /^[\d\s-]{8,12}$/i; - if (Validation.safeString(_recipientDestinationErrors.get('phone')).search(phoneRegex) < 0) { - _recipientDestinationErrors['phone'] = "Telefon mora biti ispravan"; - } - - var placeRegex = /^\s{0,1}\d{5}$/i; - if (Validation.safeString(_recipientDestinationErrors.get('place')).search(placeRegex) < 0) { - _recipientDestinationErrors['place'] = "Mjesto mora biti izabrano"; - } - - var requiredFields = ["name", "place", 'address', 'phone']; for (var i in requiredFields) { var value = _deliveryDestination.get(requiredFields[i]); if (value === undefined || value === null || value === "") { @@ -2833,9 +2809,6 @@ var isDeliveryDestinationValid = function() { return Object.getOwnPropertyNames(_deliveryDestinationErrors).length === 0; } -var isRecipientDestinationValid = function() { - return Object.getOwnPropertyNames(_recipientDestinationErrors).length === 0; -} // Extend CartStore with EventEmitter to add eventing capabilities var CartStore = _.extend({}, EventEmitter.prototype, { @@ -2900,12 +2873,7 @@ var CartStore = _.extend({}, EventEmitter.prototype, { deliveryDestinationErrors: _deliveryDestinationErrors, isDeliveryDestinationValid: isDeliveryDestinationValid(), deliveryCosts: _deliveryCosts, - destinationValid: isDeliveryDestinationValid(), - recipientDestination: _recipientDestination, - recipientDestinationErrors: _recipientDestinationErrors, - isRecipientDestinationValid: isRecipientDestinationValid(), - recipientCosts: _recipientCosts, - recipientValid: isRecipientDestinationValid() + destinationValid: isDeliveryDestinationValid() }; return state; }, @@ -2978,9 +2946,6 @@ AppDispatcher.register(function(payload) { case CartConstants.CHANGE_DELIVERY_DESTINATION_PROPERTY: changeDeliveryDestinationProperty(action.propertyName, action.value) break; - case CartConstants.CHANGE_RECIPIENT_DESTINATION_PROPERTY: - changeRecipientDestinationProperty(action.propertyName, action.value) - break; case CartConstants.CONFIRM_DELIVERY: saveDeliveryDestination(); break;