import { AccountHolderType, AdditionalInsuredType, } from '@sky-distribution/policy-service-interfaces/lib/core/SubmissionForm.type'; import { UpdateCoOwnerApiRequest } from '@sky-distribution/policy-service-interfaces/lib/rpc'; import { buildDatePartial } from './date'; import { buildAddressPartial } from './address'; import { getMaritalStatusCode } from '../utils/baseData.util'; export type AdditionalInsuredsPartialInput = { existingInsureds: AdditionalInsuredType[], updatedInsureds: UpdateCoOwnerApiRequest[] }; const groupExistingAdditionalInsuredsByPesel = ( coOwners: AdditionalInsuredType[], ): Record => coOwners.reduce( ( newObject: Record, coOwner: AdditionalInsuredType, ) => { const validNewObject = { ...newObject, }; if (coOwner.policyAdditionalInsured.pesel_SKY) { validNewObject[coOwner.policyAdditionalInsured.pesel_SKY] = coOwner; } return validNewObject; }, {}, ); const getBasicInsuredElement = (): AdditionalInsuredType => ({ policyAdditionalInsured: { subtype: 'Person', primaryAddress: buildAddressPartial(), }, }); const fillAdditionalInsuredFields = ( newData: UpdateCoOwnerApiRequest, { policyAdditionalInsured: currentData } : AdditionalInsuredType, ): AdditionalInsuredType => { const filledData: AccountHolderType = { ...currentData, firstName: newData.name ?? currentData.firstName, lastName: newData.surname ?? currentData.lastName, displayName: (newData.surname || newData.name) ? `${newData.name} ${newData.surname}` : `${currentData.firstName} ${currentData.lastName}`, dateOfBirth: buildDatePartial(newData.birthDate) ?? currentData.dateOfBirth, maritalStatus: getMaritalStatusCode(newData.maritalStatus) ?? currentData.maritalStatus, pesel_SKY: newData.pesel ?? currentData.pesel_SKY, ufgData_SKY: newData.ufgData ?? currentData.ufgData_SKY, }; if (newData.mainAddress) { filledData.primaryAddress = { ...currentData.primaryAddress, ...buildAddressPartial(newData.mainAddress), }; } if (newData.correspondenceAddress) { const actualAdditionalAddresses = filledData.additionalAddresses ?? []; filledData.additionalAddresses = [ { ...actualAdditionalAddresses[0], ...buildAddressPartial({ ...newData.correspondenceAddress, type: 'correspondence_sky' }), }, ]; } return { policyAdditionalInsured: filledData }; }; export const buildAdditionalInsuredsPartial = ( { existingInsureds, updatedInsureds }: AdditionalInsuredsPartialInput, ) => { const updatedAdditionalInsureds = [] as AdditionalInsuredType[]; const existingInsuredsGrouped = groupExistingAdditionalInsuredsByPesel(existingInsureds); updatedInsureds.forEach( (updatedInsured: UpdateCoOwnerApiRequest) => { let insuredElement: AdditionalInsuredType; if (existingInsuredsGrouped && updatedInsured.pesel) { insuredElement = existingInsuredsGrouped[updatedInsured.pesel] ?? getBasicInsuredElement(); } else { insuredElement = getBasicInsuredElement(); } updatedAdditionalInsureds.push(fillAdditionalInsuredFields(updatedInsured, insuredElement)); }, ); return updatedAdditionalInsureds.length ? updatedAdditionalInsureds : existingInsureds; };