UNPKG

@tomei/rental

Version:
189 lines 8.72 kB
"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.JointHirer = void 0; const general_1 = require("@tomei/general"); const joint_hirer_repository_1 = require("./joint-hirer.repository"); const config_1 = require("@tomei/config"); const activity_history_1 = require("@tomei/activity-history"); const rental_1 = require("../rental/rental"); const account_type_enum_1 = require("../../enum/account-type.enum"); class JointHirer extends general_1.ObjectBase { get HirerId() { return this.ObjectId; } set HirerId(value) { this.ObjectId = value; } constructor(jointHirerAttr) { super(); this.ObjectType = 'JointHirer'; this.TableName = 'rental_JointHirer'; this.Status = 'Active'; if (jointHirerAttr) { this.HirerId = jointHirerAttr.HirerId; this.RentalId = jointHirerAttr.RentalId; this.CustomerId = jointHirerAttr.CustomerId; this.CustomerType = jointHirerAttr.CustomerType; this.Status = jointHirerAttr.Status; this.CreatedById = jointHirerAttr.CreatedById; this.CreatedAt = jointHirerAttr.CreatedAt; this.UpdatedById = jointHirerAttr.UpdatedById; this.UpdatedAt = jointHirerAttr.UpdatedAt; } } toJSON() { return { HirerId: this.HirerId, RentalId: this.RentalId, CustomerId: this.CustomerId, CustomerType: this.CustomerType, Status: this.Status, CreatedById: this.CreatedById, CreatedAt: this.CreatedAt, UpdatedById: this.UpdatedById, UpdatedAt: this.UpdatedAt, }; } static init(hirerId, dbTransaction) { return __awaiter(this, void 0, void 0, function* () { try { if (hirerId) { const jr = yield JointHirer._Repository.findByPk(hirerId, dbTransaction); if (jr) { return new JointHirer(jr.get({ plain: true })); } else { throw new general_1.ClassError('JointHirer', 'JointHirerErrMsg01', 'JointHirer not found'); } } return new JointHirer(); } 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, 'JointHirer - Create'); if (!isPrivileged) { throw new general_1.ClassError('JointHirer', 'JointHirerErrMsg00', "You do not have 'JointHirer - Create' privilege."); } if (!this.RentalId || !this.CustomerId || !this.CustomerType) { throw new general_1.ClassError('JointHirer', 'JointHirerErrMsg03', 'RentalId, CustomerId and CustomerType are required.'); } this.ObjectId = this.createId(); this.CreatedById = loginUser.ObjectId; this.CreatedAt = new Date(); this.UpdatedById = loginUser.ObjectId; this.UpdatedAt = new Date(); const entityValueAfter = { HirerId: this.HirerId, RentalId: this.RentalId, CustomerId: this.CustomerId, CustomerType: this.CustomerType, Status: this.Status, CreatedById: this.CreatedById, CreatedAt: this.CreatedAt, UpdatedById: this.UpdatedById, UpdatedAt: this.UpdatedAt, }; yield JointHirer._Repository.create(entityValueAfter, { transaction: dbTransaction, }); const activity = new activity_history_1.Activity(); activity.ObjectId = this._createId(); activity.Action = activity_history_1.ActionEnum.CREATE; activity.Description = 'Add Joint Hirer'; activity.EntityId = this.ObjectId; activity.EntityType = this.ObjectType; activity.EntityValueBefore = JSON.stringify({}); activity.EntityValueAfter = JSON.stringify(entityValueAfter); yield activity.create(loginUser.ObjectId, dbTransaction); const rental = yield this.getRental(dbTransaction); const totalIds = yield rental.getCustomerIds(loginUser, dbTransaction); if (totalIds.length > 1 && rental.AccountType === 'Single') { yield rental.update(loginUser, dbTransaction, { AccountType: account_type_enum_1.RentalAccountTypeEnum.JOINT, }); } return this; } catch (error) { throw error; } }); } remove(loginUser, dbTransaction) { return __awaiter(this, void 0, void 0, function* () { try { if (!this.HirerId) { throw new general_1.ClassError('JointHirer', 'JointHirerErrMsg02', 'HirerId is required to remove Joint Hirer.'); } if (this.Status === 'Inactive') { return this; } const entityValueBefore = this.toJSON(); this.Status = 'Inactive'; this.UpdatedById = loginUser.ObjectId; this.UpdatedAt = new Date(); const entityValueAfter = this.toJSON(); yield JointHirer._Repository.update(entityValueAfter, { where: { HirerId: this.HirerId, }, transaction: dbTransaction, }); const activity = new activity_history_1.Activity(); activity.ObjectId = this._createId(); activity.Action = activity_history_1.ActionEnum.UPDATE; activity.Description = 'Mark Joint Hirer as Inactive'; activity.EntityId = this.ObjectId; activity.EntityType = this.ObjectType; activity.EntityValueBefore = JSON.stringify(entityValueBefore); activity.EntityValueAfter = JSON.stringify(entityValueAfter); yield activity.create(loginUser.ObjectId, dbTransaction); const rental = yield this.getRental(dbTransaction); const totalIds = yield rental.getCustomerIds(loginUser, dbTransaction); if (totalIds.length === 1 && rental.AccountType === 'Joint') { yield rental.update(loginUser, dbTransaction, { AccountType: account_type_enum_1.RentalAccountTypeEnum.SINGLE, }); } return this; } catch (error) { throw error; } }); } getRental(dbTransaction) { return __awaiter(this, void 0, void 0, function* () { if (this._Rental) { return this._Rental; } if (!this.RentalId) { throw new general_1.ClassError('JointHirer', 'JointHirerErrMsg04', 'RentalId is empty.'); } this._Rental = yield rental_1.Rental.init(dbTransaction, this.RentalId); if (!this._Rental) { throw new general_1.ClassError('JointHirer', 'JointHirerErrMsg05', 'Rental not found.'); } return this._Rental; }); } } exports.JointHirer = JointHirer; JointHirer._Repository = new joint_hirer_repository_1.JointHirerRepository(); //# sourceMappingURL=joint-hirer.js.map