2025-09-09 15:48:12 +00:00
|
|
|
import {
|
|
|
|
|
City,
|
|
|
|
|
Country,
|
|
|
|
|
Name,
|
|
|
|
|
PostalCode,
|
|
|
|
|
Province,
|
|
|
|
|
Street,
|
|
|
|
|
TINNumber,
|
2025-09-09 18:13:54 +00:00
|
|
|
ValidationErrorCollection,
|
|
|
|
|
ValidationErrorDetail,
|
|
|
|
|
extractOrPushError,
|
2025-09-16 11:29:45 +00:00
|
|
|
maybeFromNullableVO,
|
|
|
|
|
} from "@repo/rdx-ddd";
|
|
|
|
|
|
|
|
|
|
import { MapperParamsType } from "@erp/core/api";
|
2025-09-09 18:13:54 +00:00
|
|
|
import { Maybe, Result } from "@repo/rdx-utils";
|
2025-09-11 12:05:50 +00:00
|
|
|
import { CustomerInvoiceProps, InvoiceRecipient } from "../../../domain";
|
|
|
|
|
import { CustomerInvoiceModel } from "../../sequelize";
|
2025-09-09 15:48:12 +00:00
|
|
|
|
2025-09-13 18:45:55 +00:00
|
|
|
export class InvoiceRecipientDomainMapper {
|
2025-09-09 18:13:54 +00:00
|
|
|
public mapToDomain(
|
|
|
|
|
source: CustomerInvoiceModel,
|
|
|
|
|
params?: MapperParamsType
|
|
|
|
|
): Result<Maybe<InvoiceRecipient>, Error> {
|
2025-09-10 18:14:19 +00:00
|
|
|
/**
|
|
|
|
|
* - Factura === proforma -> datos de "current_customer"
|
|
|
|
|
* - Factura !== proforma -> snapshot de los datos (campos customer_*)
|
|
|
|
|
*/
|
|
|
|
|
|
2025-09-09 18:13:54 +00:00
|
|
|
const { errors, attributes } = params as {
|
2025-09-09 15:48:12 +00:00
|
|
|
errors: ValidationErrorDetail[];
|
2025-09-09 18:13:54 +00:00
|
|
|
attributes: Partial<CustomerInvoiceProps>;
|
2025-09-09 15:48:12 +00:00
|
|
|
};
|
|
|
|
|
|
2025-09-09 18:13:54 +00:00
|
|
|
const { isProforma } = attributes;
|
2025-09-09 15:48:12 +00:00
|
|
|
|
2025-09-10 18:14:19 +00:00
|
|
|
if (isProforma && !source.current_customer) {
|
|
|
|
|
errors.push({
|
|
|
|
|
path: "current_customer",
|
|
|
|
|
message: "Current customer not included in query (InvoiceRecipientMapper)",
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-03 19:01:38 +00:00
|
|
|
const _name = isProforma ? source.current_customer.name : source.customer_name!;
|
|
|
|
|
const _tin = isProforma ? source.current_customer.tin : source.customer_tin!;
|
|
|
|
|
const _street = isProforma ? source.current_customer.street : source.customer_street!;
|
|
|
|
|
const _street2 = isProforma ? source.current_customer.street2 : source.customer_street2!;
|
|
|
|
|
const _city = isProforma ? source.current_customer.city : source.customer_city!;
|
2025-09-09 18:13:54 +00:00
|
|
|
const _postal_code = isProforma
|
|
|
|
|
? source.current_customer.postal_code
|
2025-10-03 19:01:38 +00:00
|
|
|
: source.customer_postal_code!;
|
|
|
|
|
const _province = isProforma ? source.current_customer.province : source.customer_province!;
|
|
|
|
|
const _country = isProforma ? source.current_customer.country : source.customer_country!;
|
2025-09-09 15:48:12 +00:00
|
|
|
|
2025-09-09 18:13:54 +00:00
|
|
|
// Customer (snapshot)
|
2025-10-03 19:01:38 +00:00
|
|
|
const customerName = extractOrPushError(Name.create(_name!), "customer_name", errors);
|
2025-09-09 18:13:54 +00:00
|
|
|
|
2025-10-03 19:01:38 +00:00
|
|
|
const customerTin = extractOrPushError(TINNumber.create(_tin!), "customer_tin", errors);
|
2025-09-09 15:48:12 +00:00
|
|
|
|
|
|
|
|
const customerStreet = extractOrPushError(
|
2025-09-09 18:13:54 +00:00
|
|
|
maybeFromNullableVO(_street, (value) => Street.create(value)),
|
2025-09-09 15:48:12 +00:00
|
|
|
"customer_street",
|
|
|
|
|
errors
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const customerStreet2 = extractOrPushError(
|
2025-09-09 18:13:54 +00:00
|
|
|
maybeFromNullableVO(_street2, (value) => Street.create(value)),
|
2025-09-09 15:48:12 +00:00
|
|
|
"customer_street2",
|
|
|
|
|
errors
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const customerCity = extractOrPushError(
|
2025-09-09 18:13:54 +00:00
|
|
|
maybeFromNullableVO(_city, (value) => City.create(value)),
|
2025-09-09 15:48:12 +00:00
|
|
|
"customer_city",
|
|
|
|
|
errors
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const customerProvince = extractOrPushError(
|
2025-09-09 18:13:54 +00:00
|
|
|
maybeFromNullableVO(_province, (value) => Province.create(value)),
|
2025-09-09 15:48:12 +00:00
|
|
|
"customer_province",
|
|
|
|
|
errors
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const customerPostalCode = extractOrPushError(
|
2025-09-09 18:13:54 +00:00
|
|
|
maybeFromNullableVO(_postal_code, (value) => PostalCode.create(value)),
|
2025-09-09 15:48:12 +00:00
|
|
|
"customer_postal_code",
|
|
|
|
|
errors
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const customerCountry = extractOrPushError(
|
2025-09-09 18:13:54 +00:00
|
|
|
maybeFromNullableVO(_country, (value) => Country.create(value)),
|
2025-09-09 15:48:12 +00:00
|
|
|
"customer_country",
|
|
|
|
|
errors
|
|
|
|
|
);
|
|
|
|
|
|
2025-09-09 18:13:54 +00:00
|
|
|
const createResult = InvoiceRecipient.create({
|
2025-09-09 15:48:12 +00:00
|
|
|
name: customerName!,
|
|
|
|
|
tin: customerTin!,
|
|
|
|
|
street: customerStreet!,
|
|
|
|
|
street2: customerStreet2!,
|
|
|
|
|
city: customerCity!,
|
|
|
|
|
postalCode: customerPostalCode!,
|
|
|
|
|
province: customerProvince!,
|
|
|
|
|
country: customerCountry!,
|
|
|
|
|
});
|
|
|
|
|
|
2025-09-09 18:13:54 +00:00
|
|
|
if (createResult.isFailure) {
|
|
|
|
|
return Result.fail(
|
2025-10-18 19:57:52 +00:00
|
|
|
new ValidationErrorCollection("Invoice recipient entity creation failed", [
|
|
|
|
|
{ path: "recipient", message: createResult.error.message },
|
|
|
|
|
])
|
2025-09-09 18:13:54 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Result.ok(Maybe.some(createResult.data));
|
2025-09-09 15:48:12 +00:00
|
|
|
}
|
|
|
|
|
}
|