|
| 1 | +import type { AuthContext } from '@/auth/types'; |
| 2 | +import { EvidenceFormsService } from './evidence-forms.service'; |
| 3 | +import type { AttachmentsService } from '@/attachments/attachments.service'; |
| 4 | +import { db } from '@trycompai/db'; |
| 5 | + |
| 6 | +jest.mock( |
| 7 | + '@/attachments/attachments.service', |
| 8 | + () => ({ |
| 9 | + AttachmentsService: class AttachmentsService {}, |
| 10 | + }), |
| 11 | + { virtual: true }, |
| 12 | +); |
| 13 | + |
| 14 | +jest.mock('@trycompai/db', () => { |
| 15 | + const evidenceFormTypeEnum = { |
| 16 | + board_meeting: 'board_meeting', |
| 17 | + it_leadership_meeting: 'it_leadership_meeting', |
| 18 | + risk_committee_meeting: 'risk_committee_meeting', |
| 19 | + meeting: 'meeting', |
| 20 | + access_request: 'access_request', |
| 21 | + whistleblower_report: 'whistleblower_report', |
| 22 | + penetration_test: 'penetration_test', |
| 23 | + rbac_matrix: 'rbac_matrix', |
| 24 | + infrastructure_inventory: 'infrastructure_inventory', |
| 25 | + employee_performance_evaluation: 'employee_performance_evaluation', |
| 26 | + }; |
| 27 | + |
| 28 | + return { |
| 29 | + EvidenceFormType: evidenceFormTypeEnum, |
| 30 | + db: { |
| 31 | + evidenceSubmission: { |
| 32 | + findFirst: jest.fn(), |
| 33 | + update: jest.fn(), |
| 34 | + }, |
| 35 | + }, |
| 36 | + }; |
| 37 | +}); |
| 38 | + |
| 39 | +type MockDb = { |
| 40 | + evidenceSubmission: { |
| 41 | + findFirst: jest.Mock; |
| 42 | + update: jest.Mock; |
| 43 | + }; |
| 44 | +}; |
| 45 | + |
| 46 | +describe('EvidenceFormsService', () => { |
| 47 | + const authContext: AuthContext = { |
| 48 | + organizationId: 'org_123', |
| 49 | + authType: 'jwt', |
| 50 | + isApiKey: false, |
| 51 | + userRoles: ['admin'], |
| 52 | + userId: 'usr_reviewer', |
| 53 | + userEmail: 'reviewer@example.com', |
| 54 | + }; |
| 55 | + |
| 56 | + const attachmentsServiceMock = { |
| 57 | + uploadToS3: jest.fn(), |
| 58 | + getPresignedDownloadUrl: jest.fn(), |
| 59 | + } as unknown as AttachmentsService; |
| 60 | + |
| 61 | + const service = new EvidenceFormsService(attachmentsServiceMock); |
| 62 | + const mockedDb = db as unknown as MockDb; |
| 63 | + |
| 64 | + beforeEach(() => { |
| 65 | + jest.clearAllMocks(); |
| 66 | + }); |
| 67 | + |
| 68 | + describe('reviewSubmission', () => { |
| 69 | + it('includes submittedBy and reviewedBy relations on review update', async () => { |
| 70 | + mockedDb.evidenceSubmission.findFirst.mockResolvedValue({ |
| 71 | + id: 'sub_123', |
| 72 | + status: 'pending', |
| 73 | + }); |
| 74 | + mockedDb.evidenceSubmission.update.mockResolvedValue({ |
| 75 | + id: 'sub_123', |
| 76 | + formType: 'meeting', |
| 77 | + submittedBy: { |
| 78 | + id: 'usr_submitter', |
| 79 | + name: 'Submitter', |
| 80 | + email: 'submitter@example.com', |
| 81 | + }, |
| 82 | + reviewedBy: { |
| 83 | + id: 'usr_reviewer', |
| 84 | + name: 'Reviewer', |
| 85 | + email: 'reviewer@example.com', |
| 86 | + }, |
| 87 | + }); |
| 88 | + |
| 89 | + const result = await service.reviewSubmission({ |
| 90 | + organizationId: 'org_123', |
| 91 | + formType: 'meeting', |
| 92 | + submissionId: 'sub_123', |
| 93 | + payload: { |
| 94 | + action: 'approved', |
| 95 | + }, |
| 96 | + authContext, |
| 97 | + }); |
| 98 | + |
| 99 | + expect(mockedDb.evidenceSubmission.update).toHaveBeenCalledWith( |
| 100 | + expect.objectContaining({ |
| 101 | + where: { id: 'sub_123' }, |
| 102 | + include: { |
| 103 | + submittedBy: { |
| 104 | + select: { |
| 105 | + id: true, |
| 106 | + name: true, |
| 107 | + email: true, |
| 108 | + }, |
| 109 | + }, |
| 110 | + reviewedBy: { |
| 111 | + select: { |
| 112 | + id: true, |
| 113 | + name: true, |
| 114 | + email: true, |
| 115 | + }, |
| 116 | + }, |
| 117 | + }, |
| 118 | + }), |
| 119 | + ); |
| 120 | + expect(result).toMatchObject({ |
| 121 | + submittedBy: { |
| 122 | + id: 'usr_submitter', |
| 123 | + name: 'Submitter', |
| 124 | + email: 'submitter@example.com', |
| 125 | + }, |
| 126 | + reviewedBy: { |
| 127 | + id: 'usr_reviewer', |
| 128 | + name: 'Reviewer', |
| 129 | + email: 'reviewer@example.com', |
| 130 | + }, |
| 131 | + formType: 'meeting', |
| 132 | + }); |
| 133 | + }); |
| 134 | + }); |
| 135 | +}); |
0 commit comments