-
Notifications
You must be signed in to change notification settings - Fork 2
/
constants.js
68 lines (61 loc) · 1.68 KB
/
constants.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
const AT_FIELDS = {};
const pullAtFieldFromEnv = (fieldId) => {
const envVar = `AIRTABLE_FIELDS_${fieldId}`;
const val = process.env[envVar];
if (!val) {
throw new Error(`Variable not defined: ${envVar}`)
}
AT_FIELDS[fieldId] = val;
}
[
'ID_PARTICIPANT',
'RECRUITS_LIST',
'NAME',
'TODAY',
'RECRUITED_BY_ID',
'PHONE',
'CARRIER',
'REF_PHONE',
'REF_CARRIER',
].forEach((f) => pullAtFieldFromEnv(f));
const requireEnvironmentVariable = (varname, note) => {
const val = process.env[varname];
if (!val) {
throw new Error(`Missing environment variable: ${varname} ${note}`)
}
return val;
}
const KOBODATA = [
['KOBODATA_INDEXED_NAME', `e.g. RECRUITMENT/RECRUIT#_NAME`],
['KOBODATA_INDEXED_PHONE', `e.g. RECRUITMENT/RECRUIT#_PHONE`],
['KOBODATA_ID_PARTICIPANT', `e.g. id_participant`],
['KOBODATA_TODAY', `e.g. today`],
['KOBODATA_CARRIER', `e.g. RECRUITMENT/CARRIER`],
['KOBODATA_PHONE_INCENTIVE', `e.g. RECRUITMENT/PHONE_INCENTIVE`],
].reduce((obj, [vname, note]) => {
const val = process.env[vname];
if (!val) {
throw new Error(`Missing process.env: ${vname}\n${note}`);
}
let shortname = vname.replace('KOBODATA_', '');
return {
...obj,
[shortname]: val,
}
}, {});
['INDEXED_NAME', 'INDEXED_PHONE'].forEach((vname)=> {
if (!KOBODATA[vname].match(/#/)) {
throw new Error(`process.env.KOBODATA_${vname} must have a "#" in its value`);
}
})
module.exports = {
// field names pulled from the kobo submission
KOBODATA,
// Fields used in the column headers of the AirTable (AT) document
atfield: (vname) => {
if (!AT_FIELDS[vname]) {
throw new Error('missing field: ' + vname);
}
return AT_FIELDS[vname];
},
}