76 lines
2.0 KiB
TypeScript
76 lines
2.0 KiB
TypeScript
import { StreamRecord } from '@/modules/core/helpers/types'
|
|
import { getStreams as serviceGetStreams } from '@/modules/core/services/streams'
|
|
import { getUserStreams } from '@/modules/core/repositories/streams'
|
|
import { QueryAllWorkspaceProjects } from '@/modules/workspaces/domain/operations'
|
|
import { WorkspaceQueryError } from '@/modules/workspaces/errors/workspace'
|
|
|
|
export const queryAllWorkspaceProjectsFactory = ({
|
|
getStreams
|
|
}: {
|
|
// TODO: Core service factory functions
|
|
getStreams: typeof serviceGetStreams
|
|
}): QueryAllWorkspaceProjects =>
|
|
async function* queryAllWorkspaceProjects({
|
|
workspaceId
|
|
}): AsyncGenerator<StreamRecord[], void, unknown> {
|
|
let cursor: Date | null = null
|
|
let iterationCount = 0
|
|
|
|
do {
|
|
if (iterationCount > 500) throw new WorkspaceQueryError()
|
|
|
|
const { streams, cursorDate } = await getStreams({
|
|
cursor,
|
|
orderBy: null,
|
|
limit: 1000,
|
|
visibility: null,
|
|
searchQuery: null,
|
|
streamIdWhitelist: null,
|
|
workspaceIdWhitelist: [workspaceId]
|
|
})
|
|
|
|
yield streams
|
|
|
|
cursor = cursorDate
|
|
iterationCount++
|
|
} while (!!cursor)
|
|
}
|
|
|
|
type GetWorkspaceProjectsArgs = {
|
|
workspaceId: string
|
|
}
|
|
|
|
type GetWorkspaceProjectsOptions = {
|
|
limit: number | null
|
|
cursor: string | null
|
|
filter: {
|
|
search?: string | null
|
|
userId: string
|
|
}
|
|
}
|
|
|
|
type GetWorkspaceProjectsReturnValue = {
|
|
items: StreamRecord[]
|
|
cursor: string | null
|
|
}
|
|
|
|
export const getWorkspaceProjectsFactory =
|
|
({ getStreams }: { getStreams: typeof getUserStreams }) =>
|
|
async (
|
|
args: GetWorkspaceProjectsArgs,
|
|
opts: GetWorkspaceProjectsOptions
|
|
): Promise<GetWorkspaceProjectsReturnValue> => {
|
|
const { streams, cursor } = await getStreams({
|
|
cursor: opts.cursor,
|
|
limit: opts.limit || 25,
|
|
searchQuery: opts.filter?.search || undefined,
|
|
workspaceId: args.workspaceId,
|
|
userId: opts.filter.userId
|
|
})
|
|
|
|
return {
|
|
items: streams,
|
|
cursor
|
|
}
|
|
}
|