1
0
Fork 0
mirror of synced 2024-10-04 03:54:37 +13:00

Add doInAppMigrationContext

This commit is contained in:
Adria Navarro 2023-12-01 10:19:28 +01:00
parent fe0efc7539
commit f62dd56dd6
3 changed files with 54 additions and 15 deletions

View file

@ -147,13 +147,21 @@ export async function doInTenant<T>(
export async function doInAppContext<T>(
appId: string,
task: () => T
): Promise<T> {
return _doInAppContext(appId, task)
}
async function _doInAppContext<T>(
appId: string,
task: () => T,
extraContextSettings?: ContextMap
): Promise<T> {
if (!appId) {
throw new Error("appId is required")
}
const tenantId = getTenantIDFromAppID(appId)
const updates: ContextMap = { appId }
const updates: ContextMap = { appId, ...extraContextSettings }
if (tenantId) {
updates.tenantId = tenantId
}
@ -182,21 +190,15 @@ export async function doInAppMigrationContext<T>(
appId: string,
task: () => T
): Promise<T> {
if (!appId && !env.isTest()) {
throw new Error("appId is required")
try {
return _doInAppContext(appId, task, {
isMigrating: true,
})
} finally {
updateContext({
isMigrating: undefined,
})
}
let updates: ContextMap
if (!appId) {
updates = { appId: "" }
} else {
const tenantId = getTenantIDFromAppID(appId)
updates = { appId }
if (tenantId) {
updates.tenantId = tenantId
}
}
return newContext(updates, task)
}
export function getIdentity(): IdentityContext | undefined {

View file

@ -1,6 +1,10 @@
import { testEnv } from "../../../tests/extra"
import * as context from "../"
import { DEFAULT_TENANT_ID } from "../../constants"
import { structures } from "../../../tests"
import { db } from "../.."
import Context from "../Context"
import { ContextMap } from "../types"
describe("context", () => {
describe("doInTenant", () => {
@ -144,4 +148,36 @@ describe("context", () => {
expect(isScim).toBe(false)
})
})
describe("doInAppMigrationContext", () => {
it("the context is set correctly", async () => {
const appId = db.generateAppID()
await context.doInAppMigrationContext(appId, () => {
const context = Context.get()
const expected: ContextMap = {
appId,
isMigrating: true,
}
expect(context).toEqual(expected)
})
})
it("the context is set correctly when running in a tenant id", async () => {
const tenantId = structures.tenant.id()
const appId = db.generateAppID(tenantId)
await context.doInAppMigrationContext(appId, () => {
const context = Context.get()
const expected: ContextMap = {
appId,
isMigrating: true,
tenantId,
}
expect(context).toEqual(expected)
})
})
})
})

View file

@ -8,4 +8,5 @@ export type ContextMap = {
environmentVariables?: Record<string, string>
isScim?: boolean
automationId?: string
isMigrating?: boolean
}