From d915b575fb7a3572eec3e637892d0030885c5d19 Mon Sep 17 00:00:00 2001 From: Brock Anderson Date: Wed, 17 Jul 2024 19:12:10 -0700 Subject: [PATCH] fix: GEO-988 - use UTC timezone for dates saved to the pay_transparency_reports table (#604) --- .../v1/services/admin-report-service.spec.ts | 25 +++++++++++++++++++ .../src/v1/services/admin-report-service.ts | 8 +++--- .../src/v1/services/report-service.spec.ts | 9 +++++++ backend/src/v1/services/report-service.ts | 4 +-- 4 files changed, 40 insertions(+), 6 deletions(-) diff --git a/backend/src/v1/services/admin-report-service.spec.ts b/backend/src/v1/services/admin-report-service.spec.ts index 9bd4baa0..63c63fd8 100644 --- a/backend/src/v1/services/admin-report-service.spec.ts +++ b/backend/src/v1/services/admin-report-service.spec.ts @@ -591,6 +591,31 @@ describe('admin-report-service', () => { expect(report.admin_modified_date).toBe(report.report_unlock_date); expect(report.admin_user_id).toBe('1234'); }); + it('should update the report_unlock_date to the current date/time in UTC', async () => { + const updateSpy = jest.spyOn( + prismaClient.pay_transparency_report, + 'update', + ); + await adminReportService.changeReportLockStatus( + '4492feff-99d7-4b2b-8896-12a59a75d4e2', + '5678', + false, + ); + + const updateParam: any = updateSpy.mock.calls[0][0]; + expect(updateParam.data.admin_modified_date).toBe( + updateParam.data.report_unlock_date, + ); + + //check that the unlock report date/time submitted to the database is approximately + //equal to the current UTC date/time + const currentDateUtc = convert(ZonedDateTime.now(ZoneId.UTC)).toDate(); + const unlockDateDiffMs = + currentDateUtc.getTime() - + updateParam.data.report_unlock_date.getTime(); + expect(unlockDateDiffMs).toBeGreaterThanOrEqual(0); + expect(unlockDateDiffMs).toBeLessThan(10000); //10 seconds + }); }); describe('getReportPdf', () => { diff --git a/backend/src/v1/services/admin-report-service.ts b/backend/src/v1/services/admin-report-service.ts index ac4694ec..b0f5ea8b 100644 --- a/backend/src/v1/services/admin-report-service.ts +++ b/backend/src/v1/services/admin-report-service.ts @@ -1,4 +1,4 @@ -import { convert, LocalDateTime, ZonedDateTime, ZoneId } from '@js-joda/core'; +import { convert, ZonedDateTime, ZoneId } from '@js-joda/core'; import { Prisma } from '@prisma/client'; import prisma from '../prisma/prisma-client'; import prismaReadOnlyReplica from '../prisma/prisma-client-readonly-replica'; @@ -136,14 +136,14 @@ const adminReportService = { where: { report_id: reportId }, }); - const currentDate = convert(LocalDateTime.now(ZoneId.UTC)).toDate(); + const currentDateUtc = convert(ZonedDateTime.now(ZoneId.UTC)).toDate(); await prisma.pay_transparency_report.update({ where: { report_id: reportId }, data: { is_unlocked: isUnLocked, - report_unlock_date: currentDate, - admin_modified_date: currentDate, + report_unlock_date: currentDateUtc, + admin_modified_date: currentDateUtc, admin_user: { connect: { idir_user_guid: idirGuid } }, }, }); diff --git a/backend/src/v1/services/report-service.spec.ts b/backend/src/v1/services/report-service.spec.ts index 1fdc883c..84ea80fc 100644 --- a/backend/src/v1/services/report-service.spec.ts +++ b/backend/src/v1/services/report-service.spec.ts @@ -2,6 +2,7 @@ import { LocalDate, TemporalAdjusters, ZoneId, + ZonedDateTime, convert, nativeJs, } from '@js-joda/core'; @@ -878,6 +879,14 @@ describe('publishReport', () => { expect(updateStatement.where.report_id).toBe( mockPublishedReportInDb.report_id, ); + + // Expect the update date to be approximately equal to the current date + // in the UTC timezone (within 10 seconds) + const currentDateUtc = convert(ZonedDateTime.now(ZoneId.UTC)).toDate(); + const dateDiffMs = + currentDateUtc.getTime() - updateStatement.data.update_date.getTime(); + expect(dateDiffMs).toBeGreaterThanOrEqual(0); + expect(dateDiffMs).toBeLessThan(10000); //10 seconds }); }); describe('if the given report has status=Draft, and there is an existing Published and locked report', () => { diff --git a/backend/src/v1/services/report-service.ts b/backend/src/v1/services/report-service.ts index 627eb9ac..60e1b801 100644 --- a/backend/src/v1/services/report-service.ts +++ b/backend/src/v1/services/report-service.ts @@ -1,8 +1,8 @@ import { LocalDate, - LocalDateTime, TemporalAdjusters, ZoneId, + ZonedDateTime, convert, nativeJs, } from '@js-joda/core'; @@ -1321,7 +1321,7 @@ const reportService = { pay_transparency_user: { connect: { user_id: full_report_to_publish.user_id }, }, - update_date: convert(LocalDateTime.now(ZoneId.UTC)).toDate(), + update_date: convert(ZonedDateTime.now(ZoneId.UTC)).toDate(), //current date/time in the UTC timezone update_user: full_report_to_publish.update_user, pay_transparency_calculated_data: { createMany: {