Email for professionals and teams
feat(db): add routing rule scopes and webhook delivery attempt tracking
Oday Bakkour07e3bf7ff757
diff --git a/drizzle/migrations/0021_add_advanced_routing_and_webhook_retries.sql b/drizzle/migrations/0021_add_advanced_routing_and_webhook_retries.sqlnew file mode 100644index 0000000..267c074--- /dev/null+++ b/drizzle/migrations/0021_add_advanced_routing_and_webhook_retries.sql@@ -0,0 +1,22 @@+-- Advanced routing rules: domain-scoped catch-all, forward, and reject/block rules with priorities.+ALTER TABLE `routing_rules` ADD `scope` text DEFAULT 'mailbox' NOT NULL;--> statement-breakpoint+ALTER TABLE `routing_rules` ADD `name` text;--> statement-breakpoint+ALTER TABLE `routing_rules` ADD `enabled` integer DEFAULT true NOT NULL;--> statement-breakpoint+ALTER TABLE `routing_rules` ADD `keep_copy` integer DEFAULT false NOT NULL;--> statement-breakpoint+ALTER TABLE `routing_rules` ADD `reject_reason` text;--> statement-breakpoint+ALTER TABLE `routing_rules` ADD `last_matched_at` integer;--> statement-breakpoint+ALTER TABLE `routing_rules` ADD `match_count` integer DEFAULT 0 NOT NULL;--> statement-breakpoint+CREATE INDEX `routing_rules_domain_scope_idx` ON `routing_rules` (`domain_id`,`scope`,`enabled`);--> statement-breakpoint+CREATE INDEX `routing_rules_mailbox_idx` ON `routing_rules` (`mailbox_id`);--> statement-breakpoint+CREATE INDEX `routing_rules_priority_idx` ON `routing_rules` (`priority`);--> statement-breakpoint++-- Webhook management and delivery retry visibility.+ALTER TABLE `webhooks` ADD `description` text;--> statement-breakpoint+ALTER TABLE `webhooks` ADD `max_attempts` integer DEFAULT 5 NOT NULL;--> statement-breakpoint+ALTER TABLE `webhook_deliveries` ADD `response_status` integer;--> statement-breakpoint+ALTER TABLE `webhook_deliveries` ADD `error` text;--> statement-breakpoint+ALTER TABLE `webhook_deliveries` ADD `duration_ms` integer;--> statement-breakpoint+ALTER TABLE `webhook_deliveries` ADD `last_attempt_at` integer;--> statement-breakpoint+ALTER TABLE `webhook_deliveries` ADD `next_retry_at` integer;--> statement-breakpoint+CREATE INDEX `webhook_deliveries_webhook_idx` ON `webhook_deliveries` (`webhook_id`,`created_at`);--> statement-breakpoint+CREATE INDEX `webhook_deliveries_status_idx` ON `webhook_deliveries` (`status`);diff --git a/src/db/schema/index.ts b/src/db/schema/index.tsindex ea96166..64f1458 100644--- a/src/db/schema/index.ts+++ b/src/db/schema/index.ts@@ -260,55 +260,96 @@ export const calendarEvents = sqliteTable((t) => [index("calendar_events_user_starts_idx").on(t.userId, t.startsAt)],);-export const routingRules = sqliteTable("routing_rules", {- id: text("id").primaryKey(),- userId: text("user_id")- .notNull()- .references(() => users.id, { onDelete: "cascade" }),- domainId: text("domain_id")- .notNull()- .references(() => domains.id, { onDelete: "cascade" }),- pattern: text("pattern").notNull(),- matchField: text("match_field", { enum: ["email", "content", "title"] }).notNull().default("email"),- matchOperator: text("match_operator", { enum: ["contains", "exact"] }).notNull().default("contains"),- matchValue: text("match_value").notNull().default(""),- mailboxId: text("mailbox_id").references(() => mailboxes.id, { onDelete: "set null" }),- folderId: text("folder_id").references(() => folders.id, { onDelete: "set null" }),- action: text("action", { enum: ["store", "forward", "reject", "spam", "trash"] }).notNull().default("store"),- forwardTo: text("forward_to"),- priority: integer("priority").notNull().default(0),- createdAt: integer("created_at", { mode: "timestamp" })- .notNull()- .$defaultFn(() => new Date()),-});+export const routingRules = sqliteTable(+ "routing_rules",+ {+ id: text("id").primaryKey(),+ userId: text("user_id")+ .notNull()+ .references(() => users.id, { onDelete: "cascade" }),+ domainId: text("domain_id")+ .notNull()+ .references(() => domains.id, { onDelete: "cascade" }),+ // "mailbox" rules run after delivery and file the message into a folder or system status.+ // "domain" rules run during address resolution and can catch-all, forward, or reject.+ scope: text("scope", { enum: ["mailbox", "domain"] }).notNull().default("mailbox"),+ name: text("name"),+ enabled: integer("enabled", { mode: "boolean" }).notNull().default(true),+ pattern: text("pattern").notNull(),+ matchField: text("match_field", {+ enum: ["email", "content", "title", "sender", "recipient"],+ })+ .notNull()+ .default("email"),+ matchOperator: text("match_operator", {+ enum: ["contains", "exact", "starts_with", "ends_with", "regex"],+ })+ .notNull()+ .default("contains"),+ matchValue: text("match_value").notNull().default(""),+ mailboxId: text("mailbox_id").references(() => mailboxes.id, { onDelete: "set null" }),+ folderId: text("folder_id").references(() => folders.id, { onDelete: "set null" }),+ action: text("action", { enum: ["store", "forward", "reject", "spam", "trash"] }).notNull().default("store"),+ forwardTo: text("forward_to"),+ // Forward actions drop the message by default; keepCopy also delivers it to the mailbox.+ keepCopy: integer("keep_copy", { mode: "boolean" }).notNull().default(false),+ rejectReason: text("reject_reason"),+ priority: integer("priority").notNull().default(0),+ lastMatchedAt: integer("last_matched_at", { mode: "timestamp" }),+ matchCount: integer("match_count").notNull().default(0),+ createdAt: integer("created_at", { mode: "timestamp" })+ .notNull()+ .$defaultFn(() => new Date()),+ },+ (t) => [+ index("routing_rules_domain_scope_idx").on(t.domainId, t.scope, t.enabled),+ index("routing_rules_mailbox_idx").on(t.mailboxId),+ index("routing_rules_priority_idx").on(t.priority),+ ],+);export const webhooks = sqliteTable("webhooks", {id: text("id").primaryKey(),userId: text("user_id").notNull().references(() => users.id, { onDelete: "cascade" }),+ description: text("description"),url: text("url").notNull(),secret: text("secret").notNull(),events: text("events").notNull(),enabled: integer("enabled", { mode: "boolean" }).notNull().default(true),+ maxAttempts: integer("max_attempts").notNull().default(5),createdAt: integer("created_at", { mode: "timestamp" }).notNull().$defaultFn(() => new Date()),});-export const webhookDeliveries = sqliteTable("webhook_deliveries", {- id: text("id").primaryKey(),- webhookId: text("webhook_id")- .notNull()- .references(() => webhooks.id, { onDelete: "cascade" }),- eventType: text("event_type").notNull(),- payload: text("payload").notNull(),- status: text("status").notNull().default("pending"),- attempts: integer("attempts").notNull().default(0),- createdAt: integer("created_at", { mode: "timestamp" })- .notNull()- .$defaultFn(() => new Date()),-});+export const webhookDeliveries = sqliteTable(+ "webhook_deliveries",+ {+ id: text("id").primaryKey(),+ webhookId: text("webhook_id")+ .notNull()+ .references(() => webhooks.id, { onDelete: "cascade" }),+ eventType: text("event_type").notNull(),+ payload: text("payload").notNull(),+ // pending | delivered | failed | retrying | exhausted+ status: text("status").notNull().default("pending"),+ attempts: integer("attempts").notNull().default(0),+ responseStatus: integer("response_status"),+ error: text("error"),+ durationMs: integer("duration_ms"),+ lastAttemptAt: integer("last_attempt_at", { mode: "timestamp" }),+ nextRetryAt: integer("next_retry_at", { mode: "timestamp" }),+ createdAt: integer("created_at", { mode: "timestamp" })+ .notNull()+ .$defaultFn(() => new Date()),+ },+ (t) => [+ index("webhook_deliveries_webhook_idx").on(t.webhookId, t.createdAt),+ index("webhook_deliveries_status_idx").on(t.status),+ ],+);export const sessions = sqliteTable("sessions", {id: text("id").primaryKey(),diff --git a/src/lib/setup/migration.ts b/src/lib/setup/migration.tsindex b5819e4..4170984 100644--- a/src/lib/setup/migration.ts+++ b/src/lib/setup/migration.ts@@ -19,6 +19,7 @@ const MIGRATION_NAMES = ["0018_add_mailbox_domain_aliases.sql","0019_merge_message_bodies.sql","0020_add_calendar_templates_schedule.sql",+ "0021_add_advanced_routing_and_webhook_retries.sql",];const INITIAL_SCHEMA_SQL = `@@ -52,9 +53,14 @@ CREATE TABLE IF NOT EXISTS email_templates (id text PRIMARY KEY NOT NULL, user_iCREATE INDEX IF NOT EXISTS email_templates_user_idx ON email_templates(user_id);CREATE TABLE IF NOT EXISTS calendar_events (id text PRIMARY KEY NOT NULL, user_id text NOT NULL REFERENCES users(id) ON DELETE cascade, mailbox_id text REFERENCES mailboxes(id) ON DELETE set null, title text NOT NULL, description text DEFAULT '' NOT NULL, location text DEFAULT '' NOT NULL, attendees text DEFAULT '[]' NOT NULL, starts_at integer NOT NULL, ends_at integer NOT NULL, created_at integer NOT NULL, updated_at integer NOT NULL);CREATE INDEX IF NOT EXISTS calendar_events_user_starts_idx ON calendar_events(user_id, starts_at);-CREATE TABLE IF NOT EXISTS routing_rules (id text PRIMARY KEY NOT NULL, user_id text NOT NULL REFERENCES users(id) ON DELETE cascade, domain_id text NOT NULL REFERENCES domains(id) ON DELETE cascade, pattern text NOT NULL, match_field text DEFAULT 'email' NOT NULL, match_operator text DEFAULT 'contains' NOT NULL, match_value text DEFAULT '' NOT NULL, mailbox_id text REFERENCES mailboxes(id) ON DELETE set null, folder_id text REFERENCES folders(id) ON DELETE set null, action text DEFAULT 'store' NOT NULL, forward_to text, priority integer DEFAULT 0 NOT NULL, created_at integer NOT NULL);-CREATE TABLE IF NOT EXISTS webhooks (id text PRIMARY KEY NOT NULL, user_id text NOT NULL REFERENCES users(id) ON DELETE cascade, url text NOT NULL, secret text NOT NULL, events text NOT NULL, enabled integer DEFAULT true NOT NULL, created_at integer NOT NULL);-CREATE TABLE IF NOT EXISTS webhook_deliveries (id text PRIMARY KEY NOT NULL, webhook_id text NOT NULL REFERENCES webhooks(id) ON DELETE cascade, event_type text NOT NULL, payload text NOT NULL, status text DEFAULT 'pending' NOT NULL, attempts integer DEFAULT 0 NOT NULL, created_at integer NOT NULL);+CREATE TABLE IF NOT EXISTS routing_rules (id text PRIMARY KEY NOT NULL, user_id text NOT NULL REFERENCES users(id) ON DELETE cascade, domain_id text NOT NULL REFERENCES domains(id) ON DELETE cascade, scope text DEFAULT 'mailbox' NOT NULL, name text, enabled integer DEFAULT true NOT NULL, pattern text NOT NULL, match_field text DEFAULT 'email' NOT NULL, match_operator text DEFAULT 'contains' NOT NULL, match_value text DEFAULT '' NOT NULL, mailbox_id text REFERENCES mailboxes(id) ON DELETE set null, folder_id text REFERENCES folders(id) ON DELETE set null, action text DEFAULT 'store' NOT NULL, forward_to text, keep_copy integer DEFAULT false NOT NULL, reject_reason text, priority integer DEFAULT 0 NOT NULL, last_matched_at integer, match_count integer DEFAULT 0 NOT NULL, created_at integer NOT NULL);+CREATE INDEX IF NOT EXISTS routing_rules_domain_scope_idx ON routing_rules(domain_id, scope, enabled);+CREATE INDEX IF NOT EXISTS routing_rules_mailbox_idx ON routing_rules(mailbox_id);+CREATE INDEX IF NOT EXISTS routing_rules_priority_idx ON routing_rules(priority);+CREATE TABLE IF NOT EXISTS webhooks (id text PRIMARY KEY NOT NULL, user_id text NOT NULL REFERENCES users(id) ON DELETE cascade, description text, url text NOT NULL, secret text NOT NULL, events text NOT NULL, enabled integer DEFAULT true NOT NULL, max_attempts integer DEFAULT 5 NOT NULL, created_at integer NOT NULL);+CREATE TABLE IF NOT EXISTS webhook_deliveries (id text PRIMARY KEY NOT NULL, webhook_id text NOT NULL REFERENCES webhooks(id) ON DELETE cascade, event_type text NOT NULL, payload text NOT NULL, status text DEFAULT 'pending' NOT NULL, attempts integer DEFAULT 0 NOT NULL, response_status integer, error text, duration_ms integer, last_attempt_at integer, next_retry_at integer, created_at integer NOT NULL);+CREATE INDEX IF NOT EXISTS webhook_deliveries_webhook_idx ON webhook_deliveries(webhook_id, created_at);+CREATE INDEX IF NOT EXISTS webhook_deliveries_status_idx ON webhook_deliveries(status);CREATE TABLE IF NOT EXISTS sessions (id text PRIMARY KEY NOT NULL, user_id text NOT NULL REFERENCES users(id) ON DELETE cascade, token_hash text NOT NULL UNIQUE, expires_at integer NOT NULL, created_at integer NOT NULL);CREATE TABLE IF NOT EXISTS audit_logs (id text PRIMARY KEY NOT NULL, actor_user_id text REFERENCES users(id) ON DELETE set null, target_user_id text REFERENCES users(id) ON DELETE set null, mailbox_id text REFERENCES mailboxes(id) ON DELETE set null, message_id text REFERENCES messages(id) ON DELETE set null, action text NOT NULL, metadata text, created_at integer NOT NULL);CREATE INDEX IF NOT EXISTS audit_logs_actor_idx ON audit_logs(actor_user_id);