import { getAllRows, getRowById, insertRow, updateRow } from '../crud' import type { Assignment, AssignmentType, Consultant } from '../../../src/db/types' export const listAssignments = (): Assignment[] => getAllRows('assignments') export const listAssignmentsNewestFirst = (): Assignment[] => getAllRows('assignments').sort((a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime()) export const getAssignment = (id: number): Assignment | undefined => getRowById('assignments', id) export const addAssignment = (a: Omit): number => insertRow('assignments', a) export const updateAssignment = (id: number, patch: Partial): number => updateRow('assignments', id, patch) export interface AssignmentWithDetails extends Assignment { typeName: string instituteeNames: string[] } /** Für die Dashboard-Liste: Assignments mit aufgelöstem Typ- und Institutee-Namen, neueste zuerst. */ export function listAssignmentsWithDetails(): AssignmentWithDetails[] { const raw = listAssignmentsNewestFirst() const types = getAllRows('assignmentTypes') const consultants = getAllRows('consultants') const typeMap = new Map(types.map(t => [t.id!, t])) const consMap = new Map(consultants.map(c => [c.id!, c])) return raw.map(a => ({ ...a, typeName: typeMap.get(a.assignmentTypeId)?.name ?? '—', instituteeNames: a.instituteeIds.map(id => { const c = consMap.get(id) return c ? `${c.firstName} ${c.lastName}` : '?' }), })) }