2021-09-09 14:27:18 +02:00
|
|
|
const {
|
|
|
|
isMultiTenant,
|
|
|
|
updateTenantId,
|
|
|
|
isTenantIdSet,
|
|
|
|
DEFAULT_TENANT_ID,
|
2022-01-27 19:18:31 +01:00
|
|
|
updateAppId,
|
2021-09-09 14:27:18 +02:00
|
|
|
} = require("../tenancy")
|
|
|
|
const ContextFactory = require("../tenancy/FunctionContext")
|
|
|
|
const { getTenantIDFromAppID } = require("../db/utils")
|
|
|
|
|
|
|
|
module.exports = () => {
|
|
|
|
return ContextFactory.getMiddleware(ctx => {
|
|
|
|
// if not in multi-tenancy mode make sure its default and exit
|
|
|
|
if (!isMultiTenant()) {
|
|
|
|
updateTenantId(DEFAULT_TENANT_ID)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// if tenant ID already set no need to continue
|
|
|
|
if (isTenantIdSet()) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
const appId = ctx.appId ? ctx.appId : ctx.user ? ctx.user.appId : null
|
|
|
|
const tenantId = getTenantIDFromAppID(appId) || DEFAULT_TENANT_ID
|
|
|
|
updateTenantId(tenantId)
|
2022-01-27 19:18:31 +01:00
|
|
|
updateAppId(appId)
|
2021-09-09 14:27:18 +02:00
|
|
|
})
|
|
|
|
}
|