Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: product delete by admin #577

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 118 additions & 0 deletions src/controllers/AdminController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
AdminOrganisationService,
AdminUserService,
AdminLogService,
AdminDeleteProductService,
} from "../services";
import { HttpError } from "../middleware";
import { check, param, validationResult } from "express-validator";
Expand Down Expand Up @@ -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,
};
13 changes: 8 additions & 5 deletions src/routes/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -20,7 +20,6 @@ adminRouter.patch(
adminOrganisationController.updateOrg.bind(adminOrganisationController),
);

// Organisation
adminRouter.delete(
"/admin/organizations/:org_id/delete",
authMiddleware,
Expand All @@ -30,7 +29,6 @@ adminRouter.delete(
),
);

// User
adminRouter.get(
"/admin/users",
Limiter,
Expand All @@ -39,7 +37,6 @@ adminRouter.get(
adminUserController.listUsers.bind(adminUserController),
);

// User
adminRouter.patch(
"/admin/users/:id",
authMiddleware,
Expand All @@ -61,7 +58,6 @@ adminRouter.get(
adminUserController.getUserBySuperadmin.bind(adminUserController),
);

// Logs
adminRouter.get(
"/admin/logs",
Limiter,
Expand All @@ -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 };
28 changes: 25 additions & 3 deletions src/services/admin.services.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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<void> {
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);
}
}
Loading