🐛 Fix paging on manage user invites

This commit is contained in:
Manuel
2023-07-31 21:20:46 +02:00
parent ddfd9fb286
commit 9fcbdb43e6
2 changed files with 25 additions and 35 deletions

View File

@@ -8,24 +8,17 @@ export const inviteRouter = createTRPCRouter({
getAllInvites: publicProcedure
.input(
z.object({
limit: z.number().min(1).max(100).nullish(),
cursor: z.string().nullish(),
limit: z.number().min(1).max(100).nullish().default(10),
page: z.number().min(0)
})
)
.query(async ({ ctx, input }) => {
const limit = input.limit ?? 50;
const cursor = input.cursor;
const registrationTokens = await ctx.prisma.registrationToken.findMany({
take: limit + 1, // get an extra item at the end which we'll use as next cursor
cursor: cursor ? { id: cursor } : undefined,
take: limit,
skip: limit * input.page,
});
let nextCursor: typeof cursor | undefined = undefined;
if (registrationTokens.length > limit) {
const nextItem = registrationTokens.pop();
nextCursor = nextItem!.id;
}
const countRegistrationTokens = await ctx.prisma.registrationToken.count();
return {
@@ -33,7 +26,6 @@ export const inviteRouter = createTRPCRouter({
id: token.id,
expires: token.expires,
})),
nextCursor,
countPages: Math.ceil(countRegistrationTokens / limit)
};
}),