UNPKG

@tomei/customer-base

Version:

Tomei Customer Base Package

151 lines 6.96 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 { sequelize; constructor(sequelize) { this.sequelize = sequelize; } async write(payload, dbTransaction) { let transaction = dbTransaction; const createdTransaction = !transaction; const ctx = { CustomerId: payload?.CustomerId, Type: payload?.Type, SourceSystem: 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.'); await models_1.CustomerIndividualModel.upsert({ CustomerId: p.CustomerId, FullName: p.FullName, IdType: p.IdType, IdNo: p.IdNo, Title: p.Title ?? 'N/A', PreferredName: p.PreferredName ?? p.FullName, Birthdate: p.Birthdate ?? undefined, Gender: p.Gender ?? undefined, Ethnicity: p.Ethnicity ?? undefined, Nationality: p.Nationality ?? undefined, PreferredLanguage: p.PreferredLanguage ?? 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 (payload.Address?.length) { for (const address of payload.Address) { const key = { ObjectId: payload.CustomerId, ObjectType: 'Customer', }; const existingAddress = await models_1.ObjectAddressModel.findOne({ where: key, transaction, }); if (existingAddress) { await existingAddress.update({ AddressLine1: address.AddressLine1, AddressLine2: address.AddressLine2, City: address.City, State: address.State, PostalCode: address.PostCode, Country: address.Country, Latitude: 0, Longitude: 0, AddressType: address.AddressType ?? 'Billing Address', IsDefaultYN: address.IsDefaultYN ?? 'Y', Status: 'Active', UpdatedById: payload.UserId ?? payload.CustomerId, UpdatedAt: new Date(), }, { transaction }); } else { await models_1.ObjectAddressModel.create({ 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: address.AddressType ?? 'Billing Address', IsDefaultYN: address.IsDefaultYN ?? 'Y', Status: 'Active', CreatedById: payload.UserId ?? payload.CustomerId, CreatedAt: new Date(), UpdatedById: payload.UserId ?? payload.CustomerId, UpdatedAt: new Date(), }, { transaction }); } } } if (createdTransaction) await transaction.commit(); } catch (err) { if (createdTransaction && transaction) await transaction.rollback(); console.error('[CustomerBaseWriter.write] failed', { ...ctx, error: err?.message, }); throw err; } } } exports.CustomerBaseWriter = CustomerBaseWriter; //# sourceMappingURL=customer-base-writer.js.map