UNPKG

@tomei/customer-base

Version:

Tomei Customer Base Package

125 lines 6.54 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CustomerBaseWriter = void 0; const models_1 = require("../../models"); const cuid_1 = require("../../helpers/cuid"); const enum_1 = require("../../enum"); class CustomerBaseWriter { constructor(sequelize) { this.sequelize = sequelize; } async write(payload, dbTransaction) { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m; let transaction = dbTransaction; const createdTransaction = !transaction; const ctx = { CustomerId: payload === null || payload === void 0 ? void 0 : payload.CustomerId, Type: payload === null || payload === void 0 ? void 0 : payload.Type, SourceSystem: payload === null || payload === void 0 ? void 0 : payload.SourceSystem, }; try { if (!transaction) transaction = await this.sequelize.transaction(); if (!payload.CustomerId) throw new Error('CustomerId is required.'); if (!payload.Type) throw new Error('Type is required (Individual or Business).'); await models_1.CustomerBaseModel.upsert({ CustomerId: payload.CustomerId, Type: payload.Type, Email: payload.Email, ContactNo: payload.ContactNo, Status: enum_1.CustomerStatusEnum.Active, CreatedByUserId: payload.UserId, CreatedByCustomerId: !payload.UserId ? payload.CustomerId : null, CreatedAt: new Date(), UpdatedByUserId: payload.UserId, UpdatedByCustomerId: !payload.UserId ? payload.CustomerId : null, UpdatedAt: new Date(), UpdatedBySystemCode: payload.SourceSystem, }, { transaction }); if (payload.Type === enum_1.CustomerTypeEnum.Individual) { const p = payload; if (!p.FullName) throw new Error('FullName is required.'); if (!p.IdType) throw new Error('IdType is required.'); if (!p.IdNo) throw new Error('IdNo is required.'); await models_1.CustomerIndividualModel.upsert({ CustomerId: p.CustomerId, FullName: p.FullName, IdType: p.IdType, IdNo: p.IdNo, Title: (_a = p.Title) !== null && _a !== void 0 ? _a : 'N/A', PreferredName: (_b = p.PreferredName) !== null && _b !== void 0 ? _b : p.FullName, Birthdate: (_c = p.Birthdate) !== null && _c !== void 0 ? _c : undefined, Gender: (_d = p.Gender) !== null && _d !== void 0 ? _d : undefined, Ethnicity: (_e = p.Ethnicity) !== null && _e !== void 0 ? _e : undefined, Nationality: (_f = p.Nationality) !== null && _f !== void 0 ? _f : undefined, PreferredLanguage: (_g = p.PreferredLanguage) !== null && _g !== void 0 ? _g : undefined, }, { transaction }); } else if (payload.Type === enum_1.CustomerTypeEnum.Business) { const p = payload; if (!p.CompanyName) throw new Error('Company Name is required.'); await models_1.CustomerBusinessModel.upsert({ CustomerId: p.CustomerId, CompanyName: p.CompanyName, RegistrationNo: p.RegistrationNo, TaxIdentificationNo: p.TaxIdentificationNo, }, { transaction }); if (payload.PIC) { if (!payload.PIC.Name) throw new Error('PIC Name is required.'); if (!payload.PIC.ContactNo) throw new Error('PIC ContactNo is required.'); await models_1.BusinessContactModel.upsert({ ContactId: (0, cuid_1.default)(), CustomerId: payload.CustomerId, Name: payload.PIC.Name, Email: payload.PIC.Email, ContactNo: payload.PIC.ContactNo, Position: payload.PIC.Position, IsMainYN: payload.PIC.IsMainYN, }, { transaction }); } } if ((_h = payload.Address) === null || _h === void 0 ? void 0 : _h.length) { for (const address of payload.Address) { await models_1.ObjectAddressModel.upsert({ AddressId: (0, cuid_1.default)(), ObjectId: payload.CustomerId, ObjectType: 'Customer', AddressLine1: address.AddressLine1, AddressLine2: address.AddressLine2, City: address.City, State: address.State, PostalCode: address.PostCode, Country: address.Country, Latitude: 0, Longitude: 0, AddressType: (_j = address.AddressType) !== null && _j !== void 0 ? _j : 'Billing Address', IsDefaultYN: (_k = address.IsDefaultYN) !== null && _k !== void 0 ? _k : 'Y', Status: 'Active', CreatedById: (_l = payload.UserId) !== null && _l !== void 0 ? _l : payload.CustomerId, CreatedAt: new Date(), UpdatedById: (_m = payload.UserId) !== null && _m !== void 0 ? _m : payload.CustomerId, UpdatedAt: new Date(), }, { transaction }); } } if (createdTransaction) await transaction.commit(); } catch (err) { if (createdTransaction && transaction) await transaction.rollback(); console.error('[CustomerBaseWriter.write] failed', Object.assign(Object.assign({}, ctx), { error: err === null || err === void 0 ? void 0 : err.message })); throw err; } } } exports.CustomerBaseWriter = CustomerBaseWriter; //# sourceMappingURL=customer-base-writer.js.map