diff --git a/server/src/api/controllers/Inventory/InventoryAdjustments.ts b/server/src/api/controllers/Inventory/InventoryAdjustments.ts index 9e9481289..3f7591706 100644 --- a/server/src/api/controllers/Inventory/InventoryAdjustments.ts +++ b/server/src/api/controllers/Inventory/InventoryAdjustments.ts @@ -5,6 +5,8 @@ import { ServiceError } from 'exceptions'; import BaseController from '../BaseController'; import InventoryAdjustmentService from 'services/Inventory/InventoryAdjustmentService'; import DynamicListingService from 'services/DynamicListing/DynamicListService'; +import { Request } from 'express-validator/src/base'; +import { ResponseType } from 'axios'; @Service() export default class InventoryAdjustmentsController extends BaseController { @@ -41,6 +43,13 @@ export default class InventoryAdjustmentsController extends BaseController { this.asyncMiddleware(this.createQuickInventoryAdjustment.bind(this)), this.handleServiceErrors ); + router.get( + '/:id', + [param('id').exists().isNumeric().toInt()], + this.validationResult, + this.asyncMiddleware(this.getInventoryAdjustment.bind(this)), + this.handleServiceErrors + ); router.get( '/', [...this.validateListQuerySchema], @@ -110,11 +119,12 @@ export default class InventoryAdjustmentsController extends BaseController { const quickInventoryAdjustment = this.matchedBodyData(req); try { - const inventoryAdjustment = await this.inventoryAdjustmentService.createQuickAdjustment( - tenantId, - quickInventoryAdjustment, - user - ); + const inventoryAdjustment = + await this.inventoryAdjustmentService.createQuickAdjustment( + tenantId, + quickInventoryAdjustment, + user + ); return res.status(200).send({ id: inventoryAdjustment.id, @@ -181,6 +191,35 @@ export default class InventoryAdjustmentsController extends BaseController { } } + /** + * Retrieve the specific inventory adjustment transaction of the given id. + * @param {Request} req - + * @param {Response} res - + * @param {NextFunction} next - + */ + async getInventoryAdjustment( + req: Request, + res: Response, + next: NextFunction + ) { + const { tenantId } = req; + const { id: adjustmentId } = req.params; + + try { + const inventoryAdjustment = + await this.inventoryAdjustmentService.getInventoryAdjustment( + tenantId, + adjustmentId + ); + + return res.status(200).send({ + data: this.transfromToResponse(inventoryAdjustment), + }); + } catch (error) { + next(error); + } + } + /** * Retrieve the inventory adjustments paginated list. * @param {Request} req @@ -203,13 +242,11 @@ export default class InventoryAdjustmentsController extends BaseController { }; try { - const { - pagination, - inventoryAdjustments, - } = await this.inventoryAdjustmentService.getInventoryAdjustments( - tenantId, - filter - ); + const { pagination, inventoryAdjustments } = + await this.inventoryAdjustmentService.getInventoryAdjustments( + tenantId, + filter + ); return res.status(200).send({ inventoy_adjustments: inventoryAdjustments, diff --git a/server/src/services/Inventory/InventoryAdjustmentService.ts b/server/src/services/Inventory/InventoryAdjustmentService.ts index f352c3fec..f3006bd1c 100644 --- a/server/src/services/Inventory/InventoryAdjustmentService.ts +++ b/server/src/services/Inventory/InventoryAdjustmentService.ts @@ -188,14 +188,13 @@ export default class InventoryAdjustmentService { inventoryAdjustmentId: number ): Promise { // Retrieve the inventory adjustment or throw not found service error. - const oldInventoryAdjustment = await this.getInventoryAdjustmentOrThrowError( - tenantId, - inventoryAdjustmentId - ); - const { - InventoryAdjustmentEntry, - InventoryAdjustment, - } = this.tenancy.models(tenantId); + const oldInventoryAdjustment = + await this.getInventoryAdjustmentOrThrowError( + tenantId, + inventoryAdjustmentId + ); + const { InventoryAdjustmentEntry, InventoryAdjustment } = + this.tenancy.models(tenantId); this.logger.info('[inventory_adjustment] trying to delete adjustment.', { tenantId, @@ -236,10 +235,11 @@ export default class InventoryAdjustmentService { const { InventoryAdjustment } = this.tenancy.models(tenantId); // Retrieve the inventory adjustment or throw not found service error. - const oldInventoryAdjustment = await this.getInventoryAdjustmentOrThrowError( - tenantId, - inventoryAdjustmentId - ); + const oldInventoryAdjustment = + await this.getInventoryAdjustmentOrThrowError( + tenantId, + inventoryAdjustmentId + ); this.logger.info('[inventory_adjustment] trying to publish adjustment.', { tenantId, inventoryAdjustmentId, @@ -268,12 +268,10 @@ export default class InventoryAdjustmentService { /** * Parses inventory adjustments list filter DTO. - * @param filterDTO - + * @param filterDTO - */ private parseListFilterDTO(filterDTO) { - return R.compose( - this.dynamicListService.parseStringifiedFilter, - )(filterDTO); + return R.compose(this.dynamicListService.parseStringifiedFilter)(filterDTO); } /** @@ -297,7 +295,7 @@ export default class InventoryAdjustmentService { const dynamicFilter = await this.dynamicListService.dynamicList( tenantId, InventoryAdjustment, - filter, + filter ); const { results, pagination } = await InventoryAdjustment.query() .onBuild((query) => { @@ -330,10 +328,10 @@ export default class InventoryAdjustmentService { date: inventoryAdjustment.date, transactionType: 'InventoryAdjustment', transactionId: inventoryAdjustment.id, - createdAt: inventoryAdjustment.createdAt + createdAt: inventoryAdjustment.createdAt, }; const inventoryTransactions = []; - + inventoryAdjustment.entries.forEach((entry) => { inventoryTransactions.push({ ...commonTransaction, @@ -347,7 +345,7 @@ export default class InventoryAdjustmentService { tenantId, inventoryTransactions, override - ) + ); } /** @@ -365,4 +363,34 @@ export default class InventoryAdjustmentService { 'InventoryAdjustment' ); } + + /** + * Retrieve specific inventory adjustment transaction details. + * @param {number} tenantId + * @param {number} inventoryAdjustmentId + */ + async getInventoryAdjustment( + tenantId: number, + inventoryAdjustmentId: number + ) { + const { InventoryAdjustment } = this.tenancy.models(tenantId); + + // Retrieve inventory adjustment transation with associated models. + const inventoryAdjustment = await InventoryAdjustment.query() + .findById(inventoryAdjustmentId) + .withGraphFetched('entries.item') + .withGraphFetched('adjustmentAccount'); + + // Throw not found if the given adjustment transaction not exists. + this.throwIfAdjustmentNotFound(inventoryAdjustment) + + return inventoryAdjustment; + } + + + throwIfAdjustmentNotFound(inventoryAdjustment) { + if (!inventoryAdjustment) { + throw new ServiceError(ERRORS.INVENTORY_ADJUSTMENT_NOT_FOUND); + } + } }