Skip to content

Commit

Permalink
Merge fb700bc into d2dbb23
Browse files Browse the repository at this point in the history
  • Loading branch information
junajan committed Dec 6, 2018
2 parents d2dbb23 + fb700bc commit 9b5d5c8
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/coffee/sync/utils/product.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,26 @@ class ProductUtils extends BaseUtils

matchesByIdOrKeyOrSku: (variant1, variant2) ->
variant1 and variant2 and (
(!isNil(variant1.id) and variant1.id == variant2.id) or
(!isNil(variant1.sku) and variant1.sku == variant2.sku) or
(!isNil(variant1.key) and variant1.key == variant2.key) or
(!isNil(variant1.sku) and variant1.sku == variant2.sku)
(!isNil(variant1.id) and variant1.id == variant2.id)
)

matchesById: (variant1, variant2) ->
variant1 and variant2 and not isNil(variant1.id) and variant1.id == variant2.id

matchesByKey: (variant1, variant2) ->
variant1 and variant2 and not isNil(variant1.key) and variant1.key == variant2.key

matchesBySku: (variant1, variant2) ->
variant1 and variant2 and not isNil(variant1.sku) and variant1.sku == variant2.sku

# match variant against variants in list - match first by sku, then by key and then by id
findVariantInList: (variant, variantList) ->
variantList.find (testedVariant) =>
@matchesByIdOrKeyOrSku(testedVariant, variant)
return variantList.find((oldVariant) => @matchesBySku(variant, oldVariant)) or
variantList.find((oldVariant) => @matchesByKey(variant, oldVariant)) or
variantList.find((oldVariant) => @matchesById(variant, oldVariant)) or
undefined # if not found, return undefined

buildChangeMasterVariantAction: (newMasterVariant, oldMasterVariant) ->
if newMasterVariant and oldMasterVariant and not @matchesByIdOrKeyOrSku(newMasterVariant, oldMasterVariant)
Expand Down
86 changes: 86 additions & 0 deletions src/spec/sync/utils/product.spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,92 @@ describe 'ProductUtils', ->
{ action: 'removeVariant', sku: 'newVar' }
])

describe ':: findVariantInList', ->

it 'should find variant using sku', ->
oldVariants = [
{
id: 1
sku: 'sku1'
},
{
id: 2
sku: 'sku2'
}
]
newVariant = {
id: 2,
sku: 'sku1'
}

actions = @utils.findVariantInList(newVariant, oldVariants)
expect(actions).toEqual({
id: 1
sku: 'sku1'
})

it 'should find variant using id', ->
oldVariants = [
{
id: 1
sku: 'sku1'
},
{
id: 2,
key: 'key'
}
]
newVariant = {
id: 2,
}

actions = @utils.findVariantInList(newVariant, oldVariants)
expect(actions).toEqual({
id: 2
key: 'key'
})

it 'should find variant using key', ->
oldVariants = [
{
id: 1
sku: 'key1'
},
{
id: 2,
key: 'key2'
}
]
newVariant = {
id: 1,
key: 'key2',
}

actions = @utils.findVariantInList(newVariant, oldVariants)
expect(actions).toEqual({
id: 2
key: 'key2'
})

it 'should not find an unknown variant', ->
oldVariants = [
{
id: 1
sku: 'key1'
},
{
id: 2,
key: 'key2'
}
]
newVariant = {
id: 29,
key: 'key3',
}

actions = @utils.findVariantInList(newVariant, oldVariants)
expect(actions).toEqual(undefined)

describe ':: buildVariantPriceActions', ->

it 'should return an empty array when no price diff is provided', ->
Expand Down

0 comments on commit 9b5d5c8

Please sign in to comment.