Files
yara724api/src/request-management/file-maker-blame-v4-workflow.spec.ts
T
SepehrYahyaee 83376df4ad feat: manage expert access and merge Parsian expert seeds
Allow role-specific identities and admin account activation, support national-code-only claim experts, and remove required voice from v4/v5. Add a combined, resumable Parsian seed that preserves existing accounts and merges locations and product codes.
2026-11-18 17:16:26 +03:30

484 lines
17 KiB
TypeScript

import { Types } from "mongoose";
import { BlameRequestType } from "src/Types&Enums/blame-request-management/blameRequestType.enum";
import { WorkflowStep } from "src/Types&Enums/blame-request-management/blameWorkflow-steps.enum";
import { RoleEnum } from "src/Types&Enums/role.enum";
import { CreationMethod } from "./entities/schema/request-management.schema";
import { PartyRole } from "./entities/schema/partyRole.enum";
import { RequestManagementService } from "./request-management.service";
import { CaseStatus } from "src/Types&Enums/blame-request-management/caseStatus.enum";
import { ClaimCaseStatus } from "src/Types&Enums/claim-request-management/claim-case-status.enum";
import { ClaimWorkflowStep } from "src/Types&Enums/claim-request-management/claim-workflow-steps.enum";
describe("RequestManagementService V4 FileMaker workflow", () => {
it.each([
[4, BlameRequestType.CAR_BODY],
[4, BlameRequestType.THIRD_PARTY],
[5, BlameRequestType.CAR_BODY],
[5, BlameRequestType.THIRD_PARTY],
])(
"completes v%s %s narratives and signatures without voice",
async (flow, type) => {
const actor = {
sub: String(new Types.ObjectId()),
role: RoleEnum.FILE_MAKER,
};
const request: any = {
_id: new Types.ObjectId(),
type,
expertInitiated: true,
isMadeByFileMaker: true,
requiresFileMakerApproval: flow === 5,
initiatedByFieldExpertId: new Types.ObjectId(actor.sub),
creationMethod: CreationMethod.IN_PERSON,
status: CaseStatus.OPEN,
parties: [
PartyRole.FIRST,
...(type === BlameRequestType.THIRD_PARTY ? [PartyRole.SECOND] : []),
].map((role) => ({
role,
person: { userId: new Types.ObjectId() },
evidence: {},
})),
workflow: {
currentStep: WorkflowStep.FIRST_INITIAL_FORM,
completedSteps: [],
},
history: [],
save: jest.fn().mockResolvedValue(undefined),
};
const service: any = new (RequestManagementService as any)();
service.verifyExpertAccessForBlameV2 = jest
.fn()
.mockResolvedValue(undefined);
service.blameRequestDbService = {
findById: async () => request,
findByIdAndUpdate: async (_id, update) => {
for (const [key, value] of Object.entries(update.$set)) {
const [, index] = key.split(".");
request.parties[Number(index)].confirmation = value;
}
},
};
service.userSign = {
create: jest.fn(async () => ({ _id: new Types.ObjectId() })),
};
service.blameVoiceDbService = { create: jest.fn() };
for (const party of request.parties) {
service.markPartyInquiriesCompleteOnBlame(request, party.role, actor);
const prefix = party.role === PartyRole.FIRST ? "FIRST" : "SECOND";
expect(request.workflow.currentStep).toBe(`${prefix}_DESCRIPTION`);
const description = {
desc: "شرح حادثه ".repeat(15),
accidentDate: "2025-01-01",
accidentTime: "10:30",
};
if (flow === 4)
await service.addPartyDescriptionV4(
String(request._id),
actor,
description,
party.role,
);
else
await service.addPartyDescriptionV3(
String(request._id),
actor,
description,
party.role,
);
expect(request.workflow.currentStep).toBe(`${prefix}_SIGN`);
await service.expertUploadPartySignatureV3(
actor,
String(request._id),
party.role,
true,
{ filename: "signature.png", path: "files/signature.png" },
);
expect(party.confirmation.accepted).toBe(true);
expect(request.workflow.completedSteps).not.toContain(
`${prefix}_VOICE`,
);
}
expect(service.blameVoiceDbService.create).not.toHaveBeenCalled();
expect(request.workflow.currentStep).toBe(
type === BlameRequestType.CAR_BODY
? WorkflowStep.FIRST_COMPLETED
: WorkflowStep.SECOND_COMPLETED,
);
},
);
it.each([WorkflowStep.FIRST_VOICE, WorkflowStep.SECOND_VOICE])(
"repairs legacy %s resume values without erasing existing recordings or history",
(step) => {
const service: any = new (RequestManagementService as any)();
const file = {
workflow: {
currentStep: step,
nextStep: step.replace("VOICE", "DESCRIPTION"),
completedSteps: [step],
},
parties: [{ evidence: { voices: ["existing-recording"] } }],
history: [{ type: "V3_PARTY_VOICE_UPLOADED" }],
};
const before = JSON.stringify(file);
expect(service.fileMakerWorkflowProjection(file).currentStep).toBe(
step.replace("VOICE", "DESCRIPTION"),
);
expect(service.fileMakerWorkflowProjection(file).nextStep).toBe(step.replace("VOICE", "SIGN"));
expect(JSON.stringify(file)).toBe(before);
},
);
it("keeps voice in the non-FileMaker in-person inquiry workflow", () => {
const service: any = new (RequestManagementService as any)();
const request: any = { workflow: { completedSteps: [] }, parties: [] };
service.markPartyInquiriesCompleteOnBlame(request, PartyRole.FIRST, {});
expect(request.workflow.currentStep).toBe(WorkflowStep.FIRST_LOCATION);
expect(request.workflow.nextStep).toBe(WorkflowStep.FIRST_VOICE);
});
it("persists FIRST_INITIAL_FORM after the first party OTP is verified", async () => {
const fileMakerId = new Types.ObjectId();
const userId = new Types.ObjectId();
const request = {
_id: new Types.ObjectId(),
publicId: "BL-V4-001",
type: BlameRequestType.THIRD_PARTY,
expertInitiated: true,
isMadeByFileMaker: true,
requiresFileMakerApproval: false,
initiatedByFieldExpertId: fileMakerId,
creationMethod: CreationMethod.IN_PERSON,
parties: [{ role: PartyRole.FIRST, person: {} }],
workflow: {
currentStep: WorkflowStep.CREATED,
nextStep: WorkflowStep.FIRST_BLAME_CONFESSION,
completedSteps: [WorkflowStep.CREATED],
},
history: [],
save: jest.fn().mockResolvedValue(undefined),
};
const service =
new (RequestManagementService as any)() as RequestManagementService;
(service as any).blameRequestDbService = {
findById: jest.fn().mockResolvedValue(request),
};
(service as any).userDbService = {
findOne: jest.fn().mockResolvedValue({
_id: userId,
otp: "hashed-otp",
otpExpire: Date.now() + 60_000,
}),
};
(service as any).hashService = {
compare: jest.fn().mockResolvedValue(true),
};
(service as any).claimCaseDbService = {
findOne: jest.fn().mockResolvedValue(null),
};
(service as any).workflowStepDbService = {
findByStepNumber: jest.fn().mockImplementation((stepNumber: number) => {
if (stepNumber === 2) {
return {
stepNumber: 2,
stepKey: WorkflowStep.FIRST_BLAME_CONFESSION,
nextPossibleSteps: [WorkflowStep.FIRST_VIDEO],
};
}
if (stepNumber === 3) {
return {
stepNumber: 3,
stepKey: WorkflowStep.FIRST_VIDEO,
nextPossibleSteps: [WorkflowStep.FIRST_INITIAL_FORM],
};
}
return null;
}),
findByStepKey: jest.fn().mockResolvedValue(null),
};
await service.verifyPartyOtpV2(
{
sub: String(fileMakerId),
role: RoleEnum.FILE_MAKER,
firstName: "File",
lastName: "Maker",
},
String(request._id),
{
phoneNumber: "09121234567",
otp: "12345",
partyRole: "FIRST",
},
);
expect(request.workflow.currentStep).toBe(WorkflowStep.FIRST_INITIAL_FORM);
expect(request.workflow.currentStep).not.toBe(WorkflowStep.FIRST_VIDEO);
expect(request.workflow.nextStep).toBe(WorkflowStep.FIRST_DESCRIPTION);
expect(request.workflow.completedSteps).not.toContain(
WorkflowStep.FIRST_VIDEO,
);
expect(request.save).toHaveBeenCalled();
const reopened = await service.getMyFileMakerFileDetail(
{ sub: String(fileMakerId), role: RoleEnum.FILE_MAKER },
String(request._id),
);
expect(reopened.workflow.currentStep).toBe(WorkflowStep.FIRST_INITIAL_FORM);
expect(reopened.workflow.currentStep).not.toBe(WorkflowStep.FIRST_VIDEO);
});
it("keeps FIRST_VIDEO out of the FileMaker car-body workflow", async () => {
const fileMakerId = new Types.ObjectId();
const request = {
_id: new Types.ObjectId(),
publicId: "BL-V4-002",
type: BlameRequestType.CAR_BODY,
expertInitiated: true,
isMadeByFileMaker: true,
requiresFileMakerApproval: false,
initiatedByFieldExpertId: fileMakerId,
creationMethod: CreationMethod.IN_PERSON,
parties: [{ role: PartyRole.FIRST, person: {} }],
workflow: {
currentStep: WorkflowStep.CREATED,
nextStep: WorkflowStep.CAR_BODY_ACCIDENT_TYPE,
completedSteps: [WorkflowStep.CREATED],
},
history: [],
save: jest.fn().mockResolvedValue(undefined),
};
const service =
new (RequestManagementService as any)() as RequestManagementService;
(service as any).blameRequestDbService = {
findById: jest.fn().mockResolvedValue(request),
};
(service as any).userDbService = {
findOne: jest.fn().mockResolvedValue({
_id: new Types.ObjectId(),
otp: "hashed-otp",
otpExpire: Date.now() + 60_000,
}),
};
(service as any).hashService = {
compare: jest.fn().mockResolvedValue(true),
};
(service as any).workflowStepDbService = {
findByStepNumber: jest.fn().mockResolvedValue(null),
findByStepKey: jest.fn().mockResolvedValue({
stepNumber: 2,
stepKey: WorkflowStep.CAR_BODY_ACCIDENT_TYPE,
nextPossibleSteps: [WorkflowStep.FIRST_VIDEO],
}),
};
const actor = {
sub: String(fileMakerId),
role: RoleEnum.FILE_MAKER,
firstName: "File",
lastName: "Maker",
};
await service.verifyPartyOtpV2(actor, String(request._id), {
phoneNumber: "09121234567",
otp: "12345",
partyRole: "FIRST",
});
expect(request.workflow.currentStep).toBe(
WorkflowStep.CAR_BODY_ACCIDENT_TYPE,
);
expect(request.workflow.nextStep).toBe(WorkflowStep.FIRST_INITIAL_FORM);
await service.carBodyAccidentTypeFormV3(
String(request._id),
{ car: true, object: false },
actor,
);
expect(request.workflow.currentStep).toBe(WorkflowStep.FIRST_INITIAL_FORM);
expect(request.workflow.nextStep).toBe(WorkflowStep.FIRST_DESCRIPTION);
expect(request.workflow.completedSteps).not.toContain(
WorkflowStep.FIRST_VIDEO,
);
});
it("repairs legacy stuck workflow values when a V4 file is reopened", async () => {
const fileMakerId = new Types.ObjectId();
const request = {
_id: new Types.ObjectId(),
publicId: "BL-V4-LEGACY",
type: BlameRequestType.THIRD_PARTY,
isMadeByFileMaker: true,
initiatedByFieldExpertId: fileMakerId,
workflow: {
currentStep: WorkflowStep.FIRST_VIDEO,
nextStep: WorkflowStep.FIRST_INITIAL_FORM,
completedSteps: [
WorkflowStep.CREATED,
WorkflowStep.FIRST_BLAME_CONFESSION,
],
},
parties: [
{ role: PartyRole.FIRST, person: { userId: new Types.ObjectId() } },
],
};
const service =
new (RequestManagementService as any)() as RequestManagementService;
(service as any).blameRequestDbService = {
findById: jest.fn().mockResolvedValue(request),
};
(service as any).claimCaseDbService = {
findOne: jest.fn().mockResolvedValue(null),
};
const reopened = await service.getMyFileMakerFileDetail(
{ sub: String(fileMakerId), role: RoleEnum.FILE_MAKER },
String(request._id),
);
expect(reopened.workflow).toEqual(
expect.objectContaining({
currentStep: WorkflowStep.FIRST_INITIAL_FORM,
nextStep: WorkflowStep.FIRST_DESCRIPTION,
}),
);
expect(reopened.workflow.completedSteps).not.toContain(
WorkflowStep.FIRST_VIDEO,
);
});
it.each([false, true])(
"resumes V4/V5 FileMaker work from the linked claim during partial document upload (approval=%s)",
async (requiresFileMakerApproval) => {
const fileMakerId = new Types.ObjectId();
const blameId = new Types.ObjectId();
const claimId = new Types.ObjectId();
const request = {
_id: blameId,
publicId: "BL-FILE-MAKER-RESUME",
requestNo: "BL-RESUME",
type: BlameRequestType.THIRD_PARTY,
status: CaseStatus.OPEN,
isMadeByFileMaker: true,
initiatedByFieldExpertId: fileMakerId,
requiresFileMakerApproval,
parties: [],
workflow: {
currentStep: WorkflowStep.SECOND_COMPLETED,
nextStep: WorkflowStep.WAITING_FOR_GUILT_DECISION,
completedSteps: [
WorkflowStep.FIRST_COMPLETED,
WorkflowStep.SECOND_COMPLETED,
],
},
};
const claim = {
_id: claimId,
blameRequestId: blameId,
status: ClaimCaseStatus.UPLOADING_REQUIRED_DOCUMENTS,
workflow: {
currentStep: ClaimWorkflowStep.UPLOAD_REQUIRED_DOCUMENTS,
nextStep: ClaimWorkflowStep.SELECT_OUTER_PARTS,
completedSteps: [ClaimWorkflowStep.CLAIM_CREATED],
},
};
const service =
new (RequestManagementService as any)() as RequestManagementService;
(service as any).blameRequestDbService = {
findById: jest.fn().mockResolvedValue(request),
find: jest.fn().mockResolvedValue([request]),
};
(service as any).claimCaseDbService = {
findOne: jest.fn().mockResolvedValue(claim),
find: jest.fn().mockResolvedValue([claim]),
};
const reopened = await service.getMyFileMakerFileDetail(
{ sub: String(fileMakerId), role: RoleEnum.FILE_MAKER },
String(blameId),
);
expect(reopened.status).toBe(CaseStatus.OPEN);
expect(reopened.claimStatus).toBe(
ClaimCaseStatus.UPLOADING_REQUIRED_DOCUMENTS,
);
expect(reopened.fileMakerResume).toEqual({
action: "UPLOAD_REQUIRED_DOCUMENTS",
entity: "CLAIM",
entityId: String(claimId),
status: ClaimCaseStatus.UPLOADING_REQUIRED_DOCUMENTS,
currentStep: ClaimWorkflowStep.UPLOAD_REQUIRED_DOCUMENTS,
nextStep: ClaimWorkflowStep.SELECT_OUTER_PARTS,
});
const list = await service.getMyFileMakerFiles({
sub: String(fileMakerId),
role: RoleEnum.FILE_MAKER,
});
expect(list.list[0]).toEqual(
expect.objectContaining({
linkedClaimId: String(claimId),
claimStatus: ClaimCaseStatus.UPLOADING_REQUIRED_DOCUMENTS,
fileMakerResume: expect.objectContaining({
action: "UPLOAD_REQUIRED_DOCUMENTS",
entityId: String(claimId),
}),
}),
);
},
);
it("does not leave the blame narrative before all required signatures", async () => {
const fileMakerId = new Types.ObjectId();
const blameId = new Types.ObjectId();
const claimId = new Types.ObjectId();
const request = {
_id: blameId,
publicId: "BL-FILE-MAKER-NARRATIVE",
requestNo: "BL-NARRATIVE",
type: BlameRequestType.THIRD_PARTY,
status: CaseStatus.OPEN,
isMadeByFileMaker: true,
initiatedByFieldExpertId: fileMakerId,
parties: [],
workflow: {
currentStep: WorkflowStep.FIRST_DESCRIPTION,
nextStep: WorkflowStep.FIRST_SIGN,
completedSteps: [WorkflowStep.FIRST_VOICE],
},
};
const claim = {
_id: claimId,
blameRequestId: blameId,
status: ClaimCaseStatus.UPLOADING_REQUIRED_DOCUMENTS,
workflow: {
currentStep: ClaimWorkflowStep.UPLOAD_REQUIRED_DOCUMENTS,
nextStep: ClaimWorkflowStep.SELECT_OUTER_PARTS,
completedSteps: [ClaimWorkflowStep.CLAIM_CREATED],
},
};
const service =
new (RequestManagementService as any)() as RequestManagementService;
(service as any).blameRequestDbService = {
findById: jest.fn().mockResolvedValue(request),
};
(service as any).claimCaseDbService = {
findOne: jest.fn().mockResolvedValue(claim),
};
const reopened = await service.getMyFileMakerFileDetail(
{ sub: String(fileMakerId), role: RoleEnum.FILE_MAKER },
String(blameId),
);
expect(reopened.fileMakerResume).toEqual(
expect.objectContaining({
action: "CONTINUE_BLAME",
entity: "BLAME",
entityId: String(blameId),
currentStep: WorkflowStep.FIRST_DESCRIPTION,
}),
);
});
});