From 197d173db954203f8500054f65e9f2afa165a5a4 Mon Sep 17 00:00:00 2001 From: Ahmed Bouhuolia Date: Thu, 13 Mar 2025 02:40:09 +0200 Subject: [PATCH] refactor: warehouse transfers --- .../commands/InventoryCosts.service.ts | 16 ++++++------- .../InventoryItemsQuantitySync.service.ts | 3 +++ .../commands/InventoryTransactions.service.ts | 23 ++++++++++++++----- .../src/modules/Items/models/Item.ts | 8 +++---- .../WarehouseTransferApplication.ts | 10 ++++---- .../queries/GetWarehouseTransfers.ts | 2 +- 6 files changed, 37 insertions(+), 25 deletions(-) diff --git a/packages/server-nest/src/modules/InventoryCost/commands/InventoryCosts.service.ts b/packages/server-nest/src/modules/InventoryCost/commands/InventoryCosts.service.ts index 4da3d4f65..6882d752c 100644 --- a/packages/server-nest/src/modules/InventoryCost/commands/InventoryCosts.service.ts +++ b/packages/server-nest/src/modules/InventoryCost/commands/InventoryCosts.service.ts @@ -4,18 +4,18 @@ import { Inject, Injectable } from '@nestjs/common'; import { InventoryTransaction } from '../models/InventoryTransaction'; import { InventoryCostLotTracker } from '../models/InventoryCostLotTracker'; import { Item } from '../../Items/models/Item'; +import { TenantModelProxy } from '@/modules/System/models/TenantBaseModel'; @Injectable() export class InventoryItemCostService { constructor( - @Inject(InventoryTransaction.name) - private readonly inventoryTransactionModel: typeof InventoryTransaction, - @Inject(InventoryCostLotTracker.name) - private readonly inventoryCostLotTrackerModel: typeof InventoryCostLotTracker, + private readonly inventoryCostLotTrackerModel: TenantModelProxy< + typeof InventoryCostLotTracker + >, @Inject(Item.name) - private readonly itemModel: typeof Item, + private readonly itemModel: TenantModelProxy, ) {} /** @@ -64,12 +64,12 @@ export class InventoryItemCostService { builder.groupBy('item_id'); builder.select(['item_id']); }; - const INValuationOper = this.inventoryCostLotTrackerModel + const INValuationOper = this.inventoryCostLotTrackerModel() .query() .onBuild(commonBuilder) .where('direction', 'IN'); - const OUTValuationOper = this.inventoryCostLotTrackerModel + const OUTValuationOper = this.inventoryCostLotTrackerModel() .query() .onBuild(commonBuilder) .where('direction', 'OUT'); @@ -104,7 +104,7 @@ export class InventoryItemCostService { date: Date, ): Promise> => { // Retrieves the inventory items. - const items = await this.itemModel + const items = await this.itemModel() .query() .whereIn('id', itemsId) .where('type', 'inventory'); diff --git a/packages/server-nest/src/modules/InventoryCost/commands/InventoryItemsQuantitySync.service.ts b/packages/server-nest/src/modules/InventoryCost/commands/InventoryItemsQuantitySync.service.ts index 3790b8a98..927cab52a 100644 --- a/packages/server-nest/src/modules/InventoryCost/commands/InventoryItemsQuantitySync.service.ts +++ b/packages/server-nest/src/modules/InventoryCost/commands/InventoryItemsQuantitySync.service.ts @@ -12,6 +12,9 @@ import { TenantModelProxy } from '../../System/models/TenantBaseModel'; */ @Injectable() export class InventoryItemsQuantitySyncService { + /** + * @param {TenantModelProxy} itemModel - Item model. + */ constructor( @Inject(Item.name) private readonly itemModel: TenantModelProxy, diff --git a/packages/server-nest/src/modules/InventoryCost/commands/InventoryTransactions.service.ts b/packages/server-nest/src/modules/InventoryCost/commands/InventoryTransactions.service.ts index dc6d5ce2d..8880c9e60 100644 --- a/packages/server-nest/src/modules/InventoryCost/commands/InventoryTransactions.service.ts +++ b/packages/server-nest/src/modules/InventoryCost/commands/InventoryTransactions.service.ts @@ -15,14 +15,23 @@ import { IItemEntryTransactionType } from '../../TransactionItemEntry/ItemEntry. import { ItemEntry } from '../../TransactionItemEntry/models/ItemEntry'; export class InventoryTransactionsService { + /** + * @param {EventEmitter2} eventEmitter - Event emitter. + * @param {TenantModelProxy} inventoryTransactionModel - Inventory transaction model. + * @param {TenantModelProxy} inventoryCostLotTracker - Inventory cost lot tracker model. + */ constructor( private readonly eventEmitter: EventEmitter2, @Inject(InventoryTransaction.name) - private readonly inventoryTransactionModel: typeof InventoryTransaction, + private readonly inventoryTransactionModel: TenantModelProxy< + typeof InventoryTransaction + >, @Inject(InventoryCostLotTracker.name) - private readonly inventoryCostLotTracker: typeof InventoryCostLotTracker, + private readonly inventoryCostLotTracker: TenantModelProxy< + typeof InventoryCostLotTracker + >, ) {} /** @@ -75,9 +84,11 @@ export class InventoryTransactionsService { trx, ); } - return this.inventoryTransactionModel.query(trx).insertGraph({ - ...inventoryEntry, - }); + return this.inventoryTransactionModel() + .query(trx) + .insertGraph({ + ...inventoryEntry, + }); } /** @@ -137,7 +148,7 @@ export class InventoryTransactionsService { .where({ transactionId, transactionType }); // Deletes the inventory transactions by the given transaction type and id. - await this.inventoryTransactionModel + await this.inventoryTransactionModel() .query(trx) .where({ transactionType, transactionId }) .delete(); diff --git a/packages/server-nest/src/modules/Items/models/Item.ts b/packages/server-nest/src/modules/Items/models/Item.ts index ec6b6bf89..23ed00f2c 100644 --- a/packages/server-nest/src/modules/Items/models/Item.ts +++ b/packages/server-nest/src/modules/Items/models/Item.ts @@ -50,8 +50,8 @@ export class Item extends TenantBaseModel { // const WarehouseTransferEntry = require('../../Warehouses/'); const { InventoryAdjustmentEntry, - } = require('../../InventoryAdjutments/models/InventoryAdjustment'); - const { TaxRate } = require('../../TaxRates/models/TaxRate.model'); + } = require('../../InventoryAdjutments/models/InventoryAdjustmentEntry'); + const { TaxRateModel } = require('../../TaxRates/models/TaxRate.model'); return { /** @@ -171,7 +171,7 @@ export class Item extends TenantBaseModel { */ sellTaxRate: { relation: Model.BelongsToOneRelation, - modelClass: TaxRate, + modelClass: TaxRateModel, join: { from: 'items.sellTaxRateId', to: 'tax_rates.id', @@ -183,7 +183,7 @@ export class Item extends TenantBaseModel { */ purchaseTaxRate: { relation: Model.BelongsToOneRelation, - modelClass: TaxRate, + modelClass: TaxRateModel, join: { from: 'items.purchaseTaxRateId', to: 'tax_rates.id', diff --git a/packages/server-nest/src/modules/WarehousesTransfers/WarehouseTransferApplication.ts b/packages/server-nest/src/modules/WarehousesTransfers/WarehouseTransferApplication.ts index 4eafc2da6..3094c50fc 100644 --- a/packages/server-nest/src/modules/WarehousesTransfers/WarehouseTransferApplication.ts +++ b/packages/server-nest/src/modules/WarehousesTransfers/WarehouseTransferApplication.ts @@ -28,9 +28,8 @@ export class WarehouseTransferApplication { /** * Creates a warehouse transfer transaction. - * @param {number} tenantId - * @param {ICreateWarehouseTransferDTO} createWarehouseTransferDTO - * @returns {} + * @param {ICreateWarehouseTransferDTO} createWarehouseTransferDTO + * @returns {Promise>} */ public createWarehouseTransfer = ( createWarehouseTransferDTO: ICreateWarehouseTransferDTO, @@ -42,7 +41,6 @@ export class WarehouseTransferApplication { /** * Edits warehouse transfer transaction. - * @param {number} tenantId - * @param {number} warehouseTransferId - number * @param {IEditWarehouseTransferDTO} editWarehouseTransferDTO */ @@ -108,8 +106,8 @@ export class WarehouseTransferApplication { /** * Marks the warehouse transfer order as initiated. - * @param {number} warehouseTransferId - * @returns {Promise} + * @param {number} warehouseTransferId + * @returns {Promise>} */ public initiateWarehouseTransfer = ( warehouseTransferId: number, diff --git a/packages/server-nest/src/modules/WarehousesTransfers/queries/GetWarehouseTransfers.ts b/packages/server-nest/src/modules/WarehousesTransfers/queries/GetWarehouseTransfers.ts index 778ff7cfd..c2290830d 100644 --- a/packages/server-nest/src/modules/WarehousesTransfers/queries/GetWarehouseTransfers.ts +++ b/packages/server-nest/src/modules/WarehousesTransfers/queries/GetWarehouseTransfers.ts @@ -1,8 +1,8 @@ import * as R from 'ramda'; +import { Inject, Injectable } from '@nestjs/common'; import { WarehouseTransferTransformer } from './WarehouseTransferTransfomer'; import { IGetWarehousesTransfersFilterDTO } from '../../Warehouses/Warehouse.types'; import { TransformerInjectable } from '../../Transformer/TransformerInjectable.service'; -import { Inject, Injectable } from '@nestjs/common'; import { DynamicListService } from '../../DynamicListing/DynamicList.service'; import { TenantModelProxy } from '../../System/models/TenantBaseModel'; import { WarehouseTransfer } from '../models/WarehouseTransfer';