Skip to content

Commit

Permalink
fix: GEO-1194 - admin tool - lock/unlock no longer clobbers calculate…
Browse files Browse the repository at this point in the history
…d data (#821)
  • Loading branch information
banders authored Oct 24, 2024
1 parent daf9052 commit 169f939
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 30 deletions.
2 changes: 1 addition & 1 deletion backend/src/v1/services/admin-report-service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,7 @@ describe('admin-report-service', () => {
expect(report.is_unlocked).toBeTruthy();
expect(report.admin_modified_date).toBe(report.report_unlock_date);
expect(report.admin_user_id).toBe('1234');
expect(reportService.movePublishedReportToHistory).toHaveBeenCalled();
expect(reportService.copyPublishedReportToHistory).toHaveBeenCalled();
});
it('should change report is_unlocked to false', async () => {
const report = await adminReportService.changeReportLockStatus(
Expand Down
2 changes: 1 addition & 1 deletion backend/src/v1/services/admin-report-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ const adminReportService = {

const currentDateUtc = convert(ZonedDateTime.now(ZoneId.UTC)).toDate();

await reportService.movePublishedReportToHistory(tx, existingReport);
await reportService.copyPublishedReportToHistory(tx, existingReport);

await prisma.pay_transparency_report.update({
where: { report_id: reportId },
Expand Down
40 changes: 23 additions & 17 deletions backend/src/v1/services/report-service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ import {
} from './report-service';
import { utils } from './utils-service';

const actualMovePublishedReportToHistory =
reportService.movePublishedReportToHistory;
const actualCopyPublishedReportToHistory =
reportService.copyPublishedReportToHistory;

jest.mock('./utils-service');

Expand Down Expand Up @@ -810,14 +810,14 @@ describe('publishReport', () => {
it('changes the status from Draft to Published', async () => {
mockReportFindFirst.mockResolvedValue(null);
jest
.spyOn(reportService, 'movePublishedReportToHistory')
.spyOn(reportService, 'copyPublishedReportToHistory')
.mockReturnValueOnce(null);

await reportService.publishReport(mockDraftReportInApi);

// Expect no attempt to move a pre-existing published report to
// history (because there is no pre-existing published report)
expect(reportService.movePublishedReportToHistory).toHaveBeenCalledTimes(
expect(reportService.copyPublishedReportToHistory).toHaveBeenCalledTimes(
0,
);

Expand Down Expand Up @@ -853,17 +853,25 @@ describe('publishReport', () => {
pay_transparency_calculated_data: [],
});
jest
.spyOn(reportService, 'movePublishedReportToHistory')
.spyOn(reportService, 'copyPublishedReportToHistory')
.mockReturnValueOnce(null);

await reportService.publishReport(mockDraftReportInApi);

// Expect an attempt to move the pre-existing published report to
// history
expect(reportService.movePublishedReportToHistory).toHaveBeenCalledTimes(
expect(reportService.copyPublishedReportToHistory).toHaveBeenCalledTimes(
1,
);

// Expect the calculated data of the existing published report
// was deleted (after it was copied to history).
expect(mockCalculatedDataDeleteMany).toHaveBeenCalledTimes(1);
const deleteCalcData = mockCalculatedDataDeleteMany.mock.calls[0][0];
expect(deleteCalcData.where.report_id).toBe(
mockPublishedReportInDb.report_id,
);

// Expect one call to update a DB record in the pay_transparency_report
// table
expect(prisma.pay_transparency_report.update).toHaveBeenCalledTimes(1);
Expand Down Expand Up @@ -902,7 +910,7 @@ describe('publishReport', () => {
});

jest
.spyOn(reportService, 'movePublishedReportToHistory')
.spyOn(reportService, 'copyPublishedReportToHistory')
.mockReturnValueOnce(null);

await expect(
Expand All @@ -916,25 +924,25 @@ describe('publishReport', () => {
});
});

describe('movePublishedReportToHistory', () => {
describe('copyPublishedReportToHistory', () => {
describe("if the given report isn't Published", () => {
it('throws an error', async () => {
const tx = jest.fn();
await expect(
actualMovePublishedReportToHistory(tx, mockDraftReportInDb),
actualCopyPublishedReportToHistory(tx, mockDraftReportInDb),
).rejects.toThrow();
});
});
describe('if the given report is Published', () => {
it("copy it to history, delete it's calculated data, and delete the original record from reports", async () => {
it('copies the report and its calculated data to history tables', async () => {
(prisma.report_history.create as jest.Mock).mockResolvedValue(
mockHistoryReport,
);
(
prisma.pay_transparency_calculated_data.findMany as jest.Mock
).mockResolvedValue(mockCalculatedDatasInDB);
await prisma.$transaction(async (tx) => {
await actualMovePublishedReportToHistory(tx, mockPublishedReportInDb);
await actualCopyPublishedReportToHistory(tx, mockPublishedReportInDb);
});

// Confirm that the report was copied to the history table
Expand Down Expand Up @@ -967,12 +975,10 @@ describe('movePublishedReportToHistory', () => {
mockHistoryReport.report_history_id,
);

// Confirm that the calculated datas were deleted
expect(mockCalculatedDataDeleteMany).toHaveBeenCalledTimes(1);
const deleteCalcData = mockCalculatedDataDeleteMany.mock.calls[0][0];
expect(deleteCalcData.where.report_id).toBe(
mockPublishedReportInDb.report_id,
);
// Confirm that the calculated data was *not* deleted
// (This check is done because a previous implementation of
// copyPublishedReportToHistory did delete the calculated data)
expect(mockCalculatedDataDeleteMany).toHaveBeenCalledTimes(0);
});
});
});
Expand Down
22 changes: 11 additions & 11 deletions backend/src/v1/services/report-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1246,7 +1246,15 @@ const reportService = {
},
});

await this.movePublishedReportToHistory(tx, existing_published_report);
await this.copyPublishedReportToHistory(tx, existing_published_report);

// Delete the calculated data associated with the report (it's already been
// copied to history, and it will be rebuilt below)
await tx.pay_transparency_calculated_data.deleteMany({
where: {
report_id: existing_published_report.report_id,
},
});

// Update existing report
await tx.pay_transparency_report.update({
Expand Down Expand Up @@ -1447,11 +1455,10 @@ const reportService = {
* Copies the report and calculated data to the history table.
* Copies the report to the report history with a report_history_id.
* Copies the calculated data to the history associated with a report_history_id.
* Removes the calculated data.
* @param tx
* @param report - The entire report which should be moved
* @param report - The entire report which should be copied
*/
async movePublishedReportToHistory(tx, report: pay_transparency_report) {
async copyPublishedReportToHistory(tx, report: pay_transparency_report) {
if (report.report_status != enumReportStatus.Published) {
throw new Error(
`Only a ${enumReportStatus.Published} report can be moved to history.`,
Expand Down Expand Up @@ -1482,13 +1489,6 @@ const reportService = {
await tx.calculated_data_history.createMany({
data: updatedData,
});

// Delete the calculated data
await tx.pay_transparency_calculated_data.deleteMany({
where: {
report_id: report.report_id,
},
});
},
};

Expand Down

0 comments on commit 169f939

Please sign in to comment.