Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
static _parseParcelInfo(data) {
const transitionMap = {
'parcel.height': 'measurements.heightInMillimeter',
'parcel.length': 'measurements.lengthInMillimeter',
'parcel.width': 'measurements.widthInMillimeter',
'parcel.weight': 'measurements.weightInGram',
'parcel.trackingId': 'trackingData.trackingId',
'parcel.providerTransaction': 'trackingData.providerTransaction',
'parcel.provider': 'trackingData.provider',
'parcel.carrier': 'trackingData.carrier',
'parcel.isReturn': 'trackingData.isReturn',
'parcel.items': 'items',
}
const parcel = {
id: data['parcel.id'],
}
// Build parcel object
Object.keys(data).forEach(fieldName => {
if (!transitionMap[fieldName]) return
// All values are loaded as a string
let fieldValue = data[fieldName]
// do not set empty values
if (fieldValue === '') return
// Cast measurements to Number
if (/^measurements/.test(transitionMap[fieldName]))
fieldValue = Number(fieldValue)
// Basic delivery object with delivery item
const delivery = {
id: data['delivery.id'],
items: [
{
// there can be multiple delivery items with same item.id and
// item.quantity therefore we use unique identifier _itemGroupId
_groupId: data._itemGroupId,
id: data['item.id'],
quantity: parseInt(data['item.quantity'], 10),
},
],
}
// Add parcel info if it is present
if (data['parcel.id']) {
const parcel = DeliveriesParser._parseParcelInfo(data)
if (parcel.measurements && Object.keys(parcel.measurements).length !== 4)
return Promise.reject(new Error('All measurement fields are mandatory'))
delivery.parcels = [parcel]
}
const order = {
orderNumber: data.orderNumber,
shippingInfo: {
deliveries: [delivery],
},
}
return Promise.resolve(order)
}