mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
Do not include variant_unit_with_scale in product update check. When saving products, save variant_unit and variant_unit_scale.
This commit is contained in:
@@ -252,7 +252,7 @@ productsApp.controller "AdminBulkProductsCtrl", [
|
||||
url: "/admin/products/bulk_update"
|
||||
data: productsToSubmit
|
||||
).success((data) ->
|
||||
if angular.toJson($scope.products) == angular.toJson(data)
|
||||
if angular.toJson($scope.productsWithoutDerivedAttributes()) == angular.toJson(data)
|
||||
$scope.resetProducts data
|
||||
$scope.displaySuccess()
|
||||
else
|
||||
@@ -266,6 +266,14 @@ productsApp.controller "AdminBulkProductsCtrl", [
|
||||
$scope.updateProducts productsToSubmit
|
||||
|
||||
|
||||
$scope.productsWithoutDerivedAttributes = ->
|
||||
products = []
|
||||
if $scope.products
|
||||
products.push angular.extend {}, product for product in $scope.products
|
||||
angular.forEach products, (product) ->
|
||||
delete product.variant_unit_with_scale
|
||||
products
|
||||
|
||||
$scope.setMessage = (model, text, style, timeout) ->
|
||||
model.text = text
|
||||
model.style = style
|
||||
@@ -354,6 +362,11 @@ filterSubmitProducts = (productsToFilter) ->
|
||||
if product.hasOwnProperty("price")
|
||||
filteredProduct.price = product.price
|
||||
hasUpdatableProperty = true
|
||||
if product.hasOwnProperty("variant_unit_with_scale")
|
||||
match = product.variant_unit_with_scale.match(/^([^_]+)_([\d\.]+)$/)
|
||||
filteredProduct.variant_unit = match[1]
|
||||
filteredProduct.variant_unit_scale = parseFloat(match[2])
|
||||
hasUpdatableProperty = true
|
||||
if product.hasOwnProperty("on_hand") and filteredVariants.length == 0 #only update if no variants present
|
||||
filteredProduct.on_hand = product.on_hand
|
||||
hasUpdatableProperty = true
|
||||
|
||||
@@ -128,9 +128,21 @@ describe "filtering products", ->
|
||||
price: 10
|
||||
]
|
||||
|
||||
it 'returns variant_unit_with_scale as variant_unit and variant_unit_scale', ->
|
||||
testProduct =
|
||||
id: 1
|
||||
variant_unit: 'weight'
|
||||
variant_unit_scale: 1
|
||||
variant_unit_with_scale: 'volume_1000'
|
||||
|
||||
expect(filterSubmitProducts([testProduct])).toEqual [
|
||||
id: 1
|
||||
variant_unit: 'volume'
|
||||
variant_unit_scale: 1000
|
||||
]
|
||||
|
||||
# TODO Not an exhaustive test, is there a better way to do this?
|
||||
it "only returns properties the properties of products which ought to be updated", ->
|
||||
it "only returns the properties of products which ought to be updated", ->
|
||||
testProduct =
|
||||
id: 1
|
||||
name: "TestProduct"
|
||||
@@ -158,11 +170,17 @@ describe "filtering products", ->
|
||||
on_hand: 2
|
||||
price: 10.0
|
||||
]
|
||||
variant_unit: 'volume'
|
||||
variant_unit_scale: 1
|
||||
variant_unit_name: null
|
||||
variant_unit_with_scale: 'weight_1000'
|
||||
|
||||
expect(filterSubmitProducts([testProduct])).toEqual [
|
||||
id: 1
|
||||
name: "TestProduct"
|
||||
supplier_id: 5
|
||||
variant_unit: 'weight'
|
||||
variant_unit_scale: 1000
|
||||
available_on: new Date()
|
||||
variants_attributes: [
|
||||
id: 1
|
||||
@@ -540,6 +558,23 @@ describe "AdminBulkProductsCtrl", ->
|
||||
expect(scope.displayFailure).toHaveBeenCalled()
|
||||
|
||||
|
||||
describe 'fetching products without derived attributes', ->
|
||||
beforeEach ->
|
||||
ctrl "AdminBulkProductsCtrl",
|
||||
$scope: scope
|
||||
|
||||
it 'returns products without the variant_unit_with_scale field', ->
|
||||
scope.products = [{id: 123, variant_unit_with_scale: 'weight_1000'}]
|
||||
expect(scope.productsWithoutDerivedAttributes()).toEqual([{id: 123}])
|
||||
|
||||
it 'returns an empty array when products are undefined', ->
|
||||
expect(scope.productsWithoutDerivedAttributes()).toEqual([])
|
||||
|
||||
it 'does not alter products', ->
|
||||
scope.products = [{id: 123, variant_unit_with_scale: 'weight_1000'}]
|
||||
scope.productsWithoutDerivedAttributes()
|
||||
expect(scope.products).toEqual [{id: 123, variant_unit_with_scale: 'weight_1000'}]
|
||||
|
||||
|
||||
describe "deleting products", ->
|
||||
beforeEach ->
|
||||
|
||||
Reference in New Issue
Block a user