Starting on JSON checkout, moving Order properties to Order.order

This commit is contained in:
Will Marshall
2014-04-09 14:33:45 +10:00
parent 09d22f74ec
commit b5550c048a
7 changed files with 82 additions and 27 deletions

View File

@@ -1,6 +1,7 @@
Darkswarm.controller "CheckoutCtrl", ($scope, Order, storage) ->
window.tmp = $scope
$scope.order = $scope.Order = Order
$scope.Order = Order
$scope.order = Order.order
$scope.accordion = {}
$scope.show = (name)->
@@ -30,6 +31,10 @@ Darkswarm.controller "CheckoutCtrl", ($scope, Order, storage) ->
$scope.number = (name)->
$scope.error(name).number
$scope.purchase = (event)->
event.preventDefault()
$scope.Order.submit()
# READ THIS FIRST
# https://github.com/angular/angular.js/wiki/Understanding-Scopes
@@ -67,8 +72,5 @@ Darkswarm.controller "DetailsSubCtrl", ($scope) ->
$scope.phoneError = ->
"must be a number"
$scope.purchase = (event)->
event.preventDefault()
checkout.submit()

View File

@@ -1,5 +1,6 @@
window.Darkswarm = angular.module("Darkswarm", ["ngResource", "filters", 'mm.foundation', 'angularLocalStorage', 'pasvaz.bindonce', 'infinite-scroll']).config ($httpProvider, $tooltipProvider) ->
$httpProvider.defaults.headers.post['X-CSRF-Token'] = $('meta[name="csrf-token"]').attr('content')
$httpProvider.defaults.headers.put['X-CSRF-Token'] = $('meta[name="csrf-token"]').attr('content')
$httpProvider.defaults.headers['common']['X-Requested-With'] = 'XMLHttpRequest'
$httpProvider.defaults.headers.common.Accept = "application/json, text/javascript, */*"

View File

@@ -1,14 +1,25 @@
Darkswarm.factory 'Order', ($resource, Product, order)->
Darkswarm.factory 'Order', ($resource, Product, order, $http)->
new class Order
errors: {}
constructor: ->
@[name] = method for name, method of order # Clone all data from the order JSON object
@order = order
# Here we default to the first shipping method if none is selected
@shipping_method_id ||= parseInt(Object.keys(@shipping_methods)[0])
@ship_address_same_as_billing ?= true
@order.shipping_method_id ||= parseInt(Object.keys(@order.shipping_methods)[0])
@order.ship_address_same_as_billing ?= true
submit: ->
$http.put('/shop/checkout', {order: @preprocess()}).success (data, status)->
console.log "success"
console.log data
.error (data, status)->
console.log "error"
console.log data
preprocess: ->
@order
shippingMethod: ->
@shipping_methods[@shipping_method_id]
@order.shipping_methods[@order.shipping_method_id]
requireShipAddress: ->
@shippingMethod()?.require_ship_address
@@ -17,8 +28,8 @@ Darkswarm.factory 'Order', ($resource, Product, order)->
@shippingMethod()?.price
paymentMethod: ->
@payment_methods[@payment_method_id]
@order.payment_methods[@order.payment_method_id]
cartTotal: ->
@shippingPrice() + @display_total
@shippingPrice() + @order.display_total

View File

@@ -22,8 +22,7 @@ class Shop::CheckoutController < Spree::CheckoutController
state_callback(:after)
else
flash[:error] = t(:payment_processing_failed)
clear_ship_address
render :edit
update_failed
return
end
end
@@ -32,16 +31,26 @@ class Shop::CheckoutController < Spree::CheckoutController
flash.notice = t(:order_processed_successfully)
respond_with(@order, :location => order_path(@order))
else
clear_ship_address
render :edit
update_failed
end
else
clear_ship_address
render :edit
update_failed
end
end
private
def update_failed
clear_ship_address
respond_to do |format|
format.html do
render :edit
end
format.js do
render json: @order.errors.to_json, status: 400
end
end
end
# When we have a pickup Shipping Method, we clone the distributor address into ship_address before_save
# We don't want this data in the form, so we clear it out

View File

@@ -47,12 +47,12 @@ describe Shop::CheckoutController do
end
describe "building the order" do
before do
controller.stub(:current_distributor).and_return(distributor)
controller.stub(:current_order_cycle).and_return(order_cycle)
controller.stub(:current_order).and_return(order)
end
it "does not clone the ship address from distributor when shipping method requires address" do
get :edit
assigns[:order].ship_address.address1.should be_nil
@@ -78,6 +78,21 @@ describe Shop::CheckoutController do
end
end
context "via xhr" do
before do
controller.stub(:current_distributor).and_return(distributor)
controller.stub(:current_order_cycle).and_return(order_cycle)
controller.stub(:current_order).and_return(order)
end
it "returns errors" do
xhr :post, :update, order: {}, use_route: :spree
response.status.should == 400
response.body.should == assigns[:order].errors.to_json
end
end
describe "Paypal routing" do
let(:payment_method) { create(:payment_method, type: "Spree::BillingIntegration::PaypalExpress") }
before do

View File

@@ -5,10 +5,20 @@ describe "CheckoutCtrl", ->
beforeEach ->
module("Darkswarm")
order = {}
order = {
submit: ->
}
inject ($controller, $rootScope) ->
scope = $rootScope.$new()
ctrl = $controller 'CheckoutCtrl', {$scope: scope, Order: order}
it "defaults the user accordion to visible", ->
expect(scope.user).toEqual true
expect(scope.accordion.user).toEqual true
it "delegates to the service on submit", ->
event = {
preventDefault: ->
}
spyOn(order, "submit")
scope.purchase(event)
expect(order.submit).toHaveBeenCalled()

View File

@@ -1,6 +1,7 @@
describe 'Order service', ->
Order = null
orderData = null
$httpBackend = null
beforeEach ->
orderData = {
@@ -20,28 +21,34 @@ describe 'Order service', ->
}
angular.module('Darkswarm').value('order', orderData)
module 'Darkswarm'
inject ($injector)->
inject ($injector, _$httpBackend_)->
$httpBackend = _$httpBackend_
Order = $injector.get("Order")
it "defaults the shipping method to the first", ->
expect(Order.shipping_method_id).toEqual 7
expect(Order.order.shipping_method_id).toEqual 7
expect(Order.shippingMethod()).toEqual { require_ship_address : true, price : 0 }
it "defaults to 'same as billing' for address", ->
expect(Order.ship_address_same_as_billing).toEqual true
expect(Order.order.ship_address_same_as_billing).toEqual true
it 'Tracks whether a ship address is required', ->
expect(Order.requireShipAddress()).toEqual true
Order.shipping_method_id = 25
Order.order.shipping_method_id = 25
expect(Order.requireShipAddress()).toEqual false
it 'Gets the current shipping price', ->
expect(Order.shippingPrice()).toEqual 0.0
Order.shipping_method_id = 25
Order.order.shipping_method_id = 25
expect(Order.shippingPrice()).toEqual 13
it 'Gets the current payment method', ->
expect(Order.paymentMethod()).toEqual null
Order.payment_method_id = 99
Order.order.payment_method_id = 99
expect(Order.paymentMethod()).toEqual {test: "foo"}
it "Posts the Order to the server", ->
$httpBackend.expectPUT("/shop/checkout", {order: Order.preprocess()}).respond 200
Order.submit()
$httpBackend.flush()