import { ApplicationConfig } from '@tomei/config'; import { OrderShippingAddressRepository } from './order-shipping-address.repository'; import { IOrderShippingAddressAttr } from '../../interfaces/order-shipping-address-attr.interface'; import { LoginUser } from '@tomei/sso'; import { IUpdateOrderShippingAddress } from '../../interfaces/index'; import { ObjectBase } from '@tomei/general'; export class OrderShippingAddress extends ObjectBase { ObjectId: string; ObjectName: string; ObjectType = 'OrderShippingAddress'; TableName: string; Name: Date; Address: string; City: string; State: string; Postcode: string; Country: Date; Phone: string; get OrderNo(): string { return this.ObjectId; } set OrderNo(value: string) { this.ObjectId = value; } private static _OrderShippingAddressRepo = new OrderShippingAddressRepository(); private constructor(orderShippingAddress?: IOrderShippingAddressAttr) { super(); if (orderShippingAddress) { this.OrderNo = orderShippingAddress.OrderNo; this.Name = orderShippingAddress.Name; this.Address = orderShippingAddress.Address; this.City = orderShippingAddress.City; this.State = orderShippingAddress.State; this.Postcode = orderShippingAddress.Postcode; this.Country = orderShippingAddress.Country; this.Phone = orderShippingAddress.Phone; } } public static async init(OrderNo?: string, dbTransaction?: any) { try { if (OrderNo) { const data = await OrderShippingAddress._OrderShippingAddressRepo.findByPk( OrderNo, { transaction: dbTransaction, }, ); if (data) { return new OrderShippingAddress(data.get({ plain: true })); } else { throw Error('orderShippingAddress not found.'); } } return new OrderShippingAddress(); } catch (error) { throw error; } } public async create(loginUser: any, dbTransaction: any): Promise { //This method will create a new order shipping address data. try { //Part 1: Check Privileges const systemCode = ApplicationConfig.getComponentConfigValue('system-code'); const isPrivileged = await loginUser.checkPrivileges( systemCode, 'OrderShippingAddress - Create', ); if (!isPrivileged) { throw new Error( 'You do not have permission to create OrderShippingAddress.', ); } //Part 2: Create Order Shipping Address const entityValueAfter = { OrderNo: this.OrderNo, Name: this.Name, Address: this.Address, City: this.City, State: this.State, Postcode: this.Postcode, Country: this.Country, Phone: this.Phone, }; await OrderShippingAddress._OrderShippingAddressRepo.create( entityValueAfter, { transaction: dbTransaction, }, ); } catch (error) { throw error; } } public async update( loginUser: LoginUser, updatePayload: IUpdateOrderShippingAddress, dbTransaction: any, ) { try { //Part 1: Check Privileges const systemCode = ApplicationConfig.getComponentConfigValue('system-code'); const isPrivileged = await loginUser.checkPrivileges( systemCode, 'OrderShippingAddress - Update', ); if (!isPrivileged) { throw new Error( 'You do not have permission to edit Order Shipping Address.', ); } //Part 2: Update OrderShippingAddress with repo await OrderShippingAddress._OrderShippingAddressRepo.update( { Address: updatePayload.Address, Phone: updatePayload.Phone, }, { 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, 'OrderShippingAddress - Delete', ); if (!isPrivileged) { throw new Error( 'You do not have permission to delete Order Shipping Address.', ); } //Part 2: Delete OrderShippingAddress with repo await OrderShippingAddress._OrderShippingAddressRepo.delete( this.OrderNo, dbTransaction, ); } catch (error) { throw error; } } }