2022-11-24 19:48:51 +01:00
|
|
|
import env from "../environment"
|
2022-11-28 18:54:04 +01:00
|
|
|
import * as tenancy from "../tenancy"
|
2022-03-22 01:23:22 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2022-11-24 19:48:51 +01:00
|
|
|
function getFeatureFlags() {
|
2022-03-22 01:23:22 +01:00
|
|
|
if (!env.TENANT_FEATURE_FLAGS) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-24 19:48:51 +01:00
|
|
|
const tenantFeatureFlags: Record<string, string[]> = {}
|
2022-03-22 01:23:22 +01:00
|
|
|
|
|
|
|
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()
|
|
|
|
|
2022-11-24 19:48:51 +01:00
|
|
|
export function isEnabled(featureFlag: string) {
|
2022-03-22 01:23:22 +01:00
|
|
|
const tenantId = tenancy.getTenantId()
|
2022-11-24 19:48:51 +01:00
|
|
|
const flags = getTenantFeatureFlags(tenantId)
|
2022-09-07 13:08:10 +02:00
|
|
|
return flags.includes(featureFlag)
|
2022-03-22 01:23:22 +01:00
|
|
|
}
|
|
|
|
|
2022-11-24 19:48:51 +01:00
|
|
|
export function getTenantFeatureFlags(tenantId: string) {
|
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-11-24 19:48:51 +01:00
|
|
|
export enum TenantFeatureFlag {
|
|
|
|
LICENSING = "LICENSING",
|
|
|
|
GOOGLE_SHEETS = "GOOGLE_SHEETS",
|
|
|
|
USER_GROUPS = "USER_GROUPS",
|
2022-03-22 01:23:22 +01:00
|
|
|
}
|