Skip to content

Commit

Permalink
Merge ffd0976 into 957848e
Browse files Browse the repository at this point in the history
  • Loading branch information
Siilwyn committed Nov 16, 2016
2 parents 957848e + ffd0976 commit b21920b
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 43 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ sku,quantityOnStock,customType,customField.foo,customField.bar
123,77,my-type,12,nac
abc,-3,my-type,5,ho
```
Please note: We do not support the localized set type.

### XML Format

Expand Down
2 changes: 0 additions & 2 deletions src/coffee/constants.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,11 @@ constants =
LOG_PREFIX: "[SphereStockImport] "
CHANNEL_REFERENCE_TYPE: 'channel'


REGEX_PRICE: new RegExp /^(([A-Za-z]{2})-|)([A-Z]{3}) (-?\d+)(-?\|(\d+)|)( ([^#]*)|)(#(.*)|)$/
REGEX_MONEY: new RegExp /^([A-Z]{3}) (-?\d+)$/
REGEX_INTEGER: new RegExp /^-?\d+$/
REGEX_FLOAT: new RegExp /^-?\d+(\.\d+)?$/
REGEX_LANGUAGE: new RegExp /^([a-z]{2,3}(?:-[A-Z]{2,3}(?:-[a-zA-Z]{4})?)?)$/
REGEX_CUR: new RegExp /^AED|AFN|ALL|AMD|ANG|AOA|ARS|AUD|AWG|AZN|BAM|BBD|BDT|BGN|BHD|BIF|BMD|BND|BOB|BRL|BSD|BTN|BWP|BYR|BZD|CAD|CDF|CHF|CLP|CNY|COP|CRC|CUC|CUP|CVE|CZK|DJF|DKK|DOP|DZD|EGP|ERN|ETB|EUR|FJD|FKP|GBP|GEL|GGP|GHS|GIP|GMD|GNF|GTQ|GYD|HKD|HNL|HRK|HTG|HUF|IDR|ILS|IMP|INR|IQD|IRR|ISK|JEP|JMD|JOD|JPY|KES|KGS|KHR|KMF|KPW|KRW|KWD|KYD|KZT|LAK|LBP|LKR|LRD|LSL|LYD|MAD|MDL|MGA|MKD|MMK|MNT|MOP|MRO|MUR|MVR|MWK|MXN|MYR|MZN|NAD|NGN|NIO|NOK|NPR|NZD|OMR|PAB|PEN|PGK|PHP|PKR|PLN|PYG|QAR|RON|RSD|RUB|RWF|SAR|SBD|SCR|SDG|SEK|SGD|SHP|SLL|SOS|SPL|SRD|STD|SVC|SYP|SZL|THB|TJS|TMT|TND|TOP|TRY|TTD|TVD|TWD|TZS|UAH|UGX|USD|UYU|UZS|VEF|VND|VUV|WST|XAF|XCD|XDR|XOF|XPF|YER|ZAR|ZMW|ZWD$/


for name, value of constants
Expand Down
61 changes: 36 additions & 25 deletions src/coffee/mappings.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,37 @@ class CustomFieldMappings
_.each fieldDefinitions, (fieldDefinition) =>
if fieldDefinition.name is key
switch fieldDefinition.type.name
when 'Number' then result = @mapNumber value,typeDefinitionKey,rowIndex
when 'Boolean' then result = @mapBoolean value,typeDefinitionKey,rowIndex
when 'Money' then result = @mapMoney value,typeDefinitionKey,rowIndex
when 'LocalizedString' then result = @mapLocalizedString value, typeDefinitionKey, rowIndex,langHeader
when 'Set' then result = @mapSet value,typeDefinitionKey,rowIndex,fieldDefinition.type.elementType
else result = value
when 'Number' then result = @mapNumber value, typeDefinitionKey, rowIndex
when 'Boolean' then result = @mapBoolean value, typeDefinitionKey, rowIndex
when 'Money' then result = @mapMoney value, typeDefinitionKey, rowIndex
when 'LocalizedString' then result = @mapLocalizedString value, typeDefinitionKey, rowIndex, langHeader
when 'Set' then result = @mapSet value, typeDefinitionKey, rowIndex
when 'String', 'Enum', 'LocalizedEnum', 'Date', 'Time', 'DateTime', 'Reference'
if (!_.isUndefined(value))
result = value
break
else
@errors.push "[row #{rowIndex}:#{typeDefinitionKey}] The type '#{fieldDefinition.type.name}' is not valid."
result

mapSet: (values, typeDefinitionKey, rowIndex, elementType) ->
result = undefined
values = values.split(',')

result = _.map values, (value) =>
switch elementType.name
when 'Number' then @mapNumber value, typeDefinitionKey, rowIndex
when 'Boolean' then @mapBoolean value, typeDefinitionKey, rowIndex
when 'Money' then @mapMoney value, typeDefinitionKey, rowIndex
when 'String', 'Enum', 'LocalizedEnum', 'Date', 'Time', 'DateTime', 'Reference'
if (!_.isUndefined(value))
value
else
@errors.push "[row #{rowIndex}:#{typeDefinitionKey}] The type '#{elementType.name}' is not valid."
return

_.reject(result, _.isUndefined)

isValidValue: (rawValue) ->
return _.isString(rawValue) and rawValue.length > 0

Expand All @@ -43,24 +66,19 @@ class CustomFieldMappings
}
}
###

mapLocalizedString: (value, typeDefinitionKey, rowIndex, langHeader, regEx = CONS.REGEX_LANGUAGE) ->
if !regEx.test langHeader
@errors.push "[row #{rowIndex}:#{typeDefinitionKey}] localisedString header '#{langHeader}' format is not valid!" unless regEx.test langHeader
@errors.push "[row #{rowIndex}:#{typeDefinitionKey}] localizedString header '#{langHeader}' format is not valid!" unless regEx.test langHeader
return
else
"#{langHeader}": value

mapSet: (values, typeDefinitionKey, rowIndex, elementType) ->
result = undefined
values = values.split(',')
result = _.map values, (value) =>
switch elementType.name
when 'Number' then @mapNumber value,typeDefinitionKey,rowIndex
when 'Boolean' then @mapBoolean value,typeDefinitionKey,rowIndex
when 'Money' then @mapMoney value,typeDefinitionKey,rowIndex
when 'LocalizedString' then @mapLocalizedString value, typeDefinitionKey, rowIndex
else value
_.reject(result, _.isUndefined)
mapString: (rawString) -> rawString
mapEnum: (rawEnum) -> rawEnum
mapDate: (rawDate) -> rawDate
mapTime: (rawTime) -> rawTime
mapDateTime: (rawDateTime) -> rawDateTime

mapBoolean: (rawBoolean, typeDefinitionKey, rowIndex) ->
result = undefined
Expand All @@ -77,8 +95,6 @@ class CustomFieldMappings
@errors.push errorMsg
return



# EUR 300
# USD 999
mapMoney: (rawMoney, typeDefinitionKey, rowIndex) ->
Expand All @@ -88,11 +104,6 @@ class CustomFieldMappings
@errors.push "[row #{rowIndex}:#{typeDefinitionKey}] Can not parse money '#{rawMoney}'!"
return

validCurr = CONS.REGEX_CUR.exec matchedMoney[1]
unless validCurr
@errors.push "[row #{rowIndex}:#{typeDefinitionKey}] Parsed currency is not valid '#{rawMoney}'!"
return

money =
currencyCode: matchedMoney[1].toUpperCase()
centAmount: parseInt matchedMoney[2],10
Expand Down
75 changes: 59 additions & 16 deletions src/spec/mappings.spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ describe 'Mappings', ->
it 'should add error if value is not valid', ->
result = @map.mapLocalizedString 'blue',@customTypeDefinition.key,2,'invalid'
expect(result).not.toBeDefined()
expect(@map.errors[0]).toBe "[row 2:my-category] localisedString header 'invalid' format is not valid!"
expect(@map.errors[0]).toBe "[row 2:my-category] localizedString header 'invalid' format is not valid!"

describe '::mapBoolean', ->
it 'should convert to boolean', ->
Expand Down Expand Up @@ -172,14 +172,17 @@ describe 'Mappings', ->
})
expect(result).toBe true
expect(@map.errors).toEqual []

it 'should map LocalizedString type', ->
result = @map.mapFieldTypes({
fieldDefinitions: @customTypeDefinition.fieldDefinitions,
typeDefinitionKey: @customTypeDefinition.key,
rowIndex: 2,
key: 'booleantype',
value: 'false',
key: 'localizedstringtype',
value: 'hallo',
langHeader: 'nl',
})
expect(result).toBe false
expect(result).toEqual {nl: 'hallo'}
expect(@map.errors).toEqual []

it 'should map Enum type', ->
Expand All @@ -193,16 +196,16 @@ describe 'Mappings', ->
expect(result).toBe 'la'
expect(@map.errors).toEqual []

it 'should map localizedenumtype type', ->
it 'should map LocalizedEnum type', ->
result = @map.mapFieldTypes({
fieldDefinitions: @customTypeDefinition.fieldDefinitions,
typeDefinitionKey: @customTypeDefinition.key,
rowIndex: 2,
key: 'localizedstringtype',
key: 'localizedenumtype',
value: 'la',
langHeader: 'de',
})
expect(result).toEqual de: 'la'
expect(result).toEqual 'la'
expect(@map.errors).toEqual []

it 'should map money type', ->
Expand All @@ -217,15 +220,59 @@ describe 'Mappings', ->
expect(@map.errors).toEqual []

describe '::mapSet', ->
it 'should convert to set', ->
it 'should convert string set', ->
elementType = name: 'String'
result = @map.mapSet 'some,things', @customTypeDefinition.key, 2, elementType
expect(result).toEqual ['some', 'things']

it 'should convert boolean set', ->
elementType = name: 'Boolean'
result = @map.mapSet 'true,false,true', @customTypeDefinition.key, 2, elementType
expect(result).toEqual [true, false, true]

it 'should convert to number set', ->
elementType = name: 'Number'
result = @map.mapSet '1,2,3,4',@customTypeDefinition.key,2,elementType
expect(result).toEqual [1,2,3,4]
result = @map.mapSet '1,2,3,4', @customTypeDefinition.key, 2, elementType
expect(result).toEqual [1, 2, 3, 4]

it 'should convert to money set', ->
elementType = name: 'Money'
result = @map.mapSet 'EUR 1400,JPY 9001', @customTypeDefinition.key, 2, elementType
expect(result).toEqual [
{'currencyCode': 'EUR', 'centAmount': 1400},
{'currencyCode': 'JPY', 'centAmount': 9001}
]

it 'should convert to enum set', ->
elementType = name: 'Enum'
result = @map.mapSet 'in,live', @customTypeDefinition.key, 2, elementType
expect(result).toEqual ['in', 'live']

it 'should convert to date set', ->
elementType = name: 'Date'
result = @map.mapSet '2016-09-11,2016-09-12', @customTypeDefinition.key, 2, elementType
expect(result).toEqual ['2016-09-11', '2016-09-12']

it 'should convert to time set', ->
elementType = name: 'Time'
result = @map.mapSet '14:00:00.000,15:00:00.000', @customTypeDefinition.key, 2, elementType
expect(result).toEqual ['14:00:00.000', '15:00:00.000']

it 'should convert to datetime set', ->
elementType = name: 'DateTime'
result = @map.mapSet '2001-09-11T14:00:00.000Z,2089-09-11T14:00:00.000Z', @customTypeDefinition.key, 2, elementType
expect(result).toEqual ['2001-09-11T14:00:00.000Z', '2089-09-11T14:00:00.000Z']

it 'should add error if type is not supported', ->
elementType = name: 'Emoij'
result = @map.mapSet '/O.O/,^_^', @customTypeDefinition.key, 2, elementType
expect(result).toEqual []
expect(@map.errors[0]).toBe '[row 2:my-category] The type \'Emoij\' is not valid.'

it 'should add error if value valid and remove invalid values', ->
elementType = name: 'Number'
result = @map.mapSet '1,2,"3",4',@customTypeDefinition.key,2,elementType
expect(result).toEqual [1,2,4]
result = @map.mapSet '1,2,"3",4', @customTypeDefinition.key, 2, elementType
expect(result).toEqual [1, 2, 4]
expect(@map.errors[0]).toBe "[row 2:my-category] The number '\"3\"' isn't valid!"

describe '::mapMoney', ->
Expand All @@ -237,7 +284,3 @@ describe 'Mappings', ->
result = @map.mapMoney 'invalid',@customTypeDefinition.key,2
expect(result).not.toBeDefined()
expect(@map.errors[0]).toBe "[row 2:my-category] Can not parse money 'invalid'!"
it 'should add error if currency in money is not a valid currency', ->
result = @map.mapMoney 'ABI 140',@customTypeDefinition.key,2
expect(result).not.toBeDefined()
expect(@map.errors[0]).toBe "[row 2:my-category] Parsed currency is not valid 'ABI 140'!"

0 comments on commit b21920b

Please sign in to comment.