mirror of
https://github.com/wassname/talk.git
synced 2026-07-28 11:27:05 +08:00
[CORL-824] Feature Flags (#2756)
* feat: implemented feature flag * fix: support deprecated enums
This commit is contained in:
@@ -1,13 +1,16 @@
|
||||
import crypto from "crypto";
|
||||
import { FluentBundle } from "fluent/compat";
|
||||
|
||||
import { translate } from "coral-server/services/i18n";
|
||||
|
||||
import {
|
||||
GQLFEATURE_FLAG,
|
||||
GQLReactionConfiguration,
|
||||
GQLStaffConfiguration,
|
||||
} from "coral-server/graph/tenant/schema/__generated__/types";
|
||||
import { translate } from "coral-server/services/i18n";
|
||||
|
||||
import { SSOKey } from "../settings";
|
||||
import { Tenant } from "./tenant";
|
||||
|
||||
export const getDefaultReactionConfiguration = (
|
||||
bundle: FluentBundle
|
||||
@@ -46,3 +49,21 @@ export function generateSSOKey(createdAt: Date): SSOKey {
|
||||
|
||||
return { kid, secret, createdAt };
|
||||
}
|
||||
|
||||
/**
|
||||
* hasFeatureFlag will check to see if the Tenant has a particular feature flag
|
||||
* enabled.
|
||||
*
|
||||
* @param tenant the Tenant to test for a feature flag
|
||||
* @param flag the FEATURE_FLAG to check for
|
||||
*/
|
||||
export function hasFeatureFlag(
|
||||
tenant: Pick<Tenant, "featureFlags">,
|
||||
flag: GQLFEATURE_FLAG
|
||||
) {
|
||||
if (tenant.featureFlags && tenant.featureFlags.includes(flag)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -6,14 +6,16 @@ import { DEFAULT_SESSION_LENGTH } from "coral-common/constants";
|
||||
import { LanguageCode } from "coral-common/helpers/i18n/locales";
|
||||
import { DeepPartial, Omit, Sub } from "coral-common/types";
|
||||
import { dotize } from "coral-common/utils/dotize";
|
||||
import {
|
||||
GQLMODERATION_MODE,
|
||||
GQLSettings,
|
||||
} from "coral-server/graph/tenant/schema/__generated__/types";
|
||||
import { Settings } from "coral-server/models/settings";
|
||||
import { I18n } from "coral-server/services/i18n";
|
||||
import { tenants as collection } from "coral-server/services/mongodb/collections";
|
||||
|
||||
import {
|
||||
GQLFEATURE_FLAG,
|
||||
GQLMODERATION_MODE,
|
||||
GQLSettings,
|
||||
} from "coral-server/graph/tenant/schema/__generated__/types";
|
||||
|
||||
import {
|
||||
generateSSOKey,
|
||||
getDefaultReactionConfiguration,
|
||||
@@ -40,6 +42,11 @@ export interface TenantSettings
|
||||
* locale is the specified locale for this Tenant.
|
||||
*/
|
||||
locale: LanguageCode;
|
||||
|
||||
/**
|
||||
* featureFlags is the set of flags enabled on this Tenant.
|
||||
*/
|
||||
featureFlags?: GQLFEATURE_FLAG[];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -337,3 +344,51 @@ export async function rotateTenantSSOKey(
|
||||
|
||||
return result.value || null;
|
||||
}
|
||||
|
||||
export async function enableTenantFeatureFlag(
|
||||
mongo: Db,
|
||||
id: string,
|
||||
flag: GQLFEATURE_FLAG
|
||||
) {
|
||||
// Update the Tenant.
|
||||
const result = await collection(mongo).findOneAndUpdate(
|
||||
{ id },
|
||||
{
|
||||
// Add the flag to the set of enabled flags.
|
||||
$addToSet: {
|
||||
featureFlags: flag,
|
||||
},
|
||||
},
|
||||
{
|
||||
// False to return the updated document instead of the original
|
||||
// document.
|
||||
returnOriginal: false,
|
||||
}
|
||||
);
|
||||
|
||||
return result.value || null;
|
||||
}
|
||||
|
||||
export async function disableTenantFeatureFlag(
|
||||
mongo: Db,
|
||||
id: string,
|
||||
flag: GQLFEATURE_FLAG
|
||||
) {
|
||||
// Update the Tenant.
|
||||
const result = await collection(mongo).findOneAndUpdate(
|
||||
{ id },
|
||||
{
|
||||
// Pull the flag from the set of enabled flags.
|
||||
$pull: {
|
||||
featureFlags: flag,
|
||||
},
|
||||
},
|
||||
{
|
||||
// False to return the updated document instead of the original
|
||||
// document.
|
||||
returnOriginal: false,
|
||||
}
|
||||
);
|
||||
|
||||
return result.value || null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user