UNPKG

@tomei/rental

Version:
531 lines 26.8 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.RentalHirerChangeRequest = void 0; const general_1 = require("@tomei/general"); const rental_hirer_change_request_repository_1 = require("./rental-hirer-change-request.repository"); const config_1 = require("@tomei/config"); const activity_history_1 = require("@tomei/activity-history"); const rental_hirer_change_request_type_1 = require("../../enum/rental-hirer-change-request-type"); const rental_hirer_change_request_status_1 = require("../../enum/rental-hirer-change-request-status"); const rental_1 = require("../rental/rental"); const enum_1 = require("../../enum"); const sequelize_1 = require("sequelize"); const joint_hirer_repository_1 = require("../../components/joint-hirer/joint-hirer.repository"); const hirer_change_request_signature_repository_1 = require("../hirer-change-request-signature/hirer-change-request-signature.repository"); const hirer_change_request_signature_1 = require("../hirer-change-request-signature/hirer-change-request-signature"); const hirer_change_request_new_hirer_1 = require("../hirer-change-request-new-hirer/hirer-change-request-new-hirer"); const hirer_change_request_remove_hirer_1 = require("../hirer-change-request-remove-hirer/hirer-change-request-remove-hirer"); const joint_hirer_1 = require("../joint-hirer/joint-hirer"); class RentalHirerChangeRequest extends general_1.ObjectBase { get RequestId() { return this.ObjectId; } set RequestId(value) { this.ObjectId = value; } constructor(hirerChangeRequestAttr) { super(); this.ObjectType = 'RentalHirerChangeRequest'; this.TableName = 'rental_HirerChangeRequest'; this.Signatures = []; this.Rental = null; if (hirerChangeRequestAttr) { this.RequestId = hirerChangeRequestAttr.RequestId; this.RentalId = hirerChangeRequestAttr.RentalId; this.Type = hirerChangeRequestAttr.Type; this.Status = hirerChangeRequestAttr.Status; this.RequestedAt = hirerChangeRequestAttr.RequestedAt; this.RequestedById = hirerChangeRequestAttr.RequestedById; this.RequestingHirerId = hirerChangeRequestAttr.RequestingHirerId; this.RequestingHirerType = hirerChangeRequestAttr.RequestingHirerType; this.CancelRemarks = hirerChangeRequestAttr.CancelRemarks; this.UpdatedAt = hirerChangeRequestAttr.UpdatedAt; this.UpdatedById = hirerChangeRequestAttr.UpdatedById; } } toJSON() { return { RequestId: this.RequestId, RentalId: this.RentalId, Type: this.Type, Status: this.Status, RequestedAt: this.RequestedAt, RequestedById: this.RequestedById, RequestingHirerId: this.RequestingHirerId, RequestingHirerType: this.RequestingHirerType, CancelRemarks: this.CancelRemarks, UpdatedAt: this.UpdatedAt, UpdatedById: this.UpdatedById, }; } static async init(requestId, dbTransaction) { try { if (requestId) { const hirerChangeReq = await RentalHirerChangeRequest._Repository.findByPk(requestId, dbTransaction); if (hirerChangeReq) { return new RentalHirerChangeRequest(hirerChangeReq.get({ plain: true })); } else { throw new general_1.ClassError('HirerChangeRequest', 'HirerChangeRequestErrMsg00', 'HirerChangeRequest not found'); } } return new RentalHirerChangeRequest(); } catch (error) { console.error('Error initializing RentalHirerChangeRequest:', error); throw error; } } async checkForDuplicate(dbTransaction) { try { if (!this.RentalId) { throw new general_1.ClassError('HirerChangeRequest', 'HirerChangeRequestErrMsg08', 'RentalId is required to check for duplicate requests.'); } const isDuplicate = await RentalHirerChangeRequest._Repository.findAll({ where: { RentalId: this.RentalId, Type: this.Type, Status: { [sequelize_1.Op.notIn]: [ rental_hirer_change_request_status_1.HirerChangeRequestStatusEnum.COMPLETED, rental_hirer_change_request_status_1.HirerChangeRequestStatusEnum.CANCELLED, ], }, }, transaction: dbTransaction, }); if (isDuplicate.length > 0) { throw new general_1.ClassError('HirerChangeRequest', 'HirerChangeRequestErrMsg04', 'Duplicate record detected.'); } } catch (error) { throw error; } } async getSignatures(dbTransaction) { try { if (this.Signatures.length > 0) { return this.Signatures; } const hirers = await RentalHirerChangeRequest._HirerSignatureRepository.findAll({ where: { RequestId: this.RequestId, }, transaction: dbTransaction, }); this.Signatures = await Promise.all(hirers.map(async (hirer) => { return await hirer_change_request_signature_1.HirerChangeRequestSignature.init(hirer.SignatureId, dbTransaction); })); return this.Signatures; } catch (error) { throw error; } } async checkIsAllSigned(dbTransaction) { try { const listUpdatedHirer = await this.getSignatures(dbTransaction); if (listUpdatedHirer.length > 0) { return listUpdatedHirer.every((record) => record.SignedAt !== null); } else { return false; } } catch (error) { throw error; } } async create(loginUser, dbTransaction) { try { const systemCode = config_1.ApplicationConfig.getComponentConfigValue('system-code'); const isPrivileged = await loginUser.checkPrivileges(systemCode, 'HIRER_CHANGE_REQUEST'); if (!isPrivileged) { throw new general_1.ClassError('HirerChangeRequest', 'HirerChangeRequestErrMsg01', "You do not have 'HIRER_CHANGE_REQUEST' privilege."); } if (!this.RentalId || !this.RequestingHirerId) { throw new general_1.ClassError('HirerChangeRequest', 'HirerChangeRequestErrMsg02', 'RentalId and RequestingHirerId are required.'); } const rental = await rental_1.Rental.init(dbTransaction, this.RentalId); if (rental.AccountType !== enum_1.RentalAccountTypeEnum.JOINT) { throw new general_1.ClassError('HirerChangeRequest', 'HirerChangeRequestErrMsg03', 'Only Rental with Joint Account Type Allowed.'); } const jointHirers = await rental.getJointHirers(dbTransaction); if (this.Type === rental_hirer_change_request_type_1.HirerChangeRequestTypeEnum.ADD) { const maxJointHirer = config_1.ComponentConfig.getComponentConfigValue('@tomei/rental', 'maxJointHirerLength'); if ((jointHirers === null || jointHirers === void 0 ? void 0 : jointHirers.length) >= maxJointHirer) { throw new general_1.ClassError('HirerChangeRequest', 'HirerChangeRequestErrMsg05', 'Joint Hirer exceed max joint hirer length.'); } } if (this.Type == rental_hirer_change_request_type_1.HirerChangeRequestTypeEnum.REMOVE && (jointHirers === null || jointHirers === void 0 ? void 0 : jointHirers.length) <= 1) { throw new general_1.ClassError('HirerChangeRequest', 'HirerChangeRequestErrMsg05', 'Cannot remove the last joint hirer.'); } await this.checkForDuplicate(dbTransaction); this.ObjectId = this.createId(); this.RequestedById = loginUser.ObjectId; this.RequestedAt = new Date(); this.UpdatedById = loginUser.ObjectId; this.UpdatedAt = new Date(); this.Status = rental_hirer_change_request_status_1.HirerChangeRequestStatusEnum.PENDINGSIGNATURES; this.RequestingHirerType = rental.CustomerId === this.RequestingHirerId ? enum_1.HirerChangeRequestHirerRoleEnum.MAIN : enum_1.HirerChangeRequestHirerRoleEnum.JOINT; const entityValueAfter = { RequestId: this.RequestId, RentalId: this.RentalId, Type: this.Type, Status: this.Status, RequestedById: this.RequestedById, RequestedAt: this.RequestedAt, RequestingHirerId: this.RequestingHirerId, RequestingHirerType: this.RequestingHirerType, CancelRemarks: this.CancelRemarks, UpdatedById: this.UpdatedById, UpdatedAt: this.UpdatedAt, }; await RentalHirerChangeRequest._Repository.create(entityValueAfter, { transaction: dbTransaction, }); await this.generateSignatures(loginUser, dbTransaction); const activity = new activity_history_1.Activity(); activity.ObjectId = this._createId(); activity.Action = activity_history_1.ActionEnum.CREATE; activity.Description = this.Type === rental_hirer_change_request_type_1.HirerChangeRequestTypeEnum.ADD ? 'Add New Hirer' : 'Remove Hirer'; activity.EntityId = this.ObjectId; activity.EntityType = this.ObjectType; activity.EntityValueBefore = JSON.stringify({}); activity.EntityValueAfter = JSON.stringify(entityValueAfter); await activity.create(loginUser.ObjectId, dbTransaction); return this; } catch (error) { throw error; } } async update(loginUser, dbTransaction, params) { try { if (!this.RequestId) { throw new general_1.ClassError('HirerChangeRequest', 'HirerChangeRequestErrMsg06', 'RequestId are required.'); } const entityValueBefore = { RequestId: this.RequestId, RentalId: this.RentalId, Type: this.Type, Status: this.Status, RequestedById: this.RequestedById, RequestedAt: this.RequestedAt, RequestingHirerId: this.RequestingHirerId, RequestingHirerType: this.RequestingHirerType, CancelRemarks: this.CancelRemarks, UpdatedById: this.UpdatedById, UpdatedAt: this.UpdatedAt, }; let { jointHirerId, customerId } = params; if (!customerId && !jointHirerId) { throw new general_1.ClassError('HirerChangeRequest', 'HirerChangeRequestErrMsg07', 'At least one of customerId or jointHirerId must be provided.'); } const listHirer = await this.getSignatures(dbTransaction); if ((listHirer === null || listHirer === void 0 ? void 0 : listHirer.length) > 0) { let foundHirer = listHirer === null || listHirer === void 0 ? void 0 : listHirer.find((record) => { record.CustomerId === customerId; }); let hirer = await hirer_change_request_signature_1.HirerChangeRequestSignature.init(foundHirer.SignatureId, dbTransaction); await hirer.update(loginUser, dbTransaction); } const isSigned = await this.checkIsAllSigned(dbTransaction); if (isSigned) { if (this.Type === rental_hirer_change_request_type_1.HirerChangeRequestTypeEnum.ADD) { this.Status = rental_hirer_change_request_status_1.HirerChangeRequestStatusEnum.AWAITINGNEWHIRER; } else if (this.Type === rental_hirer_change_request_type_1.HirerChangeRequestTypeEnum.REMOVE) { await RentalHirerChangeRequest._JointHirerRepository.update({ Status: 'Inactive' }, { where: { HirerId: this.RequestId, }, transaction: dbTransaction, }); this.Status = rental_hirer_change_request_status_1.HirerChangeRequestStatusEnum.COMPLETED; } } const entityValueAfter = { RequestId: this.RequestId, RentalId: this.RentalId, Type: this.Type, Status: this.Status, RequestedById: this.RequestedById, RequestedAt: this.RequestedAt, RequestingHirerId: this.RequestingHirerId, RequestingHirerType: this.RequestingHirerType, CancelRemarks: this.CancelRemarks, UpdatedById: this.UpdatedById, UpdatedAt: this.UpdatedAt, }; await RentalHirerChangeRequest._Repository.update(entityValueAfter, { where: { RequestId: this.RequestId, }, transaction: dbTransaction, }); const activity = new activity_history_1.Activity(); activity.ObjectId = this._createId(); activity.Action = activity_history_1.ActionEnum.UPDATE; activity.Description = 'Update Hirer Change Request'; activity.EntityId = this.ObjectId; activity.EntityType = this.ObjectType; activity.EntityValueBefore = JSON.stringify(entityValueBefore); activity.EntityValueAfter = JSON.stringify(entityValueAfter); await activity.create(loginUser.ObjectId, dbTransaction); return this; } catch (error) { throw error; } } async generateSignatures(loginUser, dbTransaction) { try { if (!this.RentalId) { throw new general_1.ClassError('HirerChangeRequest', 'HirerChangeRequestErrMsg09', 'RentalId is required to create signatures.'); } if (!this.Rental) { this.Rental = await rental_1.Rental.init(dbTransaction, this.RentalId); } const jointHirers = await this.Rental.getJointHirers(dbTransaction); const mainSignature = await hirer_change_request_signature_1.HirerChangeRequestSignature.init(); mainSignature.RequestId = this.RequestId; mainSignature.CustomerId = this.Rental.CustomerId; mainSignature.HirerType = enum_1.HirerChangeRequestHirerRoleEnum.MAIN; mainSignature.Method = 'Upload'; const jointSignaturePromises = jointHirers.map(async (jointHirer) => { const signature = await hirer_change_request_signature_1.HirerChangeRequestSignature.init(); signature.RequestId = this.RequestId; signature.JointHirerId = jointHirer.HirerId; signature.HirerType = enum_1.HirerChangeRequestHirerRoleEnum.JOINT; signature.Method = 'Upload'; return signature; }); const jointSignatures = await Promise.all(jointSignaturePromises); await Promise.all([ mainSignature.create(loginUser, dbTransaction), ...jointSignatures.map((sig) => sig.create(loginUser, dbTransaction)), ]); this.Signatures = [mainSignature, ...jointSignatures]; } catch (error) { console.error('Error creating signatures:', error); throw error; } } async createAddRequest(newHirer, loginUser, dbTransaction) { try { if (!newHirer.FullName || !newHirer.IdNo || !newHirer.IdType || !newHirer.ContactNo || !newHirer.Address || !newHirer.City || !newHirer.State || !newHirer.Postcode || !newHirer.Country) { throw new general_1.ClassError('HirerChangeRequest', 'HirerChangeRequestErrMsg10', 'FullName, IdNo, IdType, ContactNo, and Address are required.'); } this.Type = rental_hirer_change_request_type_1.HirerChangeRequestTypeEnum.ADD; await this.create(loginUser, dbTransaction); const nh = await hirer_change_request_new_hirer_1.HirerChangeRequestNewHirer.init(); nh.RequestId = this.RequestId; nh.FullName = newHirer.FullName; nh.Email = newHirer.Email; nh.IdType = newHirer.IdType; nh.IdNo = newHirer.IdNo; nh.ContactNo = newHirer.ContactNo; nh.Relationship = newHirer.Relationship; nh.Address = newHirer.Address; nh.City = newHirer.City; nh.State = newHirer.State; nh.Postcode = newHirer.Postcode; nh.Country = newHirer.Country; await nh.create(loginUser, dbTransaction); return { hirerChangeRequest: this, NewHirer: nh, }; } catch (error) { console.error('Error creating add request:', error); throw error; } } async createRemoveRequest(loginUser, dbTransaction, targetHirerId) { try { await joint_hirer_1.JointHirer.init(targetHirerId, dbTransaction); this.Type = rental_hirer_change_request_type_1.HirerChangeRequestTypeEnum.REMOVE; await this.create(loginUser, dbTransaction); const removeHirer = await hirer_change_request_remove_hirer_1.HirerChangeRequestRemoveHirer.init(); removeHirer.RequestId = this.RequestId; removeHirer.TargetHirerId = targetHirerId; await removeHirer.create(loginUser, dbTransaction); return { hirerChangeRequest: this, RemoveHirer: removeHirer, }; } catch (error) { console.error('Error creating remove request:', error); throw error; } } static async findAll(loginUser, dbTransaction, rentalId) { try { const systemCode = config_1.ApplicationConfig.getComponentConfigValue('system-code'); const isPrivileged = await loginUser.checkPrivileges(systemCode, 'HIRER_CHANGE_REQUEST'); if (!isPrivileged) { throw new general_1.ClassError('HirerChangeRequest', 'HirerChangeRequestErrMsg01', "You do not have 'HIRER_CHANGE_REQUEST' privilege."); } if (!rentalId) { throw new general_1.ClassError('HirerChangeRequest', 'HirerChangeRequestErrMsg12', 'rentalId is required.'); } await rental_1.Rental.init(dbTransaction, rentalId); const records = await RentalHirerChangeRequest._Repository.findAll({ where: { RentalId: rentalId }, transaction: dbTransaction, }); const result = (records || []).map((rec) => new RentalHirerChangeRequest(rec.get({ plain: true }))); return result; } catch (error) { console.error('Error finding all RentalHirerChangeRequest:', error); throw error; } } async getNewHirer(dbTransaction) { try { if (!this.RequestId) { throw new general_1.ClassError('HirerChangeRequest', 'HirerChangeRequestErrMsg13', 'RequestId is required to get new hirer.'); } const newHirerRecords = await hirer_change_request_new_hirer_1.HirerChangeRequestNewHirer.findAll({ RequestId: this.RequestId, }, dbTransaction); return newHirerRecords[0] || null; } catch (error) { console.error('Error getting new hirer:', error); throw error; } } async getRemoveHirer(dbTransaction) { try { if (!this.RequestId) { throw new general_1.ClassError('HirerChangeRequest', 'HirerChangeRequestErrMsg13', 'RequestId is required to get remove hirer.'); } const removeHirerRecords = await hirer_change_request_remove_hirer_1.HirerChangeRequestRemoveHirer.findAll({ RequestId: this.RequestId, }, dbTransaction); return removeHirerRecords[0] || null; } catch (error) { console.error('Error getting remove hirer:', error); throw error; } } async sign(loginUser, dbTransaction, signatureId) { try { if (!this.RequestId) { throw new general_1.ClassError('HirerChangeRequest', 'HirerChangeRequestErrMsg13', 'RequestId is required to sign signature.'); } const hcrSignature = await hirer_change_request_signature_1.HirerChangeRequestSignature.init(signatureId, dbTransaction); await hcrSignature.markSigned(loginUser, dbTransaction); return hcrSignature; } catch (error) { throw error; } } async complete(loginUser, dbTransaction, customerId, customerType) { const systemCode = config_1.ApplicationConfig.getComponentConfigValue('system-code'); const isPrivileged = await loginUser.checkPrivileges(systemCode, 'HIRER_CHANGE_REQUEST'); if (!isPrivileged) { throw new general_1.ClassError('HirerChangeRequest', 'HirerChangeRequestErrMsg01', "You do not have 'HIRER_CHANGE_REQUEST' privilege."); } if (!this.RequestId) { throw new general_1.ClassError('HirerChangeRequest', 'HirerChangeRequestErrMsg06', 'RequestId is required.'); } const signatures = await this.getSignatures(dbTransaction); if (!signatures.length || !signatures.every((sig) => sig.SignedAt)) { throw new general_1.ClassError('HirerChangeRequest', 'HirerChangeRequestErrMsg14', 'All signatures must be signed before completing the request.'); } if (this.Type === rental_hirer_change_request_type_1.HirerChangeRequestTypeEnum.ADD) { if (!customerId || !customerType) { throw new general_1.ClassError('HirerChangeRequest', 'HirerChangeRequestErrMsg15', 'CustomerId and CustomerType are required for new hirer.'); } const jointHirer = await joint_hirer_1.JointHirer.init(); jointHirer.RentalId = this.RentalId; jointHirer.CustomerId = customerId; jointHirer.CustomerType = customerType; await jointHirer.create(loginUser, dbTransaction); } else if (this.Type === rental_hirer_change_request_type_1.HirerChangeRequestTypeEnum.REMOVE) { const removeHirer = await this.getRemoveHirer(dbTransaction); const jointHirer = await joint_hirer_1.JointHirer.init(removeHirer.TargetHirerId, dbTransaction); await jointHirer.remove(loginUser, dbTransaction); } const now = new Date(); const entityValueBefore = this.toJSON(); this.Status = rental_hirer_change_request_status_1.HirerChangeRequestStatusEnum.COMPLETED; this.UpdatedAt = new Date(); this.UpdatedById = loginUser.ObjectId; const entityValueAfter = this.toJSON(); await RentalHirerChangeRequest._Repository.update(entityValueAfter, { where: { RequestId: this.RequestId }, transaction: dbTransaction, }); const activity = new activity_history_1.Activity(); activity.ObjectId = this._createId(); activity.Action = activity_history_1.ActionEnum.UPDATE; activity.Description = `Mark request as completed.`; activity.EntityId = this.RequestId; activity.EntityType = this.ObjectType; activity.EntityValueBefore = JSON.stringify(entityValueBefore); activity.EntityValueAfter = JSON.stringify(entityValueAfter); await activity.create(loginUser.ObjectId, dbTransaction); return this; } async cancel(loginUser, dbTransaction, cancelRemarks) { const systemCode = config_1.ApplicationConfig.getComponentConfigValue('system-code'); const isPrivileged = await loginUser.checkPrivileges(systemCode, 'HIRER_CHANGE_REQUEST'); if (!isPrivileged) { throw new general_1.ClassError('HirerChangeRequest', 'HirerChangeRequestErrMsg01', "You do not have 'HIRER_CHANGE_REQUEST' privilege."); } if (!this.RequestId) { throw new general_1.ClassError('HirerChangeRequest', 'HirerChangeRequestErrMsg06', 'RequestId is required.'); } const entityValueBefore = this.toJSON(); this.Status = rental_hirer_change_request_status_1.HirerChangeRequestStatusEnum.CANCELLED; this.CancelRemarks = cancelRemarks; this.UpdatedAt = new Date(); this.UpdatedById = loginUser.ObjectId; const entityValueAfter = this.toJSON(); await RentalHirerChangeRequest._Repository.update(entityValueAfter, { where: { RequestId: this.RequestId }, transaction: dbTransaction, }); const activity = new activity_history_1.Activity(); activity.ObjectId = this._createId(); activity.Action = activity_history_1.ActionEnum.UPDATE; activity.Description = `Cancel hirer change request.`; activity.EntityId = this.RequestId; activity.EntityType = this.ObjectType; activity.EntityValueBefore = JSON.stringify(entityValueBefore); activity.EntityValueAfter = JSON.stringify(entityValueAfter); await activity.create(loginUser.ObjectId, dbTransaction); return this; } } exports.RentalHirerChangeRequest = RentalHirerChangeRequest; RentalHirerChangeRequest._Repository = new rental_hirer_change_request_repository_1.RentalHirerChangeRequestRepository(); RentalHirerChangeRequest._HirerSignatureRepository = new hirer_change_request_signature_repository_1.HirerChangeRequestSignatureRepository(); RentalHirerChangeRequest._JointHirerRepository = new joint_hirer_repository_1.JointHirerRepository(); //# sourceMappingURL=rental-hirer-change-request.js.map