-
Notifications
You must be signed in to change notification settings - Fork 21
/
agencyImporter.js
100 lines (95 loc) · 3.76 KB
/
agencyImporter.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
const db = require('../db');
class AgencyImporter {
agencyFromRow(row, adminUser) {
return {
name: row.name,
abbreviation: row.abbreviation,
parent: this.agencies[row.parent_name].id,
warning_threshold: Number.parseInt(row.warning_threshold, 10),
danger_threshold: Number.parseInt(row.danger_threshold, 10),
tenant_id: adminUser.tenant_id,
code: row.code,
};
}
checkRow(row, rowIndex) {
const ret = [];
const rowNumStr = `Row: ${rowIndex + 2}, `;
if (!row.name) {
ret.push(`${rowNumStr}name: Missing name`);
}
if (this.agencies[row.name]) {
ret.push(`${rowNumStr}name: Agency already exists: ${row.name}`);
}
if (!row.abbreviation) {
ret.push(`${rowNumStr}abbreviation: Missing abbreviation`);
}
if (!row.code) {
ret.push(`${rowNumStr}code: Missing code`);
}
if (!this.agencies[row.parent_name]) {
ret.push(`${rowNumStr}parent_name: Can't find parent: ${row.parent_name}`);
}
if (!row.warning_threshold) {
ret.push(`${rowNumStr}warning_threshold: Missing warning_threshold`);
}
const warn = Number.parseInt(row.warning_threshold, 10);
if (Number.isNaN(warn)) {
ret.push(`${rowNumStr}warning_threshold: should be integer, not ${row.warning_threshold}`);
} else if (warn < 1) {
ret.push(`${rowNumStr}warning_threshold: should be greater than zero, not ${row.warning_threshold}`);
}
if (!row.danger_threshold) {
ret.push(`${rowNumStr}danger_threshold: Missing danger_threshold`);
}
const danger = Number.parseInt(row.danger_threshold, 10);
if (Number.isNaN(danger)) {
ret.push(`${rowNumStr}danger_threshold: should be integer, not ${row.danger_threshold}`);
} else if (danger < 1) {
ret.push(`${rowNumStr}danger_threshold: should be greater than zero, not ${row.danger_threshold}`);
} else if (danger >= warn) {
ret.push(`${rowNumStr}danger_threshold: should be less than warning_threshold, not ${row.danger_threshold}`);
}
return ret;
}
async getAgencies(user) {
const agenciesArray = await db.getTenantAgencies(user.tenant_id);
this.agencies = {};
// eslint-disable-next-line no-restricted-syntax
for (const agency of agenciesArray) {
this.agencies[agency.name] = agency;
}
}
async import(user, rowsList) {
const retVal = {
status: {
agencies: {
added: 0,
errored: 0,
},
errors: [],
},
};
try {
await this.getAgencies(user);
for (let rowIndex = 0; rowIndex < rowsList.length; rowIndex += 1) {
const theErrors = this.checkRow(rowsList[rowIndex], rowIndex);
if (theErrors.length > 0) {
retVal.status.agencies.errored += 1;
retVal.status.errors.push(...theErrors);
} else {
const agency = this.agencyFromRow(rowsList[rowIndex], user);
// eslint-disable-next-line no-await-in-loop
await db.createAgency(agency, user.id);
// eslint-disable-next-line no-await-in-loop
await this.getAgencies(user);
retVal.status.agencies.added += 1;
}
}
} catch (e) {
console.log(e.toString());
retVal.status.errors.push(e.toString());
}
return retVal;
}
}
module.exports = AgencyImporter;