KI-AGENT: Mailantworten und Entitätsverknüpfungen ergänzen

This commit is contained in:
2026-09-07 19:00:08 +02:00
parent 4e4466ff11
commit f3384cfc74
10 changed files with 774 additions and 30 deletions

View File

@@ -0,0 +1,27 @@
CREATE TABLE IF NOT EXISTS "email_entity_links" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"tenant_id" bigint NOT NULL,
"message_id" uuid NOT NULL,
"entity_type" text NOT NULL,
"entity_id" bigint NOT NULL,
"linked_by" uuid,
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
CONSTRAINT "email_entity_links_tenant_id_tenants_id_fk"
FOREIGN KEY ("tenant_id") REFERENCES "public"."tenants"("id")
ON DELETE cascade ON UPDATE cascade,
CONSTRAINT "email_entity_links_message_id_email_messages_id_fk"
FOREIGN KEY ("message_id") REFERENCES "public"."email_messages"("id")
ON DELETE cascade ON UPDATE cascade,
CONSTRAINT "email_entity_links_linked_by_auth_users_id_fk"
FOREIGN KEY ("linked_by") REFERENCES "public"."auth_users"("id")
ON DELETE set null ON UPDATE cascade
);
CREATE UNIQUE INDEX IF NOT EXISTS "email_entity_links_message_entity_key"
ON "email_entity_links" USING btree ("message_id", "entity_type", "entity_id");
CREATE INDEX IF NOT EXISTS "email_entity_links_entity_idx"
ON "email_entity_links" USING btree ("tenant_id", "entity_type", "entity_id");
CREATE INDEX IF NOT EXISTS "email_entity_links_message_idx"
ON "email_entity_links" USING btree ("message_id");

View File

@@ -442,6 +442,13 @@
"when": 1788202715001,
"tag": "0065_document_templates",
"breakpoints": true
},
{
"idx": 63,
"version": "7",
"when": 1788799700000,
"tag": "0066_email_entity_links",
"breakpoints": true
}
]
}

View File

@@ -155,6 +155,39 @@ export const emailAttachments = pgTable(
}),
)
export const emailEntityLinks = pgTable(
"email_entity_links",
{
id: uuid("id").primaryKey().defaultRandom(),
tenantId: bigint("tenant_id", { mode: "number" })
.notNull()
.references(() => tenants.id, { onDelete: "cascade", onUpdate: "cascade" }),
messageId: uuid("message_id")
.notNull()
.references(() => emailMessages.id, { onDelete: "cascade", onUpdate: "cascade" }),
entityType: text("entity_type").notNull(),
entityId: bigint("entity_id", { mode: "number" }).notNull(),
linkedBy: uuid("linked_by")
.references(() => authUsers.id, { onDelete: "set null", onUpdate: "cascade" }),
createdAt: timestamp("created_at", { withTimezone: true })
.notNull()
.defaultNow(),
},
(table) => ({
messageEntityKey: uniqueIndex("email_entity_links_message_entity_key")
.on(table.messageId, table.entityType, table.entityId),
entityIdx: index("email_entity_links_entity_idx")
.on(table.tenantId, table.entityType, table.entityId),
messageIdx: index("email_entity_links_message_idx")
.on(table.messageId),
}),
)
export const emailSyncState = pgTable(
"email_sync_state",
{
@@ -204,5 +237,7 @@ export type EmailMessageBody = typeof emailMessageBodies.$inferSelect
export type NewEmailMessageBody = typeof emailMessageBodies.$inferInsert
export type EmailAttachment = typeof emailAttachments.$inferSelect
export type NewEmailAttachment = typeof emailAttachments.$inferInsert
export type EmailEntityLink = typeof emailEntityLinks.$inferSelect
export type NewEmailEntityLink = typeof emailEntityLinks.$inferInsert
export type EmailSyncState = typeof emailSyncState.$inferSelect
export type NewEmailSyncState = typeof emailSyncState.$inferInsert