UNPKG

@tomei/rental

Version:
186 lines (166 loc) 5.65 kB
import { ClassError, ObjectBase } from '@tomei/general'; import { AggrementStatusEnum } from '../../enum/aggrement-status.enum'; import { IAgreementAttr } from '../../interfaces/agreement-attr.interface'; import { AgreementRepository } from './agreement.repository'; import { ApplicationConfig } from '@tomei/config'; import { LoginUser } from '@tomei/sso'; import { AgreementSignatureRepository } from '../agreement-signature/agreement-signature.repository'; import * as rentalDb from '../../database'; import { QueryTypes, where } from 'sequelize'; import { IResponseAgreementSignature } from '../../interfaces/response-hirer-signature-attr.interface'; import { AgreementHistoryRepository } from '../agreement-history/agreement-history.repository'; import { IAgreementHistoryAttr } from '../../interfaces/agreement-history-attr.interface'; import { ActionEnum, Activity } from '@tomei/activity-history'; export class Agreement extends ObjectBase { ObjectId: string; ObjectName: string; ObjectType: string = 'Agreement'; TableName = 'rental_Agreement'; Status: AggrementStatusEnum; DateSigned: Date; MediaId: string; private static _Repo = new AgreementRepository(); protected static _AgreementSignatureRepo = new AgreementSignatureRepository(); protected static _AgreementHistoryRepo = new AgreementHistoryRepository(); get AgreementNo(): string { return this.ObjectId; } set AgreementNo(value: string) { this.ObjectId = value; } protected constructor(agreementAttr?: IAgreementAttr) { super(); if (agreementAttr) { this.ObjectId = agreementAttr.AgreementNo; this.Status = agreementAttr.Status; this.DateSigned = agreementAttr.DateSigned; this.MediaId = agreementAttr.MediaId; } } public static async init( agreementNo?: string, dbTransaction?: any, ): Promise<Agreement> { try { if (agreementNo) { const agreement = await this._Repo.findByPk(agreementNo, dbTransaction); return new Agreement(agreement?.get({ plain: true })); } return new Agreement(); } catch (error) { throw error; } } public static async getSignatureList( loginUser: LoginUser, agreementNo: string, dbTransaction: any, ) { try { // Part 1: Privilege Checking // Call loginUser.checkPrivileges() by passing: // SystemCode: "<get_from_app_config>" // PrivilegeCode: "AGREEMENT_SIGNATURE_LIST" const systemCode = ApplicationConfig.getComponentConfigValue('system-code'); const isPrivileged = loginUser.checkPrivileges( systemCode, 'AGREEMENT_SIGNATURE_LIST', ); if (!isPrivileged) { throw new ClassError( 'Agreement', 'AgreementErrMsg01', "You do not have 'AGREEMENT_SIGNATURE_LIST' privilege.", ); } // Part 2: Validation // Make sure agreementNo exists, if not throw new ClassError by passing: if (!agreementNo) { throw new ClassError( 'AgreementNo', 'AgreementNoErrMsg01', 'AgreementNo is missing', ); } // Part 3: Retrieve Listing const query = ` SELECT hs.* FROM rental_AgreementSignature hs WHERE hs.AgreementNo = '${agreementNo}' `; const db = rentalDb.getConnection(); const signatures: IResponseAgreementSignature[] = await db.query(query, { type: QueryTypes.SELECT, transaction: dbTransaction, }); return signatures; } catch (error) { throw error; } } async getAllActivities( loginUser: LoginUser, dbTransaction?: any, ): Promise<IAgreementHistoryAttr[]> { try { const systemCode = ApplicationConfig.getComponentConfigValue('system-code'); const isPrivileged = loginUser.checkPrivileges( systemCode, 'RENTAL_AGREEMENT_VIEW', ); if (!isPrivileged) { throw new ClassError( 'Agreement', 'AgreementErrMsg02', "You do not have 'RENTAL_AGREEMENT_VIEW' privilege.", ); } const activities = await Agreement._AgreementHistoryRepo.findAll({ where: { AgreementNo: this.ObjectId }, order: [['CreatedAt', 'DESC']], transaction: dbTransaction, }); return activities; } catch (error) { throw error; } } async createActivity( activityCompleted: string, MediaId: string, dbTransaction: any, loginUser: LoginUser, ): Promise<IAgreementHistoryAttr> { try { const activity = await Agreement._AgreementHistoryRepo.create( { AgreementNo: this.ObjectId, MediaId: MediaId, ActivityCompleted: activityCompleted, CreatedAt: new Date(), }, { transaction: dbTransaction, }, ); const entityValueAfter = activity.get({ plain: true }); const activityHistory = new Activity(); activityHistory.ActivityId = entityValueAfter.HistoryId; activityHistory.Action = ActionEnum.CREATE; activityHistory.Description = activityCompleted; activityHistory.EntityId = activity.HistoryId.toString(); activityHistory.EntityType = 'RentalAgreementHistory'; activityHistory.EntityValueBefore = JSON.stringify({}); activityHistory.EntityValueAfter = JSON.stringify(entityValueAfter); await activityHistory.create(loginUser.ObjectId, dbTransaction); return activity; } catch (error) { throw error; } } }