diff --git a/server/src/api/controllers/Items.ts b/server/src/api/controllers/Items.ts index b0f83caec..004d16fc6 100644 --- a/server/src/api/controllers/Items.ts +++ b/server/src/api/controllers/Items.ts @@ -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 diff --git a/server/src/services/Items/ItemsService.ts b/server/src/services/Items/ItemsService.ts index 4e8394579..430aa4e2a 100644 --- a/server/src/services/Items/ItemsService.ts +++ b/server/src/services/Items/ItemsService.ts @@ -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} + */ + public async activateItem(tenantId: number, itemId: number): Promise { + 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} + */ + public async inactivateItem(tenantId: number, itemId: number): Promise { + 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.