Files
speckle-server/packages/server/modules/workspaces/services/regions.ts
T
Gergő Jedlicska f210d9b749 gergo/web 2109 project region based db connection selector (#3434)
* feat(projects): add project regions, default to null

* feat(multiregion): add projectRegion Db client lookup logic

* feat(multiregion): add project region repositories and caching

* feat(multiRegion): db initialization and get project db client

* feat(docker-compose): add second db for regions testing

* feat(multiRegion): initialize region with pubs and subs working

* fix(multiRegion): get region client even if it was registered in another pod

* feat(workspaces): create workspace resolver split

* feat: update server region metadata

* feat(projects): rewrite project creation

* feat(multiRegion): getRegionDb

* fix(workspaces): get projects now can retur null

* feat(multiRegion): make local multi region DB-s work

* feat: set d efault workspace region

* CR changes

* tests

* feat(multiRegion): bind region properly

* fe update

* test fixes

* feat(multiRegion): automatically create aiven extras plugin

* ci(postgres): use published postgres with aiven extras

* fix(multiRegion): roll back the aiven extras migration, there is a better way

* tests fix

* fix(billing): we do not need to add a seat, if the workspace is on a plan, but has no sub

---------

Co-authored-by: Kristaps Fabians Geikins <fabis94@live.com>
2024-11-06 17:29:08 +01:00

72 lines
2.1 KiB
TypeScript

import { WorkspaceFeatureAccessFunction } from '@/modules/gatekeeper/domain/operations'
import { GetRegions } from '@/modules/multiregion/domain/operations'
import {
AssignRegion,
GetAvailableRegions,
GetDefaultRegion,
GetWorkspace,
UpsertRegionAssignment,
UpsertWorkspace
} from '@/modules/workspaces/domain/operations'
import { WorkspaceRegionAssignmentError } from '@/modules/workspaces/errors/regions'
export const getAvailableRegionsFactory =
(deps: {
getRegions: GetRegions
canWorkspaceUseRegions: WorkspaceFeatureAccessFunction
}): GetAvailableRegions =>
async (params) => {
const { workspaceId } = params
const canUseMultiRegion = await deps.canWorkspaceUseRegions({ workspaceId })
if (!canUseMultiRegion) {
return []
}
return await deps.getRegions()
}
export const assignRegionFactory =
(deps: {
getAvailableRegions: GetAvailableRegions
upsertRegionAssignment: UpsertRegionAssignment
getDefaultRegion: GetDefaultRegion
getWorkspace: GetWorkspace
insertRegionWorkspace: UpsertWorkspace
}): AssignRegion =>
async (params) => {
const { workspaceId, regionKey } = params
const workspace = await deps.getWorkspace({ workspaceId })
if (!workspace) {
throw new WorkspaceRegionAssignmentError('Workspace not found', {
info: { params }
})
}
const availableRegions = await deps.getAvailableRegions({ workspaceId })
if (!availableRegions.find((region) => region.key === regionKey)) {
throw new WorkspaceRegionAssignmentError(
'Specified region not available for workspace',
{
info: { params }
}
)
}
const existingRegion = await deps.getDefaultRegion({ workspaceId })
if (existingRegion) {
throw new WorkspaceRegionAssignmentError(
'Workspace already has a region assigned',
{
info: { params }
}
)
}
// Set up region
await deps.upsertRegionAssignment({ workspaceId, regionKey })
// Copy workspace into region db
await deps.insertRegionWorkspace({ workspace })
}