Skip to content

Commit

Permalink
restore feature flag logic so it can be used in future
Browse files Browse the repository at this point in the history
  • Loading branch information
Garma00 committed Nov 25, 2022
1 parent 2d194d3 commit 1ee9436
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 0 deletions.
74 changes: 74 additions & 0 deletions utils/__tests__/featureFlag.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import {
FeatureFlagEnum,
getIsUserEligibleForNewFeature
} from "../featureFlag";
import { FiscalCode } from "@pagopa/ts-commons/lib/strings";
import { aFiscalCode, anotherFiscalCode } from "../../__mocks__/mocks";

const betaUsers: FiscalCode[] = [aFiscalCode];
const isUserBeta = (fc: FiscalCode) => betaUsers.includes(fc);

describe("isUserForFeatureFlag", () => {
it("should return true when featureFlag === all", () => {
const isUserForFeatureFlag = getIsUserEligibleForNewFeature(
isUserBeta,
_ => false,
FeatureFlagEnum.ALL
);
expect(isUserForFeatureFlag(aFiscalCode)).toBeTruthy();
});

it("should return false when featureFlag === beta and the user is not beta", () => {
const isUserForFeatureFlag = getIsUserEligibleForNewFeature(
isUserBeta,
_ => false,
FeatureFlagEnum.BETA
);
expect(isUserForFeatureFlag(anotherFiscalCode)).toBeFalsy();
});

it("should return true when featureFlag === beta and the first callback return true", () => {
const isUserForFeatureFlag = getIsUserEligibleForNewFeature(
isUserBeta,
_ => false,
FeatureFlagEnum.BETA
);
expect(isUserForFeatureFlag(aFiscalCode)).toBeTruthy();
});

it("should return false when featureFlag === canary and callbacks return false", () => {
const isUserForFeatureFlag = getIsUserEligibleForNewFeature(
isUserBeta,
_ => false,
FeatureFlagEnum.CANARY
);
expect(isUserForFeatureFlag(anotherFiscalCode)).toBeFalsy();
});

it("should return true when featureFlag === canary and the first callback return true", () => {
const isUserForFeatureFlag = getIsUserEligibleForNewFeature(
isUserBeta,
_ => false,
FeatureFlagEnum.CANARY
);
expect(isUserForFeatureFlag(aFiscalCode)).toBeTruthy();
});

it("should return true when featureFlag === canary and the second callback return true", () => {
const isUserForFeatureFlag = getIsUserEligibleForNewFeature(
isUserBeta,
_ => true,
FeatureFlagEnum.CANARY
);
expect(isUserForFeatureFlag(anotherFiscalCode)).toBeTruthy();
});

it("should return false when featureFlag === none", () => {
const isUserForFeatureFlag = getIsUserEligibleForNewFeature(
isUserBeta,
_ => true,
FeatureFlagEnum.NONE
);
expect(isUserForFeatureFlag(aFiscalCode)).toBeFalsy();
});
});
3 changes: 3 additions & 0 deletions utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
} from "@pagopa/ts-commons/lib/numbers";
import { flow, pipe } from "fp-ts/lib/function";
import { CommaSeparatedListOf } from "./comma-separated-list";
import { FeatureFlag, FeatureFlagEnum } from "./featureFlag";

export const BetaUsers = t.readonlyArray(FiscalCode);
export type BetaUsers = t.TypeOf<typeof BetaUsers>;
Expand Down Expand Up @@ -76,6 +77,8 @@ export const IConfig = t.intersection([

EMAIL_NOTIFICATION_SERVICE_BLACKLIST: CommaSeparatedListOf(ServiceId),

FEATURE_FLAG: withFallback(FeatureFlag, FeatureFlagEnum.NONE),

WEBHOOK_NOTIFICATION_SERVICE_BLACKLIST: CommaSeparatedListOf(ServiceId),
// eslint-disable-next-line sort-keys
IO_FUNCTIONS_ADMIN_API_TOKEN: NonEmptyString,
Expand Down
35 changes: 35 additions & 0 deletions utils/featureFlag.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { enumType } from "@pagopa/ts-commons/lib/types";
import * as t from "io-ts";

export enum FeatureFlagEnum {
ALL = "ALL",
BETA = "BETA",
CANARY = "CANARY",
NONE = "NONE"
}

export const FeatureFlag = enumType<FeatureFlagEnum>(
FeatureFlagEnum,
"FeatureFlag"
);

export type FeatureFlag = t.TypeOf<typeof FeatureFlag>;

export const getIsUserEligibleForNewFeature = <T>(
isUserBeta: (i: T) => boolean,
isUserCanary: (i: T) => boolean,
featureFlag: FeatureFlag
): ((i: T) => boolean) => (i): boolean => {
switch (featureFlag) {
case "ALL":
return true;
case "BETA":
return isUserBeta(i);
case "CANARY":
return isUserCanary(i) || isUserBeta(i);
case "NONE":
return false;
default:
return false;
}
};

0 comments on commit 1ee9436

Please sign in to comment.