mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-05 02:41:33 +00:00
Moving variants to a service, backreferencing and linking
This commit is contained in:
@@ -1,9 +1,21 @@
|
||||
Darkswarm.factory 'Cart', (CurrentOrder)->
|
||||
Darkswarm.factory 'Cart', (CurrentOrder, Variants)->
|
||||
# Handles syncing of current cart/order state to server
|
||||
new class Cart
|
||||
order: CurrentOrder.order
|
||||
line_items: CurrentOrder.order.line_items
|
||||
constructor: ->
|
||||
console.log @order.line_items
|
||||
for line_item in @line_items
|
||||
line_item.variant.line_item = line_item
|
||||
Variants.register line_item.variant
|
||||
|
||||
register_variant: (variant)=>
|
||||
@create_line_item(variant) unless @line_items.some (li)->
|
||||
li.variant == variant
|
||||
|
||||
create_line_item: (variant)->
|
||||
li =
|
||||
variant: variant
|
||||
quantity: 0
|
||||
variant.line_item = li
|
||||
@line_items.push li
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
Darkswarm.factory 'Products', ($resource, Enterprises, Dereferencer, Taxons) ->
|
||||
Darkswarm.factory 'Products', ($resource, Enterprises, Dereferencer, Taxons, Cart, Variants) ->
|
||||
new class Products
|
||||
constructor: ->
|
||||
@update()
|
||||
@@ -13,6 +13,7 @@ Darkswarm.factory 'Products', ($resource, Enterprises, Dereferencer, Taxons) ->
|
||||
@products = $resource("/shop/products").query (products)=>
|
||||
@extend()
|
||||
@dereference()
|
||||
@registerVariants()
|
||||
@loading = false
|
||||
@
|
||||
|
||||
@@ -21,6 +22,12 @@ Darkswarm.factory 'Products', ($resource, Enterprises, Dereferencer, Taxons) ->
|
||||
product.supplier = Enterprises.enterprises_by_id[product.supplier.id]
|
||||
Dereferencer.dereference product.taxons, Taxons.taxons_by_id
|
||||
|
||||
registerVariants: ->
|
||||
for product in @products
|
||||
if product.variants
|
||||
product.variants = (Variants.register variant for variant in product.variants)
|
||||
product.master = Variants.register master if product.master
|
||||
|
||||
extend: ->
|
||||
for product in @products
|
||||
if product.variants?.length > 0
|
||||
@@ -28,3 +35,6 @@ Darkswarm.factory 'Products', ($resource, Enterprises, Dereferencer, Taxons) ->
|
||||
product.price = Math.min.apply(null, prices)
|
||||
|
||||
product.hasVariants = product.variants?.length > 0
|
||||
|
||||
# Iterate through variants
|
||||
# Cart.register_variant(v)
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
Darkswarm.factory 'Variants', ->
|
||||
new class Variants
|
||||
variants: {}
|
||||
register: (variant)->
|
||||
@variants[variant.id] ||= variant
|
||||
@@ -1,9 +1,28 @@
|
||||
describe 'Cart service', ->
|
||||
Cart = null
|
||||
orders = []
|
||||
Variants = null
|
||||
variant = {id: 1}
|
||||
order = {
|
||||
line_items: [
|
||||
variant: variant
|
||||
]
|
||||
}
|
||||
|
||||
beforeEach ->
|
||||
module 'Darkswarm'
|
||||
angular.module('Darkswarm').value('order', orders)
|
||||
angular.module('Darkswarm').value('currentOrder', order)
|
||||
inject ($injector)->
|
||||
Variants = $injector.get("Variants")
|
||||
Cart = $injector.get("Cart")
|
||||
|
||||
it "backreferences line items", ->
|
||||
expect(Cart.line_items[0].variant.line_item).toBe Cart.line_items[0]
|
||||
|
||||
it "registers variants with the Variants service", ->
|
||||
expect(Variants.variants[1]).toBe variant
|
||||
|
||||
it "creates and backreferences new line items if necessary", ->
|
||||
Cart.register_variant(v2 = {id: 2})
|
||||
expect(Cart.line_items[1].variant).toBe v2
|
||||
expect(Cart.line_items[1].variant.line_item).toBe Cart.line_items[1]
|
||||
|
||||
|
||||
@@ -2,21 +2,27 @@ describe 'Products service', ->
|
||||
$httpBackend = null
|
||||
Products = null
|
||||
Enterprises = null
|
||||
Variants = null
|
||||
CurrentHubMock = {}
|
||||
product =
|
||||
test: "cats"
|
||||
supplier:
|
||||
id: 9
|
||||
price: 11
|
||||
variants: []
|
||||
currentOrder =
|
||||
line_items: []
|
||||
product = null
|
||||
beforeEach ->
|
||||
product =
|
||||
test: "cats"
|
||||
supplier:
|
||||
id: 9
|
||||
price: 11
|
||||
variants: []
|
||||
module 'Darkswarm'
|
||||
module ($provide)->
|
||||
$provide.value "CurrentHub", CurrentHubMock
|
||||
$provide.value "currentOrder", currentOrder
|
||||
null
|
||||
inject ($injector, _$httpBackend_)->
|
||||
Products = $injector.get("Products")
|
||||
Enterprises = $injector.get("Enterprises")
|
||||
Variants = $injector.get("Variants")
|
||||
$httpBackend = _$httpBackend_
|
||||
|
||||
it "Fetches products from the backend on init", ->
|
||||
@@ -31,6 +37,13 @@ describe 'Products service', ->
|
||||
$httpBackend.flush()
|
||||
expect(Products.products[0].supplier).toBe Enterprises.enterprises_by_id["9"]
|
||||
|
||||
it "registers variants with the Variants service", ->
|
||||
spyOn(Variants, "register")
|
||||
product.variants = [{id: 1}]
|
||||
$httpBackend.expectGET("/shop/products").respond([product])
|
||||
$httpBackend.flush()
|
||||
expect(Variants.register).toHaveBeenCalledWith product.variants[0]
|
||||
|
||||
describe "determining the price to display for a product", ->
|
||||
it "displays the product price when the product does not have variants", ->
|
||||
$httpBackend.expectGET("/shop/products").respond([product])
|
||||
@@ -42,3 +55,4 @@ describe 'Products service', ->
|
||||
$httpBackend.expectGET("/shop/products").respond([product])
|
||||
$httpBackend.flush()
|
||||
expect(Products.products[0].price).toEqual 22
|
||||
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
describe 'Variants service', ->
|
||||
Variants = null
|
||||
variant =
|
||||
id: 1
|
||||
|
||||
beforeEach ->
|
||||
module 'Darkswarm'
|
||||
inject ($injector)->
|
||||
Variants = $injector.get("Variants")
|
||||
|
||||
it "will create a new variant", ->
|
||||
expect(Variants.register(variant)).toBe variant
|
||||
|
||||
it "will return an existing variant rather than duplicating", ->
|
||||
Variants.register(variant)
|
||||
expect(Variants.register({id: 1})).toBe variant
|
||||
Reference in New Issue
Block a user