Skip to content

Commit 1c8d183

Browse files
committed
fix: Add missing pagination to getUsers() and getTeams()
1 parent 0f81add commit 1c8d183

3 files changed

Lines changed: 115 additions & 7 deletions

File tree

README.md

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ await clockodo.getTeam({ id: 10 });
443443

444444
### [getTeams()](https://docs.clockodo.com/#tag/Team/operation/getTeamsV3)
445445

446-
Get list of all teams.
446+
Get list of all teams from all pages.
447447

448448
#### Example:
449449

@@ -453,6 +453,18 @@ await clockodo.getTeams();
453453

454454
---
455455

456+
### [getTeamsPage()](https://docs.clockodo.com/#tag/Team/operation/getTeamsV3)
457+
458+
Get all teams from a specific page.
459+
460+
#### Example:
461+
462+
```js
463+
await clockodo.getTeamsPage({ page: 2 });
464+
```
465+
466+
---
467+
456468
### [getLumpSumService()](https://docs.clockodo.com/#tag/LumpSumService/operation/getLumpSumServiceByIdV4)
457469

458470
Get a lumpsum service by its ID.
@@ -529,7 +541,7 @@ await clockodo.getUser({ id: 1263 });
529541

530542
### [getUsers()](https://docs.clockodo.com/#tag/User/operation/getUsersV3)
531543

532-
Get list of users
544+
Get list of users from all pages.
533545

534546
#### Example:
535547

@@ -539,6 +551,18 @@ await clockodo.getUsers();
539551

540552
---
541553

554+
### [getUsersPage()](https://docs.clockodo.com/#tag/User/operation/getUsersV3)
555+
556+
Get all users from a specific page.
557+
558+
#### Example:
559+
560+
```js
561+
await clockodo.getUsersPage({ page: 2 });
562+
```
563+
564+
---
565+
542566
### getSurchargeModel()
543567

544568
Get a surcharge model by ID.

src/clockodo.test.ts

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,50 @@ describe("Clockodo (instance)", () => {
599599
});
600600
});
601601

602+
describe("getTeam()", () => {
603+
it("correctly builds getTeam() request", async () => {
604+
const nockScope = nock(CLOCKODO_API_BASE_URL)
605+
.get("/v3/teams/1")
606+
.reply(200, {});
607+
608+
await expect(clockodo.getTeam({ id: 1 })).resolves.not.toBeInstanceOf(
609+
Error,
610+
);
611+
612+
nockScope.done();
613+
});
614+
});
615+
616+
describe("getTeamsPage()", () => {
617+
it("correctly builds getTeamsPage() request", async () => {
618+
const nockScope = nock(CLOCKODO_API_BASE_URL)
619+
.get("/v3/teams")
620+
.reply(200, {});
621+
622+
await expect(clockodo.getTeamsPage()).resolves.not.toBeInstanceOf(
623+
Error,
624+
);
625+
626+
nockScope.done();
627+
});
628+
});
629+
630+
describe("getTeams()", () => {
631+
it("requests all team pages", async () => {
632+
const nockScope = setupPaginatedApiMock({
633+
baseUrl: "/v3/teams?",
634+
countPages: 3,
635+
createPageResponse: (page) => ({ data: [page] }),
636+
});
637+
638+
const { data } = await clockodo.getTeams();
639+
640+
expect(data).toMatchObject([1, 2, 3]);
641+
642+
nockScope.done();
643+
});
644+
});
645+
602646
describe("getSingleTargetHourSet", () => {
603647
it("correctly builds getSingleTargetHourSet() request", async () => {
604648
const nockScope = nock(CLOCKODO_API_BASE_URL)
@@ -641,13 +685,31 @@ describe("Clockodo (instance)", () => {
641685
});
642686
});
643687

644-
describe("getUsers()", () => {
645-
it("correctly builds getUsers() request", async () => {
688+
describe("getUsersPage()", () => {
689+
it("correctly builds getUsersPage() request", async () => {
646690
const nockScope = nock(CLOCKODO_API_BASE_URL)
647691
.get("/v3/users")
648692
.reply(200, {});
649693

650-
await expect(clockodo.getUsers()).resolves.not.toBeInstanceOf(Error);
694+
await expect(clockodo.getUsersPage()).resolves.not.toBeInstanceOf(
695+
Error,
696+
);
697+
698+
nockScope.done();
699+
});
700+
});
701+
702+
describe("getUsers()", () => {
703+
it("requests all user pages", async () => {
704+
const nockScope = setupPaginatedApiMock({
705+
baseUrl: "/v3/users?",
706+
countPages: 3,
707+
createPageResponse: (page) => ({ data: [page] }),
708+
});
709+
710+
const { data } = await clockodo.getUsers();
711+
712+
expect(data).toMatchObject([1, 2, 3]);
651713

652714
nockScope.done();
653715
});

src/clockodo.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -340,12 +340,22 @@ export class Clockodo {
340340
return this.api.get("/v3/teams/" + id, remainingParams);
341341
}
342342

343-
async getTeams(
343+
async getTeamsPage(
344344
params?: Params<TeamsParams & ParamsWithPage>,
345345
): Promise<TeamsReturnType> {
346346
return this.api.get("/v3/teams", params);
347347
}
348348

349+
async getTeams(
350+
params?: Params<TeamsParams>,
351+
): Promise<ResponseWithoutPaging<TeamsReturnType>> {
352+
return this.getAllPagesAndMergeArray<TeamsReturnType, TeamsParams>(
353+
"/v3/teams",
354+
params,
355+
"data",
356+
);
357+
}
358+
349359
async getLumpSumService(
350360
params: Params<{ id: LumpsumService["id"] }>,
351361
): Promise<LumpsumServiceReturnType> {
@@ -395,10 +405,22 @@ export class Clockodo {
395405
return this.api.get("/v3/users/" + id, remainingParams);
396406
}
397407

398-
async getUsers(params?: Params<UsersParams>): Promise<UsersReturnType> {
408+
async getUsersPage(
409+
params?: Params<UsersParams & ParamsWithPage>,
410+
): Promise<UsersReturnType> {
399411
return this.api.get("/v3/users", params);
400412
}
401413

414+
async getUsers(
415+
params?: Params<UsersParams>,
416+
): Promise<ResponseWithoutPaging<UsersReturnType>> {
417+
return this.getAllPagesAndMergeArray<UsersReturnType, UsersParams>(
418+
"/v3/users",
419+
params,
420+
"data",
421+
);
422+
}
423+
402424
async getSurchargeModel(
403425
params: Params<{ id: SurchargeModel["id"] }>,
404426
): Promise<SurchargeModelReturnType> {

0 commit comments

Comments
 (0)