@tomei/customer-base
Version:
Tomei Customer Base Package
96 lines • 4.73 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildCustomerPayload = void 0;
const sequelize_1 = require("sequelize");
const models_1 = require("../../models");
const customer_type_enum_1 = require("../../enum/customer-type.enum");
async function buildCustomerPayload(customerId) {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p;
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) => {
var _a, _b, _c, _d;
return ({
AddressLine1: a.AddressLine1,
AddressLine2: (_a = a.AddressLine2) !== null && _a !== void 0 ? _a : '',
City: a.City,
State: (_b = a.State) !== null && _b !== void 0 ? _b : '',
PostCode: a.PostalCode,
Country: a.Country,
AddressType: a.AddressType,
IsDefaultYN: a.IsDefaultYN,
Latitude: (_c = a.Latitude) !== null && _c !== void 0 ? _c : undefined,
Longitude: (_d = a.Longitude) !== null && _d !== void 0 ? _d : undefined,
});
});
const sourceSystem = (_a = customerBase.UpdatedBySystemCode) !== null && _a !== void 0 ? _a : '';
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: (_b = customerBase.Email) !== null && _b !== void 0 ? _b : '-',
ContactNo: (_c = customerBase.ContactNo) !== null && _c !== void 0 ? _c : '-',
SourceSystem: sourceSystem,
Address,
FullName: cbIndividual.FullName,
IdType: cbIndividual.IdType,
IdNo: cbIndividual.IdNo,
Title: (_d = cbIndividual.Title) !== null && _d !== void 0 ? _d : 'N/A',
PreferredName: (_e = cbIndividual.PreferredName) !== null && _e !== void 0 ? _e : cbIndividual.FullName,
Birthdate: (_f = cbIndividual.Birthdate) !== null && _f !== void 0 ? _f : undefined,
Gender: (_g = cbIndividual.Gender) !== null && _g !== void 0 ? _g : undefined,
Ethnicity: (_h = cbIndividual.Ethnicity) !== null && _h !== void 0 ? _h : undefined,
Nationality: (_j = cbIndividual.Nationality) !== null && _j !== void 0 ? _j : undefined,
PreferredLanguage: (_k = cbIndividual.PreferredLanguage) !== null && _k !== void 0 ? _k : 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: (_l = cbBusiness.RegistrationNo) !== null && _l !== void 0 ? _l : undefined,
TaxIdentificationNo: (_m = cbBusiness.TaxIdentificationNo) !== null && _m !== void 0 ? _m : undefined,
PIC: cbBusinessPIC
? {
Name: cbBusinessPIC.Name,
ContactNo: cbBusinessPIC.ContactNo,
Email: (_o = cbBusinessPIC.Email) !== null && _o !== void 0 ? _o : undefined,
Position: (_p = cbBusinessPIC.Position) !== null && _p !== void 0 ? _p : undefined,
IsMainYN: cbBusinessPIC.IsMainYN,
}
: undefined,
};
return payload;
}
exports.buildCustomerPayload = buildCustomerPayload;
//# sourceMappingURL=payload-builder.js.map