import { ApplicationConfig } from '@tomei/config'; import { IOrderPickUpAttr } from '../../interfaces/order-pickup-attr.interface'; import { OrderPickUpRepository } from './order-pickup.repository'; import { LoginUser } from '@tomei/sso'; import { IUpdateOrderPickUp } from '../../interfaces/index'; import { ObjectBase } from '@tomei/general'; export class OrderPickUp extends ObjectBase { ObjectId: string; ObjectName: string; ObjectType = 'OrderPickUp'; TableName: string; Type: string; OutletCode: string; OutletName: string; VerifyUniqueCode: string; VerifySubmittedYN: string; VerifySubmitDateTime: Date; RepName: string; RepNRIC: string; RepContactNo: string; ConsignmentNo: string; PickUpDateTime: Date; Note: string; get OrderNo(): string { return this.ObjectId; } set OrderNo(value: string) { this.ObjectId = value; } private static _OrderPickUpRepo = new OrderPickUpRepository(); private constructor(attr?: IOrderPickUpAttr) { super(); if (attr) { this.OrderNo = attr.OrderNo; this.Type = attr.Type; this.OutletCode = attr.OutletCode; this.OutletName = attr.OutletName; this.VerifyUniqueCode = attr.VerifyUniqueCode; this.VerifySubmittedYN = attr.VerifySubmittedYN; this.VerifySubmitDateTime = attr.VerifySubmitDateTime; this.RepName = attr.RepName; this.RepNRIC = attr.RepNRIC; this.RepContactNo = attr.RepContactNo; this.ConsignmentNo = attr.ConsignmentNo; this.PickUpDateTime = attr.PickUpDateTime; this.Note = attr.Note; } } public static async init(OrderNo?: string, dbTransaction?: any) { try { if (OrderNo) { const data = await OrderPickUp._OrderPickUpRepo.findByPk(OrderNo, { transaction: dbTransaction, }); if (data) { return new OrderPickUp(data.get({ plain: true })); } else { throw Error('OrderPickUp not found.'); } } return new OrderPickUp(); } catch (error) { throw error; } } public async create(loginUser: any, dbTransaction: any): Promise { //This method will create a new order pick up data. try { //Part 1: Check Privileges const systemCode = ApplicationConfig.getComponentConfigValue('system-code'); const isPrivileged = await loginUser.checkPrivileges( systemCode, 'OrderPickUp - Create', ); if (!isPrivileged) { throw new Error('You do not have permission to create Order pick up.'); } //Part 2: Create Order Pick Up const entityValueAfter = { OrderNo: this.OrderNo, Type: this.Type, OutletCode: this.OutletCode, OutletName: this.OutletName, VerifyUniqueCode: this.VerifyUniqueCode, VerifySubmittedYN: this.VerifySubmittedYN, VerifySubmitDateTime: this.VerifySubmitDateTime, RepName: this.RepName, RepNRIC: this.RepNRIC, RepContactNo: this.RepContactNo, ConsignmentNo: this.ConsignmentNo, PickUpDateTime: this.PickUpDateTime, Note: this.Note, }; await OrderPickUp._OrderPickUpRepo.create(entityValueAfter, { transaction: dbTransaction, }); } catch (error) { throw error; } } public async update( loginUser: LoginUser, updatePayload: IUpdateOrderPickUp, dbTransaction: any, ) { try { //Part 1: Check Privileges const systemCode = ApplicationConfig.getComponentConfigValue('system-code'); const isPrivileged = await loginUser.checkPrivileges( systemCode, 'OrderPickUp - Update', ); if (!isPrivileged) { throw new Error('You do not have permission to edit Order Pickup.'); } //Part 2: Update OrderPickUp with repo await OrderPickUp._OrderPickUpRepo.update( { Type: updatePayload.Type, OutletCode: updatePayload.OutletCode, RepContactNo: updatePayload.RepContactNo, ConsignmentNo: updatePayload.ConsignmentNo, }, { where: { OrderNo: this.OrderNo, }, transaction: dbTransaction, }, ); } catch (error) { throw error; } } public async delete(loginUser: LoginUser, dbTransaction: any) { try { //Part 1: Check Privileges const systemCode = ApplicationConfig.getComponentConfigValue('system-code'); const isPrivileged = await loginUser.checkPrivileges( systemCode, 'OrderPickUp - Delete', ); //Part 2: Delete OrderPickUp with repo if (!isPrivileged) { throw new Error('You do not have permission to delete Order Pickup.'); } await OrderPickUp._OrderPickUpRepo.delete(this.OrderNo, dbTransaction); } catch (error) { throw error; } } }