1
0
forked from Yara724/api

fixed many bugs and step issues

This commit is contained in:
SepehrYahyaee
2026-04-18 17:04:19 +03:30
parent 7f5b64f2a6
commit 0086b8db4d
14 changed files with 443 additions and 89 deletions

View File

@@ -0,0 +1,19 @@
import { ApiProperty } from "@nestjs/swagger";
/**
* Computed on GET blame v2 for the current viewer — no DB field on BlameRequest.
*/
export class ClaimCreationHintDto {
@ApiProperty({
description:
"True if a v2 claim case already exists for this blame (by blameRequestId).",
})
hasClaim: boolean;
@ApiProperty({
description:
"True when this viewer is the damaged party, blame is COMPLETED, no claim exists yet, " +
"and for THIRD_PARTY all parties have signed and accepted the expert decision.",
})
shouldGuideToCreateClaim: boolean;
}

View File

@@ -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 };
}
/**

View File

@@ -16,6 +16,7 @@ import {
ApiBearerAuth,
ApiBody,
ApiConsumes,
ApiOperation,
ApiParam,
ApiTags,
} from "@nestjs/swagger";
@@ -77,6 +78,11 @@ export class RequestManagementV2Controller {
* Get one blame request by id. Allowed for the request owner (party) or the initiating field expert.
*/
@Get(":requestId")
@ApiOperation({
summary: "Get one blame request (v2)",
description:
"Response is the blame document plus **claimCreation** ({ hasClaim, shouldGuideToCreateClaim }) — computed for the current user so the app can route the damaged party to create-claim when the blame is COMPLETED and no v2 claim exists yet.",
})
@ApiParam({ name: "requestId", description: "Blame request ID" })
@Roles(RoleEnum.USER, RoleEnum.FIELD_EXPERT)
async getBlameRequestV2(