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

fix: delete faq admin #575

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
47 changes: 25 additions & 22 deletions src/controllers/FaqController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ import { NextFunction, Request, Response } from "express";
import { FAQService } from "../services";
import { UserRole } from "../enums/userRoles";
import isSuperAdmin from "../utils/isSuperAdmin";
import { ServerError, BadRequest, HttpError } from "../middleware";
import {
ServerError,
BadRequest,
HttpError,
Unauthorized,
} from "../middleware";
import { validate as isUUID } from "uuid";

const faqService = new FAQService();

Expand Down Expand Up @@ -397,14 +403,14 @@ class FAQController {

/**
* @swagger
* /faqs/{faqId}:
* /faqs/{id}:
* delete:
* summary: Delete an FAQ
* description: Deletes an existing FAQ entry by its ID. This endpoint requires the user to have super admin permissions.
* tags: [FAQ]
* parameters:
* - in: path
* name: faqId
* name: id
* required: true
* schema:
* type: string
Expand All @@ -417,12 +423,9 @@ class FAQController {
* schema:
* type: object
* properties:
* success:
* type: boolean
* example: true
* message:
* type: string
* example: The FAQ has been successfully deleted.
* example: FAQ successfully deleted.
* status_code:
* type: integer
* example: 200
Expand All @@ -438,7 +441,7 @@ class FAQController {
* example: false
* message:
* type: string
* example: Invalid request data
* example: Validation failed! Valid faq ID required
* status_code:
* type: integer
* example: 400
Expand All @@ -454,7 +457,7 @@ class FAQController {
* example: false
* message:
* type: string
* example: Unauthorized access
* example: Access Denied! Not a super admin
* status_code:
* type: integer
* example: 403
Expand All @@ -470,7 +473,7 @@ class FAQController {
* example: false
* message:
* type: string
* example: FAQ entry with ID {faqId} not found.
* example: FAQ not found.
* status_code:
* type: integer
* example: 404
Expand All @@ -486,29 +489,29 @@ class FAQController {
* example: false
* message:
* type: string
* example: Deletion failed
* example: An error occurred while processing your request
* status_code:
* type: integer
* example: 500
*/

public async deleteFaq(req: Request, res: Response, next: NextFunction) {
try {
const { faqId } = req.params;
if (!faqId) {
throw new HttpError(422, "Validation failed: Valid ID required");
const { user } = req;
if (!user.is_superadmin) {
throw new Unauthorized("Access Denied! Not a super admin");
}
const deletionSuccess = await faqService.deleteFaq(faqId);

if (!deletionSuccess) {
throw new HttpError(404, "FAQ not found or could not be deleted");
const { id } = req.params;
if (!id || !isUUID(id)) {
throw new BadRequest("Validation failed! Valid faq ID required");
}
await faqService.deleteFaq(id);
res.status(200).json({
success: true,
message: "The FAQ has been successfully deleted.",
status_code: 200,
message: "FAQ successfully deleted",
});
} catch (err) {
next(err);
} catch (error) {
next(error);
}
}
}
Expand Down
7 changes: 1 addition & 6 deletions src/routes/faq.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@ const faqController = new FAQController();
faqRouter.post("/faqs", authMiddleware, faqController.createFAQ);
faqRouter.patch("/faqs/:id", authMiddleware, faqController.updateFaq);
faqRouter.get("/faqs", faqController.getFaq);
faqRouter.delete(
"/faqs/:faqId",
authMiddleware,
checkPermissions([UserRole.SUPER_ADMIN]),
faqController.deleteFaq,
);
faqRouter.delete("/faqs/:id", authMiddleware, faqController.deleteFaq);

export { faqRouter };
7 changes: 5 additions & 2 deletions src/services/faq.services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,17 @@ class FAQService {
const faq = await this.faqRepository.findOne({ where: { id: faqId } });

if (!faq) {
throw new BadRequest(`Invalid request data`);
throw new ResourceNotFound("FAQ not found");
}

try {
const result = await this.faqRepository.delete(faqId);
return result.affected !== 0;
} catch (error) {
throw new HttpError(500, "Deletion failed");
throw new HttpError(
500,
"An error occurred while processing your request",
);
}
}
}
Expand Down
8 changes: 5 additions & 3 deletions src/test/faq.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { FAQService } from "../services";
import { Repository } from "typeorm";
import { FAQ } from "../models/faq";
import AppDataSource from "../data-source";
import { BadRequest, HttpError } from "../middleware";
import { BadRequest, HttpError, ResourceNotFound } from "../middleware";

jest.mock("../data-source");

Expand Down Expand Up @@ -103,12 +103,14 @@ describe("FaqService", () => {
expect(result).toBe(true);
});

it("should throw BadRequest if FAQ does not exist", async () => {
it("should throw ResourceNotFound if FAQ does not exist", async () => {
const faqId = "faq-123";

faqRepository.findOne.mockResolvedValue(null);

await expect(faqService.deleteFaq(faqId)).rejects.toThrow(BadRequest);
await expect(faqService.deleteFaq(faqId)).rejects.toThrow(
ResourceNotFound,
);
});

it("should throw HttpError if deletion fails", async () => {
Expand Down
Loading