fix(audit-log): address PR review — nullable workspaceId, enum usage, remove redundant queries

- Make audit_log.workspace_id nullable with ON DELETE SET NULL (logs survive workspace/user deletion)
- Make audit_log.actor_id nullable with ON DELETE SET NULL
- Replace all 53 routes' string literal action/resourceType with AuditAction.X and AuditResourceType.X enums
- Fix empty workspaceId ('') → null for OAuth, form, and org routes to avoid FK violations
- Remove redundant DB queries in chat manage route (use checkChatAccess return data)
- Fix organization routes to pass workspaceId: null instead of organizationId
This commit is contained in:
waleed
2026-02-17 23:21:18 -08:00
parent 0a2d89c049
commit ea6c27698d
65 changed files with 293 additions and 301 deletions

View File

@@ -1,7 +1,7 @@
CREATE TABLE "audit_log" (
"id" text PRIMARY KEY NOT NULL,
"workspace_id" text NOT NULL,
"actor_id" text NOT NULL,
"workspace_id" text,
"actor_id" text,
"action" text NOT NULL,
"resource_type" text NOT NULL,
"resource_id" text,
@@ -15,8 +15,8 @@ CREATE TABLE "audit_log" (
"created_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
ALTER TABLE "audit_log" ADD CONSTRAINT "audit_log_workspace_id_workspace_id_fk" FOREIGN KEY ("workspace_id") REFERENCES "public"."workspace"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "audit_log" ADD CONSTRAINT "audit_log_actor_id_user_id_fk" FOREIGN KEY ("actor_id") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "audit_log" ADD CONSTRAINT "audit_log_workspace_id_workspace_id_fk" FOREIGN KEY ("workspace_id") REFERENCES "public"."workspace"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "audit_log" ADD CONSTRAINT "audit_log_actor_id_user_id_fk" FOREIGN KEY ("actor_id") REFERENCES "public"."user"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
CREATE INDEX "audit_log_workspace_created_idx" ON "audit_log" USING btree ("workspace_id","created_at");--> statement-breakpoint
CREATE INDEX "audit_log_actor_created_idx" ON "audit_log" USING btree ("actor_id","created_at");--> statement-breakpoint
CREATE INDEX "audit_log_resource_idx" ON "audit_log" USING btree ("resource_type","resource_id");--> statement-breakpoint

View File

@@ -1,5 +1,5 @@
{
"id": "a5703ff6-4481-4c0e-abc3-218e55a84376",
"id": "878a3e46-0446-46a9-9577-663699ae0373",
"prevId": "49f580f7-7eba-4431-bdf4-61db0e606546",
"version": "7",
"dialect": "postgresql",
@@ -977,13 +977,13 @@
"name": "workspace_id",
"type": "text",
"primaryKey": false,
"notNull": true
"notNull": false
},
"actor_id": {
"name": "actor_id",
"type": "text",
"primaryKey": false,
"notNull": true
"notNull": false
},
"action": {
"name": "action",
@@ -1141,7 +1141,7 @@
"tableTo": "workspace",
"columnsFrom": ["workspace_id"],
"columnsTo": ["id"],
"onDelete": "cascade",
"onDelete": "set null",
"onUpdate": "no action"
},
"audit_log_actor_id_user_id_fk": {
@@ -1150,7 +1150,7 @@
"tableTo": "user",
"columnsFrom": ["actor_id"],
"columnsTo": ["id"],
"onDelete": "cascade",
"onDelete": "set null",
"onUpdate": "no action"
}
},

View File

@@ -1083,8 +1083,8 @@
{
"idx": 155,
"version": "7",
"when": 1771384748779,
"tag": "0155_lush_legion",
"when": 1771387813205,
"tag": "0155_ambiguous_blade",
"breakpoints": true
}
]

View File

@@ -2030,12 +2030,8 @@ export const auditLog = pgTable(
'audit_log',
{
id: text('id').primaryKey(),
workspaceId: text('workspace_id')
.notNull()
.references(() => workspace.id, { onDelete: 'cascade' }),
actorId: text('actor_id')
.notNull()
.references(() => user.id, { onDelete: 'cascade' }),
workspaceId: text('workspace_id').references(() => workspace.id, { onDelete: 'set null' }),
actorId: text('actor_id').references(() => user.id, { onDelete: 'set null' }),
action: text('action').notNull(),
resourceType: text('resource_type').notNull(),
resourceId: text('resource_id'),