Guest User

Untitled

a guest
Jan 20th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | None | 0 0
  1. // this is what was originally run, then I realized that it would only update one deliverable.
  2. // it wouldn't create a contact relation for all deliverables within an entity
  3. const updateEntities() => {
  4. allObjects.getAll((error, result) => {
  5. let deliverables = result.deliverables;
  6. let entities = result.entities;
  7.  
  8. let i = 0;
  9. async.whilst(
  10. () => {return i < deliverables.length;},
  11.  
  12. (callback) => {
  13. let deliverable = deliverables[i];
  14. let entity = entities[deliverable.entityId];
  15. i++;
  16.  
  17. let entityDeliverablesContact = entity.deliverablesContact;
  18. let deliverablesContact = deliverable.deliverablesContact;
  19. if(deliverablesContact.contact === undefined && entityDeliverablesContact.email !== undefined) {
  20. 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) => {
  21. if(error) {
  22. callback(error);
  23. }
  24. else {
  25. console.log("Updated contact");
  26. callback();
  27. }
  28. });
  29. }
  30. else {
  31. callback();
  32. }
  33. },
  34. (error) => {
  35. if(error) {
  36. console.log(error);
  37. }
  38. else {
  39. console.log("done");
  40. }
  41. }
  42. );
  43. });
  44. };
  45. // so then I ran this one. this gets all deliverables and if it has a
  46. // deliverables contact, finds all of the deliverables of its entity, then
  47. // makes a contact relation for each of them
  48. const updateDeliverables = (callback) => {
  49. allObjects.getAll((error, result) => {
  50. let deliverables = result.deliverables;
  51. let entities = result.entities;
  52. let i = 0;
  53. async.whilst(
  54. () => {return i < deliverables.length;},
  55.  
  56. (callback) => {
  57. let deliverable = deliverables[i];
  58. i++;
  59. let entity = entities[deliverable.entityId];
  60. let deliverablesContact = deliverable.deliverablesContact;
  61.  
  62. if(deliverablesContact.contact !== undefined && deliverablesContact.type === "Deliverables Contact") {
  63. let j = 0;
  64. async.whilst(
  65. () => {return j < deliverables.length;},
  66.  
  67. (callback) => {
  68. let deliv = deliverables[j];
  69. j++;
  70. if(deliv.entityId === entity.id && deliv.id !== deliverable.id && deliv.deliverablesContact.contact === undefined) {
  71. // insert
  72. pool.query("INSERT INTO contact_relations (type, child_id, parent_id) VALUES ('Deliverables Contact', $1, $2)", [deliverablesContact.email.id, deliv.id], (error, result) => {
  73. if(error) {
  74. callback(error);
  75. }
  76. });
  77. }
  78. process.nextTick(callback);
  79. },
  80.  
  81. (error) => {
  82. if(error) {
  83. callback(error);
  84. }
  85. else {
  86. process.nextTick(callback);
  87. }
  88. }
  89. );
  90. }
  91. else {
  92. process.nextTick(callback);
  93. }
  94. },
  95. (error) => {
  96. if(error) {
  97. console.log(error);
  98. }
  99. else {
  100. console.log("done");
  101. }
  102. }
  103. );
  104. callback();
  105. });
  106. };
Add Comment
Please, Sign In to add comment