2022-03-22 01:23:22 +01:00
|
|
|
const env = require("../environment")
|
|
|
|
const tenancy = require("../tenancy")
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read the TENANT_FEATURE_FLAGS env var and return an array of features flags for each tenant.
|
|
|
|
* The env var is formatted as:
|
|
|
|
* tenant1:feature1:feature2,tenant2:feature1
|
|
|
|
*/
|
|
|
|
const getFeatureFlags = () => {
|
|
|
|
if (!env.TENANT_FEATURE_FLAGS) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const tenantFeatureFlags = {}
|
|
|
|
|
|
|
|
env.TENANT_FEATURE_FLAGS.split(",").forEach(tenantToFeatures => {
|
|
|
|
const [tenantId, ...features] = tenantToFeatures.split(":")
|
|
|
|
|
|
|
|
features.forEach(feature => {
|
|
|
|
if (!tenantFeatureFlags[tenantId]) {
|
|
|
|
tenantFeatureFlags[tenantId] = []
|
|
|
|
}
|
|
|
|
tenantFeatureFlags[tenantId].push(feature)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
return tenantFeatureFlags
|
|
|
|
}
|
|
|
|
|
|
|
|
const TENANT_FEATURE_FLAGS = getFeatureFlags()
|
|
|
|
|
|
|
|
exports.isEnabled = featureFlag => {
|
|
|
|
const tenantId = tenancy.getTenantId()
|
2022-09-07 13:08:10 +02:00
|
|
|
const flags = exports.getTenantFeatureFlags(tenantId)
|
|
|
|
return flags.includes(featureFlag)
|
2022-03-22 01:23:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
exports.getTenantFeatureFlags = tenantId => {
|
2022-09-07 13:08:10 +02:00
|
|
|
const flags = []
|
|
|
|
|
|
|
|
if (TENANT_FEATURE_FLAGS) {
|
|
|
|
const globalFlags = TENANT_FEATURE_FLAGS["*"]
|
|
|
|
const tenantFlags = TENANT_FEATURE_FLAGS[tenantId]
|
|
|
|
|
|
|
|
if (globalFlags) {
|
|
|
|
flags.push(...globalFlags)
|
|
|
|
}
|
|
|
|
if (tenantFlags) {
|
|
|
|
flags.push(...tenantFlags)
|
|
|
|
}
|
2022-03-22 01:23:22 +01:00
|
|
|
}
|
|
|
|
|
2022-09-07 13:08:10 +02:00
|
|
|
return flags
|
2022-03-22 01:23:22 +01:00
|
|
|
}
|
|
|
|
|
2022-09-23 14:08:18 +02:00
|
|
|
exports.TenantFeatureFlag = {
|
2022-03-22 01:23:22 +01:00
|
|
|
LICENSING: "LICENSING",
|
2022-05-05 10:59:10 +02:00
|
|
|
GOOGLE_SHEETS: "GOOGLE_SHEETS",
|
2022-07-29 14:22:59 +02:00
|
|
|
USER_GROUPS: "USER_GROUPS",
|
2022-03-22 01:23:22 +01:00
|
|
|
}
|