-
Notifications
You must be signed in to change notification settings - Fork 21
/
agencies.js
60 lines (50 loc) · 1.53 KB
/
agencies.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
// This file exists as a shim to replace ARPA Reporter's db/users.js
// Eventually, the functions in this file should have callsites updated to use GOST's existing methods
// and this file can be deleted.
const knex = require('../connection');
const gostDb = require('..');
const { useTenantId, useUser } = require('../../arpa_reporter/use-request');
async function agencies() {
const tenantId = useTenantId();
return gostDb.getTenantAgencies(tenantId);
}
async function agencyById(id) {
const rows = await gostDb.getAgency(id);
return rows[0];
}
function agencyByCode(code) {
const tenantId = useTenantId();
return knex('agencies')
.select('*')
.where({ tenant_id: tenantId, code });
}
async function createAgency(agency) {
const tenantId = useTenantId();
const { main_agency_id } = await knex('tenants')
.select('main_agency_id')
.where('id', tenantId)
.then((rows) => rows[0]);
const creator = useUser();
return gostDb.createAgency({
...agency,
parent: main_agency_id,
abbreviation: agency.code,
warning_threshold: 30,
danger_threshold: 15,
tenant_id: tenantId,
}, creator.id);
}
async function updateAgency(agency) {
await gostDb.setAgencyName(agency.id, agency.name);
await knex('agencies')
.where('id', agency.id)
.update({ code: agency.code });
return gostDb.getAgency(agency.id);
}
module.exports = {
agencies,
agencyById,
agencyByCode,
createAgency,
updateAgency,
};