From 136f126296af65f50da598d084d1485c0e40437a Mon Sep 17 00:00:00 2001
From: Mohamed Bassem <me@mbassem.com>
Date: Sun, 27 Apr 2025 00:02:20 +0100
Subject: feat: Implement generic rule engine (#1318)

* Add schema for the new rule engine

* Add rule engine backend logic

* Implement the worker logic and event firing

* Implement the UI changesfor the rule engine

* Ensure that when a referenced list or tag are deleted, the corresponding event/action is

* Dont show smart lists in rule engine events

* Add privacy validations for attached tag and list ids

* Move the rules logic into a models
---
 packages/db/drizzle/0045_add_rule_engine.sql | 33 ++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)
 create mode 100644 packages/db/drizzle/0045_add_rule_engine.sql

(limited to 'packages/db/drizzle/0045_add_rule_engine.sql')

diff --git a/packages/db/drizzle/0045_add_rule_engine.sql b/packages/db/drizzle/0045_add_rule_engine.sql
new file mode 100644
index 00000000..d6d301dd
--- /dev/null
+++ b/packages/db/drizzle/0045_add_rule_engine.sql
@@ -0,0 +1,33 @@
+CREATE TABLE `ruleEngineActions` (
+	`id` text PRIMARY KEY NOT NULL,
+	`userId` text NOT NULL,
+	`ruleId` text NOT NULL,
+	`action` text NOT NULL,
+	`listId` text,
+	`tagId` text,
+	FOREIGN KEY (`userId`) REFERENCES `user`(`id`) ON UPDATE no action ON DELETE cascade,
+	FOREIGN KEY (`ruleId`) REFERENCES `ruleEngineRules`(`id`) ON UPDATE no action ON DELETE cascade,
+	FOREIGN KEY (`userId`,`tagId`) REFERENCES `bookmarkTags`(`userId`,`id`) ON UPDATE no action ON DELETE cascade,
+	FOREIGN KEY (`userId`,`listId`) REFERENCES `bookmarkLists`(`userId`,`id`) ON UPDATE no action ON DELETE cascade
+);
+--> statement-breakpoint
+CREATE INDEX `ruleEngineActions_userId_idx` ON `ruleEngineActions` (`userId`);--> statement-breakpoint
+CREATE INDEX `ruleEngineActions_ruleId_idx` ON `ruleEngineActions` (`ruleId`);--> statement-breakpoint
+CREATE TABLE `ruleEngineRules` (
+	`id` text PRIMARY KEY NOT NULL,
+	`enabled` integer DEFAULT true NOT NULL,
+	`name` text NOT NULL,
+	`description` text,
+	`event` text NOT NULL,
+	`condition` text NOT NULL,
+	`userId` text NOT NULL,
+	`listId` text,
+	`tagId` text,
+	FOREIGN KEY (`userId`) REFERENCES `user`(`id`) ON UPDATE no action ON DELETE cascade,
+	FOREIGN KEY (`userId`,`tagId`) REFERENCES `bookmarkTags`(`userId`,`id`) ON UPDATE no action ON DELETE cascade,
+	FOREIGN KEY (`userId`,`listId`) REFERENCES `bookmarkLists`(`userId`,`id`) ON UPDATE no action ON DELETE cascade
+);
+--> statement-breakpoint
+CREATE INDEX `ruleEngine_userId_idx` ON `ruleEngineRules` (`userId`);--> statement-breakpoint
+CREATE UNIQUE INDEX `bookmarkLists_userId_id_idx` ON `bookmarkLists` (`userId`,`id`);--> statement-breakpoint
+CREATE UNIQUE INDEX `bookmarkTags_userId_id_idx` ON `bookmarkTags` (`userId`,`id`);
\ No newline at end of file
-- 
cgit v1.2.3-70-g09d2

