feat(workspaces): add blob store as workspace create deps

This commit is contained in:
Gergő Jedlicska
2024-06-26 21:47:16 +02:00
parent ddf5480d71
commit d4ef3e97df
2 changed files with 14 additions and 5 deletions
@@ -7,6 +7,8 @@ import { Workspace, WorkspaceAcl } from '@/modules/workspaces/domain/types'
export type StoreWorkspace = (args: { workspace: Workspace }) => Promise<void>
export type UpsertWorkspaceRole = (args: WorkspaceAcl) => Promise<void>
export type StoreBlob = (args: string) => Promise<string>
export type EmitWorkspaceEvent = <TEvent extends WorkspaceEvents>(args: {
event: TEvent
payload: WorkspaceEventsPayloads[TEvent]
@@ -1,5 +1,6 @@
import {
EmitWorkspaceEvent,
StoreBlob,
StoreWorkspace,
UpsertWorkspaceRole
} from '@/modules/workspaces/domain/operations'
@@ -7,10 +8,8 @@ import { Workspace } from '@/modules/workspaces/domain/types'
import { Roles } from '@speckle/shared'
import cryptoRandomString from 'crypto-random-string'
// using Pick deliberately here, so if we change the Workspace domain model, we need to
// explicitly handle the changes here too
type WorkspaceCreateArgs = {
workspaceInput: Pick<Workspace, 'name' | 'logoUrl' | 'description'>
workspaceInput: { name: string; description: string | null; logo: string | null }
userId: string
}
@@ -18,19 +17,27 @@ export const createWorkspaceFactory =
({
storeWorkspace,
upsertWorkspaceRole,
emitWorkspaceEvent
emitWorkspaceEvent,
storeBlob
}: {
storeWorkspace: StoreWorkspace
upsertWorkspaceRole: UpsertWorkspaceRole
storeBlob: StoreBlob
emitWorkspaceEvent: EmitWorkspaceEvent
}) =>
async ({ userId, workspaceInput }: WorkspaceCreateArgs): Promise<Workspace> => {
let logoUrl: string | null = null
if (workspaceInput.logo) {
logoUrl = await storeBlob(workspaceInput.logo)
}
const workspace = {
...workspaceInput,
id: cryptoRandomString({ length: 10 }),
createdAt: new Date(),
updatedAt: new Date(),
createdByUserId: userId
createdByUserId: userId,
logoUrl
}
await storeWorkspace({ workspace })
// assign the creator as workspace administrator