feat(time): add span-based attendance tracking under 2.0
This commit is contained in:
48
backend/db/schema/staff_time_spans.ts
Normal file
48
backend/db/schema/staff_time_spans.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
import { pgTable, uuid, bigint, text, timestamp, date, integer, jsonb, primaryKey, index, uniqueIndex, check } from 'drizzle-orm/pg-core';
|
||||
import { sql } from 'drizzle-orm';
|
||||
import { tenants } from './tenants';
|
||||
import { authUsers } from './auth_users';
|
||||
import { projects } from './projects';
|
||||
const stamp = (name: string) => timestamp(name, { withTimezone: true });
|
||||
export const staffTimeV2Accounts = pgTable('staff_time_v2_accounts', {
|
||||
tenant_id: bigint('tenant_id', { mode: 'number' }).notNull().references(() => tenants.id),
|
||||
user_id: uuid('user_id').notNull().references(() => authUsers.id),
|
||||
activated_at: stamp('activated_at').notNull().defaultNow(), activated_by: uuid('activated_by').references(() => authUsers.id),
|
||||
}, t => [primaryKey({ columns: [t.tenant_id, t.user_id] })]);
|
||||
export const staffTimeSpans = pgTable('staff_time_spans', {
|
||||
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), type: text('type').notNull(),
|
||||
started_at: stamp('started_at').notNull(), ended_at: stamp('ended_at'), date_from: date('date_from'), date_to: date('date_to'),
|
||||
timezone: text('timezone').notNull().default('Europe/Berlin'), description: text('description').notNull().default(''), source: text('source').notNull(),
|
||||
status: text('status').notNull().default('factual'), version: integer('version').notNull().default(1),
|
||||
created_at: stamp('created_at').notNull().defaultNow(), updated_at: stamp('updated_at').notNull().defaultNow(),
|
||||
}, t => [index('staff_time_spans_user_time').on(t.tenant_id, t.user_id, t.started_at),
|
||||
uniqueIndex('staff_time_spans_one_open').on(t.tenant_id, t.user_id).where(sql `${t.ended_at} IS NULL`),
|
||||
check('staff_time_spans_check', sql `${t.ended_at} IS NULL OR ${t.ended_at} > ${t.started_at}`),
|
||||
check('staff_time_spans_type_check', sql `${t.type} IN ('work','vacation','sick','overtime_compensation','holiday','other')`),
|
||||
check('staff_time_spans_status_check', sql `${t.status} IN ('factual','submitted','approved','rejected')`),
|
||||
check('staff_time_spans_version_check', sql `${t.version} > 0`),
|
||||
check('staff_time_spans_check1', sql `(${t.date_from} IS NULL AND ${t.date_to} IS NULL) OR (${t.type} <> 'work' AND ${t.date_from} IS NOT NULL AND ${t.date_to} IS NOT NULL AND ${t.date_to} > ${t.date_from} AND ${t.ended_at} IS NOT NULL)`),
|
||||
check('staff_time_spans_check2', sql `${t.status} = 'factual' OR ${t.ended_at} IS NOT NULL`)]);
|
||||
export const staffTimeSpanBreaks = pgTable('staff_time_span_breaks', {
|
||||
id: uuid('id').primaryKey().defaultRandom(), span_id: uuid('span_id').notNull().references(() => staffTimeSpans.id),
|
||||
started_at: stamp('started_at').notNull(), ended_at: stamp('ended_at'),
|
||||
}, t => [index('staff_time_span_breaks_parent').on(t.span_id), uniqueIndex('staff_time_span_breaks_one_open').on(t.span_id).where(sql `${t.ended_at} IS NULL`), check('staff_time_span_breaks_check', sql `${t.ended_at} IS NULL OR ${t.ended_at} > ${t.started_at}`)]);
|
||||
export const staffTimeSpanProjects = pgTable('staff_time_span_projects', {
|
||||
id: uuid('id').primaryKey().defaultRandom(), span_id: uuid('span_id').notNull().references(() => staffTimeSpans.id),
|
||||
project_id: bigint('project_id', { mode: 'number' }).notNull().references(() => projects.id),
|
||||
started_at: stamp('started_at').notNull(), ended_at: stamp('ended_at').notNull(), notes: text('notes').notNull().default(''),
|
||||
}, t => [index('staff_time_span_projects_parent').on(t.span_id), check('staff_time_span_projects_check', sql `${t.ended_at} > ${t.started_at}`)]);
|
||||
export const staffTimeSpanHistory = pgTable('staff_time_span_history', {
|
||||
id: uuid('id').primaryKey().defaultRandom(), span_id: uuid('span_id').notNull().references(() => staffTimeSpans.id),
|
||||
actor_id: uuid('actor_id').references(() => authUsers.id), action: text('action').notNull(), reason: text('reason').notNull().default(''),
|
||||
before_data: jsonb('before_data'), after_data: jsonb('after_data').notNull(), created_at: stamp('created_at').notNull().defaultNow(),
|
||||
}, t => [index('staff_time_span_history_parent').on(t.span_id, t.created_at)]);
|
||||
export const staffTimeV2Requests = pgTable('staff_time_v2_requests', {
|
||||
tenant_id: bigint('tenant_id', { mode: 'number' }).notNull(), user_id: uuid('user_id').notNull(), request_id: text('request_id').notNull(),
|
||||
fingerprint: text('fingerprint').notNull(), response: jsonb('response').notNull(), created_at: stamp('created_at').notNull().defaultNow(),
|
||||
}, t => [primaryKey({ columns: [t.tenant_id, t.user_id, t.request_id] })]);
|
||||
export const staffTimeSpanOrigins = pgTable('staff_time_span_origins', {
|
||||
tenant_id: bigint('tenant_id', { mode: 'number' }).notNull(), source_kind: text('source_kind').notNull(), source_id: uuid('source_id').notNull(),
|
||||
span_id: uuid('span_id').notNull().references(() => staffTimeSpans.id),
|
||||
}, t => [primaryKey({ columns: [t.tenant_id, t.source_kind, t.source_id] })]);
|
||||
Reference in New Issue
Block a user