import { InputDataTypeEnum, SalesProcessTypeEnum, } from '@sky-distribution/policy-service-interfaces/lib/core/SubmissionFlags.type'; import { EntityEnum } from '@sky-distribution/policy-service-interfaces/lib/rpc/Quote'; import { SubmissionCreateArgs } from '@sky-distribution/policy-service-interfaces/lib/rpc/SubmissionCreate'; import { Service } from 'typedi'; import { SubmissionQuoteArgs } from '@sky-distribution/policy-service-interfaces/lib/rpc'; import { firstLetterUppercase } from '../../utils/String.utils'; import { AccountHolderPartialInput, buildAccountHolderPartial } from './partials/accountHolder'; import { buildAddressPartial } from './partials/address'; import { buildDatePartial } from './partials/date'; import { buildVehiclePartial, VehiclePartialInput, } from './partials/personalVehicle'; import { CoreRequestDefaults } from './utils/DefaultValues'; import { removeKeysWithUndefinedValueFromObject } from '../../utils/Object.util'; @Service() export class CreateSubmissionModel { private readonly inputsForPartials: { accountHolder: AccountHolderPartialInput; vehicle: VehiclePartialInput; }; constructor(private readonly input: SubmissionCreateArgs) { this.inputsForPartials = { accountHolder: this.prepareInputAccountHolder(), vehicle: this.prepareInputVehiclePartial(), }; } private prepareInputAccountHolder(): AccountHolderPartialInput { return { personalData: { birthDate: this.input?.basic?.birthDate, pesel: this.input?.basic?.pesel, name: this.input?.userData?.name, surname: this.input?.userData?.surname, }, }; } private prepareInputVehiclePartial(): VehiclePartialInput { return { update: { ...this.input?.vehicle, plateNumber: this.input?.basic?.plateNumber, vin: this.input?.basic?.vin, }, }; } toJson(): SubmissionQuoteArgs { const zipCode = this.input.basic?.zipCode; const policyAddress = buildAddressPartial({ ...(zipCode && { zipCode }) }); const responseJson = { submissionForm: { baseData: { termType: CoreRequestDefaults.TERM_LENGTH, accountHolder: buildAccountHolderPartial( this.inputsForPartials.accountHolder, ), periodStartDate: buildDatePartial(new Date()), productName: CoreRequestDefaults.PRODUCT_NAME, policyStartDateFromCEP_SKY: buildDatePartial( this.input?.userData?.policyStartDateFromCep, ), productCode: CoreRequestDefaults.PRODUCT_CODE, accountOrgType_SKY: CoreRequestDefaults.ORGANIZATION_TYPE, policyAddress, jobType: firstLetterUppercase(EntityEnum.submission), additionalInsureds_SKY: [], salesProcessType_SKY: SalesProcessTypeEnum[ this.input?.basic ?.salesProcessType as keyof typeof SalesProcessTypeEnum ], inputDataType_SKY: InputDataTypeEnum[ this.input?.basic?.inputDataType as keyof typeof InputDataTypeEnum ], trackingCode: this.input?.basic?.trackingCode, }, lobData: { skyPersonalAuto: { coverables: { personalVehicles: buildVehiclePartial( this.inputsForPartials.vehicle, ), }, }, }, }, agentToken: this.input.agentToken, }; return removeKeysWithUndefinedValueFromObject(responseJson); } }