@tomei/rental
Version:
Tomei Rental Package
253 lines (220 loc) • 7.92 kB
text/typescript
import { ClassError, ObjectBase } from '@tomei/general';
import { JointHirerRepository } from './joint-hirer.repository';
import { IJointHirerAttr } from '../../interfaces/joint-hirer-attr.interface';
import { ApplicationConfig } from '@tomei/config';
import { LoginUser } from '@tomei/sso';
import { ActionEnum, Activity } from '@tomei/activity-history';
import { Rental } from '../rental/rental';
import { RentalAccountTypeEnum } from '../../enum/account-type.enum';
export class JointHirer extends ObjectBase {
ObjectId: string;
ObjectName: string;
ObjectType: string = 'JointHirer';
TableName: string = 'rental_JointHirer';
RentalId: string;
CustomerId: string;
CustomerType: string;
Status = 'Active'; // Default status
CreatedById: string;
CreatedAt: Date;
UpdatedById: string;
UpdatedAt: Date;
private _Rental: Rental;
get HirerId(): string {
return this.ObjectId;
}
set HirerId(value: string) {
this.ObjectId = value;
}
protected static _Repository = new JointHirerRepository();
protected constructor(jointHirerAttr?: IJointHirerAttr) {
super();
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(): IJointHirerAttr {
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,
};
}
public static async init(hirerId?: string, dbTransaction?: any) {
try {
if (hirerId) {
const jr = await JointHirer._Repository.findByPk(
hirerId,
dbTransaction,
);
if (jr) {
return new JointHirer(jr.get({ plain: true }));
} else {
throw new ClassError(
'JointHirer',
'JointHirerErrMsg01',
'JointHirer not found',
);
}
}
return new JointHirer();
} catch (error) {
throw error;
}
}
public async create(loginUser: LoginUser, dbTransaction?: any) {
//This method will create a new joint hirer record.
try {
//Part 1: Check Privilege
const systemCode =
ApplicationConfig.getComponentConfigValue('system-code');
const isPrivileged = await loginUser.checkPrivileges(
systemCode,
'JointHirer - Create',
);
if (!isPrivileged) {
throw new ClassError(
'JointHirer',
'JointHirerErrMsg00',
"You do not have 'JointHirer - Create' privilege.",
);
}
//Part 2: Insert Joint Hirer Record
//Check if this.RentalId, this.CustomerId, this.CustomerType have value otherwise throw new ClassError with below params
if (!this.RentalId || !this.CustomerId || !this.CustomerType) {
throw new ClassError(
'JointHirer',
'JointHirerErrMsg03',
'RentalId, CustomerId and CustomerType are required.',
);
}
//Set other attributes
this.ObjectId = this.createId();
this.CreatedById = loginUser.ObjectId;
this.CreatedAt = new Date();
this.UpdatedById = loginUser.ObjectId;
this.UpdatedAt = new Date();
const entityValueAfter: IJointHirerAttr = {
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,
};
//Call repo class create method by passing the class attributes and db transaction.
await JointHirer._Repository.create(entityValueAfter, {
transaction: dbTransaction,
});
//Part 4: Record Create JointHirer Activity
const activity = new Activity();
activity.ObjectId = this._createId();
activity.Action = ActionEnum.CREATE;
activity.Description = 'Add Joint Hirer';
activity.EntityId = this.ObjectId;
activity.EntityType = this.ObjectType;
activity.EntityValueBefore = JSON.stringify({});
activity.EntityValueAfter = JSON.stringify(entityValueAfter);
await activity.create(loginUser.ObjectId, dbTransaction);
//update the AccountType
const rental = await this.getRental(dbTransaction);
const totalIds = await rental.getCustomerIds(loginUser, dbTransaction);
if (totalIds.length > 1 && rental.AccountType === 'Single') {
await rental.update(loginUser, dbTransaction, {
AccountType: RentalAccountTypeEnum.JOINT,
});
}
return this;
} catch (error) {
throw error;
}
}
public async remove(loginUser: LoginUser, dbTransaction?: any) {
try {
//Make sure this.HirerId exists
if (!this.HirerId) {
throw new ClassError(
'JointHirer',
'JointHirerErrMsg02',
'HirerId is required to remove Joint Hirer.',
);
}
//Make sure this.Status is not "Inactive"
if (this.Status === 'Inactive') {
return this;
}
//Mark Joint Hirer as Inactive
const entityValueBefore: IJointHirerAttr = this.toJSON();
this.Status = 'Inactive'; // Set status to 'Inactive' instead of deleting the record
this.UpdatedById = loginUser.ObjectId;
this.UpdatedAt = new Date();
const entityValueAfter: IJointHirerAttr = this.toJSON();
// Part 2: Remove Joint Hirer Record
await JointHirer._Repository.update(entityValueAfter, {
where: {
HirerId: this.HirerId,
},
transaction: dbTransaction,
});
// Part 3: Record Remove JointHirer Activity
const activity = new Activity();
activity.ObjectId = this._createId();
activity.Action = 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);
await activity.create(loginUser.ObjectId, dbTransaction);
//update the AccountType
const rental = await this.getRental(dbTransaction);
const totalIds = await rental.getCustomerIds(loginUser, dbTransaction);
if (totalIds.length === 1 && rental.AccountType === 'Joint') {
await rental.update(loginUser, dbTransaction, {
AccountType: RentalAccountTypeEnum.SINGLE,
});
}
return this;
} catch (error) {
throw error;
}
}
private async getRental(dbTransaction?: any) {
if (this._Rental) {
return this._Rental;
}
if (!this.RentalId) {
throw new ClassError(
'JointHirer',
'JointHirerErrMsg04',
'RentalId is empty.',
);
}
this._Rental = await Rental.init(dbTransaction, this.RentalId);
if (!this._Rental) {
throw new ClassError(
'JointHirer',
'JointHirerErrMsg05',
'Rental not found.',
);
}
return this._Rental;
}
}