Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support archiving subrecipients #2164

Merged
merged 13 commits into from
Jan 19, 2024
89 changes: 63 additions & 26 deletions packages/client/src/arpa_reporter/views/Subrecipients.vue
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
<style scoped>
.strikethrough {
text-decoration: line-through;
}
</style>

<template>
<div>
<h2>Sub-recipients</h2>
Expand All @@ -23,31 +29,35 @@
</div>

<template slot="table-row" slot-scope="props">
<span v-if="props.column.field === 'upload_id'">
<router-link :to="`/uploads/${props.row.upload_id}`">
{{ props.row.upload_id }}
</router-link>
</span>

<ul v-else-if="props.column.field === 'record'" class="list-group list-group-flush">
<template v-for="(value, col) in props.row.json">
<template v-if="col === 'Unique_Entity_Identifier__c' || col === 'EIN__c'">
<div :class="{strikethrough: props.row.archived_at && props.column.field !== 'edit'}">
<span v-if="props.column.field === 'upload_id'">
<router-link :to="`/uploads/${props.row.upload_id}`">
{{ props.row.upload_id }}
</router-link>
</span>

<ul v-else-if="props.column.field === 'record'" class="list-group list-group-flush">
<template v-for="(value, col) in props.row.json">
<template v-if="col === 'Unique_Entity_Identifier__c' || col === 'EIN__c'">
</template>

<li v-else class="list-group-item" :key="col">
<span class="font-weight-bold">{{ col }}: </span>
{{ value }}
</li>
</template>

<li v-else class="list-group-item" :key="col">
<span class="font-weight-bold">{{ col }}: </span>
{{ value }}
</li>
</template>
</ul>

<span v-else-if="props.column.field === 'edit'">
<router-link tag="button" class="btn btn-sm btn-secondary" :to="`/subrecipients/${props.row.id}`">Edit</router-link>
</span>

<span v-else>
{{props.formattedRow[props.column.field]}}
</span>
</ul>

<span class="d-flex flex-column" v-else-if="props.column.field === 'edit'" >
<router-link v-if="!props.row.archived_at" tag="button" class="btn btn-sm btn-secondary mb-2" :to="`/subrecipients/${props.row.id}`">Edit</router-link>
<button v-if="props.row.archived_at" class="btn btn-sm btn-primary" @click="archiveOrRestoreSubrecipient(props.row.id)">Restore</button>
<button v-else class="btn btn-sm btn-outline-danger" @click="archiveOrRestoreSubrecipient(props.row.id)">Archive</button>
</span>

<span v-else>
{{props.formattedRow[props.column.field]}}
</span>
</div>
</template>

</vue-good-table>
Expand All @@ -59,7 +69,7 @@ import moment from 'moment';
import 'vue-good-table/dist/vue-good-table.css';
import { VueGoodTable } from 'vue-good-table';

import { getJson } from '../store/index';
import { getJson, post } from '../store/index';

export default {
name: 'Subrecipients',
Expand All @@ -71,7 +81,9 @@ export default {
},
computed: {
rows() {
return this.recipients.map((r) => ({ ...r, json: JSON.parse(r.record) }));
const rows = this.recipients.map((r) => ({ ...r, json: JSON.parse(r.record) }));
console.log(rows);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we want to leave the console.log in here?

return rows;
},
columns() {
return [
Expand Down Expand Up @@ -123,6 +135,31 @@ export default {
this.$refs.recipientsTable.reset();
this.$refs.recipientsTable.changeSort([]);
},

/**
* Archive or restore a subrecipient.
*
* Call this method to archive or unarchive a subrecipient record.
*
* @param {*} id
* The ID of the subrecipient to archive or restore.
*/
async archiveOrRestoreSubrecipient(id) {
this.loading = true;

const result = await post(`/api/subrecipients/archive/${id}`);
if (result.error) {
this.$store.commit('addAlert', {
text: `archiveOrRestoreSubrecipient Error (${result.status}): ${result.error}`,
level: 'err',
});
} else {
this.recipients = this.recipients.map((recipient) => (recipient.id === id ? result.recipient : recipient));
}

this.loading = false;
},

async loadRecipients() {
this.loading = true;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const audit_report = require('../../../../src/arpa_reporter/lib/audit-report');
const aws = require('../../../../src/lib/gost-aws');
const { withTenantId } = require('../helpers/with-tenant-id');

const OLD_AUDIT_REPORT_BUCKET = process.env.AUDIT_REPORT_BUCKET;

function handleUploadFake(type) {
if (type === 'success') {
return () => {
Expand All @@ -26,7 +28,11 @@ function handleUploadFake(type) {

describe('audit report generation', () => {
const sandbox = sinon.createSandbox();
beforeEach(() => {
process.env.AUDIT_REPORT_BUCKET = 'arpa-audit-reports';
});
afterEach(() => {
process.env.AUDIT_REPORT_BUCKET = OLD_AUDIT_REPORT_BUCKET;
sandbox.restore();
});
it('sendEmailWithLink creates a presigned url and sends email to recipient', async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.up = function (knex) {
return knex.schema.table('arpa_subrecipients', (table) => {
table.timestamp('archived_at');
});
};

/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.down = function (knex) {
return knex.schema.table('arpa_subrecipients', (table) => {
table.dropColumn('archived_at');
});
};
26 changes: 25 additions & 1 deletion packages/server/src/arpa_reporter/db/arpa-subrecipients.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,29 @@ function baseQuery(trns) {
.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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be required? At the very least, we might want to always update the updated_at field.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have a strong opinion here. I just copied this pattern from the updateRecipient method in the same file below. I figure they should at least be consistent with each other.

I did mark session as required in the API endpoint though, so it should always be set in practice.

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)) {
Expand Down Expand Up @@ -70,10 +93,11 @@ async function listRecipients(trns = knex) {
}

async function listRecipientsForReportingPeriod(periodId, trns = knex) {
return baseQuery(trns).where('reporting_period_id', periodId);
return baseQuery(trns).where('reporting_period_id', periodId).whereNotNull('archived_at');
}

module.exports = {
archiveOrRestoreRecipient,
createRecipient,
getRecipient,
findRecipient,
Expand Down
19 changes: 18 additions & 1 deletion packages/server/src/arpa_reporter/routes/subrecipients.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ const express = require('express');
const router = express.Router();
const { requireUser } = require('../../lib/access-helpers');

const { listRecipients, getRecipient, updateRecipient } = require('../db/arpa-subrecipients');
const {
listRecipients, getRecipient, updateRecipient, archiveOrRestoreRecipient,
} = require('../db/arpa-subrecipients');
const { getRules } = require('../services/validation-rules');

router.get('/', requireUser, async (req, res) => {
Expand Down Expand Up @@ -46,6 +48,21 @@ router.post('/:id', requireUser, async (req, res) => {
res.json({ recipient: updatedRecipient });
});

router.post('/archive/:id', requireUser, async (req, res) => {
const id = Number(req.params.id);
const { user } = req.session;

const recipient = await getRecipient(id);
if (!recipient || recipient.tenant_id !== req.session.user.tenant_id) {
res.sendStatus(404);
res.end();
return;
}

const updatedRecipient = await archiveOrRestoreRecipient(recipient.id, { updatedByUser: user });
res.json({ recipient: updatedRecipient });
});

module.exports = router;

// NOTE: This file was copied from src/server/routes/subrecipients.js (git @ ada8bfdc98) in the arpa-reporter repo on 2022-09-23T20:05:47.735Z
Loading