Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/implement risk rule ( WIP ) BAL-2445 #2528

Open
wants to merge 10 commits into
base: dev
Choose a base branch
from
1 change: 1 addition & 0 deletions packages/common/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export type {
TDefaultSchemaDocumentPage,
TDocument,
} from './schemas';

export type { AnyRecord, LoggerInterface, Serializable, SortDirection } from './types';

export {
Expand Down
1 change: 1 addition & 0 deletions packages/common/src/rule-engine/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export type {
FailedRuleResult,
PassedRuleResult,
RuleResult,
RuleSetWithChildren,
TFindAllRulesOptions,
} from './types';

Expand Down
18 changes: 16 additions & 2 deletions packages/common/src/rule-engine/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Rule, RuleSet } from './rules/types';
import { EngineErrors } from './errors';
import { TOperator } from '@/rule-engine/operators/types';

export type PassedRuleResult = {
status: 'PASSED' | 'SKIPPED';
Expand All @@ -8,6 +9,12 @@ export type PassedRuleResult = {
rules?: Rule | RuleSet; // 'rules' should be present
};

export type RuleSetWithChildren = {
rules: Rule[];
operator: TOperator;
childRuleSet: RuleSetWithChildren[];
};

export type FailedRuleResult = {
status: 'FAILED';
message?: string;
Expand All @@ -18,10 +25,17 @@ export type RuleResult = PassedRuleResult | FailedRuleResult;

export type RuleResultSet = RuleResult[];

export interface TFindAllRulesOptions {
export type TNotionRulesOptions = {
databaseId: string;
source: 'notion';
}
};

export type TDatabaseRulesOptions = {
policyId: string;
source: 'database';
};

export type TFindAllRulesOptions = TNotionRulesOptions | TDatabaseRulesOptions;

export * from './operators/types';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ export class RiskRulePlugin {
logger.error(`Risk Rules Plugin - Failed`, {
context,
name: this.name,
databaseId: this.rulesSource.databaseId,
databaseId:
this.rulesSource.source === 'notion'
? this.rulesSource.databaseId
: this.rulesSource.policyId,
source: this.rulesSource.source,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,7 @@ export interface IterativePluginParams {

export interface RiskRulesPluginParams {
name: string;
rulesSource: {
source: 'notion';
databaseId: string;
};
rulesSource: TFindAllRulesOptions;
stateNames: string[];
successAction?: string;
errorAction?: string;
Expand Down
2 changes: 1 addition & 1 deletion services/workflows-service/prisma/data-migrations
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
-- CreateEnum
CREATE TYPE "IndicatorRiskLevel" AS ENUM ('positive', 'moderate', 'critical');

-- CreateEnum
CREATE TYPE "RuleEngine" AS ENUM ('Ballerine', 'JsonLogic');

-- CreateEnum
CREATE TYPE "RulesetOperator" AS ENUM ('and', 'or');

-- CreateTable
CREATE TABLE "RiskRulesPolicy" (
"id" TEXT NOT NULL,
"name" TEXT NOT NULL,
"projectId" TEXT NOT NULL,
"isPublic" BOOLEAN NOT NULL DEFAULT false,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,

CONSTRAINT "RiskRulesPolicy_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "RiskRule" (
"id" TEXT NOT NULL,
"name" TEXT NOT NULL,
"riskRulePolicyId" TEXT NOT NULL,
"operator" "RulesetOperator" NOT NULL,
"projectId" TEXT,
"isPublic" BOOLEAN NOT NULL DEFAULT false,
"domain" TEXT NOT NULL,
"indicator" TEXT NOT NULL,
"riskLevel" "IndicatorRiskLevel" NOT NULL,
"baseRiskScore" INTEGER NOT NULL,
"additionalRiskScore" INTEGER NOT NULL,
"minRiskScore" INTEGER,
"maxRiskScore" INTEGER,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,

CONSTRAINT "RiskRule_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "RiskRuleRuleSet" (
"id" TEXT NOT NULL,
"riskRuleId" TEXT NOT NULL,
"ruleSetId" TEXT NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,

CONSTRAINT "RiskRuleRuleSet_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "RuleSet" (
"id" TEXT NOT NULL,
"name" TEXT NOT NULL,
"projectId" TEXT,
"isPublic" BOOLEAN NOT NULL DEFAULT false,
"operator" "RulesetOperator" NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,

CONSTRAINT "RuleSet_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "RuleSetToRuleSet" (
"id" TEXT NOT NULL,
"parentId" TEXT NOT NULL,
"childId" TEXT NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,

CONSTRAINT "RuleSetToRuleSet_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "RuleSetRule" (
"id" TEXT NOT NULL,
"ruleId" TEXT NOT NULL,
"ruleSetId" TEXT NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,

CONSTRAINT "RuleSetRule_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "Rule" (
"id" TEXT NOT NULL,
"name" TEXT NOT NULL,
"projectId" TEXT,
"isPublic" BOOLEAN NOT NULL DEFAULT false,
"key" TEXT NOT NULL,
"operation" TEXT NOT NULL,
"value" JSONB NOT NULL,
"comparisonValue" JSONB NOT NULL,
"engine" "RuleEngine" NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,

CONSTRAINT "Rule_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "_RiskRuleSetToRuleSet" (
"A" TEXT NOT NULL,
"B" TEXT NOT NULL
);

-- CreateIndex
CREATE INDEX "RiskRulesPolicy_projectId_idx" ON "RiskRulesPolicy"("projectId");

-- CreateIndex
CREATE INDEX "RiskRule_riskRulePolicyId_idx" ON "RiskRule"("riskRulePolicyId");

-- CreateIndex
CREATE UNIQUE INDEX "RiskRule_id_riskRulePolicyId_key" ON "RiskRule"("id", "riskRulePolicyId");

-- CreateIndex
CREATE INDEX "RiskRuleRuleSet_riskRuleId_idx" ON "RiskRuleRuleSet"("riskRuleId");

-- CreateIndex
CREATE INDEX "RiskRuleRuleSet_ruleSetId_idx" ON "RiskRuleRuleSet"("ruleSetId");

-- CreateIndex
CREATE UNIQUE INDEX "RiskRuleRuleSet_riskRuleId_ruleSetId_key" ON "RiskRuleRuleSet"("riskRuleId", "ruleSetId");

-- CreateIndex
CREATE INDEX "RuleSetToRuleSet_parentId_idx" ON "RuleSetToRuleSet"("parentId");

-- CreateIndex
CREATE INDEX "RuleSetToRuleSet_childId_idx" ON "RuleSetToRuleSet"("childId");

-- CreateIndex
CREATE UNIQUE INDEX "RuleSetToRuleSet_parentId_childId_key" ON "RuleSetToRuleSet"("parentId", "childId");

-- CreateIndex
CREATE INDEX "RuleSetRule_ruleId_idx" ON "RuleSetRule"("ruleId");

-- CreateIndex
CREATE INDEX "RuleSetRule_ruleSetId_idx" ON "RuleSetRule"("ruleSetId");

-- CreateIndex
CREATE UNIQUE INDEX "RuleSetRule_ruleId_ruleSetId_key" ON "RuleSetRule"("ruleId", "ruleSetId");

-- CreateIndex
CREATE UNIQUE INDEX "_RiskRuleSetToRuleSet_AB_unique" ON "_RiskRuleSetToRuleSet"("A", "B");

-- CreateIndex
CREATE INDEX "_RiskRuleSetToRuleSet_B_index" ON "_RiskRuleSetToRuleSet"("B");

-- AddForeignKey
ALTER TABLE "RiskRulesPolicy" ADD CONSTRAINT "RiskRulesPolicy_projectId_fkey" FOREIGN KEY ("projectId") REFERENCES "Project"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "RiskRule" ADD CONSTRAINT "RiskRule_riskRulePolicyId_fkey" FOREIGN KEY ("riskRulePolicyId") REFERENCES "RiskRulesPolicy"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "RiskRuleRuleSet" ADD CONSTRAINT "RiskRuleRuleSet_riskRuleId_fkey" FOREIGN KEY ("riskRuleId") REFERENCES "RiskRule"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "RiskRuleRuleSet" ADD CONSTRAINT "RiskRuleRuleSet_ruleSetId_fkey" FOREIGN KEY ("ruleSetId") REFERENCES "RuleSet"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "RuleSetToRuleSet" ADD CONSTRAINT "RuleSetToRuleSet_parentId_fkey" FOREIGN KEY ("parentId") REFERENCES "RuleSet"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "RuleSetToRuleSet" ADD CONSTRAINT "RuleSetToRuleSet_childId_fkey" FOREIGN KEY ("childId") REFERENCES "RuleSet"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "RuleSetRule" ADD CONSTRAINT "RuleSetRule_ruleId_fkey" FOREIGN KEY ("ruleId") REFERENCES "Rule"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "RuleSetRule" ADD CONSTRAINT "RuleSetRule_ruleSetId_fkey" FOREIGN KEY ("ruleSetId") REFERENCES "RuleSet"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Rule" ADD CONSTRAINT "Rule_projectId_fkey" FOREIGN KEY ("projectId") REFERENCES "Project"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "_RiskRuleSetToRuleSet" ADD CONSTRAINT "_RiskRuleSetToRuleSet_A_fkey" FOREIGN KEY ("A") REFERENCES "RiskRule"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "_RiskRuleSetToRuleSet" ADD CONSTRAINT "_RiskRuleSetToRuleSet_B_fkey" FOREIGN KEY ("B") REFERENCES "RuleSet"("id") ON DELETE CASCADE ON UPDATE CASCADE;
Loading
Loading