Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // this is what was originally run, then I realized that it would only update one deliverable.
- // it wouldn't create a contact relation for all deliverables within an entity
- const updateEntities() => {
- allObjects.getAll((error, result) => {
- let deliverables = result.deliverables;
- let entities = result.entities;
- let i = 0;
- async.whilst(
- () => {return i < deliverables.length;},
- (callback) => {
- let deliverable = deliverables[i];
- let entity = entities[deliverable.entityId];
- i++;
- let entityDeliverablesContact = entity.deliverablesContact;
- let deliverablesContact = deliverable.deliverablesContact;
- if(deliverablesContact.contact === undefined && entityDeliverablesContact.email !== undefined) {
- pool.query(`UPDATE contact_relations SET parent_id = $1 WHERE child_id = $2 AND type = 'Deliverables Contact' AND parent_id = $3`, [deliverable.id, entityDeliverablesContact.email.id, entity.id], (error, result) => {
- if(error) {
- callback(error);
- }
- else {
- console.log("Updated contact");
- callback();
- }
- });
- }
- else {
- callback();
- }
- },
- (error) => {
- if(error) {
- console.log(error);
- }
- else {
- console.log("done");
- }
- }
- );
- });
- };
- // so then I ran this one. this gets all deliverables and if it has a
- // deliverables contact, finds all of the deliverables of its entity, then
- // makes a contact relation for each of them
- const updateDeliverables = (callback) => {
- allObjects.getAll((error, result) => {
- let deliverables = result.deliverables;
- let entities = result.entities;
- let i = 0;
- async.whilst(
- () => {return i < deliverables.length;},
- (callback) => {
- let deliverable = deliverables[i];
- i++;
- let entity = entities[deliverable.entityId];
- let deliverablesContact = deliverable.deliverablesContact;
- if(deliverablesContact.contact !== undefined && deliverablesContact.type === "Deliverables Contact") {
- let j = 0;
- async.whilst(
- () => {return j < deliverables.length;},
- (callback) => {
- let deliv = deliverables[j];
- j++;
- if(deliv.entityId === entity.id && deliv.id !== deliverable.id && deliv.deliverablesContact.contact === undefined) {
- // insert
- pool.query("INSERT INTO contact_relations (type, child_id, parent_id) VALUES ('Deliverables Contact', $1, $2)", [deliverablesContact.email.id, deliv.id], (error, result) => {
- if(error) {
- callback(error);
- }
- });
- }
- process.nextTick(callback);
- },
- (error) => {
- if(error) {
- callback(error);
- }
- else {
- process.nextTick(callback);
- }
- }
- );
- }
- else {
- process.nextTick(callback);
- }
- },
- (error) => {
- if(error) {
- console.log(error);
- }
- else {
- console.log("done");
- }
- }
- );
- callback();
- });
- };
Add Comment
Please, Sign In to add comment