From baac633443536928130b96835f05fcdc6b5c4ea2 Mon Sep 17 00:00:00 2001 From: SepehrYahyaee <7heycallmegray@gmail.com> Date: Mon, 10 Aug 2026 16:51:00 +0330 Subject: [PATCH] Fixed vin inquiry --- .../request-management.service.ts | 20 ++++++-- src/sand-hub/sand-hub.service.ts | 47 +++++++++++++++++++ 2 files changed, 63 insertions(+), 4 deletions(-) diff --git a/src/request-management/request-management.service.ts b/src/request-management/request-management.service.ts index d7e4718..64821e8 100644 --- a/src/request-management/request-management.service.ts +++ b/src/request-management/request-management.service.ts @@ -1842,8 +1842,14 @@ export class RequestManagementService { if (!party.vehicle) party.vehicle = {} as any; party.vehicle.isNew = body.isNewCar; - // Store VIN in vehicle.vin; leave plateId empty for VIN-based inquiries - party.vehicle.vin = body.vin; + // Derive plateId from the plate parts returned by the VIN inquiry (plk → Plk1/Plk2/Plk3/PlkSrl). + // Falls back to the submitted VIN so plateId is never empty. + party.vehicle.plateId = this.plateToPlateIdString({ + ir: inquiryMapped?.PlkSrl, + leftDigits: inquiryMapped?.Plk1, + centerAlphabet: inquiryMapped?.Plk2 ?? inquiryMapped?.plateLetterid, + centerDigits: inquiryMapped?.Plk3, + }) || body.vin; party.vehicle.name = inquiryMapped?.MapTypNam; party.vehicle.type = `${inquiryMapped?.UsageField} / ${inquiryMapped?.MapUsageName || "-"}`; party.vehicle.inquiry = { @@ -9508,8 +9514,14 @@ export class RequestManagementService { (party.person as any).licenseType = (partyData as any).licenseType; if (!party.vehicle) party.vehicle = {} as any; - // Store VIN in vehicle.vin; leave plateId empty for VIN-based inquiries - party.vehicle.vin = partyData.vin; + // Derive plateId from the plate parts returned by the VIN inquiry (plk → Plk1/Plk2/Plk3/PlkSrl). + // Falls back to the submitted VIN so plateId is never empty. + party.vehicle.plateId = this.plateToPlateIdString({ + ir: inquiryMapped?.PlkSrl, + leftDigits: inquiryMapped?.Plk1, + centerAlphabet: inquiryMapped?.Plk2 ?? inquiryMapped?.plateLetterid, + centerDigits: inquiryMapped?.Plk3, + }) || partyData.vin; party.vehicle.name = inquiryMapped?.MapTypNam; party.vehicle.type = `${inquiryMapped?.UsageField ?? ""} / ${inquiryMapped?.UsageName ?? inquiryMapped?.MapUsageName ?? "-"}`; party.vehicle.inquiry = { diff --git a/src/sand-hub/sand-hub.service.ts b/src/sand-hub/sand-hub.service.ts index 677d030..6f90fb2 100644 --- a/src/sand-hub/sand-hub.service.ts +++ b/src/sand-hub/sand-hub.service.ts @@ -1027,12 +1027,59 @@ export class SandHubService { /** * Maps the new SandHub API response format to the old format expected by the application */ + /** + * Parse the `plk` string returned by the ESG VIN inquiry into the four + * numeric/text plate parts used everywhere in the system. + * + * ESG format example: `"782د29 60"` + * → Plk3=782 (center digits) Plk2="د" (center letter) + * Plk1=29 (left digits) PlkSrl=60 (IR region code) + * + * Returns null if the string does not match the expected pattern. + */ + private parsePlkString( + plk: string, + ): { Plk1: number; Plk2: string; Plk3: number; PlkSrl: number } | null { + if (!plk || typeof plk !== "string") return null; + // Pattern: {centerDigits}{centerLetter(s)}{leftDigits}{ir} + const m = plk.trim().match(/^(\d+)([^\d\s]+)(\d+)\s+(\d+)$/); + if (!m) return null; + const Plk3 = parseInt(m[1], 10); // center digits + const Plk2 = this.plateNormalizer.normalizePlateText(m[2]); + const Plk1 = parseInt(m[3], 10); // left digits + const PlkSrl = parseInt(m[4], 10); // IR region code + if (!Number.isFinite(Plk3) || !Number.isFinite(Plk1) || !Number.isFinite(PlkSrl) || !Plk2) { + return null; + } + return { Plk1, Plk2, Plk3, PlkSrl }; + } + private mapNewApiResponseToOldFormat(newResponse: any): any { if (!newResponse) return newResponse; + // If the response carries a `plk` plate string (VIN inquiry) but lacks the + // individual Plk1/Plk2/Plk3/PlkSrl fields, parse and inject them so that + // all downstream plate-handling code works identically to the plate flow. + let plkParts: { Plk1: number; Plk2: string; Plk3: number; PlkSrl: number } | null = null; + if ( + newResponse.plk && + newResponse.Plk1 == null && + newResponse.Plk3 == null && + newResponse.PlkSrl == null + ) { + plkParts = this.parsePlkString(String(newResponse.plk)); + } + // Map the new field names to the old field names return { ...newResponse, + ...(plkParts ? { + Plk1: plkParts.Plk1, + Plk2: plkParts.Plk2, + Plk3: plkParts.Plk3, + PlkSrl: plkParts.PlkSrl, + plateLetterid: plkParts.Plk2, + } : {}), // Company information CompanyCode: newResponse.companyId || newResponse.CompanyCode, CompanyName: newResponse.companyPersianName || newResponse.CompanyName,