Skip to content

Commit

Permalink
feat: Pagination details
Browse files Browse the repository at this point in the history
  • Loading branch information
areknawo committed Aug 6, 2024
1 parent 9e78213 commit 775105e
Show file tree
Hide file tree
Showing 13 changed files with 331 additions and 74 deletions.
47 changes: 38 additions & 9 deletions packages/backend/src/routes/extensions/handlers/list.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { z } from "zod";
import { Filter, ObjectId } from "mongodb";
import { AuthenticatedContext } from "#lib/middleware";
import { extension, getExtensionsCollection } from "#collections";
import { FullExtension, extension, getExtensionsCollection } from "#collections";
import { UnderscoreID } from "#lib/mongo";

const inputSchema = z
.object({
Expand All @@ -14,14 +16,41 @@ const handler = async (
input: z.infer<typeof inputSchema>
): Promise<z.infer<typeof outputSchema>> => {
const extensionsCollection = getExtensionsCollection(ctx.db);
const extensions = await extensionsCollection
.find({
workspaceId: ctx.auth.workspaceId
})
.sort({ _id: -1 })
.skip((input.page - 1) * input.perPage)
.limit(input.perPage)
.toArray();
const filter: Filter<UnderscoreID<FullExtension<ObjectId>>> = {
workspaceId: ctx.auth.workspaceId
};
const cursor = extensionsCollection.find(filter).sort({ _id: -1 });

if (input.perPage) {
cursor.skip((input.page - 1) * input.perPage);
}

let extensions: Array<UnderscoreID<FullExtension<ObjectId>>> = [];

if (input.perPage) {
extensions = await cursor.limit(input.perPage).toArray();
} else {
extensions = await cursor.toArray();
}

let totalCount = 0;

if (input.perPage) {
totalCount += (input.page - 1) * input.perPage + extensions.length;

if (extensions.length === input.perPage) {
totalCount += await extensionsCollection.countDocuments(filter, { skip: totalCount });
}
} else {
totalCount = extensions.length;
}

ctx.res.headers({
"x-pagination-total": totalCount,
"x-pagination-pages": Math.ceil(totalCount / (input.perPage || 1)),
"x-pagination-per-page": input.perPage,
"x-pagination-page": input.page
});

return extensions.map(({ _id, workspaceId, ...extension }) => ({
...extension,
Expand Down
40 changes: 34 additions & 6 deletions packages/backend/src/routes/roles/handlers/list.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { z } from "zod";
import { ObjectId } from "mongodb";
import { Filter, ObjectId } from "mongodb";
import { AuthenticatedContext } from "#lib/middleware";
import { role, baseRoleType, getRolesCollection } from "#collections";
import { zodId } from "#lib/mongo";
import { role, baseRoleType, getRolesCollection, FullRole } from "#collections";
import { UnderscoreID, zodId } from "#lib/mongo";

const inputSchema = z
.object({
Expand All @@ -19,18 +19,46 @@ const handler = async (
input: z.infer<typeof inputSchema>
): Promise<z.infer<typeof outputSchema>> => {
const rolesCollection = getRolesCollection(ctx.db);
const filter: Filter<UnderscoreID<FullRole<ObjectId>>> = {
workspaceId: ctx.auth.workspaceId
};
const cursor = rolesCollection
.find({
workspaceId: ctx.auth.workspaceId,
...filter,
...(input.lastId && { _id: { $lt: new ObjectId(input.lastId) } })
})
.sort({ _id: -1 });

if (!input.lastId) {
if (!input.lastId && input.perPage) {
cursor.skip((input.page - 1) * input.perPage);
}

const roles = await cursor.limit(input.perPage).toArray();
let roles: Array<UnderscoreID<FullRole<ObjectId>>> = [];

if (input.perPage) {
roles = await cursor.limit(input.perPage).toArray();
} else {
roles = await cursor.toArray();
}

let totalCount = 0;

if (input.perPage) {
totalCount += (input.page - 1) * input.perPage + roles.length;

if (roles.length === input.perPage) {
totalCount += await rolesCollection.countDocuments(filter, { skip: totalCount });
}
} else {
totalCount = roles.length;
}

ctx.res.headers({
"x-pagination-total": totalCount,
"x-pagination-pages": Math.ceil(totalCount / (input.perPage || 1)),
"x-pagination-per-page": input.perPage,
"x-pagination-page": input.page
});

return roles.map(({ _id, workspaceId, ...role }) => ({
id: `${_id}`,
Expand Down
47 changes: 38 additions & 9 deletions packages/backend/src/routes/tags/handlers/list.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { z } from "zod";
import { Filter, ObjectId } from "mongodb";
import { AuthenticatedContext } from "#lib/middleware";
import { getTagsCollection, tag } from "#collections";
import { FullTag, getTagsCollection, tag } from "#collections";
import { UnderscoreID } from "#lib/mongo";

const inputSchema = z.object({
perPage: z.number().describe("The number of tags to return per page").default(20),
Expand All @@ -12,14 +14,41 @@ const handler = async (
input: z.infer<typeof inputSchema>
): Promise<z.infer<typeof outputSchema>> => {
const tagsCollection = getTagsCollection(ctx.db);
const tags = await tagsCollection
.find({
workspaceId: ctx.auth.workspaceId
})
.sort("_id", -1)
.skip((input.page - 1) * input.perPage)
.limit(input.perPage)
.toArray();
const filter: Filter<UnderscoreID<FullTag<ObjectId>>> = {
workspaceId: ctx.auth.workspaceId
};
const cursor = tagsCollection.find(filter).sort({ _id: -1 });

if (input.perPage) {
cursor.skip((input.page - 1) * input.perPage);
}

let tags: Array<UnderscoreID<FullTag<ObjectId>>> = [];

if (input.perPage) {
tags = await cursor.limit(input.perPage).toArray();
} else {
tags = await cursor.toArray();
}

let totalCount = 0;

if (input.perPage) {
totalCount += (input.page - 1) * input.perPage + tags.length;

if (tags.length === input.perPage) {
totalCount += await tagsCollection.countDocuments(filter, { skip: totalCount });
}
} else {
totalCount = tags.length;
}

ctx.res.headers({
"x-pagination-total": totalCount,
"x-pagination-pages": Math.ceil(totalCount / (input.perPage || 1)),
"x-pagination-per-page": input.perPage,
"x-pagination-page": input.page
});

return tags.map((tag) => {
return { ...tag, id: `${tag._id}` };
Expand Down
40 changes: 34 additions & 6 deletions packages/backend/src/routes/tokens/handlers/list.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { z } from "zod";
import { ObjectId } from "mongodb";
import { Filter, ObjectId } from "mongodb";
import { AuthenticatedContext } from "#lib/middleware";
import { getTokensCollection, token } from "#collections";
import { zodId } from "#lib/mongo";
import { FullToken, getTokensCollection, token } from "#collections";
import { UnderscoreID, zodId } from "#lib/mongo";

const inputSchema = z
.object({
Expand All @@ -21,18 +21,46 @@ const handler = async (
input: z.infer<typeof inputSchema>
): Promise<z.infer<typeof outputSchema>> => {
const tokensCollection = getTokensCollection(ctx.db);
const filter: Filter<UnderscoreID<FullToken<ObjectId>>> = {
workspaceId: ctx.auth.workspaceId
};
const cursor = tokensCollection
.find({
workspaceId: ctx.auth.workspaceId,
...filter,
...(input.lastId ? { _id: { $lt: new ObjectId(input.lastId) } } : {})
})
.sort("_id", -1);

if (!input.lastId) {
if (!input.lastId && input.perPage) {
cursor.skip((input.page - 1) * input.perPage);
}

const tokens = await cursor.limit(input.perPage).toArray();
let tokens: Array<UnderscoreID<FullToken<ObjectId>>> = [];

if (input.perPage) {
tokens = await cursor.limit(input.perPage).toArray();
} else {
tokens = await cursor.toArray();
}

let totalCount = 0;

if (input.perPage) {
totalCount += (input.page - 1) * input.perPage + tokens.length;

if (tokens.length === input.perPage) {
totalCount += await tokensCollection.countDocuments(filter, { skip: totalCount });
}
} else {
totalCount = tokens.length;
}

ctx.res.headers({
"x-pagination-total": totalCount,
"x-pagination-pages": Math.ceil(totalCount / (input.perPage || 1)),
"x-pagination-per-page": input.perPage,
"x-pagination-page": input.page
});

return tokens.map((token) => ({
id: `${token._id}`,
Expand Down
45 changes: 37 additions & 8 deletions packages/backend/src/routes/versions/handlers/list.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { z } from "zod";
import { ObjectId } from "mongodb";
import { Filter, ObjectId } from "mongodb";
import {
FullVersion,
getContentVersionsCollection,
getVersionsCollection,
version,
versionMember
} from "#collections";
import { AuthenticatedContext } from "#lib/middleware";
import { zodId } from "#lib/mongo";
import { UnderscoreID, zodId } from "#lib/mongo";
import { fetchEntryMembers } from "#lib/utils";
import { DocJSON, bufferToJSON } from "#lib/content-processing";

Expand All @@ -30,21 +31,49 @@ const handler = async (
input: z.infer<typeof inputSchema>
): Promise<z.infer<typeof outputSchema>> => {
const versionsCollection = getVersionsCollection(ctx.db);
const filter: Filter<UnderscoreID<FullVersion<ObjectId>>> = {
workspaceId: ctx.auth.workspaceId,
contentPieceId: new ObjectId(input.contentPieceId),
...(input.variantId ? { variantId: new ObjectId(input.variantId) } : {})
};
const cursor = versionsCollection
.find({
workspaceId: ctx.auth.workspaceId,
contentPieceId: new ObjectId(input.contentPieceId),
...(input.variantId ? { variantId: new ObjectId(input.variantId) } : {}),
...filter,
...(input.lastId ? { _id: { $lt: new ObjectId(input.lastId) } } : {})
})
.sort({ date: -1 });
const contents = new Map<string, DocJSON>();

if (!input.lastId) {
if (!input.lastId && input.perPage) {
cursor.skip((input.page - 1) * input.perPage);
}

const versions = await cursor.limit(input.perPage).toArray();
const contents = new Map<string, DocJSON>();
let versions: Array<UnderscoreID<FullVersion<ObjectId>>> = [];

if (input.perPage) {
versions = await cursor.limit(input.perPage).toArray();
} else {
versions = await cursor.toArray();
}

let totalCount = 0;

if (input.perPage) {
totalCount += (input.page - 1) * input.perPage + versions.length;

if (versions.length === input.perPage) {
totalCount += await versionsCollection.countDocuments(filter, { skip: totalCount });
}
} else {
totalCount = versions.length;
}

ctx.res.headers({
"x-pagination-total": totalCount,
"x-pagination-pages": Math.ceil(totalCount / (input.perPage || 1)),
"x-pagination-per-page": input.perPage,
"x-pagination-page": input.page
});

if (input.content) {
const versionContentsCollection = getContentVersionsCollection(ctx.db);
Expand Down
42 changes: 35 additions & 7 deletions packages/backend/src/routes/webhooks/handlers/list.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { z } from "zod";
import { ObjectId } from "mongodb";
import { Filter, ObjectId } from "mongodb";
import { AuthenticatedContext } from "#lib/middleware";
import { webhook, getWebhooksCollection, Webhook } from "#collections";
import { zodId } from "#lib/mongo";
import { webhook, getWebhooksCollection, Webhook, FullWebhook } from "#collections";
import { UnderscoreID, zodId } from "#lib/mongo";

const inputSchema = z.object({
perPage: z.number().describe("Number of webhooks to return per page").default(20),
Expand All @@ -27,19 +27,47 @@ const handler = async (
): Promise<z.infer<typeof outputSchema>> => {
const extensionId = ctx.req.headers["x-vrite-extension-id"] as string | undefined;
const webhooksCollection = getWebhooksCollection(ctx.db);
const filter: Filter<UnderscoreID<FullWebhook<ObjectId>>> = {
workspaceId: ctx.auth.workspaceId,
...(input.extensionOnly && extensionId ? { extensionId: new ObjectId(extensionId) } : {})
};
const cursor = webhooksCollection
.find({
workspaceId: ctx.auth.workspaceId,
...(input.lastId ? { _id: { $lt: new ObjectId(input.lastId) } } : {}),
...(input.extensionOnly && extensionId ? { extensionId: new ObjectId(extensionId) } : {})
...filter,
...(input.lastId ? { _id: { $lt: new ObjectId(input.lastId) } } : {})
})
.sort("_id", -1);

if (!input.lastId) {
cursor.skip((input.page - 1) * input.perPage);
}

const webhooks = await cursor.limit(input.perPage).toArray();
let webhooks: Array<UnderscoreID<FullWebhook<ObjectId>>> = [];

if (input.perPage) {
webhooks = await cursor.limit(input.perPage).toArray();
} else {
webhooks = await cursor.toArray();
}

let totalCount = 0;

if (input.perPage) {
totalCount += (input.page - 1) * input.perPage + webhooks.length;

if (webhooks.length === input.perPage) {
totalCount += await webhooksCollection.countDocuments(filter, { skip: totalCount });
}
} else {
totalCount = webhooks.length;
}

ctx.res.headers({
"x-pagination-total": totalCount,
"x-pagination-pages": Math.ceil(totalCount / (input.perPage || 1)),
"x-pagination-per-page": input.perPage,
"x-pagination-page": input.page
});

return webhooks.map(({ _id, workspaceId, metadata, extensionId, secret, ...webhook }) => {
const result: Webhook & { id: string; extension?: boolean } = {
Expand Down
Loading

0 comments on commit 775105e

Please sign in to comment.