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 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,

View File

@@ -188,14 +188,13 @@ export default class InventoryAdjustmentService {
inventoryAdjustmentId: number
): Promise<void> {
// 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);
}
}
}