-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor and update mappings and CSV data handling. The header row is not static anymore and thus includes a major breaking change, in both the naming of the fields as in the removed static header option.
- Loading branch information
1 parent
1771e2e
commit 2fdad47
Showing
16 changed files
with
996 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
sku,quantity | ||
sku,quantityOnStock | ||
foo,9 | ||
bar,-1 | ||
SKU-123,42 | ||
SKU-123,42 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,4 +9,4 @@ | |
<quantity>1</quantity> | ||
<CommittedDeliveryDate>2013-11-19T00:00:00</CommittedDeliveryDate> | ||
</row> | ||
</root> | ||
</root> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
constants = | ||
HEADER_SKU: 'sku' | ||
HEADER_QUANTITY: 'quantityOnStock' | ||
DEPRECATED_HEADER_QUANTITY: 'quantity' | ||
HEADER_RESTOCKABLE: 'restockableInDays' | ||
HEADER_EXPECTED_DELIVERY: 'expectedDelivery' | ||
HEADER_SUPPLY_CHANNEL: 'supplyChannel' | ||
HEADER_CUSTOM_TYPE: 'customType' | ||
HEADER_CUSTOM_SEPERATOR: '.' | ||
HEADER_CUSTOM_REGEX: new RegExp /^customField\./ | ||
|
||
CHANNEL_KEY_FOR_XML_MAPPING: 'expectedStock' | ||
CHANNEL_REF_NAME: 'supplyChannel' | ||
CHANNEL_ROLES: ['InventorySupply', 'OrderExport', 'OrderImport'] | ||
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 | ||
exports[name] = value |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
_ = require 'underscore' | ||
_.mixin require('underscore-mixins') | ||
csv = require 'csv' | ||
CONS = require './constants' | ||
|
||
class CustomFieldMappings | ||
|
||
constructor: (options = {}) -> | ||
@errors = [] | ||
|
||
mapFieldTypes: ({fieldDefinitions, typeDefinitionKey, rowIndex, key, value, langHeader}) -> | ||
result = undefined | ||
_.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 | ||
result | ||
|
||
isValidValue: (rawValue) -> | ||
return _.isString(rawValue) and rawValue.length > 0 | ||
|
||
mapNumber: (rawNumber, typeDefinitionKey, rowIndex, regEx = CONS.REGEX_INTEGER) -> | ||
return unless @isValidValue(rawNumber) | ||
matchedNumber = regEx.exec rawNumber | ||
unless matchedNumber | ||
@errors.push "[row #{rowIndex}:#{typeDefinitionKey}] The number '#{rawNumber}' isn't valid!" | ||
return | ||
parseInt matchedNumber[0],10 | ||
### | ||
custom,customField.name.de,customField.name.en | ||
my-type,Hajo,Abi | ||
//- { | ||
custom: { | ||
name: { | ||
de: 'Hajo', | ||
en: 'Abi' | ||
} | ||
} | ||
} | ||
### | ||
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 | ||
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) | ||
|
||
mapBoolean: (rawBoolean, typeDefinitionKey, rowIndex) -> | ||
result = undefined | ||
if _.isUndefined(rawBoolean) or (_.isString(rawBoolean) and _.isEmpty(rawBoolean)) | ||
return | ||
errorMsg = "[row #{rowIndex}:#{typeDefinitionKey}] The value '#{rawBoolean}' isn't a valid boolean!" | ||
try | ||
b = JSON.parse(rawBoolean.toLowerCase()) | ||
if not _.isBoolean b | ||
@errors.push error | ||
return | ||
b | ||
catch | ||
@errors.push errorMsg | ||
return | ||
|
||
|
||
|
||
# EUR 300 | ||
# USD 999 | ||
mapMoney: (rawMoney, typeDefinitionKey, rowIndex) -> | ||
return unless @isValidValue(rawMoney) | ||
matchedMoney = CONS.REGEX_MONEY.exec rawMoney | ||
unless matchedMoney | ||
@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 | ||
|
||
module.exports = CustomFieldMappings |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.