32 lines
1.6 KiB
TypeScript
32 lines
1.6 KiB
TypeScript
import { getAllRows, getRowById, insertRow, updateRow } from '../crud'
|
|
import type { Assignment, AssignmentType, Consultant } from '../../../src/db/types'
|
|
|
|
export const listAssignments = (): Assignment[] => getAllRows<Assignment>('assignments')
|
|
export const listAssignmentsNewestFirst = (): Assignment[] =>
|
|
getAllRows<Assignment>('assignments').sort((a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime())
|
|
export const getAssignment = (id: number): Assignment | undefined => getRowById<Assignment>('assignments', id)
|
|
export const addAssignment = (a: Omit<Assignment, 'id'>): number => insertRow('assignments', a)
|
|
export const updateAssignment = (id: number, patch: Partial<Assignment>): 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<AssignmentType>('assignmentTypes')
|
|
const consultants = getAllRows<Consultant>('consultants')
|
|
const typeMap = new Map<number, AssignmentType>(types.map(t => [t.id!, t]))
|
|
const consMap = new Map<number, Consultant>(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}` : '?'
|
|
}),
|
|
}))
|
|
}
|