-
Notifications
You must be signed in to change notification settings - Fork 21
/
arpa-subrecipients.js
109 lines (90 loc) · 3.17 KB
/
arpa-subrecipients.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
const knex = require('../../db/connection');
const { useTenantId } = require('../use-request');
function baseQuery(trns) {
return trns('arpa_subrecipients')
.select(
'arpa_subrecipients.*',
'uploads.reporting_period_id AS reporting_period_id',
'users.email AS created_by',
'users2.email AS updated_by_email',
)
.leftJoin('uploads', 'arpa_subrecipients.upload_id', 'uploads.id')
.leftJoin('users', 'uploads.user_id', 'users.id')
.leftJoin('users AS users2', 'arpa_subrecipients.updated_by', 'users2.id');
}
/**
* Archive or restore a subrecipient.
*
* Call t his method to archive or restore an arpa_subrecipients table.
*/
async function archiveOrRestoreRecipient(id, { updatedByUser }, trns = knex) {
const query = trns('arpa_subrecipients')
.where('id', id)
.returning('*');
if (updatedByUser) {
query.update('updated_by', updatedByUser.id);
query.update('updated_at', knex.fn.now());
}
query.update(
'archived_at',
knex.raw('CASE WHEN archived_at IS NULL THEN ?? ELSE NULL END', [knex.fn.now()]),
);
return query.then((rows) => rows[0]);
}
async function createRecipient(recipient, trns = knex) {
const tenantId = useTenantId();
if (!(recipient.uei || recipient.tin)) {
throw new Error('recipient row must include a `uei` or a `tin` field');
}
return trns('arpa_subrecipients')
.insert({ ...recipient, tenant_id: tenantId })
.returning('*')
.then((rows) => rows[0]);
}
async function updateRecipient(id, { updatedByUser, record }, trns = knex) {
const query = trns('arpa_subrecipients')
.where('id', id)
.returning('*');
if (record) {
query.update('record', record);
}
if (updatedByUser) {
query.update('updated_by', updatedByUser.id);
query.update('updated_at', knex.fn.now());
}
return query.then((rows) => rows[0]);
}
async function getRecipient(id, trns = knex) {
return baseQuery(trns)
.where('arpa_subrecipients.id', id)
.then((rows) => rows[0]);
}
async function findRecipient(uei = null, tin = null, trns = knex) {
const tenantId = useTenantId();
const query = baseQuery(trns).where('arpa_subrecipients.tenant_id', tenantId);
if (uei) {
query.where('arpa_subrecipients.uei', uei);
} else if (tin) {
query.where('arpa_subrecipients.tin', tin);
} else {
return null;
}
return query.then((rows) => rows[0]);
}
async function listRecipients(trns = knex) {
const tenantId = useTenantId();
return baseQuery(trns).where('arpa_subrecipients.tenant_id', tenantId);
}
async function listRecipientsForReportingPeriod(periodId, trns = knex) {
return baseQuery(trns).where('reporting_period_id', periodId).whereNull('archived_at');
}
module.exports = {
archiveOrRestoreRecipient,
createRecipient,
getRecipient,
findRecipient,
updateRecipient,
listRecipients,
listRecipientsForReportingPeriod,
};
// NOTE: This file was copied from src/server/db/arpa-subrecipients.js (git @ ada8bfdc98) in the arpa-reporter repo on 2022-09-23T20:05:47.735Z