Skip to content

Commit

Permalink
feat: subadmin get all organisations with pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
kobiowuquadri committed Jul 24, 2024
1 parent 5b97d0f commit 473464c
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/controllers/OrgController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
})
}
}
}
3 changes: 3 additions & 0 deletions src/routes/organisation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
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.",
});
});
});

0 comments on commit 473464c

Please sign in to comment.