Deleting a reminder that had already fired failed with FK violation 'reminder_runs_reminder_id_reminders_id_fk'. Add ON DELETE CASCADE so the run history is removed alongside its reminder. reminder_run_targets cascades on run_id (already), so the chain is: reminder → reminder_runs → reminder_run_targets, all removed in one go.
164 lines
6.3 KiB
TypeScript
164 lines
6.3 KiB
TypeScript
import {
|
|
pgTable,
|
|
uuid,
|
|
text,
|
|
bigint,
|
|
integer,
|
|
boolean,
|
|
timestamp,
|
|
jsonb,
|
|
primaryKey,
|
|
uniqueIndex,
|
|
inet,
|
|
} from "drizzle-orm/pg-core";
|
|
|
|
export const operators = pgTable(
|
|
"operators",
|
|
{
|
|
id: uuid("id").primaryKey().defaultRandom(),
|
|
telegramUserId: bigint("telegram_user_id", { mode: "number" }).notNull(),
|
|
displayName: text("display_name").notNull(),
|
|
role: text("role").notNull().default("admin"),
|
|
defaultTimezone: text("default_timezone").notNull().default("Asia/Kuala_Lumpur"),
|
|
createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(),
|
|
},
|
|
(t) => ({
|
|
telegramUserIdUnique: uniqueIndex("operators_telegram_user_id_uq").on(t.telegramUserId),
|
|
}),
|
|
);
|
|
|
|
export const whatsappAccounts = pgTable(
|
|
"whatsapp_accounts",
|
|
{
|
|
id: uuid("id").primaryKey().defaultRandom(),
|
|
operatorId: uuid("operator_id").notNull().references(() => operators.id),
|
|
label: text("label").notNull(),
|
|
phoneNumber: text("phone_number"),
|
|
status: text("status").notNull().default("pending"),
|
|
lastConnectedAt: timestamp("last_connected_at", { withTimezone: true }),
|
|
lastQrAt: timestamp("last_qr_at", { withTimezone: true }),
|
|
createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(),
|
|
},
|
|
(t) => ({
|
|
operatorLabelUnique: uniqueIndex("whatsapp_accounts_operator_label_uq").on(t.operatorId, t.label),
|
|
}),
|
|
);
|
|
|
|
export const whatsappGroups = pgTable(
|
|
"whatsapp_groups",
|
|
{
|
|
id: uuid("id").primaryKey().defaultRandom(),
|
|
accountId: uuid("account_id").notNull().references(() => whatsappAccounts.id),
|
|
waGroupJid: text("wa_group_jid").notNull(),
|
|
name: text("name").notNull(),
|
|
participantCount: integer("participant_count").notNull().default(0),
|
|
isArchived: boolean("is_archived").notNull().default(false),
|
|
lastSyncedAt: timestamp("last_synced_at", { withTimezone: true }).notNull().defaultNow(),
|
|
},
|
|
(t) => ({
|
|
accountJidUnique: uniqueIndex("whatsapp_groups_account_jid_uq").on(t.accountId, t.waGroupJid),
|
|
}),
|
|
);
|
|
|
|
export const mediaFiles = pgTable("media_files", {
|
|
id: uuid("id").primaryKey().defaultRandom(),
|
|
operatorId: uuid("operator_id").notNull().references(() => operators.id),
|
|
filenameOriginal: text("filename_original").notNull(),
|
|
mimeType: text("mime_type").notNull(),
|
|
sizeBytes: bigint("size_bytes", { mode: "number" }).notNull(),
|
|
sha256: text("sha256").notNull(),
|
|
storagePath: text("storage_path").notNull(),
|
|
createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(),
|
|
});
|
|
|
|
export const reminders = pgTable("reminders", {
|
|
id: uuid("id").primaryKey().defaultRandom(),
|
|
accountId: uuid("account_id").notNull().references(() => whatsappAccounts.id),
|
|
name: text("name").notNull(),
|
|
scheduleKind: text("schedule_kind").notNull(),
|
|
scheduledAt: timestamp("scheduled_at", { withTimezone: true }),
|
|
rrule: text("rrule"),
|
|
timezone: text("timezone").notNull(),
|
|
endsAt: timestamp("ends_at", { withTimezone: true }),
|
|
maxRuns: integer("max_runs"),
|
|
status: text("status").notNull().default("active"),
|
|
createdBy: uuid("created_by").notNull().references(() => operators.id),
|
|
createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(),
|
|
updatedAt: timestamp("updated_at", { withTimezone: true }).notNull().defaultNow(),
|
|
});
|
|
|
|
export const reminderTargets = pgTable(
|
|
"reminder_targets",
|
|
{
|
|
reminderId: uuid("reminder_id").notNull().references(() => reminders.id, { onDelete: "cascade" }),
|
|
groupId: uuid("group_id").notNull().references(() => whatsappGroups.id),
|
|
position: integer("position").notNull().default(0),
|
|
},
|
|
(t) => ({
|
|
pk: primaryKey({ columns: [t.reminderId, t.groupId] }),
|
|
}),
|
|
);
|
|
|
|
export const reminderMessages = pgTable("reminder_messages", {
|
|
id: uuid("id").primaryKey().defaultRandom(),
|
|
reminderId: uuid("reminder_id").notNull().references(() => reminders.id, { onDelete: "cascade" }),
|
|
position: integer("position").notNull(),
|
|
kind: text("kind").notNull(),
|
|
textContent: text("text_content"),
|
|
mediaId: uuid("media_id").references(() => mediaFiles.id),
|
|
});
|
|
|
|
export const reminderRuns = pgTable("reminder_runs", {
|
|
id: uuid("id").primaryKey().defaultRandom(),
|
|
reminderId: uuid("reminder_id").notNull().references(() => reminders.id, { onDelete: "cascade" }),
|
|
firedAt: timestamp("fired_at", { withTimezone: true }).notNull().defaultNow(),
|
|
status: text("status").notNull(),
|
|
errorSummary: text("error_summary"),
|
|
});
|
|
|
|
export const reminderRunTargets = pgTable(
|
|
"reminder_run_targets",
|
|
{
|
|
runId: uuid("run_id").notNull().references(() => reminderRuns.id, { onDelete: "cascade" }),
|
|
groupId: uuid("group_id").notNull().references(() => whatsappGroups.id),
|
|
status: text("status").notNull(),
|
|
waMessageId: text("wa_message_id"),
|
|
error: text("error"),
|
|
latencyMs: integer("latency_ms"),
|
|
},
|
|
(t) => ({
|
|
pk: primaryKey({ columns: [t.runId, t.groupId] }),
|
|
}),
|
|
);
|
|
|
|
export const auditLog = pgTable("audit_log", {
|
|
id: uuid("id").primaryKey().defaultRandom(),
|
|
operatorId: uuid("operator_id").references(() => operators.id),
|
|
source: text("source").notNull(),
|
|
action: text("action").notNull(),
|
|
targetType: text("target_type"),
|
|
targetId: uuid("target_id"),
|
|
payload: jsonb("payload").notNull().default({}),
|
|
createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(),
|
|
});
|
|
|
|
export const authSessions = pgTable("auth_sessions", {
|
|
id: uuid("id").primaryKey().defaultRandom(),
|
|
operatorId: uuid("operator_id").notNull().references(() => operators.id),
|
|
tokenHash: text("token_hash").notNull().unique(),
|
|
createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(),
|
|
expiresAt: timestamp("expires_at", { withTimezone: true }).notNull(),
|
|
lastUsedAt: timestamp("last_used_at", { withTimezone: true }).notNull().defaultNow(),
|
|
ipAddress: inet("ip_address"),
|
|
userAgent: text("user_agent"),
|
|
});
|
|
|
|
export type Operator = typeof operators.$inferSelect;
|
|
export type NewOperator = typeof operators.$inferInsert;
|
|
export type WhatsappAccount = typeof whatsappAccounts.$inferSelect;
|
|
export type NewWhatsappAccount = typeof whatsappAccounts.$inferInsert;
|
|
export type WhatsappGroup = typeof whatsappGroups.$inferSelect;
|
|
export type NewWhatsappGroup = typeof whatsappGroups.$inferInsert;
|
|
export type AuditLogEntry = typeof auditLog.$inferSelect;
|
|
export type NewAuditLogEntry = typeof auditLog.$inferInsert;
|