Created
September 3, 2019 10:58
-
-
Save Callonski/0b7d7098ef16a29a13fbe21112a18dcf 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
// Transaction for atomic execution into all tables | |
database.runTransaction(async (err, transaction) => { | |
if (err) { | |
console.error("Error running transaction: " + err); | |
return; | |
} | |
if (profileExists) { | |
const deleted = await deleteProfile(retailUnit, partyUId, transaction); | |
console.log(`Deleted customer ${deleted} ${partyUId}`); | |
} | |
// if (profileStatus == "Active") { | |
transaction.upsert(tableNames.customer, customerData); | |
if (json.contactMethods && json.contactMethods.length > 0) { | |
let contactArray = json.contactMethods.map(method => { | |
return new contactMethod(method, partyUId, retailUnit, operation); | |
}); | |
transaction.upsert(tableNames.contact, contactArray); | |
} | |
if (json.addresses && json.addresses.length > 0) { | |
let addressArray = json.addresses.map(addres => { | |
return new address(addres, partyUId, retailUnit, operation); | |
}); | |
transaction.upsert(tableNames.address, addressArray); | |
} | |
if (json.children && json.children.length > 0) { | |
let childrenArray = json.children.map(child => { | |
return new childObject(child, partyUId, retailUnit, operation); | |
}); | |
transaction.upsert(tableNames.child, childrenArray); | |
} | |
if (json.cifAccountReference && json.cifAccountReference.length > 0) { | |
let filteredArray = json.cifAccountReference.filter(reference => { | |
if (reference.systemNumber && reference.systemNumber === 'ICMC') { | |
return false; | |
} else { | |
return true; | |
} | |
}); | |
if (filteredArray && filteredArray.length > 0) { | |
let crossArray = filteredArray.map(reference => { | |
return new crossReference(reference, partyUId, retailUnit, operation); | |
}); | |
transaction.upsert(tableNames.crossRef, crossArray); | |
} | |
} | |
if (json.contactResidences && json.contactResidences.length > 0) { | |
let residenceArray = json.contactResidences.map(residence => { | |
return new residenceObject(residence, partyUId, retailUnit, operation); | |
}); | |
transaction.upsert(tableNames.residence, residenceArray); | |
} | |
if (json.interestAreas && json.interestAreas.length > 0) { | |
let interestArray = json.interestAreas.map(area => { | |
return new interestAreaObject(area, partyUId, retailUnit, operation); | |
}); | |
transaction.upsert(tableNames.interest, interestArray); | |
} | |
if (json.serviceQuestionnaires && json.serviceQuestionnaires.length > 0) { | |
let questionArray = json.serviceQuestionnaires.map(questionnaire => { | |
return new serviceQuestionnaireObject(questionnaire, partyUId, retailUnit, operation); | |
}); | |
transaction.upsert(tableNames.questionaire, questionArray); | |
} | |
if (json.customerIdentifiers && json.customerIdentifiers.length > 0) { | |
let identifierArray = json.customerIdentifiers.map(identifier => { | |
return new identifierObject(identifier, partyUId, retailUnit, operation); | |
}); | |
transaction.upsert(tableNames.identifier, identifierArray); | |
} | |
if (json.integrityCodes && json.integrityCodes.length > 0) { | |
let consentArray = json.integrityCodes.map(code => { | |
return new consentObject(code, partyUId, retailUnit, operation); | |
}); | |
transaction.upsert(tableNames.consent, consentArray); | |
} | |
if (json.loyaltyMemberships && json.loyaltyMemberships.length > 0) { | |
let loyaltyCardArray = []; | |
let loyaltyProgArray = json.loyaltyMemberships.map(membership => { | |
if (membership.membershipCards) { | |
membership.membershipCards.forEach(card => { | |
loyaltyCardArray.push(new loyaltyCardObject(card, partyUId, retailUnit, membership.id, operation)); | |
}); | |
} | |
return new loyaltyProgObject(membership, partyUId, retailUnit, operation); | |
}); | |
transaction.upsert(tableNames.loyaltyProg, loyaltyProgArray); | |
if (loyaltyCardArray.length != 0) { | |
transaction.upsert(tableNames.loyaltyCard, loyaltyCardArray); | |
} | |
} | |
try { | |
await transaction.commit(); | |
console.log("Tables where successfully updated"); | |
logObject.saved = true; | |
log("spannerSyncLogs", logObject); | |
} catch (err) { | |
console.log("ERROR " + error); | |
logObject.error = error; | |
logObject.severity = error.severity; | |
log("spannerSyncLogs", logObject).then(result => { | |
return; | |
}); | |
} finally { | |
console.log("Finally - ending transaction"); | |
transaction.end(); | |
// Close the database when finished. | |
//await database.close()// ;Error: Database is closed. at SessionPool.<anonymous> | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment