forked from Yara724/api
fixed many bugs and step issues
This commit is contained in:
@@ -46,6 +46,7 @@ import { RequestManagementModel } from "./entities/schema/request-management.sch
|
||||
import { BlameDocumentType } from "./entities/schema/blame-document.schema";
|
||||
import { SandHubDbService } from "src/sand-hub/entity/db-service/sand-hub.db.service";
|
||||
import { ClaimRequestManagementDbService } from "src/claim-request-management/entites/db-service/claim-request-management.db.service";
|
||||
import { ClaimCaseDbService } from "src/claim-request-management/entites/db-service/claim-case.db.service";
|
||||
import {
|
||||
CreationMethod,
|
||||
FilledBy,
|
||||
@@ -300,6 +301,7 @@ export class RequestManagementService {
|
||||
private readonly expertDbService: ExpertDbService,
|
||||
private readonly autoCloseRequestService: AutoCloseRequestService,
|
||||
private readonly claimRequestManagementDbService: ClaimRequestManagementDbService,
|
||||
private readonly claimCaseDbService: ClaimCaseDbService,
|
||||
private readonly publicIdService: PublicIdService,
|
||||
private readonly workflowStepDbService: WorkflowStepDbService,
|
||||
private readonly hashService: HashService,
|
||||
@@ -3916,6 +3918,81 @@ export class RequestManagementService {
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* V2: Hint for UX — damaged user should open create-claim when blame is done and no claim exists.
|
||||
* Mirrors eligibility rules in createClaimFromBlameV2 (without throwing).
|
||||
*/
|
||||
private async computeClaimCreationHintForViewer(
|
||||
blame: any,
|
||||
user: any,
|
||||
): Promise<{ hasClaim: boolean; shouldGuideToCreateClaim: boolean }> {
|
||||
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 hasClaim = !!existingClaim;
|
||||
if (hasClaim) {
|
||||
return { hasClaim: true, shouldGuideToCreateClaim: false };
|
||||
}
|
||||
|
||||
if (blame.status !== CaseStatus.COMPLETED) {
|
||||
return { hasClaim: false, shouldGuideToCreateClaim: false };
|
||||
}
|
||||
|
||||
const parties = blame.parties || [];
|
||||
|
||||
if (blame.type === BlameRequestType.CAR_BODY) {
|
||||
const first = parties.find(
|
||||
(p: any) => p?.role === PartyRole.FIRST || p?.role === "FIRST",
|
||||
);
|
||||
const damagedId = first?.person?.userId
|
||||
? String(first.person.userId)
|
||||
: null;
|
||||
if (!damagedId || damagedId !== currentUserId) {
|
||||
return { hasClaim: false, shouldGuideToCreateClaim: false };
|
||||
}
|
||||
return { hasClaim: false, shouldGuideToCreateClaim: true };
|
||||
}
|
||||
|
||||
if (blame.type === BlameRequestType.THIRD_PARTY) {
|
||||
const guiltyRaw = blame.expert?.decision?.guiltyPartyId;
|
||||
if (!guiltyRaw) {
|
||||
return { hasClaim: false, shouldGuideToCreateClaim: false };
|
||||
}
|
||||
const guiltyId = String(guiltyRaw);
|
||||
if (currentUserId === guiltyId) {
|
||||
return { hasClaim: false, shouldGuideToCreateClaim: false };
|
||||
}
|
||||
const userParty = parties.find(
|
||||
(p: any) =>
|
||||
p?.person?.userId && String(p.person.userId) === currentUserId,
|
||||
);
|
||||
if (!userParty) {
|
||||
return { hasClaim: false, shouldGuideToCreateClaim: false };
|
||||
}
|
||||
if (parties.length < 2) {
|
||||
return { hasClaim: false, shouldGuideToCreateClaim: false };
|
||||
}
|
||||
const allSigned = parties.every(
|
||||
(p: any) => p.confirmation !== undefined && p.confirmation !== null,
|
||||
);
|
||||
const allAccepted = parties.every(
|
||||
(p: any) => p.confirmation?.accepted === true,
|
||||
);
|
||||
if (!allSigned || !allAccepted) {
|
||||
return { hasClaim: false, shouldGuideToCreateClaim: false };
|
||||
}
|
||||
return { hasClaim: false, shouldGuideToCreateClaim: true };
|
||||
}
|
||||
|
||||
return none;
|
||||
}
|
||||
|
||||
/**
|
||||
* V2: Get one blame request by id. Access allowed if current user is a party (by userId or phone)
|
||||
* or the initiating field expert. For expert-initiated LINK, party access by phone is sufficient.
|
||||
@@ -3958,7 +4035,13 @@ export class RequestManagementService {
|
||||
await (req as any).save();
|
||||
}
|
||||
}
|
||||
return req;
|
||||
|
||||
const claimCreation = await this.computeClaimCreationHintForViewer(req, user);
|
||||
const plain =
|
||||
typeof (req as any).toObject === "function"
|
||||
? (req as any).toObject({ versionKey: false })
|
||||
: { ...(req as any) };
|
||||
return { ...plain, claimCreation };
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user