enforced matching client id for moving on with the case

This commit is contained in:
SepehrYahyaee
2026-09-07 15:06:06 +03:30
parent 7e3b308572
commit 229957e283
4 changed files with 317 additions and 45 deletions

View File

@@ -134,6 +134,30 @@ function formatJalaliCompact(raw: number | string | null | undefined): string |
export class RequestManagementService {
private readonly logger = new Logger(RequestManagementService.name);
/**
* Only the guilty (currently first) party's THIRD_PARTY policy must belong
* to this deployment. CAR_BODY flows deliberately do not constrain that
* separate third-party policy; their car-body lookup is checked on its own.
*/
private policyInquiryOptions(
requestType: BlameRequestType | string,
partyRole: PartyRole,
clientId?: string,
) {
const enforceDeploymentClientMatch =
requestType === BlameRequestType.THIRD_PARTY &&
partyRole === PartyRole.FIRST;
if (!clientId && !enforceDeploymentClientMatch) return undefined;
return {
...(clientId ? { clientId } : {}),
...(enforceDeploymentClientMatch
? { enforceDeploymentClientMatch: true }
: {}),
};
}
private stepKeyToPartyRole(stepKey: WorkflowStep): PartyRole {
if (String(stepKey).startsWith("FIRST_")) return PartyRole.FIRST;
if (String(stepKey).startsWith("SECOND_")) return PartyRole.SECOND;
@@ -1283,9 +1307,11 @@ export class RequestManagementService {
const inquiryClientId = party.person?.clientId
? String(party.person.clientId)
: undefined;
const inquiryOptions = inquiryClientId
? { clientId: inquiryClientId }
: undefined;
const inquiryOptions = this.policyInquiryOptions(
req.type,
role,
inquiryClientId,
);
try {
const inquiry = await this.sandHubService.getTejaratBlockInquiry(
{
@@ -1513,15 +1539,10 @@ export class RequestManagementService {
if (req.type === BlameRequestType.CAR_BODY && role === PartyRole.FIRST) {
let carBodyInfo: any;
try {
carBodyInfo = await this.sandHubService.getCarBodyInquiry(
{
nationalCodeOfInsurer: body.nationalCodeOfInsurer,
plate: body.plate,
},
resolvedClientId
? { clientId: String(resolvedClientId) }
: inquiryOptions,
);
carBodyInfo = await this.sandHubService.getCarBodyInquiry({
nationalCodeOfInsurer: body.nationalCodeOfInsurer,
plate: body.plate,
});
this.recordPartyCaseInquiryStatus(req, "carBody", role, true, {
source: carBodyInfo.source,
raw: carBodyInfo.raw,
@@ -1763,9 +1784,11 @@ export class RequestManagementService {
const inquiryClientId = party.person?.clientId
? String(party.person.clientId)
: undefined;
const inquiryOptions = inquiryClientId
? { clientId: inquiryClientId }
: undefined;
const inquiryOptions = this.policyInquiryOptions(
req.type,
role,
inquiryClientId,
);
let inquiryRaw: any;
let inquiryMapped: any;
@@ -1952,6 +1975,79 @@ export class RequestManagementService {
inquiryMapped?.HEndDte ||
inquiryMapped?.persianEndDate;
// CAR_BODY eligibility is independent of the third-party policy above.
// The VIN flow must make the same deployment-scoped lookup as the plate
// flow before the case is allowed to advance.
if (req.type === BlameRequestType.CAR_BODY && role === PartyRole.FIRST) {
try {
const carBodyInfo = await this.sandHubService.getCarBodyInquiry({
nationalCodeOfInsurer: body.nationalCodeOfInsurer,
plate: body.vin,
});
this.recordPartyCaseInquiryStatus(req, "carBody", role, true, {
source: carBodyInfo.source,
raw: carBodyInfo.raw,
mapped: carBodyInfo.mapped,
});
party.vehicle.inquiry = {
...party.vehicle.inquiry,
carBody: {
source: carBodyInfo.source,
raw: carBodyInfo.raw,
mapped: carBodyInfo.mapped,
},
};
const m = carBodyInfo.mapped as any;
(party.insurance as any).carBodyInsurance = {
policyNumber: m.policyNumber ?? null,
companyId: m.companyId ?? m.CompanyCode ?? null,
companyName: m.CompanyName ?? null,
insurerName: m.insurerName ?? null,
chassisNumber: m.ChassisNumberField ?? null,
vin: m.VinNumberField ?? null,
motorNumber: m.EngineNumberField ?? null,
startDate: m.StartDate ?? null,
endDate: m.EndDate ?? null,
issueDate: m.IssueDate ?? null,
noLossYearsCount: m.noLossYearsCount ?? null,
lossDocuments: m.lossDocuments ?? [],
};
const carBodyCompanyCode = m.companyId ?? m.CompanyCode;
const carBodyCompanyName = m.CompanyName ?? m.companyPersianName;
if (carBodyCompanyCode && carBodyCompanyName) {
const carBodyClient =
await this.clientService.findOrCreateClientByCompanyCode(
carBodyCompanyCode,
carBodyCompanyName,
);
const carBodyClientId =
(carBodyClient as any)?._id ?? (carBodyClient as any)?._doc?._id;
if (carBodyClientId) party.person.clientId = carBodyClientId;
}
} catch (err: any) {
this.logger.error(
`[CAR_BODY] VIN inquiry failed for request=${req._id}: ${err?.message || err}`,
);
this.recordPartyCaseInquiryStatus(
req,
"carBody",
role,
false,
{},
err,
);
await this.blameRequestDbService.findByIdAndUpdate(req._id, {
$set: { inquiries: req.inquiries },
});
throw new HttpException(
"Car body inquiry failed",
HttpStatus.BAD_REQUEST,
);
}
}
// Advance workflow
await this.advanceWorkflowToNext(req, stepKey);
@@ -2934,6 +3030,13 @@ export class RequestManagementService {
body: AddPlateDto,
partyType: "firstParty" | "secondParty",
) {
if (request.type === "THIRD_PARTY" && partyType === "firstParty") {
this.sandHubService.assertInsuranceMatchesDeployment(
sandHubReport,
"THIRD_PARTY",
);
}
const clientName = sandHubReport?.CompanyName;
const companyCode = sandHubReport?.CompanyCode;
@@ -3024,12 +3127,10 @@ export class RequestManagementService {
// For CAR_BODY type, persist the provider response and its mapped fields.
if (request.type === "CAR_BODY" && partyType === "firstParty") {
const carBodyInquiry = await this.sandHubService.getCarBodyInquiry(
{
nationalCodeOfInsurer: body.nationalCodeOfInsurer,
plate: body.plate,
} as any,
);
const carBodyInquiry = await this.sandHubService.getCarBodyInquiry({
nationalCodeOfInsurer: body.nationalCodeOfInsurer,
plate: body.plate,
} as any);
const carBodyInfo = carBodyInquiry.mapped as any;
this.logger.log(
@@ -6470,6 +6571,13 @@ export class RequestManagementService {
"secondParty and guiltyPartyPhoneNumber are required.",
);
}
if (
formData.guiltyPartyPhoneNumber !== formData.firstPartyPhoneNumber
) {
throw new BadRequestException(
"The first party is the guilty party in this flow.",
);
}
if (!formData?.expertDescription?.desc) {
throw new BadRequestException("expertDescription.desc is required.");
}
@@ -6508,6 +6616,12 @@ export class RequestManagementService {
throw err;
}
const sandHubReport = (sandHubResponse["_doc"] || sandHubResponse) as any;
if (role === PartyRole.FIRST) {
this.sandHubService.assertInsuranceMatchesDeployment(
sandHubReport,
"THIRD_PARTY",
);
}
this.recordPartyCaseInquiryStatus(
req,
"thirdParty",
@@ -7119,15 +7233,13 @@ export class RequestManagementService {
);
}
// Validate guilty party phone number matches one of the parties
// The first party is the guilty party in every supported THIRD_PARTY flow.
if (request.type === "THIRD_PARTY") {
const guiltyMatchesFirst =
formData.guiltyPartyPhoneNumber === formData.firstPartyPhoneNumber;
const guiltyMatchesSecond =
formData.guiltyPartyPhoneNumber === formData.secondParty.phoneNumber;
if (!guiltyMatchesFirst && !guiltyMatchesSecond) {
if (
formData.guiltyPartyPhoneNumber !== formData.firstPartyPhoneNumber
) {
throw new BadRequestException(
"Guilty party phone number must match either first or second party phone number",
"The first party is the guilty party in this flow.",
);
}
}
@@ -7199,6 +7311,10 @@ export class RequestManagementService {
nationalCodeOfInsurer: firstPartyPlate.nationalCodeOfInsurer,
});
const sandHubReport = sandHubResponse["_doc"] || sandHubResponse;
this.sandHubService.assertInsuranceMatchesDeployment(
sandHubReport,
"THIRD_PARTY",
);
const clientName =
sandHubReport?.CompanyName || sandHubReport?.LastCompanyName;
@@ -8693,7 +8809,7 @@ export class RequestManagementService {
const roleLabel = partyRole === PartyRole.FIRST ? "FIRST" : "SECOND";
let clientId: string | undefined;
const inquiryOptions = (cid?: string) =>
cid ? { clientId: cid } : undefined;
this.policyInquiryOptions(req.type, partyRole, cid);
let inquiryRaw: any;
let inquiryMapped: any;
@@ -8813,13 +8929,10 @@ export class RequestManagementService {
partyRole === PartyRole.FIRST
) {
try {
const carBodyInfo = await this.sandHubService.getCarBodyInquiry(
{
nationalCodeOfInsurer: partyData.nationalCodeOfInsurer,
plate: partyData.plate as any,
},
clientId ? { clientId } : undefined,
);
const carBodyInfo = await this.sandHubService.getCarBodyInquiry({
nationalCodeOfInsurer: partyData.nationalCodeOfInsurer,
plate: partyData.plate as any,
});
this.recordPartyCaseInquiryStatus(req, "carBody", partyRole, true, {
source: carBodyInfo.source,
raw: carBodyInfo.raw,
@@ -9548,7 +9661,7 @@ export class RequestManagementService {
const roleLabel = partyRole === PartyRole.FIRST ? "FIRST" : "SECOND";
let clientId: string | undefined;
const inquiryOptions = (cid?: string) =>
cid ? { clientId: cid } : undefined;
this.policyInquiryOptions(req.type, partyRole, cid);
let inquiryRaw: any;
let inquiryMapped: any;
@@ -9668,13 +9781,10 @@ export class RequestManagementService {
partyRole === PartyRole.FIRST
) {
try {
const carBodyInfo = await this.sandHubService.getCarBodyInquiry(
{
nationalCodeOfInsurer: partyData.nationalCodeOfInsurer,
plate: partyData.vin as any, // VIN used as identifier for CAR_BODY
},
clientId ? { clientId } : undefined,
);
const carBodyInfo = await this.sandHubService.getCarBodyInquiry({
nationalCodeOfInsurer: partyData.nationalCodeOfInsurer,
plate: partyData.vin as any, // VIN used as identifier for CAR_BODY
});
this.recordPartyCaseInquiryStatus(req, "carBody", partyRole, true, {
source: carBodyInfo.source,
raw: carBodyInfo.raw,