|
| 1 | +import { Test, type TestingModule } from '@nestjs/testing'; |
| 2 | +import { PoliciesService } from './policies.service'; |
| 3 | +import { AttachmentsService } from '../attachments/attachments.service'; |
| 4 | +import { PolicyPdfRendererService } from '../trust-portal/policy-pdf-renderer.service'; |
| 5 | + |
| 6 | +jest.mock('@db', () => ({ |
| 7 | + db: { |
| 8 | + policy: { |
| 9 | + findMany: jest.fn(), |
| 10 | + findFirst: jest.fn(), |
| 11 | + update: jest.fn(), |
| 12 | + }, |
| 13 | + member: { |
| 14 | + findMany: jest.fn(), |
| 15 | + }, |
| 16 | + auditLog: { |
| 17 | + createMany: jest.fn(), |
| 18 | + }, |
| 19 | + $transaction: jest.fn(), |
| 20 | + }, |
| 21 | + Frequency: { |
| 22 | + monthly: 'monthly', |
| 23 | + quarterly: 'quarterly', |
| 24 | + yearly: 'yearly', |
| 25 | + }, |
| 26 | + PolicyStatus: { |
| 27 | + draft: 'draft', |
| 28 | + published: 'published', |
| 29 | + needs_review: 'needs_review', |
| 30 | + }, |
| 31 | + Prisma: { |
| 32 | + PrismaClientKnownRequestError: class PrismaClientKnownRequestError extends Error { |
| 33 | + code: string; |
| 34 | + constructor(message: string, { code }: { code: string }) { |
| 35 | + super(message); |
| 36 | + this.code = code; |
| 37 | + } |
| 38 | + }, |
| 39 | + }, |
| 40 | +})); |
| 41 | + |
| 42 | +jest.mock('../utils/compliance-filters', () => ({ |
| 43 | + filterComplianceMembers: jest.fn(async (members: unknown[]) => members), |
| 44 | +})); |
| 45 | + |
| 46 | +// eslint-disable-next-line @typescript-eslint/no-require-imports |
| 47 | +const { db } = require('@db') as { |
| 48 | + db: { |
| 49 | + policy: { findMany: jest.Mock; findFirst: jest.Mock; update: jest.Mock }; |
| 50 | + member: { findMany: jest.Mock }; |
| 51 | + auditLog: { createMany: jest.Mock }; |
| 52 | + $transaction: jest.Mock; |
| 53 | + }; |
| 54 | +}; |
| 55 | + |
| 56 | +// eslint-disable-next-line @typescript-eslint/no-require-imports |
| 57 | +const { filterComplianceMembers: mockedFilterComplianceMembers } = require('../utils/compliance-filters') as { |
| 58 | + filterComplianceMembers: jest.Mock; |
| 59 | +}; |
| 60 | + |
| 61 | +describe('PoliciesService', () => { |
| 62 | + let service: PoliciesService; |
| 63 | + |
| 64 | + beforeEach(async () => { |
| 65 | + jest.clearAllMocks(); |
| 66 | + const module: TestingModule = await Test.createTestingModule({ |
| 67 | + providers: [ |
| 68 | + PoliciesService, |
| 69 | + { provide: AttachmentsService, useValue: {} }, |
| 70 | + { provide: PolicyPdfRendererService, useValue: {} }, |
| 71 | + ], |
| 72 | + }).compile(); |
| 73 | + service = module.get<PoliciesService>(PoliciesService); |
| 74 | + }); |
| 75 | + |
| 76 | + describe('updateById', () => { |
| 77 | + it('clears signedBy[] when the status transitions to published', async () => { |
| 78 | + const orgId = 'org_abc'; |
| 79 | + const existing = { id: 'pol_1', organizationId: orgId, status: 'draft' }; |
| 80 | + const updatedResult = { ...existing, status: 'published', signedBy: [], name: 'Test Policy' }; |
| 81 | + |
| 82 | + // Make $transaction execute the callback with a tx proxy backed by db mocks |
| 83 | + db.$transaction.mockImplementation(async (callback: (tx: unknown) => Promise<unknown>) => { |
| 84 | + const tx = { policy: { findFirst: db.policy.findFirst, update: db.policy.update } }; |
| 85 | + return callback(tx); |
| 86 | + }); |
| 87 | + db.policy.findFirst.mockResolvedValueOnce(existing); |
| 88 | + db.policy.update.mockResolvedValueOnce(updatedResult); |
| 89 | + |
| 90 | + await service.updateById('pol_1', orgId, { status: 'published' } as never); |
| 91 | + |
| 92 | + expect(db.policy.update).toHaveBeenCalledTimes(1); |
| 93 | + const updateArg = db.policy.update.mock.calls[0][0]; |
| 94 | + expect(updateArg.data.signedBy).toEqual([]); |
| 95 | + expect(updateArg.data.status).toBe('published'); |
| 96 | + expect(updateArg.data.lastPublishedAt).toBeInstanceOf(Date); |
| 97 | + }); |
| 98 | + |
| 99 | + it('does not clear signedBy when the policy is already published and status is re-sent', async () => { |
| 100 | + const orgId = 'org_abc'; |
| 101 | + const existing = { id: 'pol_1', organizationId: orgId, status: 'published' }; |
| 102 | + const updatedResult = { ...existing, description: 'tweak', name: 'Test' }; |
| 103 | + |
| 104 | + db.$transaction.mockImplementation(async (callback: (tx: unknown) => Promise<unknown>) => { |
| 105 | + const tx = { policy: { findFirst: db.policy.findFirst, update: db.policy.update } }; |
| 106 | + return callback(tx); |
| 107 | + }); |
| 108 | + db.policy.findFirst.mockResolvedValueOnce(existing); |
| 109 | + db.policy.update.mockResolvedValueOnce(updatedResult); |
| 110 | + |
| 111 | + await service.updateById('pol_1', orgId, { |
| 112 | + status: 'published', |
| 113 | + description: 'tweak', |
| 114 | + } as never); |
| 115 | + |
| 116 | + const updateArg = db.policy.update.mock.calls[0][0]; |
| 117 | + expect(updateArg.data.signedBy).toBeUndefined(); |
| 118 | + expect(updateArg.data.lastPublishedAt).toBeUndefined(); |
| 119 | + }); |
| 120 | + |
| 121 | + it('does not clear signedBy[] on non-publish updates', async () => { |
| 122 | + const orgId = 'org_abc'; |
| 123 | + const existing = { id: 'pol_1', organizationId: orgId, status: 'published', signedBy: ['usr_a'] }; |
| 124 | + const updatedResult = { ...existing, description: 'new desc', name: 'Test Policy' }; |
| 125 | + |
| 126 | + db.$transaction.mockImplementation(async (callback: (tx: unknown) => Promise<unknown>) => { |
| 127 | + const tx = { policy: { findFirst: db.policy.findFirst, update: db.policy.update } }; |
| 128 | + return callback(tx); |
| 129 | + }); |
| 130 | + db.policy.findFirst.mockResolvedValueOnce(existing); |
| 131 | + db.policy.update.mockResolvedValueOnce(updatedResult); |
| 132 | + |
| 133 | + await service.updateById('pol_1', orgId, { description: 'new desc' } as never); |
| 134 | + |
| 135 | + const updateArg = db.policy.update.mock.calls[0][0]; |
| 136 | + expect(updateArg.data.signedBy).toBeUndefined(); |
| 137 | + }); |
| 138 | + }); |
| 139 | + |
| 140 | + describe('publishAll', () => { |
| 141 | + it('clears signedBy[] on every published policy and returns { success, publishedCount, members }', async () => { |
| 142 | + const orgId = 'org_abc'; |
| 143 | + const drafts = [ |
| 144 | + { id: 'pol_1', name: 'Access', frequency: 'yearly' }, |
| 145 | + { id: 'pol_2', name: 'Backup', frequency: null }, |
| 146 | + ]; |
| 147 | + db.policy.findMany.mockResolvedValueOnce(drafts); |
| 148 | + db.$transaction.mockImplementation((updates: unknown[]) => Promise.resolve(updates)); |
| 149 | + db.policy.update.mockImplementation((args) => args); |
| 150 | + db.member.findMany.mockResolvedValueOnce([]); |
| 151 | + |
| 152 | + const result = await service.publishAll(orgId); |
| 153 | + |
| 154 | + expect(db.$transaction).toHaveBeenCalledTimes(1); |
| 155 | + const txArg = db.$transaction.mock.calls[0][0] as Array<{ |
| 156 | + where: { id: string }; |
| 157 | + data: Record<string, unknown>; |
| 158 | + }>; |
| 159 | + expect(txArg).toHaveLength(2); |
| 160 | + for (const update of txArg) { |
| 161 | + expect(update.data.status).toBe('published'); |
| 162 | + expect(update.data.signedBy).toEqual([]); |
| 163 | + expect(update.data.lastPublishedAt).toBeInstanceOf(Date); |
| 164 | + } |
| 165 | + expect(result.success).toBe(true); |
| 166 | + expect(result.publishedCount).toBe(2); |
| 167 | + expect(result.members).toEqual([]); |
| 168 | + }); |
| 169 | + |
| 170 | + it('returns early with publishedCount 0 when there are no drafts', async () => { |
| 171 | + db.policy.findMany.mockResolvedValueOnce([]); |
| 172 | + const result = await service.publishAll('org_empty'); |
| 173 | + expect(result).toEqual({ success: true, publishedCount: 0, members: [] }); |
| 174 | + expect(db.$transaction).not.toHaveBeenCalled(); |
| 175 | + }); |
| 176 | + |
| 177 | + it('returns only compliance-obligated members in the members array', async () => { |
| 178 | + const orgId = 'org_abc'; |
| 179 | + db.policy.findMany.mockResolvedValueOnce([ |
| 180 | + { id: 'pol_1', name: 'P', frequency: 'yearly' }, |
| 181 | + ]); |
| 182 | + db.$transaction.mockImplementation((updates: unknown[]) => |
| 183 | + Promise.resolve(updates), |
| 184 | + ); |
| 185 | + db.policy.update.mockImplementation((args) => args); |
| 186 | + db.member.findMany.mockResolvedValueOnce([ |
| 187 | + { |
| 188 | + role: 'employee', |
| 189 | + user: { email: 'alice@example.com', name: 'Alice', role: null }, |
| 190 | + organization: { id: orgId, name: 'Acme' }, |
| 191 | + }, |
| 192 | + { |
| 193 | + role: 'auditor', |
| 194 | + user: { email: 'audit@example.com', name: 'Aud', role: null }, |
| 195 | + organization: { id: orgId, name: 'Acme' }, |
| 196 | + }, |
| 197 | + ]); |
| 198 | + // Mock filterComplianceMembers to return only Alice |
| 199 | + mockedFilterComplianceMembers.mockResolvedValueOnce([ |
| 200 | + { |
| 201 | + role: 'employee', |
| 202 | + user: { email: 'alice@example.com', name: 'Alice', role: null }, |
| 203 | + organization: { id: orgId, name: 'Acme' }, |
| 204 | + }, |
| 205 | + ] as never); |
| 206 | + |
| 207 | + const result = await service.publishAll(orgId); |
| 208 | + |
| 209 | + expect(result.members).toEqual([ |
| 210 | + { |
| 211 | + email: 'alice@example.com', |
| 212 | + userName: 'Alice', |
| 213 | + organizationName: 'Acme', |
| 214 | + organizationId: orgId, |
| 215 | + }, |
| 216 | + ]); |
| 217 | + }); |
| 218 | + }); |
| 219 | +}); |
0 commit comments