import { randomUUID } from 'node:crypto' import { desc, sql } from 'drizzle-orm' import { db } from '~/data/db' import { auditLog, brands, projects, users, type AuditEntry, type Brand, type Project, type User } from '~/data/schema' import type { Viewer } from '~/lib/auth-access' export async function resetAccounts(): Promise { await db.execute(sql`truncate table audit_log, user_project_role, "user" restart identity cascade`) } export function form(values: Record): FormData { const data = new FormData() for (const [key, value] of Object.entries(values)) { data.set(key, value) } return data } export function adminViewer(overrides: Partial = {}): Viewer { return { id: 'admin-1', name: 'Admin', email: 'admin@example.test', isAdmin: true, assignments: [], ...overrides, } } export function moderatorViewer(projectIds: string[] = [], overrides: Partial = {}): Viewer { return { id: 'moderator-1', name: 'Moderator', email: 'moderator@example.test', isAdmin: false, assignments: projectIds.map(projectId => ({ projectId, role: 'moderator' as const })), ...overrides, } } export async function makeBrand(slug = 'nyo', name = 'NYO'): Promise { const rows = await db.insert(brands).values({ slug, name }).returning() return rows[0]! } export async function makeProject(brandId: string, slug = 'trakk', name = 'Trakk'): Promise { const rows = await db.insert(projects).values({ brandId, slug, name }).returning() return rows[0]! } export async function makeUser(email = 'paul@example.test', name = 'Paul'): Promise { const rows = await db.insert(users).values({ id: randomUUID(), email, name }).returning() return rows[0]! } export async function auditEntries(): Promise { return db.select().from(auditLog).orderBy(desc(auditLog.createdAt)) }