2022-05-10 17:37:24 +02:00
|
|
|
const { setTenantId, setGlobalDB, closeTenancy } = require("../tenancy")
|
|
|
|
const cls = require("../context/FunctionContext")
|
2021-08-05 10:59:08 +02:00
|
|
|
const { buildMatcherRegex, matches } = require("./matchers")
|
|
|
|
|
2021-09-09 14:27:18 +02:00
|
|
|
module.exports = (
|
|
|
|
allowQueryStringPatterns,
|
|
|
|
noTenancyPatterns,
|
|
|
|
opts = { noTenancyRequired: false }
|
|
|
|
) => {
|
2021-08-05 10:59:08 +02:00
|
|
|
const allowQsOptions = buildMatcherRegex(allowQueryStringPatterns)
|
|
|
|
const noTenancyOptions = buildMatcherRegex(noTenancyPatterns)
|
|
|
|
|
2022-05-10 17:37:24 +02:00
|
|
|
return async function (ctx, next) {
|
|
|
|
return cls.run(async () => {
|
|
|
|
const allowNoTenant =
|
|
|
|
opts.noTenancyRequired || !!matches(ctx, noTenancyOptions)
|
|
|
|
const allowQs = !!matches(ctx, allowQsOptions)
|
|
|
|
const tenantId = setTenantId(ctx, { allowQs, allowNoTenant })
|
|
|
|
setGlobalDB(tenantId)
|
|
|
|
const res = await next()
|
|
|
|
await closeTenancy()
|
|
|
|
return res
|
|
|
|
})
|
2022-04-19 20:42:52 +02:00
|
|
|
}
|
2021-08-05 10:59:08 +02:00
|
|
|
}
|