Files
speckle-server/packages/server/modules/workspaces/services/projects.ts
T
Chuck Driesler ac6dd70d27 feat(workspaces): default workspace project role (#3013)
* feat(workspaces): enable default project role in workspace

* fix(workspaces): not satisfying

* chore(workspaces): phrasing

* fix(workspaces): use new field in role mapping

* chore(workspaces): use roles

* fix(workspaces): update tests

* fix(workspaces): more parallel role update errors

* chore(workspaces): like this now

* chore(workspaces): revert changes to deps

* fix(workspaces): assert domain type conversion at gql layer

* fix(workspaces): repair tests

* fix(workspaces): fix more tests
2024-09-17 22:17:10 +02:00

104 lines
2.6 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 {
GetWorkspace,
GetWorkspaceRoleToDefaultProjectRoleMapping,
QueryAllWorkspaceProjects
} from '@/modules/workspaces/domain/operations'
import {
WorkspaceNotFoundError,
WorkspaceQueryError
} from '@/modules/workspaces/errors/workspace'
import { Roles } from '@speckle/shared'
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
}
}
export const getWorkspaceRoleToDefaultProjectRoleMappingFactory =
({
getWorkspace
}: {
getWorkspace: GetWorkspace
}): GetWorkspaceRoleToDefaultProjectRoleMapping =>
async ({ workspaceId }) => {
const workspace = await getWorkspace({ workspaceId })
if (!workspace) {
throw new WorkspaceNotFoundError()
}
return {
[Roles.Workspace.Guest]: null,
[Roles.Workspace.Member]: workspace.defaultProjectRole,
[Roles.Workspace.Admin]: Roles.Stream.Owner
}
}