Files
dinggadingga-backend/src/models/converter/customerAddress.converter.ts
MastersGame ad8e39f63c feat(sync): add company details to customer addresses
Addresses with a company are completed through
GET /customers_addresses/:id (tax number, company number, legal status),
which the customer payload does not embed. Addresses without company get
these fields set to null; a failed call keeps the stored values.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-07 10:48:49 +01:00

50 lines
2.2 KiB
TypeScript

import { Injectable } from '@nestjs/common';
import { Converter } from './converter';
import { CustomerAddressCreateInput } from '../../../prisma/generated/models/CustomerAddress';
import { CustomerAddressDetailsHiboutikDto, CustomerAddressHiboutikDto } from '../hiboutikDTO/customer.hiboutik.dto';
/** Trimmed value, `null` when empty so that a value removed in Hiboutik is also removed in database. */
const optional = (value: string | null | undefined): string | null => value?.trim() || null;
const required = (value: string | null | undefined): string => value?.trim() ?? '';
/**
* Maps an address. Company details (tax/company number, legal status) are:
* - `null` when the address has no company,
* - mapped when `dto` comes from `GET /customers_addresses/:id` ({@link CustomerAddressDetailsHiboutikDto}),
* - `undefined` (stored values kept) when the details were not fetched.
*/
@Injectable()
export class CustomerAddressConverter extends Converter<
CustomerAddressCreateInput,
CustomerAddressHiboutikDto | CustomerAddressDetailsHiboutikDto
> {
fromHiboutik(dto: CustomerAddressHiboutikDto | CustomerAddressDetailsHiboutikDto): CustomerAddressCreateInput {
const company = optional(dto.company);
const hasDetails = 'tax_number' in dto;
const companyDetail = (value: string | null | undefined) => (!company ? null : hasDetails ? optional(value) : undefined);
const details = dto as Partial<CustomerAddressDetailsHiboutikDto>;
return {
address: required(dto.address),
city: required(dto.city),
company,
companyNumber: companyDetail(details.company_number),
country: required(dto.country),
firstName: required(dto.first_name),
hiboutikId: Number(dto.address_id),
isDefault: Number(dto.default) === 1,
lastName: required(dto.last_name),
legalStatus: companyDetail(details.legal_status),
other: optional(dto.other),
phone: optional(dto.phone),
state: optional(dto.state),
taxNumber: companyDetail(details.tax_number),
zipCode: required(dto.zip_code),
customer: { connect: { hiboutikId: Number(dto.customers_id) } },
};
}
toHiboutik(): CustomerAddressHiboutikDto {
throw new Error('Method not implemented.');
}
}