From 473464c7650874dfbbfd501c552a4227ca933044 Mon Sep 17 00:00:00 2001 From: kobiowuquadri Date: Wed, 24 Jul 2024 18:39:46 +0100 Subject: [PATCH 1/2] feat: subadmin get all organisations with pagination --- src/controllers/OrgController.ts | 33 +++++++++++++ src/routes/organisation.ts | 3 ++ src/services/organisation.service.ts | 11 +++++ src/test/organisation.spec.ts | 71 ++++++++++++++++++++++++++++ 4 files changed, 118 insertions(+) diff --git a/src/controllers/OrgController.ts b/src/controllers/OrgController.ts index dccecbcf..57f65e95 100644 --- a/src/controllers/OrgController.ts +++ b/src/controllers/OrgController.ts @@ -123,4 +123,37 @@ export class OrgController { }); } } + + async getAllOrgs(req: Request, res: Response) { + try { + const page = parseInt(req.query.page as string) || 1 + const limit = parseInt(req.query.limit as string) || 10 + const { data, total } = await this.orgService.getAllOrgs(page, limit) + + if (data.length === 0) { + return res.status(404).json({ + status: "unsuccessful", + status_code: 404, + message: "No organizations found.", + }) + } + + res.status(200).json({ + status: "success", + status_code: 200, + data, + pagination: { + page, + limit, + total + } + }) + } catch (error) { + res.status(500).json({ + status: "unsuccessful", + status_code: 500, + message: "Failed to retrieve organizations. Please try again later.", + }) + } + } } diff --git a/src/routes/organisation.ts b/src/routes/organisation.ts index de36b464..de6da542 100644 --- a/src/routes/organisation.ts +++ b/src/routes/organisation.ts @@ -15,4 +15,7 @@ orgRouter.delete( orgRouter.get("/organisations/:org_id", authMiddleware, validateOrgId, orgController.getSingleOrg.bind(orgController), ); + +orgRouter.get("/organisations", authMiddleware, orgController.getAllOrgs.bind(orgController)) + export { orgRouter }; diff --git a/src/services/organisation.service.ts b/src/services/organisation.service.ts index c121827e..424d64e9 100644 --- a/src/services/organisation.service.ts +++ b/src/services/organisation.service.ts @@ -56,4 +56,15 @@ export class OrgService implements IOrgService { } return organization; } + + public async getAllOrgs(page: number, limit: number): Promise<{ data: Organization[], total: number }> { + const organizationRepository = AppDataSource.getRepository(Organization) + + const [result, total] = await organizationRepository.findAndCount({ + skip: (page - 1) * limit, + take: limit, + }) + + return { data: result, total } + } } diff --git a/src/test/organisation.spec.ts b/src/test/organisation.spec.ts index 66c7b090..593a5a45 100644 --- a/src/test/organisation.spec.ts +++ b/src/test/organisation.spec.ts @@ -162,3 +162,74 @@ describe("single organization", () => { ); }); }); + +describe("getAllOrgs", () => { + let orgService: OrgService; + let orgController: OrgController; + let mockRepository; + + beforeEach(() => { + orgService = new OrgService(); + orgController = new OrgController(); + + mockRepository = { + findAndCount: jest.fn(), + }; + + AppDataSource.getRepository = jest.fn().mockReturnValue(mockRepository); + }); + + it("should get all organizations with pagination", async () => { + const organizations = [ + { id: "1", name: "Org 1", description: "Org 1 description" }, + { id: "2", name: "Org 2", description: "Org 2 description" }, + ]; + const total = 2; + mockRepository.findAndCount.mockResolvedValue([organizations, total]); + + const req = { + query: { page: "1", limit: "2" }, + } as unknown as Request; + + const res = { + status: jest.fn().mockReturnThis(), + json: jest.fn(), + } as unknown as Response; + + await orgController.getAllOrgs(req, res); + + expect(res.status).toHaveBeenCalledWith(200); + expect(res.json).toHaveBeenCalledWith({ + status: "success", + status_code: 200, + data: organizations, + pagination: { + total, + page: 1, + limit: 2, + }, + }); + }); + + it("should handle errors", async () => { + mockRepository.findAndCount.mockRejectedValue(new Error("Failed to get organizations")); + + const req = { + query: { page: "1", limit: "2" }, + } as unknown as Request; + + const res = { + status: jest.fn().mockReturnThis(), + json: jest.fn(), + } as unknown as Response; + + await orgController.getAllOrgs(req, res); + + expect(res.status).toHaveBeenCalledWith(500); + expect(res.json).toHaveBeenCalledWith({ + status: "unsuccessful", + status_code: 500, + message: "Failed to retrieve organizations. Please try again later.", + }); + }); +}); \ No newline at end of file From 755caf52fc261bd73f54464887f514246818c957 Mon Sep 17 00:00:00 2001 From: kobiowuquadri Date: Wed, 24 Jul 2024 20:04:34 +0100 Subject: [PATCH 2/2] feat: subadmin get all organisations with pagination --- src/routes/organisation.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/routes/organisation.ts b/src/routes/organisation.ts index de6da542..d00104b5 100644 --- a/src/routes/organisation.ts +++ b/src/routes/organisation.ts @@ -1,7 +1,8 @@ import Router from "express"; import { OrgController } from "../controllers/OrgController"; -import { authMiddleware } from "../middleware"; +import { authMiddleware, checkPermissions } from "../middleware"; import { validateOrgId } from "../middleware/organization.validation"; +import { UserRole } from "../enums/userRoles"; const orgRouter = Router(); const orgController = new OrgController(); @@ -16,6 +17,6 @@ orgRouter.get("/organisations/:org_id", authMiddleware, validateOrgId, orgController.getSingleOrg.bind(orgController), ); -orgRouter.get("/organisations", authMiddleware, orgController.getAllOrgs.bind(orgController)) +orgRouter.get("/organisations", authMiddleware, checkPermissions([UserRole.SUPER_ADMIN]), orgController.getAllOrgs.bind(orgController)) export { orgRouter };