feat: retrieve specific inventory adjustment transaction.

This commit is contained in:
a.bouhuolia
2021-08-25 20:56:03 +02:00
parent 5abf007de7
commit 47da64e625
2 changed files with 97 additions and 32 deletions

View File

@@ -5,6 +5,8 @@ import { ServiceError } from 'exceptions';
import BaseController from '../BaseController'; import BaseController from '../BaseController';
import InventoryAdjustmentService from 'services/Inventory/InventoryAdjustmentService'; import InventoryAdjustmentService from 'services/Inventory/InventoryAdjustmentService';
import DynamicListingService from 'services/DynamicListing/DynamicListService'; import DynamicListingService from 'services/DynamicListing/DynamicListService';
import { Request } from 'express-validator/src/base';
import { ResponseType } from 'axios';
@Service() @Service()
export default class InventoryAdjustmentsController extends BaseController { export default class InventoryAdjustmentsController extends BaseController {
@@ -41,6 +43,13 @@ export default class InventoryAdjustmentsController extends BaseController {
this.asyncMiddleware(this.createQuickInventoryAdjustment.bind(this)), this.asyncMiddleware(this.createQuickInventoryAdjustment.bind(this)),
this.handleServiceErrors this.handleServiceErrors
); );
router.get(
'/:id',
[param('id').exists().isNumeric().toInt()],
this.validationResult,
this.asyncMiddleware(this.getInventoryAdjustment.bind(this)),
this.handleServiceErrors
);
router.get( router.get(
'/', '/',
[...this.validateListQuerySchema], [...this.validateListQuerySchema],
@@ -110,11 +119,12 @@ export default class InventoryAdjustmentsController extends BaseController {
const quickInventoryAdjustment = this.matchedBodyData(req); const quickInventoryAdjustment = this.matchedBodyData(req);
try { try {
const inventoryAdjustment = await this.inventoryAdjustmentService.createQuickAdjustment( const inventoryAdjustment =
tenantId, await this.inventoryAdjustmentService.createQuickAdjustment(
quickInventoryAdjustment, tenantId,
user quickInventoryAdjustment,
); user
);
return res.status(200).send({ return res.status(200).send({
id: inventoryAdjustment.id, 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. * Retrieve the inventory adjustments paginated list.
* @param {Request} req * @param {Request} req
@@ -203,13 +242,11 @@ export default class InventoryAdjustmentsController extends BaseController {
}; };
try { try {
const { const { pagination, inventoryAdjustments } =
pagination, await this.inventoryAdjustmentService.getInventoryAdjustments(
inventoryAdjustments, tenantId,
} = await this.inventoryAdjustmentService.getInventoryAdjustments( filter
tenantId, );
filter
);
return res.status(200).send({ return res.status(200).send({
inventoy_adjustments: inventoryAdjustments, inventoy_adjustments: inventoryAdjustments,

View File

@@ -188,14 +188,13 @@ export default class InventoryAdjustmentService {
inventoryAdjustmentId: number inventoryAdjustmentId: number
): Promise<void> { ): Promise<void> {
// Retrieve the inventory adjustment or throw not found service error. // Retrieve the inventory adjustment or throw not found service error.
const oldInventoryAdjustment = await this.getInventoryAdjustmentOrThrowError( const oldInventoryAdjustment =
tenantId, await this.getInventoryAdjustmentOrThrowError(
inventoryAdjustmentId tenantId,
); inventoryAdjustmentId
const { );
InventoryAdjustmentEntry, const { InventoryAdjustmentEntry, InventoryAdjustment } =
InventoryAdjustment, this.tenancy.models(tenantId);
} = this.tenancy.models(tenantId);
this.logger.info('[inventory_adjustment] trying to delete adjustment.', { this.logger.info('[inventory_adjustment] trying to delete adjustment.', {
tenantId, tenantId,
@@ -236,10 +235,11 @@ export default class InventoryAdjustmentService {
const { InventoryAdjustment } = this.tenancy.models(tenantId); const { InventoryAdjustment } = this.tenancy.models(tenantId);
// Retrieve the inventory adjustment or throw not found service error. // Retrieve the inventory adjustment or throw not found service error.
const oldInventoryAdjustment = await this.getInventoryAdjustmentOrThrowError( const oldInventoryAdjustment =
tenantId, await this.getInventoryAdjustmentOrThrowError(
inventoryAdjustmentId tenantId,
); inventoryAdjustmentId
);
this.logger.info('[inventory_adjustment] trying to publish adjustment.', { this.logger.info('[inventory_adjustment] trying to publish adjustment.', {
tenantId, tenantId,
inventoryAdjustmentId, inventoryAdjustmentId,
@@ -268,12 +268,10 @@ export default class InventoryAdjustmentService {
/** /**
* Parses inventory adjustments list filter DTO. * Parses inventory adjustments list filter DTO.
* @param filterDTO - * @param filterDTO -
*/ */
private parseListFilterDTO(filterDTO) { private parseListFilterDTO(filterDTO) {
return R.compose( return R.compose(this.dynamicListService.parseStringifiedFilter)(filterDTO);
this.dynamicListService.parseStringifiedFilter,
)(filterDTO);
} }
/** /**
@@ -297,7 +295,7 @@ export default class InventoryAdjustmentService {
const dynamicFilter = await this.dynamicListService.dynamicList( const dynamicFilter = await this.dynamicListService.dynamicList(
tenantId, tenantId,
InventoryAdjustment, InventoryAdjustment,
filter, filter
); );
const { results, pagination } = await InventoryAdjustment.query() const { results, pagination } = await InventoryAdjustment.query()
.onBuild((query) => { .onBuild((query) => {
@@ -330,10 +328,10 @@ export default class InventoryAdjustmentService {
date: inventoryAdjustment.date, date: inventoryAdjustment.date,
transactionType: 'InventoryAdjustment', transactionType: 'InventoryAdjustment',
transactionId: inventoryAdjustment.id, transactionId: inventoryAdjustment.id,
createdAt: inventoryAdjustment.createdAt createdAt: inventoryAdjustment.createdAt,
}; };
const inventoryTransactions = []; const inventoryTransactions = [];
inventoryAdjustment.entries.forEach((entry) => { inventoryAdjustment.entries.forEach((entry) => {
inventoryTransactions.push({ inventoryTransactions.push({
...commonTransaction, ...commonTransaction,
@@ -347,7 +345,7 @@ export default class InventoryAdjustmentService {
tenantId, tenantId,
inventoryTransactions, inventoryTransactions,
override override
) );
} }
/** /**
@@ -365,4 +363,34 @@ export default class InventoryAdjustmentService {
'InventoryAdjustment' '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);
}
}
} }