forked from Yara724/api
fix(api contract): trimmed user blame/claim lists and queues and removing unused data
This commit is contained in:
@@ -70,6 +70,8 @@ import {
|
||||
import { ListQueryV2Dto } from "src/common/dto/list-query-v2.dto";
|
||||
import { GetUserBlameListV2ResponseDto } from "src/request-management/dto/blame-list-user-v2.dto";
|
||||
import { resolveUnifiedFileStatus } from "src/helpers/unified-file-status";
|
||||
import { resolveBlameUserNextAction } from "src/helpers/blame-user-progress";
|
||||
import { toPublicListItems } from "src/helpers/public-list-response";
|
||||
import { AutoCloseRequestService } from "src/utils/cron/cron.service";
|
||||
import { SmsOrchestrationService } from "src/sms-orchestration/sms-orchestration.service";
|
||||
import {
|
||||
@@ -6620,7 +6622,7 @@ export class RequestManagementService {
|
||||
: ({ $or: orConditions } as any),
|
||||
{
|
||||
select:
|
||||
"publicId requestNo type status blameStatus createdAt updatedAt parties expertInitiated registrarInitiated initiatedByFieldExpertId initiatedByRegistrarId creationMethod",
|
||||
"publicId requestNo type status blameStatus createdAt updatedAt parties expert.decision.guiltyPartyId expertInitiated registrarInitiated initiatedByFieldExpertId initiatedByRegistrarId creationMethod",
|
||||
},
|
||||
);
|
||||
|
||||
@@ -6637,53 +6639,75 @@ export class RequestManagementService {
|
||||
.filter((claim) => claim?.blameRequestId && claim?.status)
|
||||
.map((claim) => [String(claim.blameRequestId), claim.status]),
|
||||
);
|
||||
const claimByBlameId = new Map<string, any>(
|
||||
claimsForStatus.map((claim) => [String(claim.blameRequestId), claim]),
|
||||
);
|
||||
|
||||
const enriched = requests.map((req: any) => {
|
||||
const isInitiator =
|
||||
(user?.role === RoleEnum.FIELD_EXPERT &&
|
||||
req.expertInitiated &&
|
||||
String(req.initiatedByFieldExpertId) === String(user.sub)) ||
|
||||
(user?.role === RoleEnum.REGISTRAR &&
|
||||
req.registrarInitiated &&
|
||||
String(req.initiatedByRegistrarId) === String(user.sub));
|
||||
const enriched = await Promise.all(
|
||||
requests.map(async (req: any) => {
|
||||
const isInitiator =
|
||||
(user?.role === RoleEnum.FIELD_EXPERT &&
|
||||
req.expertInitiated &&
|
||||
String(req.initiatedByFieldExpertId) === String(user.sub)) ||
|
||||
(user?.role === RoleEnum.REGISTRAR &&
|
||||
req.registrarInitiated &&
|
||||
String(req.initiatedByRegistrarId) === String(user.sub));
|
||||
|
||||
const party = req.parties?.find((p: any) =>
|
||||
partyPersonMatchesUser(p?.person, user, linkedUserIds),
|
||||
);
|
||||
const party = req.parties?.find((p: any) =>
|
||||
partyPersonMatchesUser(p?.person, user, linkedUserIds),
|
||||
);
|
||||
|
||||
const obj = req.toObject();
|
||||
const partyIdentities = Array.isArray(req.parties)
|
||||
? req.parties.map((entry: any) => ({
|
||||
phoneNumber: entry?.person?.phoneNumber,
|
||||
nationalCode: entry?.person?.nationalCode,
|
||||
nationalCodeOfInsurer: entry?.person?.nationalCodeOfInsurer,
|
||||
nationalCodeOfDriver: entry?.person?.nationalCodeOfDriver,
|
||||
participants: (
|
||||
Array.isArray(entry?.participants)
|
||||
const claimCreation = await this.computeClaimCreationHintForViewer(
|
||||
req,
|
||||
user,
|
||||
claimByBlameId.get(String(req._id)) ?? null,
|
||||
);
|
||||
const partyIdentities = Array.isArray(req.parties)
|
||||
? req.parties.map((entry: any) => ({
|
||||
phoneNumber: entry?.person?.phoneNumber,
|
||||
nationalCode: entry?.person?.nationalCode,
|
||||
nationalCodeOfInsurer: entry?.person?.nationalCodeOfInsurer,
|
||||
nationalCodeOfDriver: entry?.person?.nationalCodeOfDriver,
|
||||
participants: (Array.isArray(entry?.participants)
|
||||
? entry.participants
|
||||
: entry?.inquiryParticipants
|
||||
)?.map((participant: any) => ({
|
||||
phoneNumber: participant?.phoneNumber,
|
||||
nationalCode: participant?.nationalCode,
|
||||
nationalCodeOfInsurer: participant?.nationalCodeOfInsurer,
|
||||
nationalCodeOfDriver: participant?.nationalCodeOfDriver,
|
||||
})),
|
||||
}))
|
||||
: [];
|
||||
)?.map((participant: any) => ({
|
||||
phoneNumber: participant?.phoneNumber,
|
||||
nationalCode: participant?.nationalCode,
|
||||
nationalCodeOfInsurer: participant?.nationalCodeOfInsurer,
|
||||
nationalCodeOfDriver: participant?.nationalCodeOfDriver,
|
||||
})),
|
||||
}))
|
||||
: [];
|
||||
|
||||
delete obj.parties;
|
||||
|
||||
return {
|
||||
...obj,
|
||||
partyIdentities,
|
||||
userSide: party?.role ?? null,
|
||||
initiatedByMe: isInitiator,
|
||||
unifiedFileStatus: resolveUnifiedFileStatus({
|
||||
blameStatus: req.status,
|
||||
claimStatus: claimStatusByBlameId.get(String(req._id)),
|
||||
}),
|
||||
};
|
||||
});
|
||||
return {
|
||||
_id: String(req._id),
|
||||
publicId: req.publicId,
|
||||
requestNo: req.requestNo,
|
||||
type: req.type,
|
||||
status: req.status,
|
||||
blameStatus: req.blameStatus,
|
||||
createdAt: req.createdAt,
|
||||
updatedAt: req.updatedAt,
|
||||
creationMethod: req.creationMethod,
|
||||
expertInitiated: req.expertInitiated,
|
||||
registrarInitiated: req.registrarInitiated,
|
||||
partyIdentities,
|
||||
userSide: party?.role ?? null,
|
||||
initiatedByMe: isInitiator,
|
||||
claimCreation,
|
||||
nextAction: resolveBlameUserNextAction(
|
||||
req.status,
|
||||
party,
|
||||
claimCreation,
|
||||
),
|
||||
unifiedFileStatus: resolveUnifiedFileStatus({
|
||||
blameStatus: req.status,
|
||||
claimStatus: claimStatusByBlameId.get(String(req._id)),
|
||||
}),
|
||||
};
|
||||
}),
|
||||
);
|
||||
|
||||
let filtered = enriched;
|
||||
if (query.unifiedStatus) {
|
||||
@@ -6734,7 +6758,7 @@ export class RequestManagementService {
|
||||
);
|
||||
|
||||
return {
|
||||
list: paged.list,
|
||||
list: toPublicListItems(paged.list),
|
||||
total: paged.total,
|
||||
page: paged.page,
|
||||
limit: paged.limit,
|
||||
@@ -6753,19 +6777,29 @@ export class RequestManagementService {
|
||||
private async computeClaimCreationHintForViewer(
|
||||
blame: any,
|
||||
user: any,
|
||||
): Promise<{ hasClaim: boolean; shouldGuideToCreateClaim: boolean }> {
|
||||
knownClaim?: { _id: unknown } | null,
|
||||
): Promise<{
|
||||
hasClaim: boolean;
|
||||
shouldGuideToCreateClaim: boolean;
|
||||
claimRequestId?: string;
|
||||
}> {
|
||||
const none = { hasClaim: false, shouldGuideToCreateClaim: false };
|
||||
if (!user?.sub || !Types.ObjectId.isValid(String(user.sub))) {
|
||||
return none;
|
||||
}
|
||||
const currentUserId = String(user.sub);
|
||||
|
||||
const existingClaim = await this.claimCaseDbService.findOne({
|
||||
blameRequestId: blame._id,
|
||||
});
|
||||
const existingClaim =
|
||||
knownClaim === undefined
|
||||
? await this.claimCaseDbService.findOne({ blameRequestId: blame._id })
|
||||
: knownClaim;
|
||||
const hasClaim = !!existingClaim;
|
||||
if (hasClaim) {
|
||||
return { hasClaim: true, shouldGuideToCreateClaim: false };
|
||||
return {
|
||||
hasClaim: true,
|
||||
shouldGuideToCreateClaim: false,
|
||||
claimRequestId: String(existingClaim._id),
|
||||
};
|
||||
}
|
||||
|
||||
if (blame.status !== CaseStatus.COMPLETED) {
|
||||
@@ -7044,6 +7078,9 @@ export class RequestManagementService {
|
||||
visibleParties.map((p: any) => this.sanitizePartyForBlameUserView(p)),
|
||||
);
|
||||
const expert = await this.buildExpertForBlameUserView(plain.expert);
|
||||
const viewerParty = allParties.find((p: any) =>
|
||||
partyPersonMatchesUser(p?.person, user, linkedUserIds),
|
||||
);
|
||||
|
||||
return {
|
||||
requestNo: plain.requestNo,
|
||||
@@ -7057,6 +7094,12 @@ export class RequestManagementService {
|
||||
expert,
|
||||
carBodyInsuranceDetail: plain.carBodyInsuranceDetail,
|
||||
claimCreation,
|
||||
userSide: viewerParty?.role ?? null,
|
||||
nextAction: resolveBlameUserNextAction(
|
||||
plain.status,
|
||||
viewerParty,
|
||||
claimCreation,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,230 @@
|
||||
import { Types } from "mongoose";
|
||||
import { RequestManagementService } from "./request-management.service";
|
||||
import { resolveBlameUserNextAction } from "src/helpers/blame-user-progress";
|
||||
import { RoleEnum } from "src/Types&Enums/role.enum";
|
||||
|
||||
const guiltyId = "650000000000000000000001";
|
||||
const damagedId = "650000000000000000000002";
|
||||
const blameId = "650000000000000000000003";
|
||||
const signed = { accepted: true, signature: { fileId: "signature" } };
|
||||
const none = { hasClaim: false, shouldGuideToCreateClaim: false };
|
||||
|
||||
function blame(overrides: Record<string, unknown> = {}) {
|
||||
return {
|
||||
_id: new Types.ObjectId(blameId),
|
||||
requestNo: "A100",
|
||||
publicId: "A100",
|
||||
type: "THIRD_PARTY",
|
||||
status: "COMPLETED",
|
||||
blameStatus: "AGREED",
|
||||
createdAt: new Date(),
|
||||
expert: { decision: { guiltyPartyId: guiltyId } },
|
||||
parties: [
|
||||
{
|
||||
role: "FIRST",
|
||||
person: { userId: guiltyId, phoneNumber: "09120000001" },
|
||||
confirmation: signed,
|
||||
},
|
||||
{
|
||||
role: "SECOND",
|
||||
person: {
|
||||
userId: damagedId,
|
||||
phoneNumber: "09120000002",
|
||||
nationalCode: "1234567890",
|
||||
},
|
||||
confirmation: signed,
|
||||
},
|
||||
],
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
function subject() {
|
||||
const service = Object.create(RequestManagementService.prototype) as any;
|
||||
service.claimCaseDbService = {
|
||||
findOne: jest.fn().mockResolvedValue(null),
|
||||
find: jest.fn().mockResolvedValue([]),
|
||||
};
|
||||
service.logger = { error: jest.fn() };
|
||||
return service;
|
||||
}
|
||||
|
||||
describe("user blame next action and claim eligibility", () => {
|
||||
it("distinguishes an unsigned viewer from a viewer waiting for the other signature", () => {
|
||||
expect(resolveBlameUserNextAction("WAITING_FOR_SIGNATURES", {}, none)).toBe(
|
||||
"SIGN",
|
||||
);
|
||||
expect(
|
||||
resolveBlameUserNextAction(
|
||||
"WAITING_FOR_SIGNATURES",
|
||||
{ confirmation: signed },
|
||||
none,
|
||||
),
|
||||
).toBe("WAIT_FOR_OTHER_SIGNATURE");
|
||||
expect(
|
||||
resolveBlameUserNextAction("WAITING_FOR_SIGNATURES", undefined, none),
|
||||
).toBeNull();
|
||||
expect(
|
||||
resolveBlameUserNextAction("WAITING_FOR_EXPERT", {}, none),
|
||||
).toBeNull();
|
||||
expect(resolveBlameUserNextAction("STOPPED", {}, none)).toBe("VIEW_RESULT");
|
||||
});
|
||||
|
||||
it("permits a new third-party claim only for the damaged party after both accept", async () => {
|
||||
const service = subject();
|
||||
for (const [viewer, eligible] of [
|
||||
[damagedId, true],
|
||||
[guiltyId, false],
|
||||
[blameId, false],
|
||||
] as const) {
|
||||
const hint = await service.computeClaimCreationHintForViewer(
|
||||
blame(),
|
||||
{ sub: viewer },
|
||||
null,
|
||||
);
|
||||
expect(hint.shouldGuideToCreateClaim).toBe(eligible);
|
||||
}
|
||||
for (const status of [
|
||||
"WAITING_FOR_SIGNATURES",
|
||||
"WAITING_FOR_EXPERT",
|
||||
"STOPPED",
|
||||
]) {
|
||||
expect(
|
||||
(
|
||||
await service.computeClaimCreationHintForViewer(
|
||||
blame({ status }),
|
||||
{ sub: damagedId },
|
||||
null,
|
||||
)
|
||||
).shouldGuideToCreateClaim,
|
||||
).toBe(false);
|
||||
}
|
||||
const missingSignature = blame();
|
||||
delete (missingSignature.parties[0] as any).confirmation;
|
||||
expect(
|
||||
(
|
||||
await service.computeClaimCreationHintForViewer(
|
||||
missingSignature,
|
||||
{ sub: damagedId },
|
||||
null,
|
||||
)
|
||||
).shouldGuideToCreateClaim,
|
||||
).toBe(false);
|
||||
});
|
||||
|
||||
it("does not guide rejected or existing claims to duplicate creation", async () => {
|
||||
const service = subject();
|
||||
const rejected = blame();
|
||||
rejected.parties[0].confirmation = { ...signed, accepted: false };
|
||||
expect(
|
||||
(
|
||||
await service.computeClaimCreationHintForViewer(
|
||||
rejected,
|
||||
{ sub: damagedId },
|
||||
null,
|
||||
)
|
||||
).shouldGuideToCreateClaim,
|
||||
).toBe(false);
|
||||
const hint = await service.computeClaimCreationHintForViewer(
|
||||
blame(),
|
||||
{ sub: damagedId },
|
||||
{ _id: blameId },
|
||||
);
|
||||
expect(hint).toEqual({
|
||||
hasClaim: true,
|
||||
shouldGuideToCreateClaim: false,
|
||||
claimRequestId: blameId,
|
||||
});
|
||||
expect(resolveBlameUserNextAction("COMPLETED", {}, hint)).toBe(
|
||||
"CONTINUE_CLAIM",
|
||||
);
|
||||
expect(service.claimCaseDbService.findOne).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("permits a completed body-insurance claim only for its own party", async () => {
|
||||
const service = subject();
|
||||
const body = blame({
|
||||
type: "CAR_BODY",
|
||||
parties: [
|
||||
{ role: "FIRST", person: { userId: damagedId }, confirmation: signed },
|
||||
],
|
||||
});
|
||||
expect(
|
||||
(
|
||||
await service.computeClaimCreationHintForViewer(
|
||||
body,
|
||||
{ sub: damagedId },
|
||||
null,
|
||||
)
|
||||
).shouldGuideToCreateClaim,
|
||||
).toBe(true);
|
||||
expect(
|
||||
(
|
||||
await service.computeClaimCreationHintForViewer(
|
||||
body,
|
||||
{ sub: guiltyId },
|
||||
null,
|
||||
)
|
||||
).shouldGuideToCreateClaim,
|
||||
).toBe(false);
|
||||
});
|
||||
|
||||
it("keeps identity filters working but omits private/search-only fields in list responses", async () => {
|
||||
const service = subject();
|
||||
service.blameRequestDbService = {
|
||||
find: jest
|
||||
.fn()
|
||||
.mockResolvedValue([
|
||||
blame({
|
||||
__v: 42,
|
||||
inquiries: { raw: "private" },
|
||||
initiatedByFieldExpertId: guiltyId,
|
||||
}),
|
||||
]),
|
||||
};
|
||||
const response = await service.getAllBlameRequestsV2(
|
||||
{ sub: damagedId, role: RoleEnum.USER },
|
||||
{ nationalCode: "۱۲۳۴۵۶۷۸۹۰", page: 1, limit: 10 },
|
||||
);
|
||||
expect(response.total).toBe(1);
|
||||
expect(response.list[0]).toMatchObject({
|
||||
_id: blameId,
|
||||
nextAction: "CREATE_CLAIM",
|
||||
userSide: "SECOND",
|
||||
});
|
||||
for (const key of [
|
||||
"parties",
|
||||
"partyIdentities",
|
||||
"__v",
|
||||
"inquiries",
|
||||
"expert",
|
||||
"initiatedByFieldExpertId",
|
||||
]) {
|
||||
expect(response.list[0]).not.toHaveProperty(key);
|
||||
}
|
||||
expect(JSON.stringify(response)).not.toContain("1234567890");
|
||||
expect(service.claimCaseDbService.findOne).not.toHaveBeenCalled();
|
||||
expect(service.claimCaseDbService.find).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
it("provides signing guidance in detail without returning the other party", async () => {
|
||||
const service = subject();
|
||||
const record = blame({
|
||||
status: "WAITING_FOR_SIGNATURES",
|
||||
workflow: { currentStep: "WAITING_FOR_SIGNATURES" },
|
||||
});
|
||||
delete (record.parties[1] as any).confirmation;
|
||||
service.blameRequestDbService = {
|
||||
findById: jest.fn().mockResolvedValue(record),
|
||||
};
|
||||
const response = await service.getBlameRequestV2(blameId, {
|
||||
sub: damagedId,
|
||||
role: RoleEnum.USER,
|
||||
});
|
||||
expect(response.nextAction).toBe("SIGN");
|
||||
expect(response.userSide).toBe("SECOND");
|
||||
expect(response.parties).toHaveLength(1);
|
||||
expect(response.parties[0].role).toBe("SECOND");
|
||||
expect(JSON.stringify(response)).not.toContain("09120000001");
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user