From cc8a96c875e7064b9ea7108f4e3e073686b9bb0e Mon Sep 17 00:00:00 2001 From: SepehrYahyaee <7heycallmegray@gmail.com> Date: Sun, 13 Sep 2026 12:59:39 +0330 Subject: [PATCH] Persist inquiry audit on client resolution failures --- .../request-management.service.ts | 145 ++++++++++++++++-- 1 file changed, 136 insertions(+), 9 deletions(-) diff --git a/src/request-management/request-management.service.ts b/src/request-management/request-management.service.ts index 8035063..2d7b633 100644 --- a/src/request-management/request-management.service.ts +++ b/src/request-management/request-management.service.ts @@ -1850,9 +1850,25 @@ export class RequestManagementService { // Find client by company code const clientName = inquiryMapped?.CompanyName; if (!clientName && !policyholderUnknown) { - throw new BadRequestException( + const error = new BadRequestException( `CompanyName missing from inquiry response`, ); + this.recordPartyCaseInquiryStatus( + req, + "thirdParty", + role, + false, + { + source: inquirySource, + plateKind: inquiryPlateKind, + attempts: inquiryAttempts, + raw: inquiryRaw, + mapped: inquiryMapped, + }, + error, + ); + await this.persistBlameInquiryAudit(req); + throw error; } const companyCode = inquiryMapped?.CompanyCode; const client = clientName @@ -1862,9 +1878,25 @@ export class RequestManagementService { ) : null; if (clientName && !client) { - throw new BadRequestException( + const error = new BadRequestException( `CompanyCode missing or invalid in inquiry response`, ); + this.recordPartyCaseInquiryStatus( + req, + "thirdParty", + role, + false, + { + source: inquirySource, + plateKind: inquiryPlateKind, + attempts: inquiryAttempts, + raw: inquiryRaw, + mapped: inquiryMapped, + }, + error, + ); + await this.persistBlameInquiryAudit(req); + throw error; } // Persist inquiry/body data into new model @@ -2240,9 +2272,23 @@ export class RequestManagementService { // Resolve insurer client from inquiry response const clientName = inquiryMapped?.CompanyName; if (!clientName) { - throw new BadRequestException( + const error = new BadRequestException( "CompanyName missing from VIN inquiry response", ); + this.recordPartyCaseInquiryStatus( + req, + "thirdParty", + role, + false, + { + source: "ESG_VIN_INQUIRY", + raw: inquiryRaw, + mapped: inquiryMapped, + }, + error, + ); + await this.persistBlameInquiryAudit(req); + throw error; } const companyCode = inquiryMapped?.CompanyCode; const client = await this.clientService.findOrCreateClientByCompanyCode( @@ -2250,9 +2296,23 @@ export class RequestManagementService { clientName, ); if (!client) { - throw new BadRequestException( + const error = new BadRequestException( "CompanyCode missing or invalid in VIN inquiry response", ); + this.recordPartyCaseInquiryStatus( + req, + "thirdParty", + role, + false, + { + source: "ESG_VIN_INQUIRY", + raw: inquiryRaw, + mapped: inquiryMapped, + }, + error, + ); + await this.persistBlameInquiryAudit(req); + throw error; } // Persist party data @@ -6849,6 +6909,7 @@ export class RequestManagementService { } const firstPartyPlate = inquirySubmission.dto; let sandHubReport: any; + let thirdPartyInquiry: any; try { const inquiry = inquirySubmission.participants.legacy ? await (async () => { @@ -6867,6 +6928,7 @@ export class RequestManagementService { }; })() : await this.getThirdPartyPlateInquiry(inquirySubmission); + thirdPartyInquiry = inquiry; sandHubReport = inquiry.mapped; this.recordPartyCaseInquiryStatus( req, @@ -6907,9 +6969,25 @@ export class RequestManagementService { ) : await this.clientService.findOne({ clientName }); if (!client) { - throw new NotFoundException( + const error = new NotFoundException( `Client not found for company: ${clientName}`, ); + this.recordPartyCaseInquiryStatus( + req, + "thirdParty", + PartyRole.FIRST, + false, + { + source: thirdPartyInquiry?.offline?.source ?? "TEJARAT_BLOCK_INQUIRY", + plateKind: thirdPartyInquiry?.plateKind, + attempts: thirdPartyInquiry?.attempts, + raw: thirdPartyInquiry?.raw, + mapped: thirdPartyInquiry?.mapped ?? sandHubReport, + }, + error, + ); + await this.persistBlameInquiryAudit(req); + throw error; } let carBodyInquiry: any; @@ -7223,9 +7301,25 @@ export class RequestManagementService { : await this.clientService.findOne({ clientName }) : null; if (!client && !policyholderUnknown) { - throw new NotFoundException( + const error = new NotFoundException( `Client not found for company: ${clientName}`, ); + this.recordPartyCaseInquiryStatus( + req, + "thirdParty", + role, + false, + { + source: inquiry.offline?.source ?? "TEJARAT_BLOCK_INQUIRY", + plateKind: inquiry.plateKind, + attempts: inquiry.attempts, + raw: inquiry.raw, + mapped: inquiry.mapped, + }, + error, + ); + await this.persistBlameInquiryAudit(req); + throw error; } const resolvedClientId = (client as any)?._id ?? (client as any)?._doc?._id; @@ -7965,9 +8059,20 @@ export class RequestManagementService { : await this.clientService.findOne({ clientName: clientName }); if (!client) { - throw new NotFoundException( + const error = new NotFoundException( `Client not found for company: ${clientName}`, ); + await this.persistLegacyInquiryAudit( + requestId, + "firstPartyDetails", + firstInquirySubmission, + { + ...(firstPolicyInquiry ?? {}), + failed: true, + error: this.normalizeInquiryError(error), + }, + ); + throw error; } // Add first party plate and related data to the existing firstPartyDetails object @@ -8092,9 +8197,20 @@ export class RequestManagementService { : null; if (!client && !policyholderUnknown) { - throw new NotFoundException( + const error = new NotFoundException( `Client not found for company: ${clientName}${companyCode ? ` (code: ${companyCode})` : ""}`, ); + await this.persistLegacyInquiryAudit( + requestId, + "secondPartyDetails", + secondInquirySubmission, + { + ...(secondPolicyInquiry ?? {}), + failed: true, + error: this.normalizeInquiryError(error), + }, + ); + throw error; } // Add second party plate and related data to the existing secondPartyDetails object @@ -8423,9 +8539,20 @@ export class RequestManagementService { : await this.clientService.findOne({ clientName: clientName }); if (!client) { - throw new NotFoundException( + const error = new NotFoundException( `Client not found for company: ${clientName}`, ); + await this.persistLegacyInquiryAudit( + requestId, + "firstPartyDetails", + firstInquirySubmission, + { + thirdParty: thirdPartyPolicyInquiry, + failed: true, + error: this.normalizeInquiryError(error), + }, + ); + throw error; } // Add first party plate and related data to the existing firstPartyDetails object