@tomei/rental
Version:
Tomei Rental Package
571 lines • 30.1 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
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 init(requestId, dbTransaction) {
return __awaiter(this, void 0, void 0, function* () {
try {
if (requestId) {
const hirerChangeReq = yield 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;
}
});
}
checkForDuplicate(dbTransaction) {
return __awaiter(this, void 0, void 0, function* () {
try {
if (!this.RentalId) {
throw new general_1.ClassError('HirerChangeRequest', 'HirerChangeRequestErrMsg08', 'RentalId is required to check for duplicate requests.');
}
const isDuplicate = yield 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;
}
});
}
getSignatures(dbTransaction) {
return __awaiter(this, void 0, void 0, function* () {
try {
if (this.Signatures.length > 0) {
return this.Signatures;
}
const hirers = yield RentalHirerChangeRequest._HirerSignatureRepository.findAll({
where: {
RequestId: this.RequestId,
},
transaction: dbTransaction,
});
this.Signatures = yield Promise.all(hirers.map((hirer) => __awaiter(this, void 0, void 0, function* () {
return yield hirer_change_request_signature_1.HirerChangeRequestSignature.init(hirer.SignatureId, dbTransaction);
})));
return this.Signatures;
}
catch (error) {
throw error;
}
});
}
checkIsAllSigned(dbTransaction) {
return __awaiter(this, void 0, void 0, function* () {
try {
const listUpdatedHirer = yield this.getSignatures(dbTransaction);
if (listUpdatedHirer.length > 0) {
return listUpdatedHirer.every((record) => record.SignedAt !== null);
}
else {
return false;
}
}
catch (error) {
throw error;
}
});
}
create(loginUser, dbTransaction) {
return __awaiter(this, void 0, void 0, function* () {
try {
const systemCode = config_1.ApplicationConfig.getComponentConfigValue('system-code');
const isPrivileged = yield 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 = yield rental_1.Rental.init(dbTransaction, this.RentalId);
rental.AccountType = enum_1.RentalAccountTypeEnum.JOINT;
const jointHirers = yield 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.');
}
}
yield 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,
};
yield RentalHirerChangeRequest._Repository.create(entityValueAfter, {
transaction: dbTransaction,
});
yield 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);
yield activity.create(loginUser.ObjectId, dbTransaction);
return this;
}
catch (error) {
throw error;
}
});
}
update(loginUser, dbTransaction, params) {
return __awaiter(this, void 0, void 0, function* () {
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 = yield 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 = yield hirer_change_request_signature_1.HirerChangeRequestSignature.init(foundHirer.SignatureId, dbTransaction);
yield hirer.update(loginUser, dbTransaction);
}
const isSigned = yield 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) {
yield 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,
};
yield 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);
yield activity.create(loginUser.ObjectId, dbTransaction);
return this;
}
catch (error) {
throw error;
}
});
}
generateSignatures(loginUser, dbTransaction) {
return __awaiter(this, void 0, void 0, function* () {
try {
if (!this.RentalId) {
throw new general_1.ClassError('HirerChangeRequest', 'HirerChangeRequestErrMsg09', 'RentalId is required to create signatures.');
}
if (!this.Rental) {
this.Rental = yield rental_1.Rental.init(dbTransaction, this.RentalId);
}
this.Rental.AccountType = enum_1.RentalAccountTypeEnum.JOINT;
const jointHirers = yield this.Rental.getJointHirers(dbTransaction);
const mainSignature = yield 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';
let jointSignatures = [];
if (jointHirers.length > 0) {
const jointSignaturePromises = jointHirers.map((jointHirer) => __awaiter(this, void 0, void 0, function* () {
const signature = yield 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;
}));
jointSignatures = yield Promise.all(jointSignaturePromises);
}
const signaturesToCreate = [
mainSignature.create(loginUser, dbTransaction),
];
if (jointSignatures.length > 0) {
signaturesToCreate.push(...jointSignatures.map((sig) => sig.create(loginUser, dbTransaction)));
}
yield Promise.all(signaturesToCreate);
this.Signatures = [mainSignature, ...jointSignatures];
}
catch (error) {
console.error('Error creating signatures:', error);
throw error;
}
});
}
createAddRequest(newHirer, loginUser, dbTransaction) {
return __awaiter(this, void 0, void 0, function* () {
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;
yield this.create(loginUser, dbTransaction);
const nh = yield 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;
yield nh.create(loginUser, dbTransaction);
return {
hirerChangeRequest: this,
NewHirer: nh,
};
}
catch (error) {
console.error('Error creating add request:', error);
throw error;
}
});
}
createRemoveRequest(loginUser, dbTransaction, targetHirerId) {
return __awaiter(this, void 0, void 0, function* () {
try {
yield joint_hirer_1.JointHirer.init(targetHirerId, dbTransaction);
this.Type = rental_hirer_change_request_type_1.HirerChangeRequestTypeEnum.REMOVE;
yield this.create(loginUser, dbTransaction);
const removeHirer = yield hirer_change_request_remove_hirer_1.HirerChangeRequestRemoveHirer.init();
removeHirer.RequestId = this.RequestId;
removeHirer.TargetHirerId = targetHirerId;
yield removeHirer.create(loginUser, dbTransaction);
return {
hirerChangeRequest: this,
RemoveHirer: removeHirer,
};
}
catch (error) {
console.error('Error creating remove request:', error);
throw error;
}
});
}
static findAll(loginUser, dbTransaction, rentalId) {
return __awaiter(this, void 0, void 0, function* () {
try {
const systemCode = config_1.ApplicationConfig.getComponentConfigValue('system-code');
const isPrivileged = yield 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.');
}
yield rental_1.Rental.init(dbTransaction, rentalId);
const records = yield 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;
}
});
}
getNewHirer(dbTransaction) {
return __awaiter(this, void 0, void 0, function* () {
try {
if (!this.RequestId) {
throw new general_1.ClassError('HirerChangeRequest', 'HirerChangeRequestErrMsg13', 'RequestId is required to get new hirer.');
}
const newHirerRecords = yield 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;
}
});
}
getRemoveHirer(dbTransaction) {
return __awaiter(this, void 0, void 0, function* () {
try {
if (!this.RequestId) {
throw new general_1.ClassError('HirerChangeRequest', 'HirerChangeRequestErrMsg13', 'RequestId is required to get remove hirer.');
}
const removeHirerRecords = yield 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;
}
});
}
sign(loginUser, dbTransaction, signatureId) {
return __awaiter(this, void 0, void 0, function* () {
try {
if (!this.RequestId) {
throw new general_1.ClassError('HirerChangeRequest', 'HirerChangeRequestErrMsg13', 'RequestId is required to sign signature.');
}
const hcrSignature = yield hirer_change_request_signature_1.HirerChangeRequestSignature.init(signatureId, dbTransaction);
yield hcrSignature.markSigned(loginUser, dbTransaction);
return hcrSignature;
}
catch (error) {
throw error;
}
});
}
complete(loginUser, dbTransaction, customerId, customerType) {
return __awaiter(this, void 0, void 0, function* () {
const systemCode = config_1.ApplicationConfig.getComponentConfigValue('system-code');
const isPrivileged = yield 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 = yield 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 = yield joint_hirer_1.JointHirer.init();
jointHirer.RentalId = this.RentalId;
jointHirer.CustomerId = customerId;
jointHirer.CustomerType = customerType;
yield jointHirer.create(loginUser, dbTransaction);
}
else if (this.Type === rental_hirer_change_request_type_1.HirerChangeRequestTypeEnum.REMOVE) {
const removeHirer = yield this.getRemoveHirer(dbTransaction);
const jointHirer = yield joint_hirer_1.JointHirer.init(removeHirer.TargetHirerId, dbTransaction);
yield 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();
yield 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);
yield activity.create(loginUser.ObjectId, dbTransaction);
return this;
});
}
cancel(loginUser, dbTransaction, cancelRemarks) {
return __awaiter(this, void 0, void 0, function* () {
const systemCode = config_1.ApplicationConfig.getComponentConfigValue('system-code');
const isPrivileged = yield 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();
yield 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);
yield 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