forked from Yara724/api
script updated , inquiry field added to blame case and claim case
This commit is contained in:
@@ -123,6 +123,64 @@ export class RequestManagementService {
|
||||
: -1;
|
||||
}
|
||||
|
||||
private normalizeInquiryError(err: any): any {
|
||||
if (!err) return undefined;
|
||||
return {
|
||||
message: err?.message || String(err),
|
||||
status: err?.response?.status,
|
||||
data: err?.response?.data,
|
||||
};
|
||||
}
|
||||
|
||||
private recordCaseInquiryStatus(
|
||||
req: any,
|
||||
key: "thirdParty" | "carBody" | "person" | "drivingLicence" | string,
|
||||
has: boolean,
|
||||
data: any = {},
|
||||
error?: any,
|
||||
): void {
|
||||
const existingInquiries =
|
||||
req.inquiries?.toObject?.() ??
|
||||
(req.inquiries && typeof req.inquiries === "object" ? req.inquiries : {});
|
||||
req.inquiries = {
|
||||
...existingInquiries,
|
||||
[key]: {
|
||||
has,
|
||||
data: data ?? {},
|
||||
...(error ? { error: this.normalizeInquiryError(error) } : {}),
|
||||
updatedAt: new Date(),
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
private recordPartyCaseInquiryStatus(
|
||||
req: any,
|
||||
key: "thirdParty" | "carBody" | "person" | "drivingLicence" | string,
|
||||
role: PartyRole,
|
||||
has: boolean,
|
||||
data: any = {},
|
||||
error?: any,
|
||||
): void {
|
||||
const existingData =
|
||||
req?.inquiries?.[key]?.data?.toObject?.() ?? req?.inquiries?.[key]?.data;
|
||||
const dataByRole =
|
||||
existingData &&
|
||||
typeof existingData === "object" &&
|
||||
!Array.isArray(existingData)
|
||||
? existingData
|
||||
: {};
|
||||
this.recordCaseInquiryStatus(
|
||||
req,
|
||||
key,
|
||||
has,
|
||||
{
|
||||
...dataByRole,
|
||||
[role]: data ?? {},
|
||||
},
|
||||
error,
|
||||
);
|
||||
}
|
||||
|
||||
private assertPartyOwner(party: any, user: any, errMsg: string) {
|
||||
const partyUserId = party?.person?.userId
|
||||
? String(party.person.userId)
|
||||
@@ -967,6 +1025,11 @@ export class RequestManagementService {
|
||||
this.logger.log(
|
||||
`[TEJARAT] block inquiry mapped for request=${req._id}: ${JSON.stringify(inquiryMapped)}`,
|
||||
);
|
||||
this.recordPartyCaseInquiryStatus(req, "thirdParty", role, true, {
|
||||
source: "TEJARAT_BLOCK_INQUIRY",
|
||||
raw: inquiryRaw,
|
||||
mapped: inquiryMapped,
|
||||
});
|
||||
} catch (err: any) {
|
||||
this.logger.error(
|
||||
`[TEJARAT] block inquiry failed for request=${req._id}: ${err?.message || err}`,
|
||||
@@ -978,6 +1041,15 @@ export class RequestManagementService {
|
||||
}, data=${JSON.stringify(err.response.data)}`,
|
||||
);
|
||||
}
|
||||
this.recordPartyCaseInquiryStatus(
|
||||
req,
|
||||
"thirdParty",
|
||||
role,
|
||||
false,
|
||||
{},
|
||||
err,
|
||||
);
|
||||
await (req as any).save();
|
||||
throw new HttpException("Inquiry failed", HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
|
||||
@@ -987,6 +1059,12 @@ export class RequestManagementService {
|
||||
inquiryMapped.Error,
|
||||
)}`,
|
||||
);
|
||||
this.recordPartyCaseInquiryStatus(req, "thirdParty", role, false, {
|
||||
source: "TEJARAT_BLOCK_INQUIRY",
|
||||
raw: inquiryRaw,
|
||||
mapped: inquiryMapped,
|
||||
});
|
||||
await (req as any).save();
|
||||
throw new HttpException(
|
||||
inquiryMapped.Error.Message || "Inquiry returned error",
|
||||
HttpStatus.BAD_REQUEST,
|
||||
@@ -1020,6 +1098,13 @@ export class RequestManagementService {
|
||||
personalNationalCode,
|
||||
personalBirthDate,
|
||||
);
|
||||
this.recordPartyCaseInquiryStatus(
|
||||
req,
|
||||
"person",
|
||||
role,
|
||||
true,
|
||||
personalInquiry,
|
||||
);
|
||||
this.logger.log(
|
||||
`[SANDHUB] personal inquiry success request=${req._id} nationalCode=${personalNationalCode}: ${JSON.stringify(
|
||||
personalInquiry,
|
||||
@@ -1029,6 +1114,8 @@ export class RequestManagementService {
|
||||
this.logger.error(
|
||||
`[SANDHUB] personal inquiry failed request=${req._id} nationalCode=${personalNationalCode}: ${err?.message || err}`,
|
||||
);
|
||||
this.recordPartyCaseInquiryStatus(req, "person", role, false, {}, err);
|
||||
await (req as any).save();
|
||||
throw new HttpException(
|
||||
"Personal identity inquiry failed",
|
||||
HttpStatus.BAD_REQUEST,
|
||||
@@ -1120,10 +1207,28 @@ export class RequestManagementService {
|
||||
|
||||
// CAR BODY EXTERNAL API INQUIRY
|
||||
if (req.type === BlameRequestType.CAR_BODY && role === PartyRole.FIRST) {
|
||||
const carBodyInfo = await this.sandHubService.getTejaratCarBodyInquiry({
|
||||
nationalCodeOfInsurer: body.nationalCodeOfInsurer,
|
||||
plate: body.plate,
|
||||
});
|
||||
let carBodyInfo: any;
|
||||
try {
|
||||
carBodyInfo = await this.sandHubService.getTejaratCarBodyInquiry({
|
||||
nationalCodeOfInsurer: body.nationalCodeOfInsurer,
|
||||
plate: body.plate,
|
||||
});
|
||||
this.recordPartyCaseInquiryStatus(req, "carBody", role, true, {
|
||||
source: "TEJARAT_CAR_BODY_INQUIRY",
|
||||
raw: carBodyInfo.raw,
|
||||
mapped: carBodyInfo.mapped,
|
||||
});
|
||||
} catch (err: any) {
|
||||
this.logger.error(
|
||||
`[TEJARAT] car body inquiry failed for request=${req._id}: ${err?.message || err}`,
|
||||
);
|
||||
this.recordPartyCaseInquiryStatus(req, "carBody", role, false, {}, err);
|
||||
await (req as any).save();
|
||||
throw new HttpException(
|
||||
"Car body inquiry failed",
|
||||
HttpStatus.BAD_REQUEST,
|
||||
);
|
||||
}
|
||||
|
||||
// Raw + mapped stored under vehicle (same pattern as third-party inquiry)
|
||||
party.vehicle.inquiry = {
|
||||
@@ -4829,8 +4934,24 @@ export class RequestManagementService {
|
||||
nationalCodeOfInsurer: firstPartyPlate.nationalCodeOfInsurer,
|
||||
});
|
||||
sandHubReport = sandHubResponse["_doc"] || sandHubResponse;
|
||||
this.recordPartyCaseInquiryStatus(
|
||||
req,
|
||||
"thirdParty",
|
||||
PartyRole.FIRST,
|
||||
true,
|
||||
sandHubReport,
|
||||
);
|
||||
} catch (e) {
|
||||
this.logger.error("SandHub error in expertCompleteCarBodyFormV2:", e);
|
||||
this.recordPartyCaseInquiryStatus(
|
||||
req,
|
||||
"thirdParty",
|
||||
PartyRole.FIRST,
|
||||
false,
|
||||
{},
|
||||
e,
|
||||
);
|
||||
await (req as any).save();
|
||||
throw new InternalServerErrorException(
|
||||
"Failed to process plate information.",
|
||||
);
|
||||
@@ -4864,6 +4985,13 @@ export class RequestManagementService {
|
||||
firstPartyPlate.plate,
|
||||
firstPartyPlate.nationalCodeOfInsurer,
|
||||
);
|
||||
this.recordPartyCaseInquiryStatus(
|
||||
req,
|
||||
"carBody",
|
||||
PartyRole.FIRST,
|
||||
true,
|
||||
carBodyInfo,
|
||||
);
|
||||
|
||||
const firstParty: any = {
|
||||
role: PartyRole.FIRST,
|
||||
@@ -5010,11 +5138,32 @@ export class RequestManagementService {
|
||||
desc: string,
|
||||
role: PartyRole,
|
||||
) => {
|
||||
const sandHubResponse = await this.sandHubService.getSandHubResponse({
|
||||
plate: plateDto.plate,
|
||||
nationalCodeOfInsurer: plateDto.nationalCodeOfInsurer,
|
||||
});
|
||||
let sandHubResponse: any;
|
||||
try {
|
||||
sandHubResponse = await this.sandHubService.getSandHubResponse({
|
||||
plate: plateDto.plate,
|
||||
nationalCodeOfInsurer: plateDto.nationalCodeOfInsurer,
|
||||
});
|
||||
} catch (err: any) {
|
||||
this.recordPartyCaseInquiryStatus(
|
||||
req,
|
||||
"thirdParty",
|
||||
role,
|
||||
false,
|
||||
{},
|
||||
err,
|
||||
);
|
||||
await (req as any).save();
|
||||
throw err;
|
||||
}
|
||||
const sandHubReport = (sandHubResponse["_doc"] || sandHubResponse) as any;
|
||||
this.recordPartyCaseInquiryStatus(
|
||||
req,
|
||||
"thirdParty",
|
||||
role,
|
||||
true,
|
||||
sandHubReport,
|
||||
);
|
||||
const clientName =
|
||||
sandHubReport?.CompanyName || sandHubReport?.LastCompanyName;
|
||||
const companyCode = sandHubReport?.CompanyCode;
|
||||
|
||||
Reference in New Issue
Block a user