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
11 changes: 9 additions & 2 deletions packages/common/src/rule-engine/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,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,162 @@
-- 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 "WorkflowDefinitionRiskRulePolicy" (
"id" TEXT NOT NULL,
"workflowDefinitionId" TEXT NOT NULL,
"riskRulesPolicyId" TEXT NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,

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

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

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

-- 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 "RiskRuleSet" (
"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,
"ruleId" TEXT,

CONSTRAINT "RiskRuleSet_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,
"operator" TEXT 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 "_riskRuleSetRules" (
"A" TEXT NOT NULL,
"B" TEXT NOT NULL
);

-- CreateIndex
CREATE UNIQUE INDEX "WorkflowDefinitionRiskRulePolicy_workflowDefinitionId_key" ON "WorkflowDefinitionRiskRulePolicy"("workflowDefinitionId");

-- CreateIndex
CREATE INDEX "WorkflowDefinitionRiskRulePolicy_workflowDefinitionId_idx" ON "WorkflowDefinitionRiskRulePolicy"("workflowDefinitionId");

-- CreateIndex
CREATE INDEX "WorkflowDefinitionRiskRulePolicy_riskRulesPolicyId_idx" ON "WorkflowDefinitionRiskRulePolicy"("riskRulesPolicyId");

-- CreateIndex
CREATE UNIQUE INDEX "WorkflowDefinitionRiskRulePolicy_workflowDefinitionId_riskR_key" ON "WorkflowDefinitionRiskRulePolicy"("workflowDefinitionId", "riskRulesPolicyId");

-- CreateIndex
CREATE INDEX "RiskRuleSetRule_riskRuleSetId_idx" ON "RiskRuleSetRule"("riskRuleSetId");

-- CreateIndex
CREATE INDEX "RiskRuleSetRule_ruleId_idx" ON "RiskRuleSetRule"("ruleId");

-- CreateIndex
CREATE UNIQUE INDEX "RiskRuleSetRule_riskRuleSetId_ruleId_key" ON "RiskRuleSetRule"("riskRuleSetId", "ruleId");

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

-- CreateIndex
CREATE INDEX "RiskRuleSet_riskRulePolicyId_idx" ON "RiskRuleSet"("riskRulePolicyId");

-- CreateIndex
CREATE UNIQUE INDEX "RiskRuleSet_id_riskRulePolicyId_key" ON "RiskRuleSet"("id", "riskRulePolicyId");

-- CreateIndex
CREATE UNIQUE INDEX "_riskRuleSetRules_AB_unique" ON "_riskRuleSetRules"("A", "B");

-- CreateIndex
CREATE INDEX "_riskRuleSetRules_B_index" ON "_riskRuleSetRules"("B");

-- AddForeignKey
ALTER TABLE "WorkflowDefinitionRiskRulePolicy" ADD CONSTRAINT "WorkflowDefinitionRiskRulePolicy_riskRulesPolicyId_fkey" FOREIGN KEY ("riskRulesPolicyId") REFERENCES "RiskRulesPolicy"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "WorkflowDefinitionRiskRulePolicy" ADD CONSTRAINT "WorkflowDefinitionRiskRulePolicy_workflowDefinitionId_fkey" FOREIGN KEY ("workflowDefinitionId") REFERENCES "WorkflowDefinition"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "RiskRuleSetRule" ADD CONSTRAINT "RiskRuleSetRule_riskRuleSetId_fkey" FOREIGN KEY ("riskRuleSetId") REFERENCES "RiskRuleSet"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

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

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

-- AddForeignKey
ALTER TABLE "RiskRuleSet" ADD CONSTRAINT "RiskRuleSet_riskRulePolicyId_fkey" FOREIGN KEY ("riskRulePolicyId") REFERENCES "RiskRulesPolicy"("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 "_riskRuleSetRules" ADD CONSTRAINT "_riskRuleSetRules_A_fkey" FOREIGN KEY ("A") REFERENCES "RiskRuleSet"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "_riskRuleSetRules" ADD CONSTRAINT "_riskRuleSetRules_B_fkey" FOREIGN KEY ("B") REFERENCES "Rule"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- Checkers
ALTER TABLE "Rule"
ADD CONSTRAINT "is_rule_public_or_project_id_check"
CHECK (("isPublic" = true AND "projectId" IS NULL) OR ("isPublic" = false AND "projectId" IS NOT NULL));

ALTER TABLE "RiskRuleSet"
ADD CONSTRAINT "is_risk_rule_set_public_or_project_id_check"
CHECK (("isPublic" = true AND "projectId" IS NULL) OR ("isPublic" = false AND "projectId" IS NOT NULL));

ALTER TABLE "RiskRulesPolicy"
ADD CONSTRAINT "is_risk_rule_policy_public_or_project_id_check"
CHECK (("isPublic" = true AND "projectId" IS NULL) OR ("isPublic" = false AND "projectId" IS NOT NULL));
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
Warnings:

- You are about to drop the column `operator` on the `Rule` table. All the data in the column will be lost.
- Added the required column `operation` to the `Rule` table without a default value. This is not possible if the table is not empty.

*/
-- DropIndex
DROP INDEX "WorkflowDefinitionRiskRulePolicy_workflowDefinitionId_key";

-- AlterTable
ALTER TABLE "Rule" DROP COLUMN "operator",
ADD COLUMN "operation" TEXT NOT NULL;
126 changes: 120 additions & 6 deletions services/workflows-service/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -188,12 +188,13 @@ model WorkflowDefinition {
persistStates Json?
submitStates Json?

createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
createdBy String @default("SYSTEM")
project Project? @relation(fields: [projectId], references: [id])
workflowRuntimeData WorkflowRuntimeData[]
uiDefinitions UiDefinition[]
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
createdBy String @default("SYSTEM")
project Project? @relation(fields: [projectId], references: [id])
workflowRuntimeData WorkflowRuntimeData[]
uiDefinitions UiDefinition[]
workflowDefinitionRiskRulePolicy WorkflowDefinitionRiskRulePolicy[]

@@unique([name, version, projectId, definitionType])
@@index([projectId, name, definitionType])
Expand Down Expand Up @@ -373,12 +374,14 @@ model Project {
SalesforceIntegration SalesforceIntegration?
workflowDefinitions WorkflowDefinition[]
uiDefinitions UiDefinition[]
riskRulesPolicies RiskRulesPolicy[]
WorkflowRuntimeDataToken WorkflowRuntimeDataToken[]
TransactionRecord TransactionRecord[]
AlertDefinition AlertDefinition[]
Alert Alert[]
Counterparty Counterparty[]
BusinessReport BusinessReport[]
rules Rule[]

@@unique([name, customerId])
@@index(name)
Expand Down Expand Up @@ -815,6 +818,12 @@ enum RiskCategory {
high
}

enum IndicatorRiskLevel {
positive
moderate
critical
}

enum ComplianceStatus {
compliant
non_compliant
Expand Down Expand Up @@ -864,6 +873,16 @@ enum BusinessReportStatus {
completed
}

enum RuleEngine {
Ballerine
JsonLogic
}

enum RulesetOperator {
and
or
}

model BusinessReport {
id String @id @default(cuid())
type BusinessReportType
Expand All @@ -887,3 +906,98 @@ model BusinessReport {
@@index([riskScore])
@@index([type])
}

model WorkflowDefinitionRiskRulePolicy {
id String @id @default(cuid())
workflowDefinitionId String
riskRulesPolicyId String
riskRulesPolicy RiskRulesPolicy @relation(fields: [riskRulesPolicyId], references: [id])
workflowDefinition WorkflowDefinition @relation(fields: [workflowDefinitionId], references: [id])

createdAt DateTime @default(now())
updatedAt DateTime @updatedAt

@@unique([workflowDefinitionId, riskRulesPolicyId])
@@index([workflowDefinitionId])
@@index([riskRulesPolicyId])
}

model RiskRuleSetRule {
id String @id @default(cuid())
riskRuleSetId String
ruleId String

riskRulesPolicy RiskRuleSet @relation(fields: [riskRuleSetId], references: [id])
rule Rule @relation(fields: [ruleId], references: [id])

createdAt DateTime @default(now())
updatedAt DateTime @updatedAt

@@unique([riskRuleSetId, ruleId])
@@index([riskRuleSetId])
@@index([ruleId])
}

model RiskRulesPolicy {
id String @id @default(cuid())
name String
projectId String
isPublic Boolean @default(false)

riskRuleSets RiskRuleSet[]
workflowDefinitionRiskRulePolicies WorkflowDefinitionRiskRulePolicy[]
project Project @relation(fields: [projectId], references: [id])

createdAt DateTime @default(now())
updatedAt DateTime @updatedAt

@@index([projectId])
}

model RiskRuleSet {
id String @id @default(cuid())
name String
riskRulePolicyId String
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

name

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

operator

operator RulesetOperator
projectId String?
isPublic Boolean @default(false)

domain String
indicator String
riskLevel IndicatorRiskLevel

baseRiskScore Int
additionalRiskScore Int
minRiskScore Int?
maxRiskScore Int?

riskRulePolicy RiskRulesPolicy @relation(fields: [riskRulePolicyId], references: [id])
riskRuleSetRule RiskRuleSetRule[]
rules Rule[] @relation("riskRuleSetRules")

createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
ruleId String?

@@unique([id, riskRulePolicyId])
@@index([riskRulePolicyId])
}

model Rule {
id String @id @default(cuid())
name String
projectId String?
isPublic Boolean @default(false)

key String
operation String
comparisonValue Json
engine RuleEngine

project Project? @relation(fields: [projectId], references: [id])
riskRuleSetRules RiskRuleSetRule[]
riskRuleSets RiskRuleSet[] @relation("riskRuleSetRules")

createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
Loading
Loading