Created
January 17, 2019 00:11
-
-
Save Streek/b046fdf40f4cae8f8fe5446750220472 to your computer and use it in GitHub Desktop.
This file contains hidden or 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.fieldMappings = { | |
"firstName": { | |
"value": "AnnualRevenue", | |
"option": "never" | |
}, | |
} | |
this.config = { | |
"contact": { | |
"firstName": { | |
"label": "First Name", //where this field is displayed, this is the name we call it. | |
"defaults": { | |
"salesforce": "First_Name" | |
}, //this is also the default value for this provider (compare with DB) | |
"getter": "name", //this is our local accessor for this field (on ES object) | |
"specialFunction": "parseFirstName", //is this a special function like tech stack? if this exists check it first... | |
"remoteReadOnly": false, //use this for fields like ID that should be no overwritten. | |
} | |
} | |
} | |
this.provider = "salesforce" | |
function buildIntermediaryObject(localObject, remoteObject) { | |
//FOR EACH FIELD IN DB, | |
return Object.entries(this.config).reduce( (newObject, [internalKey, { defaults, remoteReadOnly }]) => { | |
const { value, option } = this.fieldMappings[internalKey]; | |
const remoteGetter = value || defaults[this.provider]; | |
if (option === "never") return newObject; //bail | |
if (option === "empty" && _.get(remoteObject, remoteGetter, null) !== null) return newObject; //if empty, and empty set, bail. | |
if (remoteReadOnly) { //if we are preserving the remote value, set it. | |
newObject[internalKey] = _.get(remoteObject, remoteGetter, null); | |
} else { | |
newObject[internalKey] = _.get(localObject, internalKey, _.get(remoteObject, remoteGetter, null)); //get remote values, then put local values on top. | |
} | |
return newObject; | |
}, {}); | |
} | |
function mergeLocalAndRemote(localObject, remoteObject) { | |
const newLocalObject = buildIntermediaryObject(localObject, remoteObject); | |
const newRemoteObject = Object.entries(this.config).reduce((newRemoteObject, [internalKey]) => { | |
const { value } = this.fieldMappings[internalKey]; | |
const remoteGetter = value || defaults[this.provider]; | |
newRemoteObject[remoteGetter] = newLocalObject[internalKey]; | |
return newRemoteObject; | |
}, {}); | |
const diff = Object.entries(newLocalObject).reduce(diff, ([key, values]) => { | |
if (values !== remoteObject[key]) { | |
diff[key] = values; | |
} | |
return diff; | |
}, {}); | |
return { | |
original: remoteObject, | |
merge: newRemoteObject | |
diff, | |
} | |
} | |
//usage... | |
//it assumes fieldMappings, config, and provider are set. | |
const newObject = buildIntermediaryObject(localObject, remoteObject); | |
const {original, merge, diff} = mergeLocalAndRemote(localObject, remoteObject); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment