Files
speckle-server/packages/server/modules/shared/services/paginatedItems.ts
T
Alessandro Magionami fd68c6ef2a feat(workspaces): user workspace join requests (#4023)
* feat(workspaces): user workspace join requests

* chore(workspaces): return limited workspace

* chore(workspaces): fix tests

* chore(workspaces): add index for userId

* chore(shared): fix totalcount on getpaginateditems

* chore(workspaces): add workspace core resolvers to throw specific error
2025-02-25 12:19:21 +01:00

52 lines
1.2 KiB
TypeScript

import {
decodeIsoDateCursor,
encodeIsoDateCursor
} from '@/modules/shared/helpers/graphqlHelper'
type Collection<T> = {
cursor: string | null
totalCount: number
items: T[]
}
type GetPaginatedItemsArgs = {
limit: number
cursor?: string
}
export const getPaginatedItemsFactory =
<TArgs extends GetPaginatedItemsArgs, T extends { createdAt: Date }>({
getItems,
getTotalCount
}: {
getItems: (args: TArgs) => Promise<T[]>
getTotalCount: (args: Omit<TArgs, 'cursor' | 'limit'>) => Promise<number>
}) =>
async (args: TArgs): Promise<Collection<T>> => {
const totalCount = await getTotalCount(args)
if (args.limit === 0) {
return {
cursor: null,
items: [],
totalCount
}
}
const maybeDecodedCursor = args.cursor ? decodeIsoDateCursor(args.cursor) : null
const items = await getItems({
...args,
cursor: maybeDecodedCursor ?? undefined
})
let cursor = null
if (items.length === args.limit) {
const lastItem = items.at(-1)
cursor = lastItem ? encodeIsoDateCursor(lastItem.createdAt) : null
}
return {
items,
cursor,
totalCount
}
}