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: Endpoint for Subadmin to get Organisations with Pagination #265

Closed
Closed
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
33 changes: 33 additions & 0 deletions src/controllers/OrgController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
})
}
}
}
4 changes: 4 additions & 0 deletions src/routes/organisation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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 };
11 changes: 11 additions & 0 deletions src/services/organisation.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}
}
71 changes: 71 additions & 0 deletions src/test/organisation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
});
});
});
Loading