import { CarData, Source, SubmissionVehicleApiRequest, } from '@sky-distribution/policy-service-interfaces/lib/common/vehicle/SubmissionVehicle.type'; import { VehicleBodyType } from '@sky-distribution/policy-service-interfaces/lib/common/vehicle/VehicleBody.type'; import { logger } from '../../../config/logger'; import { buildDatePartial } from '../partials/date'; import { CoreRequestDefaults } from './DefaultValues'; const INFO_EXPERT_CAR_DATA_OVERRIDE_WARNING_MSG = `In process of updating vehicleApiRequestToVehicleBodyType application overrited infoExpertCarData in vehicleValues`; const EURO_TAX_CAR_DATA_OVERRIDE_WARNING_MSG = `In process of updating vehicleApiRequestToVehicleBodyType application overrited euroTaxCarData in vehicleValues`; const MANUALLY_OVERRIDE_WARNING_MSG = `In process of updating vehicleApiRequestToVehicleBodyType application overrited manually added data about car in vehicleValues`; // eslint-disable-next-line arrow-body-style export const findPublicIDInVehicleValues = (vehicleValues: CarData[], source: Source): string | undefined => { return vehicleValues.find((element) => element.source === source)?.publicID; }; export const buildVehicleValues = ( inputCurrent: Partial, euroTaxCarData: CarData, infoExpertCarData: CarData, manual: CarData, ): CarData[] => { if (euroTaxCarData || infoExpertCarData || manual) { let publicIDEuroTax: string | undefined; let publicIDInfoExpert: string | undefined; let publicIDManual: string | undefined; if (inputCurrent.vehicleValues) { publicIDEuroTax = findPublicIDInVehicleValues(inputCurrent.vehicleValues, 'eurotax'); publicIDInfoExpert = findPublicIDInVehicleValues(inputCurrent.vehicleValues, 'info_expert'); publicIDManual = findPublicIDInVehicleValues(inputCurrent.vehicleValues, 'manual'); } return [ { ...euroTaxCarData, ...(publicIDEuroTax && { publicID: publicIDEuroTax }), }, { ...infoExpertCarData, ...(publicIDInfoExpert && { publicID: publicIDInfoExpert }), }, { ...manual, ...(publicIDManual && { publicID: publicIDManual }), }].filter((item) => item.source); } return inputCurrent.vehicleValues ? inputCurrent.vehicleValues : []; }; export const vehicleApiRequestToVehicleBodyType = ( inputUpdate: Partial = {}, inputCurrent: Partial = {}, ): Partial => { const output: Partial = { licenseState: CoreRequestDefaults.STATE, license: inputUpdate.plateNumber, licensePlate: inputUpdate.plateNumber, make: inputUpdate.brand, model: inputUpdate.model, vin: inputUpdate.vin, year: parseInt(inputUpdate.year ?? '', 10) || undefined, bodyType_SKY: inputUpdate.bodyType, version_SKY: inputUpdate.version, enginePower_SKY: inputUpdate.coreHorsePower, engineCapacity_SKY: parseInt(inputUpdate.engineCapacity ?? '', 10) || undefined, vehicleType_SKY: inputUpdate.vehicleType, primaryUse: CoreRequestDefaults.VEHICLE_DATA.PRIMARY_USE, fuelType_SKY: inputUpdate.coreFuelType?.name, LPG_SKY: inputUpdate.coreFuelType?.isLpg, doorCount_SKY: parseInt(inputUpdate.doors ?? '', 10) || undefined, vehicleLength_SKY: inputUpdate.coreLength, vehicleWeight_SKY: inputUpdate.coreWeight, keysNumber_SKY: inputUpdate.keys, seatsNumber_SKY: inputUpdate.seats, autoalarm: inputUpdate.alarm, immobilizer: inputUpdate.immobilizer, transmissionLock: inputUpdate.transmissionLock, securityLocationDevice: inputUpdate.trackingDevice, steeringWheelLock: inputUpdate.steeringLock, dnaMapping: inputUpdate.dna, leftHandDrive_SKY: inputUpdate.leftHandDrive, firstRegistrationCountry_SKY: inputUpdate.firstRegistrationCountry, firstRegistrationDate_SKY: buildDatePartial(inputUpdate.firstRegistrationDate), actualMileage_SKY: inputUpdate.actualMileage, yearOfCarPurchase: inputUpdate.yearOfCarPurchase, damageExists: inputUpdate.damageExists, abroadUsageTime_SKY: inputUpdate.abroadUsageTime, annualMileage: inputUpdate.annualMileage, whereGaraged: inputUpdate.whereGaraged, carLeased: inputUpdate.carLeased, yobYoungestAdditionalDriver: inputUpdate.yobYoungestAdditionalDriver, addlYoungDriversNumber: inputUpdate.addlYoungDriversNumber, otherAddlDriversUnder26: inputUpdate.otherAddlDriversUnder26, youngestDriverLicenseYear: inputUpdate.youngestDriverLicenseYear, usedAbroadLongerThanMonth: inputUpdate.usedAbroadLongerThanMonth, techInspectionDate_SKY: buildDatePartial(inputUpdate.techInspectionDate), vehicleValues: buildVehicleValues( inputCurrent, inputUpdate.euroTaxCarData as CarData, inputUpdate.infoExpertCarData as CarData, inputUpdate.manual as CarData, ), }; return JSON.parse(JSON.stringify(output)) as typeof output; }; export const checkCurrentAndUpdateInputVehicleValues = ( current: Partial, update: Partial, ): void => { if (current.vehicleValues) { // TODO: after 01.07.2020 change login leavel to debug. current.vehicleValues.forEach((record: any) => { if (update.infoExpertCarData) { // eslint-disable-next-line @typescript-eslint/no-unused-expressions update.infoExpertCarData.externalSystemID === record.externalSystemID ? null : logger.info(INFO_EXPERT_CAR_DATA_OVERRIDE_WARNING_MSG); } if (update.euroTaxCarData) { // eslint-disable-next-line @typescript-eslint/no-unused-expressions update.euroTaxCarData.externalSystemID === record.externalSystemID ? null : logger.info(EURO_TAX_CAR_DATA_OVERRIDE_WARNING_MSG); } if (update.manual) { logger.info(MANUALLY_OVERRIDE_WARNING_MSG); } }); } };