From d0b7a4abe2446396e2bcd44d62d4fa058816c157 Mon Sep 17 00:00:00 2001 From: Brijesh Date: Mon, 10 Aug 2026 11:34:15 -0700 Subject: [PATCH 1/3] #538 - fix plan version approval dates --- .../PlanSnapshot.amendmentSubmissions.spec.js | 65 +++++++++++++++++++ src/libs/db2/model/plansnapshot.ts | 14 ++-- 2 files changed, 72 insertions(+), 7 deletions(-) create mode 100644 __tests__/unit/PlanSnapshot.amendmentSubmissions.spec.js diff --git a/__tests__/unit/PlanSnapshot.amendmentSubmissions.spec.js b/__tests__/unit/PlanSnapshot.amendmentSubmissions.spec.js new file mode 100644 index 00000000..c3ce3ba7 --- /dev/null +++ b/__tests__/unit/PlanSnapshot.amendmentSubmissions.spec.js @@ -0,0 +1,65 @@ +import { describe, expect, it, vi } from 'vitest'; + +vi.mock('../../src/libs/db2/model/amendmenttype.js', () => ({ + default: { + find: vi.fn().mockResolvedValue([ + { id: 1, description: 'Minor Amendment' }, + { id: 2, description: 'Mandatory Amendment' }, + ]), + }, +})); + +describe('PlanSnapshot.fetchAmendmentSubmissions', () => { + it('pairs a mandatory amendment with its later approval', async () => { + const snapshots = [ + { + id: 1, + plan_id: 1444, + version: 1, + status_id: 12, + created_at: '2021-06-17T23:43:07.837Z', + snapshot: { statusId: 12, amendmentTypeId: null }, + given_name: 'Jason', + family_name: 'Caswell', + }, + { + id: 2, + plan_id: 1444, + version: 2, + status_id: 22, + created_at: '2024-02-22T23:38:15.337Z', + snapshot: { statusId: 22, amendmentTypeId: 2 }, + given_name: 'Taylor', + family_name: 'Grafton', + }, + { + id: 3, + plan_id: 1444, + version: 3, + status_id: 12, + created_at: '2024-05-30T23:00:48.697Z', + snapshot: { statusId: 12, amendmentTypeId: 2 }, + given_name: 'Tara', + family_name: 'Bogh', + }, + ]; + const query = { + leftJoin: vi.fn().mockReturnThis(), + select: vi.fn().mockReturnThis(), + where: vi.fn().mockReturnThis(), + orderBy: vi.fn().mockReturnThis(), + execute: vi.fn().mockResolvedValue(snapshots), + }; + const db = { selectFrom: vi.fn().mockReturnValue(query) }; + const { default: PlanSnapshot } = await import('../../src/libs/db2/model/plansnapshot.ts'); + + const result = await PlanSnapshot.fetchAmendmentSubmissions(db, 1444); + + expect(result[1]).toMatchObject({ + createdAt: '2024-02-22T23:38:15.337Z', + submittedBy: 'Taylor Grafton', + approvedAt: '2024-05-30T23:00:48.697Z', + approvedBy: 'Tara Bogh', + }); + }); +}); diff --git a/src/libs/db2/model/plansnapshot.ts b/src/libs/db2/model/plansnapshot.ts index e7e186cb..daa2f29a 100644 --- a/src/libs/db2/model/plansnapshot.ts +++ b/src/libs/db2/model/plansnapshot.ts @@ -94,7 +94,7 @@ export default class PlanSnapshot extends KyselyModel { 'user_account.given_name', ]) .where('plan_snapshot.plan_id', '=', planId) - .orderBy('plan_snapshot.created_at', 'desc'); + .orderBy('plan_snapshot.created_at', 'asc'); if (startDate) query = query.where('plan_snapshot.created_at', '<=', startDate); const results = await query.execute(); const response: any[] = []; @@ -102,7 +102,7 @@ export default class PlanSnapshot extends KyselyModel { let lastMinorAmendment: number | null = null; for (let index = 0; index < results.length; index++) { const row: any = results[index]; - const nextRow: any = results[index + 1]; + const previousRow: any = results[index - 1]; row.isCurrentLegalVersion = false; if (row.snapshot?.amendmentTypeId === 4) { response.push({ @@ -130,7 +130,7 @@ export default class PlanSnapshot extends KyselyModel { amendmentType: amendmentTypeArray[1], snapshot: row.snapshot, }); - } else if (row.status_id === 22 || (row.status_id === 23 && nextRow?.status_id !== 21)) { + } else if (row.status_id === 22 || (row.status_id === 23 && previousRow?.status_id !== 21)) { lastMandatoryAmendment = response.length; response.push({ id: row.id, @@ -175,10 +175,10 @@ export default class PlanSnapshot extends KyselyModel { } } } - const responseSorted = response.reverse(); - const currentLegalVersion = responseSorted.find( - (resp: any) => Plan.legalStatuses.indexOf(resp.snapshot.statusId) !== -1, - ); + const responseSorted = response; + const currentLegalVersion = [...responseSorted] + .reverse() + .find((resp: any) => Plan.legalStatuses.indexOf(resp.snapshot.statusId) !== -1); if (currentLegalVersion) currentLegalVersion.isCurrentLegalVersion = true; return responseSorted; } From f6d63f3176f1a61bd2c5833b5cb6523012c98a75 Mon Sep 17 00:00:00 2001 From: Brijesh Date: Mon, 10 Aug 2026 11:44:49 -0700 Subject: [PATCH 2/3] Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- .../PlanSnapshot.amendmentSubmissions.spec.js | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/__tests__/unit/PlanSnapshot.amendmentSubmissions.spec.js b/__tests__/unit/PlanSnapshot.amendmentSubmissions.spec.js index c3ce3ba7..7829f198 100644 --- a/__tests__/unit/PlanSnapshot.amendmentSubmissions.spec.js +++ b/__tests__/unit/PlanSnapshot.amendmentSubmissions.spec.js @@ -62,4 +62,47 @@ describe('PlanSnapshot.fetchAmendmentSubmissions', () => { approvedBy: 'Tara Bogh', }); }); + + it('does not pair a mandatory amendment with an earlier approval', async () => { + const snapshots = [ + { + id: 1, + plan_id: 1444, + version: 1, + status_id: 12, + created_at: '2021-06-17T23:43:07.837Z', + snapshot: { statusId: 12, amendmentTypeId: null }, + given_name: 'Jason', + family_name: 'Caswell', + }, + { + id: 2, + plan_id: 1444, + version: 2, + status_id: 22, + created_at: '2024-02-22T23:38:15.337Z', + snapshot: { statusId: 22, amendmentTypeId: 2 }, + given_name: 'Taylor', + family_name: 'Grafton', + }, + ]; + const query = { + leftJoin: vi.fn().mockReturnThis(), + select: vi.fn().mockReturnThis(), + where: vi.fn().mockReturnThis(), + orderBy: vi.fn().mockReturnThis(), + execute: vi.fn().mockResolvedValue(snapshots), + }; + const db = { selectFrom: vi.fn().mockReturnValue(query) }; + const { default: PlanSnapshot } = await import('../../src/libs/db2/model/plansnapshot.ts'); + + const result = await PlanSnapshot.fetchAmendmentSubmissions(db, 1444); + + expect(result[1]).toMatchObject({ + createdAt: '2024-02-22T23:38:15.337Z', + submittedBy: 'Taylor Grafton', + approvedAt: null, + approvedBy: null, + }); + }); }); From b2851076e1d73968c8241037e28db81aba5a388a Mon Sep 17 00:00:00 2001 From: Brijesh Date: Mon, 10 Aug 2026 11:45:06 -0700 Subject: [PATCH 3/3] Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- __tests__/unit/PlanSnapshot.amendmentSubmissions.spec.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/__tests__/unit/PlanSnapshot.amendmentSubmissions.spec.js b/__tests__/unit/PlanSnapshot.amendmentSubmissions.spec.js index 7829f198..4eb9f400 100644 --- a/__tests__/unit/PlanSnapshot.amendmentSubmissions.spec.js +++ b/__tests__/unit/PlanSnapshot.amendmentSubmissions.spec.js @@ -1,5 +1,8 @@ -import { describe, expect, it, vi } from 'vitest'; +import { describe, expect, it, vi, beforeAll } from 'vitest'; +beforeAll(() => { + process.env.SSO_URL = process.env.SSO_URL || 'https://sso.example.com'; +}); vi.mock('../../src/libs/db2/model/amendmenttype.js', () => ({ default: { find: vi.fn().mockResolvedValue([