@tomei/customer-base
Version:
Tomei Customer Base Package
91 lines • 3.77 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildCustomerPayload = buildCustomerPayload;
const sequelize_1 = require("sequelize");
const models_1 = require("../../models");
const customer_type_enum_1 = require("../../enum/customer-type.enum");
async function buildCustomerPayload(customerId) {
const customerBase = await models_1.CustomerBaseModel.findByPk(customerId);
if (!customerBase) {
throw new Error(`CustomerBase not found for CustomerId=${customerId}`);
}
const addresses = await models_1.ObjectAddressModel.findAll({
where: {
ObjectId: customerId,
ObjectType: 'Customer',
Status: { [sequelize_1.Op.ne]: 'Deleted' },
},
order: [
['IsDefaultYN', 'DESC'],
['UpdatedAt', 'DESC'],
],
});
const Address = addresses.map((a) => ({
AddressLine1: a.AddressLine1,
AddressLine2: a.AddressLine2 ?? '',
City: a.City,
State: a.State ?? '',
PostCode: a.PostalCode,
Country: a.Country,
AddressType: a.AddressType,
IsDefaultYN: a.IsDefaultYN,
Latitude: a.Latitude ?? undefined,
Longitude: a.Longitude ?? undefined,
}));
const sourceSystem = customerBase.UpdatedBySystemCode ?? '';
if (customerBase.Type === customer_type_enum_1.CustomerTypeEnum.Individual) {
const cbIndividual = await models_1.CustomerIndividualModel.findByPk(customerId);
if (!cbIndividual) {
throw new Error(`CustomerIndividual not found for CustomerId=${customerId}`);
}
const payload = {
CustomerId: customerBase.CustomerId,
Type: customer_type_enum_1.CustomerTypeEnum.Individual,
Email: customerBase.Email ?? '-',
ContactNo: customerBase.ContactNo ?? '-',
SourceSystem: sourceSystem,
Address,
FullName: cbIndividual.FullName,
IdType: cbIndividual.IdType,
IdNo: cbIndividual.IdNo,
Title: cbIndividual.Title ?? 'N/A',
PreferredName: cbIndividual.PreferredName ?? cbIndividual.FullName,
Birthdate: cbIndividual.Birthdate ?? undefined,
Gender: cbIndividual.Gender ?? undefined,
Ethnicity: cbIndividual.Ethnicity ?? undefined,
Nationality: cbIndividual.Nationality ?? undefined,
PreferredLanguage: cbIndividual.PreferredLanguage ?? undefined,
};
return payload;
}
const cbBusiness = await models_1.CustomerBusinessModel.findByPk(customerId);
if (!cbBusiness) {
throw new Error(`CustomerBusiness not found for CustomerId=${customerId}`);
}
const cbBusinessPIC = await models_1.BusinessContactModel.findOne({
where: { CustomerId: customerId, IsMainYN: 'Y' },
order: [['UpdatedAt', 'DESC']],
});
const payload = {
CustomerId: customerBase.CustomerId,
Type: customer_type_enum_1.CustomerTypeEnum.Business,
Email: customerBase.Email,
ContactNo: customerBase.ContactNo,
SourceSystem: sourceSystem,
Address,
CompanyName: cbBusiness.CompanyName,
RegistrationNo: cbBusiness.RegistrationNo ?? undefined,
TaxIdentificationNo: cbBusiness.TaxIdentificationNo ?? undefined,
PIC: cbBusinessPIC
? {
Name: cbBusinessPIC.Name,
ContactNo: cbBusinessPIC.ContactNo,
Email: cbBusinessPIC.Email ?? undefined,
Position: cbBusinessPIC.Position ?? undefined,
IsMainYN: cbBusinessPIC.IsMainYN,
}
: undefined,
};
return payload;
}
//# sourceMappingURL=payload-builder.js.map