From fe1d44e4ee75baa6c81d7a1f50e3ac560f4c5ef0 Mon Sep 17 00:00:00 2001 From: Agboola Akorede Date: Sat, 10 Aug 2024 12:35:43 +0100 Subject: [PATCH] feat: product delete by admin --- src/controllers/AdminController.ts | 118 +++++++++++++++++++++++++++++ src/routes/admin.ts | 13 ++-- src/services/admin.services.ts | 28 ++++++- 3 files changed, 151 insertions(+), 8 deletions(-) diff --git a/src/controllers/AdminController.ts b/src/controllers/AdminController.ts index 6baad6ac..6e79364b 100644 --- a/src/controllers/AdminController.ts +++ b/src/controllers/AdminController.ts @@ -3,6 +3,7 @@ import { AdminOrganisationService, AdminUserService, AdminLogService, + AdminDeleteProductService, } from "../services"; import { HttpError } from "../middleware"; import { check, param, validationResult } from "express-validator"; @@ -675,8 +676,125 @@ class AdminLogController { } } +class AdminDeleteProductController { + private adminService: AdminDeleteProductService; + + constructor() { + this.adminService = new AdminDeleteProductService(); + } + + /** + * @swagger + * /api/v1/organisations/{orgId}/products/{productId}: + * delete: + * summary: Admin-Delete an existing product + * tags: [Admin] + * parameters: + * - in: path + * name: orgId + * required: true + * description: The ID of the organization + * schema: + * type: string + * - in: path + * name: productId + * required: true + * description: The ID of the product to delete + * schema: + * type: string + * responses: + * 200: + * description: Product successfully deleted + * content: + * application/json: + * schema: + * type: object + * properties: + * status: + * type: string + * status_code: + * type: integer + * example: 200 + * message: + * type: string + * data: + * type: object + * 400: + * description: Valid organization and product IDs must be provided + * content: + * application/json: + * schema: + * type: object + * properties: + * status: + * type: string + * status_code: + * type: integer + * example: 400 + * message: + * type: string + * 404: + * description: Product not found + * content: + * application/json: + * schema: + * type: object + * properties: + * status: + * type: string + * status_code: + * type: integer + * example: 404 + * message: + * type: string + * 500: + * description: Failed to delete product. Please try again later. + * content: + * application/json: + * schema: + * type: object + * properties: + * status: + * type: string + * status_code: + * type: integer + * example: 500 + * message: + * type: string + */ + + async deleteProduct(req: Request, res: Response) { + const { orgId, productId } = req.params; + + if (!orgId || !productId) { + return res.status(400).json({ + status: "unsuccessful", + status_code: 400, + message: "Valid organization and product IDs must be provided.", + }); + } + + try { + await this.adminService.deleteProduct(orgId, productId); + res.status(200).json({ + status: "success", + status_code: 200, + message: "Product successfully deleted", + data: {}, + }); + } catch (error) { + res.status(500).json({ + status: "unsuccessful", + status_code: 500, + message: "Failed to delete product. Please try again later.", + }); + } + } +} + export default { AdminOrganisationController, AdminUserController, AdminLogController, + AdminDeleteProductController, }; diff --git a/src/routes/admin.ts b/src/routes/admin.ts index ec618c06..25867efb 100644 --- a/src/routes/admin.ts +++ b/src/routes/admin.ts @@ -10,8 +10,8 @@ const adminRouter = Router(); const adminOrganisationController = new admin.AdminOrganisationController(); const adminUserController = new admin.AdminUserController(); const adminLogController = new admin.AdminLogController(); +const adminDeleteProductController = new admin.AdminDeleteProductController(); -// Organisation adminRouter.patch( "/admin/organisation/:id", Limiter, @@ -20,7 +20,6 @@ adminRouter.patch( adminOrganisationController.updateOrg.bind(adminOrganisationController), ); -// Organisation adminRouter.delete( "/admin/organizations/:org_id/delete", authMiddleware, @@ -30,7 +29,6 @@ adminRouter.delete( ), ); -// User adminRouter.get( "/admin/users", Limiter, @@ -39,7 +37,6 @@ adminRouter.get( adminUserController.listUsers.bind(adminUserController), ); -// User adminRouter.patch( "/admin/users/:id", authMiddleware, @@ -61,7 +58,6 @@ adminRouter.get( adminUserController.getUserBySuperadmin.bind(adminUserController), ); -// Logs adminRouter.get( "/admin/logs", Limiter, @@ -70,4 +66,11 @@ adminRouter.get( adminLogController.getLogs.bind(adminLogController), ); +adminRouter.delete( + "/api/v1/organisations/:orgId/products/:productId", + authMiddleware, + checkPermissions([UserRole.SUPER_ADMIN]), + adminDeleteProductController.deleteProduct.bind(adminDeleteProductController), +); + export { adminRouter }; diff --git a/src/services/admin.services.ts b/src/services/admin.services.ts index a4825a1e..ed31b40f 100644 --- a/src/services/admin.services.ts +++ b/src/services/admin.services.ts @@ -1,7 +1,5 @@ -// / src/services/AdminOrganisationService.ts import { NextFunction, Request, Response } from "express"; -// import { getRepository, Repository } from 'typeorm'; -import { User, Organization, Log } from "../models"; +import { User, Organization, Log, Product } from "../models"; import AppDataSource from "../data-source"; import { HttpError } from "../middleware"; import { hashPassword } from "../utils/index"; @@ -203,3 +201,27 @@ export class AdminLogService { } } } + +export class AdminDeleteProductService { + private productRepository = AppDataSource.getRepository(Product); + private organizationRepository = AppDataSource.getRepository(Organization); + + async deleteProduct(orgId: string, productId: string): Promise { + const organization = await this.organizationRepository.findOne({ + where: { id: orgId }, + }); + if (!organization) { + throw new Error("Organization not found"); + } + + const product = await this.productRepository.findOne({ + where: { id: productId, org: { id: orgId } }, + }); + + if (!product) { + throw new Error("Product not found"); + } + + await this.productRepository.delete(productId); + } +}