Created
August 21, 2020 16:13
-
-
Save ahuggins-nhs/f94584f89adb6326b39d9ac34040c52a to your computer and use it in GitHub Desktop.
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
function foldIntoParentCB (result, objectResult, propertyName, objectName) { | |
if (propertyName === 'order' && objectName === 'Order') { | |
const { | |
externalOrderId, | |
shippingIntegrationId, | |
orderIntegrationId | |
} = objectResult | |
result.externalOrderId = externalOrderId | |
result.shippingIntegrationId = shippingIntegrationId | |
result.orderIntegrationId = orderIntegrationId | |
return true | |
} | |
if (propertyName == 'warehouse' && objectName === 'Account') { | |
const { | |
warehouseType, | |
name | |
} = objectResult | |
result.warehouseType = warehouseType | |
result.warehouseName = name | |
return true | |
} | |
return false | |
} | |
function modifyResultCB (result, objectName) { | |
if (objectName === 'OrderItem') { | |
return { | |
...result.product2, | |
...result.listing | |
} | |
} | |
return result | |
} | |
const skipProps = ['partitionkey', 'rowkey', 'timestamp', 'attributes'] | |
/** | |
* @param {any} object | |
* @param {string} name | |
* @param {FieldMapper} fieldMapper | |
*/ | |
function transformObject (object, name, fieldMapper, skipProps, foldIntoParent, modResult) { | |
const result = Object.create(null) | |
const objectMap = fieldMapper.getObjectMap(name) | |
const isSObject = (value) => typeof value.attributes === 'object' && typeof value.attributes.type === 'string' | |
const isSObjectArray = (value) => Array.isArray(value.records) | |
const foldToParent = typeof foldIntoParent === 'function' ? foldIntoParent : () => {} | |
const modifyResult = typeof modResult === 'function' ? modResult : (res) => res | |
const skipPropArr = Array.isArray(skipProps) ? skipProps : [] | |
for (const [key, value] of Object.entries(object || {})) { | |
const { propertyName } = objectMap.getFieldMap(key) | |
if (skipPropArr.includes(propertyName)) continue | |
if (propertyName) { | |
if (isSObject(value)) { | |
const objectName = value.attributes.type | |
const objectResult = transformObject(value, objectName, fieldMapper) | |
if (foldToParent(result, objectResult, propertyName, objectName)) continue | |
result[propertyName] = objectResult | |
continue | |
} | |
if (isSObjectArray(value)) { | |
const objectName = value.records[0].attributes.type | |
result[propertyName] = value.records.map(item => transformObject(item, objectName, fieldMapper)) | |
continue | |
} | |
result[propertyName] = value | |
} | |
} | |
return modifyResult(result) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment