Add doInAppMigrationContext
This commit is contained in:
parent
fe0efc7539
commit
f62dd56dd6
|
@ -147,13 +147,21 @@ export async function doInTenant<T>(
|
||||||
export async function doInAppContext<T>(
|
export async function doInAppContext<T>(
|
||||||
appId: string,
|
appId: string,
|
||||||
task: () => T
|
task: () => T
|
||||||
|
): Promise<T> {
|
||||||
|
return _doInAppContext(appId, task)
|
||||||
|
}
|
||||||
|
|
||||||
|
async function _doInAppContext<T>(
|
||||||
|
appId: string,
|
||||||
|
task: () => T,
|
||||||
|
extraContextSettings?: ContextMap
|
||||||
): Promise<T> {
|
): Promise<T> {
|
||||||
if (!appId) {
|
if (!appId) {
|
||||||
throw new Error("appId is required")
|
throw new Error("appId is required")
|
||||||
}
|
}
|
||||||
|
|
||||||
const tenantId = getTenantIDFromAppID(appId)
|
const tenantId = getTenantIDFromAppID(appId)
|
||||||
const updates: ContextMap = { appId }
|
const updates: ContextMap = { appId, ...extraContextSettings }
|
||||||
if (tenantId) {
|
if (tenantId) {
|
||||||
updates.tenantId = tenantId
|
updates.tenantId = tenantId
|
||||||
}
|
}
|
||||||
|
@ -182,21 +190,15 @@ export async function doInAppMigrationContext<T>(
|
||||||
appId: string,
|
appId: string,
|
||||||
task: () => T
|
task: () => T
|
||||||
): Promise<T> {
|
): Promise<T> {
|
||||||
if (!appId && !env.isTest()) {
|
try {
|
||||||
throw new Error("appId is required")
|
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 {
|
export function getIdentity(): IdentityContext | undefined {
|
||||||
|
|
|
@ -1,6 +1,10 @@
|
||||||
import { testEnv } from "../../../tests/extra"
|
import { testEnv } from "../../../tests/extra"
|
||||||
import * as context from "../"
|
import * as context from "../"
|
||||||
import { DEFAULT_TENANT_ID } from "../../constants"
|
import { DEFAULT_TENANT_ID } from "../../constants"
|
||||||
|
import { structures } from "../../../tests"
|
||||||
|
import { db } from "../.."
|
||||||
|
import Context from "../Context"
|
||||||
|
import { ContextMap } from "../types"
|
||||||
|
|
||||||
describe("context", () => {
|
describe("context", () => {
|
||||||
describe("doInTenant", () => {
|
describe("doInTenant", () => {
|
||||||
|
@ -144,4 +148,36 @@ describe("context", () => {
|
||||||
expect(isScim).toBe(false)
|
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)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
|
@ -8,4 +8,5 @@ export type ContextMap = {
|
||||||
environmentVariables?: Record<string, string>
|
environmentVariables?: Record<string, string>
|
||||||
isScim?: boolean
|
isScim?: boolean
|
||||||
automationId?: string
|
automationId?: string
|
||||||
|
isMigrating?: boolean
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue