Skip to content

Commit

Permalink
fix: Paginate zones (#2)
Browse files Browse the repository at this point in the history
* fix: Paginate account zones

* fix: Typo
  • Loading branch information
pettermachado authored Sep 26, 2024
1 parent 50d3722 commit 74a30a5
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 11 deletions.
16 changes: 8 additions & 8 deletions api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,25 +345,25 @@ router.get("/events/:eventId/actions", async (req, res) => {
res.json(r.get("actions"));
});

const soundtracApi = new Api();
const soundtrackApi = new Api();

router.get("/zones/:zoneId", async (req, res) => {
const zone = await soundtracApi.getZone(req.params.zoneId);
const zone = await soundtrackApi.getZone(req.params.zoneId);
res.json(zone);
});

router.get("/zones", async (req, res) => {
const zones = await soundtracApi.getZones();
const zones = await soundtrackApi.getZones();
res.json(zones);
});

router.get("/accounts/:accountId/library", async (req, res) => {
const library = await soundtracApi.getLibrary(req.params.accountId);
const library = await soundtrackApi.getLibrary(req.params.accountId);
res.json(library);
});

router.get("/accounts/:accountId/zones", async (req, res) => {
const zones = await soundtracApi.getAccountZones(req.params.accountId);
const zones = await soundtrackApi.getAccountZones(req.params.accountId);
res.json(zones);
});

Expand All @@ -383,17 +383,17 @@ router.get("/accounts/:accountId/events", async (req, res) => {
});

router.get("/accounts/:accountId", async (req, res) => {
const account = await soundtracApi.getAccount(req.params.accountId);
const account = await soundtrackApi.getAccount(req.params.accountId);
res.json(account);
});

router.get("/accounts", async (req, res) => {
const accounts = await soundtracApi.getAccounts();
const accounts = await soundtrackApi.getAccounts();
res.json(accounts);
});

router.get("/assignable/:id", async (req, res) => {
const assignable = await soundtracApi.getAssignable(req.params.id);
const assignable = await soundtrackApi.getAssignable(req.params.id);
if (assignable) {
res.json(assignable);
} else {
Expand Down
33 changes: 30 additions & 3 deletions lib/soundtrack-api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,27 @@ export class Api {
return res.data.account;
}
async getAccountZones(accountId: string): Promise<Zone[]> {
return await this.getAccountZonesPage(accountId, null, []);
}
private async getAccountZonesPage(
accountId: string,
cursor: string | null,
acc: Zone[],
): Promise<Zone[]> {
const res = await runQuery<AccountZonesQuery>(accountZonesQuery, {
id: accountId,
cursor,
});
const zoneFn = toZoneFn(accountId);
return res.data.account.soundZones.edges.map(({ node }) => zoneFn(node));
const zones: Zone[] = acc.concat(
res.data.account.soundZones.edges.map(({ node }) => zoneFn(node)),
);
const pageInfo = res.data.account.soundZones.pageInfo;
if (pageInfo.hasNextPage && pageInfo.endCursor) {
return this.getAccountZonesPage(accountId, pageInfo.endCursor, zones);
} else {
return zones;
}
}
async getZone(zoneId: string): Promise<Zone> {
const res = await runQuery<ZoneQuery>(zoneQuery, { id: zoneId });
Expand Down Expand Up @@ -132,6 +148,7 @@ type AccountZonesQuery = {
account: {
id: string;
soundZones: {
pageInfo: PageInfo;
edges: {
node: AccountZone;
}[];
Expand All @@ -140,10 +157,14 @@ type AccountZonesQuery = {
};

const accountZonesQuery = `
query Scheduler_Zones($id: ID!) {
query Scheduler_Zones($id: ID!, $cursor: String) {
account(id: $id) {
id
soundZones(first: 500) {
soundZones(first: 100, after: $cursor) {
pageInfo {
hasNextPage
endCursor
}
edges {
node {
id
Expand All @@ -158,6 +179,7 @@ query Scheduler_Zones($id: ID!) {
}
}
`;

type ZoneQuery = {
soundZone: Zone;
};
Expand Down Expand Up @@ -294,3 +316,8 @@ query Scheduler_Assignable($assignableId: ID!) {
}
}
`;

type PageInfo = {
hasNextPage: boolean;
endCursor?: string | null;
};

0 comments on commit 74a30a5

Please sign in to comment.