101 lines
2.7 KiB
TypeScript
101 lines
2.7 KiB
TypeScript
|
|
import {
|
||
|
|
City,
|
||
|
|
Country,
|
||
|
|
Name,
|
||
|
|
PostalCode,
|
||
|
|
Province,
|
||
|
|
Street,
|
||
|
|
TINNumber,
|
||
|
|
maybeFromNullableVO,
|
||
|
|
} from "@repo/rdx-ddd";
|
||
|
|
|
||
|
|
import { MapperParamsType, ValidationErrorDetail, extractOrPushError } from "@erp/core/api";
|
||
|
|
import { Maybe, isNullishOrEmpty } from "@repo/rdx-utils";
|
||
|
|
import { InferCreationAttributes } from "sequelize";
|
||
|
|
import { CustomerInvoice, InvoiceRecipient } from "../../domain";
|
||
|
|
import { CustomerInvoiceModel } from "../sequelize";
|
||
|
|
|
||
|
|
export class InvoiceRecipientMapper {
|
||
|
|
public mapToDomain(source: CustomerInvoiceModel, params?: MapperParamsType) {
|
||
|
|
const { errors } = params as {
|
||
|
|
errors: ValidationErrorDetail[];
|
||
|
|
};
|
||
|
|
|
||
|
|
// Customer (snapshot)
|
||
|
|
|
||
|
|
const customerName = extractOrPushError(
|
||
|
|
Name.create(source.customer_name),
|
||
|
|
"customer_name",
|
||
|
|
errors
|
||
|
|
);
|
||
|
|
|
||
|
|
const customerTin = extractOrPushError(
|
||
|
|
TINNumber.create(source.customer_tin),
|
||
|
|
"customer_tin",
|
||
|
|
errors
|
||
|
|
);
|
||
|
|
|
||
|
|
const customerStreet = extractOrPushError(
|
||
|
|
maybeFromNullableVO(source.customer_street, (value) => Street.create(value)),
|
||
|
|
"customer_street",
|
||
|
|
errors
|
||
|
|
);
|
||
|
|
|
||
|
|
const customerStreet2 = extractOrPushError(
|
||
|
|
maybeFromNullableVO(source.customer_street2, (value) => Street.create(value)),
|
||
|
|
"customer_street2",
|
||
|
|
errors
|
||
|
|
);
|
||
|
|
|
||
|
|
const customerCity = extractOrPushError(
|
||
|
|
maybeFromNullableVO(source.customer_city, (value) => City.create(value)),
|
||
|
|
"customer_city",
|
||
|
|
errors
|
||
|
|
);
|
||
|
|
|
||
|
|
const customerProvince = extractOrPushError(
|
||
|
|
maybeFromNullableVO(source.customer_province, (value) => Province.create(value)),
|
||
|
|
"customer_province",
|
||
|
|
errors
|
||
|
|
);
|
||
|
|
|
||
|
|
const customerPostalCode = extractOrPushError(
|
||
|
|
maybeFromNullableVO(source.customer_postal_code, (value) => PostalCode.create(value)),
|
||
|
|
"customer_postal_code",
|
||
|
|
errors
|
||
|
|
);
|
||
|
|
|
||
|
|
const customerCountry = extractOrPushError(
|
||
|
|
maybeFromNullableVO(source.customer_country, (value) => Country.create(value)),
|
||
|
|
"customer_country",
|
||
|
|
errors
|
||
|
|
);
|
||
|
|
|
||
|
|
const recipientOrError = InvoiceRecipient.create({
|
||
|
|
name: customerName!,
|
||
|
|
tin: customerTin!,
|
||
|
|
street: customerStreet!,
|
||
|
|
street2: customerStreet2!,
|
||
|
|
city: customerCity!,
|
||
|
|
postalCode: customerPostalCode!,
|
||
|
|
province: customerProvince!,
|
||
|
|
country: customerCountry!,
|
||
|
|
});
|
||
|
|
|
||
|
|
return isNullishOrEmpty(recipientOrError)
|
||
|
|
? Maybe.none<InvoiceRecipient>()
|
||
|
|
: Maybe.some(recipientOrError.data);
|
||
|
|
}
|
||
|
|
|
||
|
|
public mapToPersistence(
|
||
|
|
source: InvoiceRecipient,
|
||
|
|
params?: MapperParamsType
|
||
|
|
): Partial<InferCreationAttributes<CustomerInvoiceModel, {}>> {
|
||
|
|
1;
|
||
|
|
const { index, sourceParent } = params as {
|
||
|
|
index: number;
|
||
|
|
sourceParent: CustomerInvoice;
|
||
|
|
};
|
||
|
|
}
|
||
|
|
}
|