Files
FEDEO/backend/db/schema/staff_time_events.ts
2026-01-06 12:07:43 +01:00

86 lines
2.2 KiB
TypeScript

import {
pgTable,
uuid,
bigint,
text,
timestamp,
jsonb,
index,
check,
} from "drizzle-orm/pg-core";
import { sql } from "drizzle-orm";
import {tenants} from "./tenants";
import {authUsers} from "./auth_users";
export const stafftimeevents = pgTable(
"staff_time_events",
{
id: uuid("id").primaryKey().defaultRandom(),
tenant_id: bigint("tenant_id", { mode: "number" })
.notNull()
.references(() => tenants.id),
user_id: uuid("user_id")
.notNull()
.references(() => authUsers.id),
// Akteur
actortype: text("actor_type").notNull(), // 'user' | 'system'
actoruser_id: uuid("actor_user_id").references(() => authUsers.id),
// Zeit
eventtime: timestamp("event_time", {
withTimezone: true,
}).notNull(),
// Fachliche Bedeutung
eventtype: text("event_type").notNull(),
// Quelle
source: text("source").notNull(), // web | mobile | terminal | system
// Entkräftung
invalidates_event_id: uuid("invalidates_event_id")
.references(() => stafftimeevents.id),
//Beziehung Approval etc
related_event_id: uuid("related_event_id")
.references(() => stafftimeevents.id),
// Zusatzdaten
metadata: jsonb("metadata"),
// Technisch
created_at: timestamp("created_at", {
withTimezone: true,
})
.defaultNow()
.notNull(),
},
(table) => ({
// Indizes
tenantUserTimeIdx: index("idx_time_events_tenant_user_time").on(
table.tenant_id,
table.user_id,
table.eventtime
),
createdAtIdx: index("idx_time_events_created_at").on(table.created_at),
invalidatesIdx: index("idx_time_events_invalidates").on(
table.invalidates_event_id
),
// Constraints
actorUserCheck: check(
"time_events_actor_user_check",
sql`
(actor_type = 'system' AND actor_user_id IS NULL)
OR
(actor_type = 'user' AND actor_user_id IS NOT NULL)
`
),
})
);