Files
speckle-server/packages/server/modules/gatekeeper/services/workspacePlans.ts
T
Gergő Jedlicska bf80347abf gergo/web 2664 workspace backend powered metrics (#3985)
* feat(workspaces): delete workspace emit event

* feat(workspaces): move workspace group metrics to the backend

* Removed FE mixpanel group update

* Remove fragment

* test(gatekeeper): add unittest to new gatekeeper service

---------

Co-authored-by: Mike Tasset <mike.tasset@gmail.com>
2025-02-17 09:50:16 +01:00

96 lines
2.9 KiB
TypeScript

import { UpsertWorkspacePlan } from '@/modules/gatekeeper/domain/billing'
import { InvalidWorkspacePlanStatus } from '@/modules/gatekeeper/errors/billing'
import { WorkspacePlan } from '@/modules/gatekeeperCore/domain/billing'
import { EventBusEmit } from '@/modules/shared/services/eventBus'
import { GetWorkspace } from '@/modules/workspaces/domain/operations'
import { WorkspaceNotFoundError } from '@/modules/workspaces/errors/workspace'
import { throwUncoveredError } 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':
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 '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 } }
})
}