forked from politics-rewired/Spoke
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassociate-messages.js
112 lines (97 loc) · 2.89 KB
/
associate-messages.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
const moment = require("moment");
const config = {
client: "mysql",
connection: process.env.DATABASE_URL,
pool: {
min: 2,
max: 30
}
};
const db = require("knex")(config);
const prettyPrint = obj => JSON.stringify(obj, null, 2);
const BATCH_SIZE = 10000;
let amountDone = 0;
let lastId = 0;
main()
.then(console.log)
.catch(console.error);
const IGNORE = [];
async function main() {
const messages = await db("message")
.whereNull("campaign_contact_id")
.where("id", ">", lastId)
// .where('queued_at', '>', "2019-02-19T21:02:31.000Z")
.limit(BATCH_SIZE);
lastId = messages[messages.length - 1].id;
if (messages.length == 0) {
console.log("Fully done!");
process.exit();
}
await Promise.all(
messages.map(async message => {
try {
await associateMessage(message);
} catch (ex) {
// console.error(ex)
}
})
);
amountDone = amountDone + messages.length;
console.log(`Did ${amountDone}`);
return main();
}
async function associateMessage(message) {
const matchingCampaignContact = await findMatchingCampaignContact(message);
const update = await db("message")
.where({ id: message.id })
.update({ campaign_contact_id: matchingCampaignContact.id });
return update;
}
async function findMatchingCampaignContact(message) {
// console.log(`\n\nDoing ${message.id}\n\n`)
const campaign_contacts = await db("campaign_contact").where({
cell: message.contact_number
});
const with_same_assignment_id = campaign_contacts.filter(
cc => cc.assignment_id == message.assignment_id
);
let selection;
try {
selection = await selectCampaignContact(
message,
with_same_assignment_id,
"SAME ASSIGNMENT"
);
} catch (ex) {
// console.log('Looking for other assignment ids')
selection = await selectCampaignContact(
message,
campaign_contacts,
"ALL ASSIGNMENTS"
);
}
return selection;
}
async function selectCampaignContact(message, options, mode) {
if (options.length == 1) {
return options[0];
} else {
const created_before_message = options.filter(cc => {
const message_created_at = moment(message.created_at);
const cc_created_at = moment(cc.created_at);
// console.log({ message_created_at, cc_created_at })
return cc_created_at.isBefore(message_created_at);
});
if (created_before_message.length == 1) {
return created_before_message[0];
} else if (created_before_message.length == 0) {
throw new Error(`Could not associate message with campaign contact in mode ${mode}: ${prettyPrint(
message
)}.
Options were all too late: ${prettyPrint(options)}`);
} else {
return created_before_message[0];
// throw new Error(`Multiple options for associations in mode ${mode} with message ${prettyPrint(message)}: options are ${prettyPrint(created_before_message)}`)
}
}
}