Files
speckle-server/packages/server/modules/gatekeeper/services/workspacePlans.ts
T
Gergő Jedlicska f501cc4ad5 gergo/web 2888 workspace project cancreate (#4294)
* WIP can create project

* WIP can create project more work

* complete body, stencil tests

* feat(shared): move workspace plan types into shared

* test progress wip

* feat(shared): add more logic to canCreateWorkspaceProject

* a few more tests, as a treat

* chore(authz): round out tests

* fixed loaders, new GQL checks, dataLoaders in auth loaders

* fix(authz): get workspace limits loader

* chore(authz): update loaders

* frontend fixed up to snuff

* fix(authz): fix workspace plans for tests

* fix(authz): classic

* fix(authz): 0 counts

---------

Co-authored-by: Chuck Driesler <chuck@speckle.systems>
Co-authored-by: Kristaps Fabians Geikins <fabis94@live.com>
2025-04-01 16:38:20 +01:00

98 lines
2.9 KiB
TypeScript

import { UpsertWorkspacePlan } from '@/modules/gatekeeper/domain/billing'
import { InvalidWorkspacePlanStatus } from '@/modules/gatekeeper/errors/billing'
import { EventBusEmit } from '@/modules/shared/services/eventBus'
import { GetWorkspace } from '@/modules/workspaces/domain/operations'
import { WorkspaceNotFoundError } from '@/modules/workspaces/errors/workspace'
import { throwUncoveredError, WorkspacePlan } from '@speckle/shared'
export const updateWorkspacePlanFactory =
({
getWorkspace,
upsertWorkspacePlan,
emitEvent
}: {
getWorkspace: GetWorkspace
// im using the generic function here, cause the service is
// responsible for protecting the permutations
upsertWorkspacePlan: UpsertWorkspacePlan
emitEvent: EventBusEmit
}) =>
async ({
workspaceId,
name,
status
}: Pick<WorkspacePlan, 'workspaceId' | 'name' | 'status'>): Promise<void> => {
const workspace = await getWorkspace({
workspaceId
})
if (!workspace) throw new WorkspaceNotFoundError()
const createdAt = new Date()
switch (name) {
case 'starter':
switch (status) {
case 'trial':
case 'expired':
case 'valid':
case 'cancelationScheduled':
case 'canceled':
case 'paymentFailed':
await upsertWorkspacePlan({
workspacePlan: { workspaceId, status, name, createdAt }
})
break
default:
throwUncoveredError(status)
}
break
case 'business':
case 'plus':
case 'team':
case 'pro':
switch (status) {
case 'trial':
case 'expired':
throw new InvalidWorkspacePlanStatus()
case 'valid':
case 'cancelationScheduled':
case 'canceled':
case 'paymentFailed':
await upsertWorkspacePlan({
workspacePlan: { workspaceId, status, name, createdAt }
})
break
default:
throwUncoveredError(status)
}
break
case 'free':
case 'academia':
case 'unlimited':
case 'starterInvoiced':
case 'plusInvoiced':
case 'businessInvoiced':
switch (status) {
case 'valid':
await upsertWorkspacePlan({
workspacePlan: { workspaceId, status, name, createdAt }
})
break
case 'cancelationScheduled':
case 'canceled':
case 'expired':
case 'paymentFailed':
case 'trial':
throw new InvalidWorkspacePlanStatus()
default:
throwUncoveredError(status)
}
break
default:
throwUncoveredError(name)
}
await emitEvent({
eventName: 'gatekeeper.workspace-plan-updated',
payload: { workspacePlan: { name, status, workspaceId } }
})
}