Skip to content

Commit

Permalink
add default rules
Browse files Browse the repository at this point in the history
  • Loading branch information
puncsky committed Jan 18, 2021
1 parent 2a88f68 commit 1377f35
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 13 deletions.
6 changes: 3 additions & 3 deletions src/api-gateway/api-gateway.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ type FlagDetails {
salt: String!
sel: String!
targets: [Target!]!
rules: [Rule!]!
rules: [Rule!]
fallthrough: Fallthrough!
offVariation: Int!
variations: [Boolean!]!
Expand Down Expand Up @@ -102,14 +102,14 @@ type Query {
}

type Rule {
id: ID!
id: ID
variation: Int!
clauses: [Clause!]!
trackEvents: Boolean!
}

input RuleInput {
id: ID!
id: ID
variation: Int!
clauses: [ClauseInput!]!
trackEvents: Boolean!
Expand Down
16 changes: 10 additions & 6 deletions src/api-gateway/resolvers/flag-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
Query,
Resolver,
} from "type-graphql";
import { v4 as uuid } from "uuid";
import { IContext } from "@/api-gateway/api-gateway";
import { ForbiddenError } from "apollo-server-koa";
import { UserWorkspaceModel } from "@/model/user-workspace-model";
Expand Down Expand Up @@ -72,8 +73,8 @@ class Fallthrough {

@ObjectType()
class Rule {
@Field(() => ID)
id: string;
@Field(() => ID, { nullable: true })
id?: string;

@Field(() => Int)
variation: number;
Expand All @@ -87,8 +88,8 @@ class Rule {

@InputType()
class RuleInput {
@Field(() => ID)
id: string;
@Field(() => ID, { nullable: true })
id?: string;

@Field(() => Int)
variation: number;
Expand Down Expand Up @@ -147,7 +148,7 @@ class FlagDetails {
@Field(() => [Target])
targets: Array<Target>;

@Field(() => [Rule])
@Field(() => [Rule], { nullable: true })
rules: Array<Rule>;

@Field(() => Fallthrough)
Expand Down Expand Up @@ -242,7 +243,7 @@ export class FlagResolver {

const [total, flags] = await Promise.all([
flagModel.estimatedDocumentCount({ workspace: workspaceId }),
flagModel.find(query).skip(skip).limit(limit).exec(),
flagModel.find(query).skip(skip).limit(limit).lean(),
]);
for (const f of flags) {
// @ts-ignore
Expand Down Expand Up @@ -291,6 +292,9 @@ export class FlagResolver {
const updated = {} as Record<string, unknown>;

if (rules) {
for (const r of rules) {
r.id = r.id || uuid();
}
updated.rules = rules;
}

Expand Down
4 changes: 2 additions & 2 deletions src/model/flag-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ export class Flag extends TimeStamps {
}>;

@prop()
rules: Array<{
id: string;
rules?: Array<{
id?: string;
variation: number;
clauses: Array<{
attribute: string;
Expand Down
2 changes: 1 addition & 1 deletion src/shared/feature-flags/flags-status-table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const columns: ColumnsType<FlagsStatus_flagsStatus_flags> = [
val = variations[record.offVariation];
} else {
const set = new Set();
for (const r of record.rules) {
for (const r of record.rules || []) {
set.add(variations[r.variation]);
}
set.add(variations[record.fallthrough.variation]);
Expand Down
32 changes: 31 additions & 1 deletion src/shared/flag-details/rules.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,41 @@ export type RulesProps = {
variance: boolean[];
};

const getDefaultRules = (): FlagDetails_flagDetails_rules[] => [
{
id: null,
clauses: [
{
attribute: "email",
op: "endsWith",
values: [],
negate: false,
},
],
variation: 0,
trackEvents: false,
},
{
id: null,
clauses: [
{
attribute: "email",
op: "startsWith",
values: [],
negate: false,
},
],
variation: 0,
trackEvents: false,
},
];

export function Rules({ rules, variance }: RulesProps): JSX.Element {
const conertedRules = rules && rules.length ? rules : getDefaultRules();
return (
<Row gutter={[12, 12]}>
<Col>Target users who match these rules</Col>
{rules.map((r, i) => (
{conertedRules.map((r, i) => (
<Col key={i} span={24}>
<Rule rule={r} variance={variance} index={i} />
</Col>
Expand Down

0 comments on commit 1377f35

Please sign in to comment.