Skip to content

Commit

Permalink
[Closes #447] Add deletedAt to Reports for soft-delete support
Browse files Browse the repository at this point in the history
  • Loading branch information
francisli committed Sep 4, 2024
1 parent 918a4c5 commit 2cc63ec
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 1 deletion.
14 changes: 14 additions & 0 deletions migrations/20240904232001-add-deleted-at-to-reports.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/** @type {import('sequelize-cli').Migration} */
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.sequelize.transaction(async (transaction) => {
await queryInterface.addColumn('reports', 'deleted_at', Sequelize.DATE, { transaction });
});
},

async down(queryInterface, Sequelize) {
await queryInterface.sequelize.transaction(async (transaction) => {
await queryInterface.removeColumn('reports', 'deleted_at', { transaction });
});
},
};
1 change: 1 addition & 0 deletions models/patient.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const PatientPriority = {
EXPECTANT: 3,
DECEASED: 4,
TRANSPORTED: 5,
DELETED: 6,
};
Object.freeze(PatientPriority);

Expand Down
17 changes: 16 additions & 1 deletion models/report.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ module.exports = (sequelize, DataTypes) => {
'signatureIds',
'ringdownId',
'predictions',
'deletedAt',
],
options,
);
Expand Down Expand Up @@ -478,6 +479,7 @@ module.exports = (sequelize, DataTypes) => {
'predictions',
'data',
'isValid',
'deletedAt',
'createdAt',
'createdById',
'createdByAgencyId',
Expand All @@ -492,7 +494,13 @@ module.exports = (sequelize, DataTypes) => {
filterPriority: {
type: DataTypes.VIRTUAL(DataTypes.INTEGER),
get() {
return this.disposition?.destinationFacilityId ? sequelize.models.Patient.Priority.TRANSPORTED : this.patient?.priority;
if (this.isDeleted) {
return sequelize.models.Patient.Priority.DELETED;
}
if (this.disposition?.destinationFacilityId) {
return sequelize.models.Patient.Priority.TRANSPORTED;
}
return this.patient?.priority;
},
},
isCanonical: {
Expand All @@ -514,6 +522,13 @@ module.exports = (sequelize, DataTypes) => {
signatureIds: DataTypes.JSONB,
ringdownId: DataTypes.STRING,
predictions: DataTypes.JSONB,
deletedAt: DataTypes.DATE,
isDeleted: {
type: DataTypes.VIRTUAL(DataTypes.BOOLEAN, ['deletedAt']),
get() {
return !!this.deletedAt;
},
},
emsDataSet: DataTypes.TEXT,
emsDataSetFile: DataTypes.STRING,
emsDataSetFileUrl: {
Expand Down
16 changes: 16 additions & 0 deletions test/unit/models/report.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,22 @@ describe('models', () => {
});
assert.deepStrictEqual(report.filterPriority, models.Patient.Priority.TRANSPORTED);
});

it('returns DELETED if Report is soft-deleted', async () => {
const user = await models.User.findByPk('ffc7a312-50ba-475f-b10f-76ce793dc62a');
const agency = await models.Agency.findByPk('9eeb6591-12f8-4036-8af8-6b235153d444');
// soft-delete the Report by setting deletedAt
await models.Report.createOrUpdate(user, agency, {
id: '447d3625-744c-4622-b20f-3305c4093811',
parentId: 'c19bb731-5e9e-4feb-9192-720782ecf9a8',
deletedAt: new Date().toISOString(),
});
// now check canonical record
const report = await models.Report.findByPk('9242e8de-9d22-457f-96c8-00a43dfc1f3a', {
include: ['disposition', 'patient'],
});
assert.deepStrictEqual(report.filterPriority, models.Patient.Priority.DELETED);
});
});

describe('createOrUpdate()', () => {
Expand Down

0 comments on commit 2cc63ec

Please sign in to comment.