Skip to content

Commit

Permalink
Refactor: is_archived: bool -> archived_at: timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Hyndman committed Dec 16, 2023
1 parent e5e0711 commit 8a80b21
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
8 changes: 4 additions & 4 deletions packages/client/src/arpa_reporter/views/Subrecipients.vue
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
</div>

<template slot="table-row" slot-scope="props">
<div :class="{strikethrough: props.row.is_archived && props.column.field !== 'edit'}">
<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 }}
Expand All @@ -49,8 +49,8 @@
</ul>

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

Expand Down Expand Up @@ -139,7 +139,7 @@ export default {
/**
* Archive or restore a subrecipient.
*
* Call this method to taggle the `is_archived` flag for a subrecipient record.
* Call this method to archive or unarchive a subrecipient record.
*
* @param {*} id
* The ID of the subrecipient to archive or restore.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/
exports.up = function (knex) {
return knex.schema.table('arpa_subrecipients', (table) => {
table.boolean('is_archived').notNullable().defaultTo(false);
table.timestamp('archived_at');
});
};

Expand All @@ -14,6 +14,6 @@ exports.up = function (knex) {
*/
exports.down = function (knex) {
return knex.schema.table('arpa_subrecipients', (table) => {
table.dropColumn('is_archived');
table.dropColumn('archived_at');
});
};
7 changes: 5 additions & 2 deletions packages/server/src/arpa_reporter/db/arpa-subrecipients.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function baseQuery(trns) {
/**
* Archive or restore a subrecipient.
*
* Call t his method to toggle the `is_archived` column in the arpa_subrecipients table.
* Call t his method to archive or restore an arpa_subrecipients table.
*/
async function archiveOrRestoreRecipient(id, { updatedByUser }, trns = knex) {
const query = trns('arpa_subrecipients')
Expand All @@ -29,7 +29,10 @@ async function archiveOrRestoreRecipient(id, { updatedByUser }, trns = knex) {
query.update('updated_at', knex.fn.now());
}

query.update('is_archived', knex.raw('NOT ??', ['is_archived']));
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]);
}
Expand Down

0 comments on commit 8a80b21

Please sign in to comment.