-
Notifications
You must be signed in to change notification settings - Fork 2
/
updateReferrals.js
172 lines (149 loc) · 4.63 KB
/
updateReferrals.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
const { atfield } = require('./constants');
// Airtable configs
const { table } = require('./airtableBase');
// add a n millisecond delay to each request to airtable
const DELAY = 0;
// airtable api request page size (100 is default)
const RECORDS_PER_PAGE = 100;
const callAirtableRefresher = async () => {
const participants = await getParticipants();
const updates = calculateUpdates(participants);
if (updates.length > 0) {
return saveReferralUpdatesToAT(participants, updates);
} else {
return {
message: 'no referrals to update',
finished: [],
};
}
}
const allResults = (params) => {
let pages = [];
return new Promise((success, reject) => {
table.select(params).eachPage((records, fetchNextPage) => {
pages = [...pages, ...records];
setTimeout(() => {
fetchNextPage();
}, DELAY);
}, (err) => {
if (err) {
reject(err);
}
success(pages);
});
})
}
const getParticipants = async () => {
const results = await allResults({
pageSize: RECORDS_PER_PAGE,
fields: [atfield('ID_PARTICIPANT'),
atfield('NAME'),
atfield('RECRUITED_BY_ID'),
atfield('PHONE'),
atfield('REF_PHONE'),
atfield('CARRIER'),
atfield('REF_CARRIER'),
atfield('RECRUITS_LIST')],
});
return results.map((rec) => new Participant(rec));
}
const main = async () => callAirtableRefresher();
const PARTICIPANTS_BY_SIMPLE_ID = {};
class Participant {
constructor ({ _rawJson, fields, id }) {
this.id = id;
this.fields = fields;
this.createdTime = new Date(_rawJson.createdTime);
this._AT_NAME = fields[atfield('NAME')];
this._AT_RECRUITED_BY_ID = fields[atfield('RECRUITED_BY_ID')];
this._AT_RECRUITS = fields[atfield('RECRUITS_LIST')] || [];
this._AT_ID_OF_PARTICIPANT = fields[atfield('ID_PARTICIPANT')];
PARTICIPANTS_BY_SIMPLE_ID[this._AT_ID_OF_PARTICIPANT] = this;
this._CALCULATED_REFERRALS = [];
this._REFERRED_PARTICIPANTS = [];
this._update = { id: this.id, fields: {} };
}
setReferrer () {
this._REFERRER = PARTICIPANTS_BY_SIMPLE_ID[this._AT_RECRUITED_BY_ID];
if (this._REFERRER === this) {
throw new Error('Referrer is self')
}
if (this._REFERRER) {
this._REFERRER._CALCULATED_REFERRALS.push(this.id);
this._REFERRER._REFERRED_PARTICIPANTS.push(this);
}
}
setCarrierIfNeeded () {
if (this._REFERRER) {
const updateIfChanged = (orig, dest) => {
let val = this.fields[orig];
if (val && this._REFERRER.fields[dest] !== val) {
this._REFERRER._update.fields[dest] = val;
}
}
// copy these fields:
let ref_phone = atfield('REF_PHONE');
let ref_carrier = atfield('REF_CARRIER');
// to the REFERRER's airtable fields:
let Phone = atfield('PHONE')
let Carrier = atfield('CARRIER');
updateIfChanged(ref_phone, Phone);
updateIfChanged(ref_carrier, Carrier);
}
}
changes () {
const knownReferrals = this.stringifyList(this._AT_RECRUITS);
const derivedReferrals = this.stringifyList(this._CALCULATED_REFERRALS);
if (derivedReferrals !== knownReferrals) {
this._update.fields[atfield('RECRUITS_LIST')] = this._CALCULATED_REFERRALS;
}
return {...this._update}
}
stringifyList (arr) {
return JSON.stringify(arr.sort());
}
}
const calculateUpdates = (_participants) => {
let participants = _participants.sort((a, b)=>{
if (a.createdTime == b.createdTime) { return 0 }
return a.createdTime < b.createdTime ? -1 : 1
});
participants.forEach((p) => p.setReferrer());
participants.forEach((p) => p.setCarrierIfNeeded());
return participants.reduce((updates, participant) => {
const changes = participant.changes();
if (Object.keys(changes.fields).length === 0) {
return updates;
}
return [...updates, changes];
}, []);
}
const saveReferralUpdatesToAT = async (participants, updates) => {
const records = await table.update(updates);
let byId = {};
participants.forEach((p) => byId[p.id] = p );
let messages = [];
let finished = [];
records.forEach((record) => {
let { fields } = record;
let referredList = fields[atfield('RECRUITS_LIST')] || [];
let ids = referredList.map(
(pId) => (
byId[pId] ? `${byId[pId]._AT_ID_OF_PARTICIPANT}` : '?'
)
);
let destId = fields[atfield('ID_PARTICIPANT')];
finished.push([destId, ids]);
messages.push(
`UPDATED ${destId}: [${ids.join(', ')}]`
)
});
return {
finished,
message: messages.join('\n'),
};
}
module.exports = {
callAirtableRefresher,
main,
};