feat: ability to activate and inactivate the given item.

This commit is contained in:
a.bouhuolia
2020-12-16 16:23:00 +02:00
parent b3eef8718d
commit 42c2e583ed
2 changed files with 94 additions and 0 deletions

View File

@@ -30,6 +30,22 @@ export default class ItemsController extends BaseController {
asyncMiddleware(this.newItem.bind(this)),
this.handlerServiceErrors,
);
router.post(
'/:id/activate', [
...this.validateSpecificItemSchema,
],
this.validationResult,
asyncMiddleware(this.activateItem.bind(this)),
this.handlerServiceErrors
);
router.post(
'/:id/inactivate', [
...this.validateSpecificItemSchema,
],
this.validationResult,
asyncMiddleware(this.inactivateItem.bind(this)),
this.handlerServiceErrors,
)
router.post(
'/:id', [
...this.validateItemSchema,
@@ -226,6 +242,50 @@ export default class ItemsController extends BaseController {
}
}
/**
* Activates the given item.
* @param {Request} req
* @param {Response} res
* @param {NextFunction} next
*/
async activateItem(req: Request, res: Response, next: NextFunction) {
const { tenantId } = req;
const itemId: number = req.params.id;
try {
await this.itemsService.activateItem(tenantId, itemId);
return res.status(200).send({
id: itemId,
message: 'The item has been activated successfully.',
});
} catch (error) {
next(error);
}
}
/**
* Inactivates the given item.
* @param {Request} req
* @param {Response} res
* @param {NextFunction} next
*/
async inactivateItem(req: Request, res: Response, next: NextFunction) {
const { tenantId } = req;
const itemId: number = req.params.id;
try {
await this.itemsService.inactivateItem(tenantId, itemId);
return res.status(200).send({
id: itemId,
message: 'The item has been inactivated successfully.',
});
} catch (error) {
next(error);
}
}
/**
* Deletes the given item from the storage.
* @param {Request} req

View File

@@ -232,6 +232,40 @@ export default class ItemsService implements IItemsService {
await Item.query().findById(itemId).delete();
this.logger.info('[items] deleted successfully.', { tenantId, itemId });
}
/**
* Activates the given item on the storage.
* @param {number} tenantId -
* @param {number} itemId -
* @return {Promise<void>}
*/
public async activateItem(tenantId: number, itemId: number): Promise<void> {
const { Item } = this.tenancy.models(tenantId);
this.logger.info('[items] trying to activate the given item.', { tenantId, itemId });
const item = await this.getItemOrThrowError(tenantId, itemId);
await Item.query().findById(itemId).patch({ active: true });
this.logger.info('[items] activated successfully.', { tenantId, itemId });
}
/**
* Inactivates the given item on the storage.
* @param {number} tenantId
* @param {number} itemId
* @return {Promise<void>}
*/
public async inactivateItem(tenantId: number, itemId: number): Promise<void> {
const { Item } = this.tenancy.models(tenantId);
this.logger.info('[items] trying to inactivate the given item.', { tenantId, itemId });
const item = await this.getItemOrThrowError(tenantId, itemId);
await Item.query().findById(itemId).patch({ active: false });
this.logger.info('[items] activated successfully.', { tenantId, itemId });
}
/**
* Retrieve the item details of the given id with associated details.