diff --git a/src/controllers/OrgController.ts b/src/controllers/OrgController.ts index e29c7589..1d7ae0bc 100644 --- a/src/controllers/OrgController.ts +++ b/src/controllers/OrgController.ts @@ -236,4 +236,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 698bd68d..c1f35ae7 100644 --- a/src/routes/organisation.ts +++ b/src/routes/organisation.ts @@ -3,6 +3,7 @@ import { OrgController } from "../controllers/OrgController"; import { authMiddleware, checkPermissions } from "../middleware"; import { UserRole } from "../enums/userRoles"; import { validateOrgId } from "../middleware/organization.validation"; +import { UserRole } from "../enums/userRoles"; const orgRouter = Router(); const orgController = new OrgController(); @@ -28,9 +29,12 @@ orgRouter.get( orgController.getSingleOrg.bind(orgController), ); +orgRouter.get("/organisations", authMiddleware, checkPermissions([UserRole.SUPER_ADMIN]), orgController.getAllOrgs.bind(orgController)) + orgRouter.get( "/users/:id/organizations", authMiddleware, orgController.getOrganizations.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