forked from Yara724/api
Added factorLink and branchName support
This commit is contained in:
@@ -1900,6 +1900,90 @@ export class ClaimRequestManagementService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* V2 response mapper:
|
||||
* - convert factorLink ObjectId -> public URL
|
||||
* - enrich daghi.branchId with branchName when available
|
||||
*/
|
||||
private async mapEvaluationForClient(evaluation: any): Promise<any | undefined> {
|
||||
if (!evaluation) return undefined;
|
||||
|
||||
const mapReply = async (reply: any) => {
|
||||
if (!reply) return reply;
|
||||
|
||||
const mapped = {
|
||||
...(typeof reply?.toObject === "function" ? reply.toObject() : reply),
|
||||
};
|
||||
const parts = Array.isArray(mapped.parts) ? mapped.parts : [];
|
||||
|
||||
const factorIds = new Set<string>();
|
||||
const branchIds = new Set<string>();
|
||||
for (const p of parts) {
|
||||
if (p?.factorLink && Types.ObjectId.isValid(String(p.factorLink))) {
|
||||
factorIds.add(String(p.factorLink));
|
||||
}
|
||||
const daghi = p?.daghi;
|
||||
const rawBranchId =
|
||||
daghi && typeof daghi === "object" ? (daghi as any).branchId : undefined;
|
||||
if (rawBranchId && Types.ObjectId.isValid(String(rawBranchId))) {
|
||||
branchIds.add(String(rawBranchId));
|
||||
}
|
||||
}
|
||||
|
||||
const factorMap = new Map<string, string>();
|
||||
await Promise.all(
|
||||
Array.from(factorIds).map(async (id) => {
|
||||
const factorDoc = await this.claimFactorsImageDbService.findById(id);
|
||||
if (factorDoc?.path) {
|
||||
factorMap.set(id, buildFileLink(factorDoc.path));
|
||||
}
|
||||
}),
|
||||
);
|
||||
|
||||
const branchMap = new Map<string, string>();
|
||||
await Promise.all(
|
||||
Array.from(branchIds).map(async (id) => {
|
||||
const branch = await this.branchDbService.findById(id);
|
||||
if (branch?.name) {
|
||||
branchMap.set(id, branch.name);
|
||||
}
|
||||
}),
|
||||
);
|
||||
|
||||
mapped.parts = parts.map((part: any) => {
|
||||
const nextPart = { ...part };
|
||||
|
||||
if (nextPart.factorLink && Types.ObjectId.isValid(String(nextPart.factorLink))) {
|
||||
const link = factorMap.get(String(nextPart.factorLink));
|
||||
if (link) nextPart.factorLink = link;
|
||||
}
|
||||
|
||||
const daghi = nextPart.daghi;
|
||||
if (daghi && typeof daghi === "object" && !Array.isArray(daghi)) {
|
||||
const branchId = (daghi as any).branchId;
|
||||
const branchName =
|
||||
branchId && Types.ObjectId.isValid(String(branchId))
|
||||
? branchMap.get(String(branchId))
|
||||
: undefined;
|
||||
nextPart.daghi = {
|
||||
...daghi,
|
||||
...(branchName ? { branchName } : {}),
|
||||
};
|
||||
}
|
||||
|
||||
return nextPart;
|
||||
});
|
||||
|
||||
return mapped;
|
||||
};
|
||||
|
||||
return {
|
||||
...evaluation,
|
||||
damageExpertReply: await mapReply(evaluation.damageExpertReply),
|
||||
damageExpertReplyFinal: await mapReply(evaluation.damageExpertReplyFinal),
|
||||
};
|
||||
}
|
||||
|
||||
async submitUserReply(
|
||||
requestId: string,
|
||||
body: UserCommentDto,
|
||||
@@ -2189,6 +2273,7 @@ export class ClaimRequestManagementService {
|
||||
return {
|
||||
message: "Factor uploaded successfully. Awaiting expert validation.",
|
||||
factorId: factorRecord._id,
|
||||
factorLink: buildFileLink(file.path),
|
||||
url: buildFileLink(file.path),
|
||||
};
|
||||
} catch (error) {
|
||||
@@ -5191,6 +5276,8 @@ export class ClaimRequestManagementService {
|
||||
}
|
||||
: undefined;
|
||||
|
||||
const mappedEvaluation = await this.mapEvaluationForClient(claim.evaluation);
|
||||
|
||||
return {
|
||||
claimRequestId: claim._id.toString(),
|
||||
publicId: claim.publicId,
|
||||
@@ -5210,10 +5297,10 @@ export class ClaimRequestManagementService {
|
||||
carAngles,
|
||||
damagedParts,
|
||||
expertResend,
|
||||
evaluation: claim.evaluation
|
||||
evaluation: mappedEvaluation
|
||||
? {
|
||||
damageExpertReply: claim.evaluation.damageExpertReply,
|
||||
damageExpertReplyFinal: claim.evaluation.damageExpertReplyFinal,
|
||||
damageExpertReply: mappedEvaluation.damageExpertReply,
|
||||
damageExpertReplyFinal: mappedEvaluation.damageExpertReplyFinal,
|
||||
}
|
||||
: undefined,
|
||||
...(isExpertViewer
|
||||
|
||||
Reference in New Issue
Block a user