Files
speckle-server/packages/server/modules/shared/services/paginatedItems.ts
T
Chuck Driesler 855185245e feat(workspaces): consolidate workspace role/seat reporting (#4315)
* feat(workspaces: shuffle seat type counts around

* fix(workspaces): use new counts

* fix(workspaces): better field placement

* Updated FE

* chore(workspaces): fix tests

* fix(workspaces): use correct flags

* fix(workspaces): correct another test

* fix(workspaces): use correct model count function

---------

Co-authored-by: Mike Tasset <mike.tasset@gmail.com>
2025-04-04 10:27:49 +01:00

47 lines
1.1 KiB
TypeScript

import {
decodeIsoDateCursor,
encodeIsoDateCursor,
Collection
} from '@/modules/shared/helpers/graphqlHelper'
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
}
}