import { sql } from 'drizzle-orm'; import { bigint, index, pgTable, uniqueIndex, uuid } from 'drizzle-orm/pg-core'; import { branches } from './branches-table'; import { checkInColumns, checkOutColumns } from './checkpoint-columns'; import { employees } from './employees-table'; import { primaryEntityColumns } from './primary-entity-columns'; import { users } from './schema'; export const attendances = pgTable( 'attendances', { id: uuid('id').defaultRandom().notNull().primaryKey(), employeeId: uuid('employee_id') .notNull() .references(() => employees.id, { onDelete: 'restrict' }), branchId: uuid('branch_id') .notNull() .references(() => branches.id, { onDelete: 'restrict' }), date: bigint('date', { mode: 'number' }).notNull(), ...checkInColumns, ...checkOutColumns, ...primaryEntityColumns(users), }, (t) => [ uniqueIndex('attendances_employee_date_live_unique') .on(t.employeeId, t.date) .where(sql`${t.status} <> 'archived'`), uniqueIndex('attendances_employee_open_unique') .on(t.employeeId) .where(sql`${t.checkOutAt} IS NULL AND ${t.status} <> 'archived'`), index('attendances_employee_id_idx').on(t.employeeId), index('attendances_branch_id_idx').on(t.branchId), ], ); export type AttendanceRow = typeof attendances.$inferSelect; export type NewAttendanceRow = typeof attendances.$inferInsert;