From 8730e9af626721dca36ab6c16ac5b36b463ff2a4 Mon Sep 17 00:00:00 2001 From: "s.hajizadeh" Date: Mon, 1 Jun 2026 18:14:21 +0330 Subject: [PATCH] refresh script modified --- scripts/refresh-blame-inquiries.js | 225 ++++++++++++++++++----------- 1 file changed, 142 insertions(+), 83 deletions(-) diff --git a/scripts/refresh-blame-inquiries.js b/scripts/refresh-blame-inquiries.js index 38745c8..c68d3f5 100644 --- a/scripts/refresh-blame-inquiries.js +++ b/scripts/refresh-blame-inquiries.js @@ -1,8 +1,9 @@ #!/usr/bin/env node /* - * One-time script to replace mocked blameCases party vehicle inquiry data with - * Tejarat block inquiry responses. + * One-time script to replace mocked blameCases party vehicle inquiry data. + * THIRD_PARTY cases use Tejarat block inquiry; CAR_BODY cases use both + * Tejarat third-party block inquiry and car-body inquiry. * * Defaults to DRY_RUN=true. Set DRY_RUN=false to write changes. */ @@ -150,33 +151,45 @@ async function main() { const party = parties[index]; const partyLabel = `${label} parties[${index}] role=${party && party.role ? party.role : "-"}`; - const input = buildInquiryInput(doc, party); + const input = buildInquiryInputs(doc, party); if (!input.ok) { summary.partiesSkipped += 1; console.warn(`[skip] ${partyLabel}: ${input.reason}`); continue; } - console.log(`[request] ${partyLabel} body=${JSON.stringify(input.body)}`); + let nextParty = party; + let partyChanged = false; - try { - const inquiryResponse = await inquiryWithRetry(doc.type, input.body); - const successful = isInquirySuccessful(doc.type, inquiryResponse); + for (const request of input.requests) { console.log( - `[response] ${partyLabel} successful=${successful} body=${JSON.stringify(inquiryResponse)}`, + `[request] ${partyLabel} type=${request.type} body=${JSON.stringify(request.body)}`, ); - if (!successful) { - summary.partiesFailed += 1; - continue; - } + try { + const inquiryResponse = await inquiryWithRetry(request.type, request.body); + const successful = isInquirySuccessful(request.type, inquiryResponse); + console.log( + `[response] ${partyLabel} type=${request.type} successful=${successful} body=${JSON.stringify(inquiryResponse)}`, + ); - parties[index] = applyInquiryToParty(doc.type, party, inquiryResponse, input.plate); - summary.partiesInquired += 1; + if (!successful) { + summary.partiesFailed += 1; + continue; + } + + nextParty = applyInquiryToParty(request.type, nextParty, inquiryResponse, input.plate); + summary.partiesInquired += 1; + partyChanged = true; + } catch (error) { + summary.partiesFailed += 1; + console.error(`[error] ${partyLabel} type=${request.type}: ${error.message}`); + } + } + + if (partyChanged) { + parties[index] = nextParty; docChanged = true; - } catch (error) { - summary.partiesFailed += 1; - console.error(`[error] ${partyLabel}: ${error.message}`); } } @@ -207,7 +220,7 @@ async function main() { console.log(`[done] ${JSON.stringify(summary)}`); } -function buildInquiryInput(doc, party) { +function buildInquiryInputs(doc, party) { if (!party || typeof party !== "object") return { ok: false, reason: "party is empty" }; if (!party.vehicle || typeof party.vehicle !== "object") { return { ok: false, reason: "party.vehicle is missing" }; @@ -224,37 +237,54 @@ function buildInquiryInput(doc, party) { const serialLetter = NUMBER_TO_LETTER[String(plate.Plk2)] || cleanString(plate.Plk2); if (!serialLetter) return { ok: false, reason: `no Persian letter mapping for Plk2=${plate.Plk2}` }; - if (doc.type === "CAR_BODY") { - return { - ok: true, - plate, - body: { - part1: toNumber(plate.Plk1), - part2: serialLetter, - part3: toNumber(plate.Plk3), - part4: toNumber(plate.PlkSrl), - nationalCode, - }, - }; - } - if (doc.type === "THIRD_PARTY") { return { ok: true, plate, - body: { - leftTwoDigits: String(plate.Plk1), - serialLetter, - threeDigits: String(plate.Plk3), - rightTwoDigits: String(plate.PlkSrl), - nationalCode, - }, + requests: [buildThirdPartyRequest(plate, serialLetter, nationalCode)], + }; + } + + if (doc.type === "CAR_BODY") { + return { + ok: true, + plate, + requests: [ + buildThirdPartyRequest(plate, serialLetter, nationalCode), + buildCarBodyRequest(plate, serialLetter, nationalCode), + ], }; } return { ok: false, reason: `unsupported type=${doc.type}` }; } +function buildThirdPartyRequest(plate, serialLetter, nationalCode) { + return { + type: "THIRD_PARTY", + body: { + leftTwoDigits: String(plate.Plk1), + serialLetter, + threeDigits: String(plate.Plk3), + rightTwoDigits: String(plate.PlkSrl), + nationalCode, + }, + }; +} + +function buildCarBodyRequest(plate, serialLetter, nationalCode) { + return { + type: "CAR_BODY", + body: { + part1: toNumber(plate.Plk1), + part2: serialLetter, + part3: toNumber(plate.Plk3), + part4: toNumber(plate.PlkSrl), + nationalCode, + }, + }; +} + function parsePlateId(plateId) { const value = cleanString(plateId); if (!value) return null; @@ -377,52 +407,34 @@ function isInquirySuccessful(type, response) { } function applyInquiryToParty(type, party, response, originalPlate) { + if (type === "CAR_BODY") return applyCarBodyInquiryToParty(party, response, originalPlate); + return applyThirdPartyInquiryToParty(party, response, originalPlate); +} + +function applyThirdPartyInquiryToParty(party, response, originalPlate) { const next = clone(party); if (!next.vehicle) next.vehicle = {}; if (!next.insurance) next.insurance = {}; - const mapped = type === "CAR_BODY" - ? normalizeCarBodyResponse(response, originalPlate) - : normalizeThirdPartyResponse(response, originalPlate); + const mapped = normalizeThirdPartyResponse(response, originalPlate); const plateId = buildPlateId(originalPlate); next.vehicle.plateId = plateId || next.vehicle.plateId; next.vehicle.inquiry = { - source: type === "CAR_BODY" ? "TEJARAT_CAR_BODY_BLOCK_INQUIRY" : "TEJARAT_BLOCK_INQUIRY", - raw: type === "CAR_BODY" ? normalizeCarBodyRaw(response, originalPlate) : response, + source: "TEJARAT_BLOCK_INQUIRY", + raw: response, mapped, refreshedAt: new Date().toISOString(), }; - - if (type === "CAR_BODY") { - next.vehicle.inquiry.responseMeta = { - isSuccess: response.isSuccess, - statusCode: response.statusCode, - message: response.message, - }; - } - - if (type === "CAR_BODY") { - next.vehicle.name = mapped.vehicleSystemTitle || next.vehicle.name; - next.vehicle.type = mapped.vehicleGroupTitle || next.vehicle.type; - next.insurance.policyNumber = mapped.printNumber || next.insurance.policyNumber; - next.insurance.company = mapped.companyName || next.insurance.company; - next.insurance.startDate = mapped.beginDate || next.insurance.startDate; - next.insurance.endDate = mapped.endDate || next.insurance.endDate; - next.insurance.carBodyInsurance = { - policyNumber: mapped.printNumber, - startDate: mapped.beginDate, - endDate: mapped.endDate, - insurerCompany: mapped.companyName, - coverages: Array.isArray(mapped.coverages) ? mapped.coverages : [], - }; - return next; + if (party.vehicle && party.vehicle.inquiry && party.vehicle.inquiry.carBody) { + next.vehicle.inquiry.carBody = party.vehicle.inquiry.carBody; } next.vehicle.name = mapped.vehiclePersianName || mapped.MapTypNam || "اطلاعات این گزینه در استعلام موجود نیست"; next.vehicle.type = mapped.persianCarType || mapped.MapUsageName || next.vehicle.type; - next.insurance.policyNumber = mapped.insuranceNumber || next.insurance.policyNumber; + next.insurance.policyNumber = + mapped.LastCompanyDocumentNumber || mapped.insuranceNumber || next.insurance.policyNumber; next.insurance.company = mapped.companyPersianName || next.insurance.company; next.insurance.financialCeiling = mapped.financeCoverage || next.insurance.financialCeiling; next.insurance.startDate = mapped.persianStartDate || next.insurance.startDate; @@ -430,6 +442,50 @@ function applyInquiryToParty(type, party, response, originalPlate) { return next; } +function applyCarBodyInquiryToParty(party, response, originalPlate) { + const next = clone(party); + if (!next.vehicle) next.vehicle = {}; + if (!next.insurance) next.insurance = {}; + + const mapped = normalizeCarBodyResponse(response, originalPlate); + const plateId = buildPlateId(originalPlate); + + next.vehicle.plateId = plateId || next.vehicle.plateId; + if (!next.vehicle.inquiry || typeof next.vehicle.inquiry !== "object") { + next.vehicle.inquiry = {}; + } + + next.vehicle.inquiry.carBody = { + source: "TEJARAT_CAR_BODY_INQUIRY", + raw: response, + mapped, + refreshedAt: new Date().toISOString(), + }; + + next.vehicle.name = mapped.vehicleSystemTitle || next.vehicle.name; + next.vehicle.type = mapped.vehicleGroupTitle || next.vehicle.type; + next.insurance.carBodyInsurance = { + policyNumber: mapped.policyNumber, + companyId: mapped.companyId, + companyName: mapped.CompanyName, + insurerName: mapped.insurerName, + insurerNationalCode: mapped.insurerNationalCode, + ownerNationalCode: mapped.ownerNationalCode, + chassisNumber: mapped.chassisNumber, + vin: mapped.vin, + motorNumber: mapped.motorNumber, + vehicleGroup: mapped.vehicleGroupTitle, + vehicleSystem: mapped.vehicleSystemTitle, + startDate: mapped.StartDate, + endDate: mapped.EndDate, + issueDate: mapped.IssueDate, + noLossYearsCount: mapped.noLossYearsCount, + lossDocuments: Array.isArray(mapped.lossDocuments) ? mapped.lossDocuments : [], + hasEndorsement: mapped.hasEndorsement, + }; + return next; +} + function buildPlateId(plate) { if (!plate) return ""; const serialLetter = NUMBER_TO_LETTER[String(plate.Plk2)] || cleanString(plate.Plk2); @@ -454,24 +510,19 @@ function normalizeThirdPartyResponse(response, originalPlate) { PlkSrl: toNumber(originalPlate.PlkSrl), CompanyName: response.companyPersianName, CompanyCode: response.companyId, - LastCompanyDocumentNumber: response.insuranceNumber, + LastCompanyDocumentNumber: response.lastCompanyInsuranceNumber || response.insuranceNumber, FinancialCvrCptl: response.financeCoverage, IssueDate: response.hIsuDte || response.persianStartDate, SatrtDate: response.persianStartDate, EndDate: response.persianEndDate, MapTypNam: response.vehiclePersianName, - MapUsageName: response.MapUsageName, - }; -} - -function normalizeCarBodyRaw(response, originalPlate) { - const data = response.data || {}; - return { - ...data, - Plk1: toNumber(originalPlate.Plk1), - Plk2: toNumber(originalPlate.Plk2), - Plk3: toNumber(originalPlate.Plk3), - PlkSrl: toNumber(originalPlate.PlkSrl), + MapUsageName: response.MapUsageName || response.persianCarType, + UsageField: response.persianCarType, + UsageCode: response.usgCod, + VehicleSystemCode: response.vehSysCod, + CarGroupCode: response.carGrpCod, + EdrsJson: Array.isArray(response.edrSes) ? JSON.stringify(response.edrSes) : response.EdrsJson, + InsuranceFullName: response.fullname, }; } @@ -483,16 +534,24 @@ function normalizeCarBodyResponse(response, originalPlate) { Plk2: toNumber(originalPlate.Plk2), Plk3: toNumber(originalPlate.Plk3), PlkSrl: toNumber(originalPlate.PlkSrl), + policyNumber: data.printNumber, CompanyName: data.companyName, CompanyCode: data.companyId, LastCompanyDocumentNumber: data.printNumber, + InsuranceFullName: data.insurerName, IssueDate: data.issueDate, + StartDate: data.beginDate, SatrtDate: data.beginDate, EndDate: data.endDate, + EngineNumberField: data.motorNumber, MtrNum: data.motorNumber, + ChassisNumberField: data.chassisNumber, ShsNum: data.chassisNumber, VinNumberField: data.vin, - MapTypNam: data.vehicleSystemTitle, + MapTypNam: data.vehicleGroupTitle, + isSuccess: response.isSuccess, + statusCode: response.statusCode, + message: response.message, }; }