Das Formular der Personalabteilung ist übernommen, nicht durch eine Bildschirmansicht ersetzt: schwarzer Rahmen, Kopf mit Kennung und Klassifizierungsbalken, Raster aus fünf Spalten, Unterschriftenzeilen, Fußzeile. Es steht bewusst außerhalb des Design-Systems - es wird gedruckt und muss auf Papier aussehen wie das Blatt daneben. Datengetrieben bleibt es trotzdem. Kein Feld steht im Programm: sheetTitle und sheetSubtitle an der Vorlage, span für die Zellbreite, rows für das Lichtbild, dateKey für das Datum unter einer Unterschrift, slot für den Klassifizierungsbalken. Eine weitere Dokumentart mit layout = DOSSIER braucht keine Zeile Code. Der Balken zeigt die Foundation-Stufe aus der neuen Tabelle FoundationClearance. Sie ist Inhalt, kein Zugriff - was jemand sehen darf, entscheidet weiterhin allein die interne Stufe. Ohne Auswahl bleibt der Balken neutral, statt eine Einstufung zu behaupten. Die Dienstnummer ist entfallen, es gibt sie nicht. Der Status ist zu mehreren Etiketten geworden: "Gekündigt" und "Verhaftet" schließen einander nicht aus. Unbekannte Schlüssel werden verworfen und die Reihenfolge kommt aus der Konfiguration, nicht aus der Eingabe. PersonnelAssignment hängt AfiS-Mitglieder an eine Person - Aktenführung, Ermittler, Ansprechpartner - und ist zugleich der ausnahmsweise Zugang, wie CaseParticipant bei der Fallakte. Betroffen zu sein gibt weiterhin keinen Zugang: das ist eine Eigenschaft der Person, die Zuordnung eine Entscheidung der Abteilung. Fall und Person lassen sich von beiden Seiten verknüpfen und schon beim Anlegen einer Fallakte. Alle drei Wege laufen in addParticipant zusammen. Noch offen: das Lichtbild. Feldtyp und Platz stehen, hochladen kommt mit der Prüfkette in Phase 5 - ein Bild als Data-URL im Abschnittswert verstiesse gegen harte Regel 4. 102 Tests im Web-Paket, davon sechs auf dem gerenderten Blatt selbst.
394 lines
12 KiB
TypeScript
394 lines
12 KiB
TypeScript
import { db } from '@/lib/db'
|
|
import { writeAudit } from '@/lib/audit'
|
|
import { visibleCaseWhere } from '@/lib/access/caseFilter'
|
|
import { visiblePersonnelWhere } from '@/lib/access/personnelFilter'
|
|
import { canWritePersonnel } from '@/lib/access/documentWrite'
|
|
import { AccessError, must } from '@/lib/errors'
|
|
import type { AssignmentRoleName } from '@/lib/personnel/terms'
|
|
import type { EffectivePermissions } from '@/lib/auth/permissions'
|
|
import type { Prisma } from '@afis/db'
|
|
|
|
/**
|
|
* Personalakten. Eine Akte zu einer Person, auch zu einer **ohne
|
|
* Discord-Konto**: Zeugen, ehemalige Mitarbeiter und Personen, über die
|
|
* lediglich Aktenlage besteht, brauchen einen Platz.
|
|
*
|
|
* Die Rückverweise sind der heikelste Teil. Siehe `loadBacklinks`.
|
|
*/
|
|
|
|
const KOPFDATEN = {
|
|
id: true,
|
|
userId: true,
|
|
displayName: true,
|
|
rank: true,
|
|
department: true,
|
|
statusTags: true,
|
|
note: true,
|
|
classification: true,
|
|
createdById: true,
|
|
createdAt: true,
|
|
updatedAt: true,
|
|
} satisfies Prisma.PersonnelFileSelect
|
|
|
|
export async function loadVisiblePersonnelFile(p: EffectivePermissions, id: string) {
|
|
return db.personnelFile.findFirst({
|
|
where: { AND: [visiblePersonnelWhere(p), { id }] },
|
|
select: KOPFDATEN,
|
|
})
|
|
}
|
|
|
|
/**
|
|
* Liste der Personalakten. Suchbegriff und Zählung laufen durch dieselbe
|
|
* Abfrage wie die Sichtbarkeit, damit sich aus einer Trefferzahl nichts über
|
|
* verborgene Akten ableiten lässt.
|
|
*/
|
|
export async function listPersonnelFiles(
|
|
p: EffectivePermissions,
|
|
options: { query?: string; statusTag?: string; take?: number } = {},
|
|
) {
|
|
const begriff = options.query?.trim()
|
|
const where: Prisma.PersonnelFileWhereInput = {
|
|
AND: [
|
|
visiblePersonnelWhere(p),
|
|
...(options.statusTag ? [{ statusTags: { has: options.statusTag } }] : []),
|
|
...(begriff
|
|
? [
|
|
{
|
|
OR: [
|
|
{ displayName: { contains: begriff, mode: 'insensitive' as const } },
|
|
{ rank: { contains: begriff, mode: 'insensitive' as const } },
|
|
{ department: { contains: begriff, mode: 'insensitive' as const } },
|
|
],
|
|
},
|
|
]
|
|
: []),
|
|
],
|
|
}
|
|
|
|
const [files, total] = await Promise.all([
|
|
db.personnelFile.findMany({
|
|
where,
|
|
orderBy: [{ displayName: 'asc' }],
|
|
take: options.take ?? 50,
|
|
select: KOPFDATEN,
|
|
}),
|
|
db.personnelFile.count({ where }),
|
|
])
|
|
|
|
return { files, total }
|
|
}
|
|
|
|
export async function createPersonnelFile(
|
|
p: EffectivePermissions,
|
|
input: {
|
|
displayName: string
|
|
userId?: string | null
|
|
rank?: string | null
|
|
department?: string | null
|
|
statusTags?: string[]
|
|
note?: string | null
|
|
classification: number
|
|
},
|
|
): Promise<{ id: string }> {
|
|
must(canWritePersonnel(p, input.classification))
|
|
|
|
const name = input.displayName.trim()
|
|
if (name.length < 2) throw new AccessError('name-required')
|
|
|
|
return db.$transaction(async (tx) => {
|
|
const created = await tx.personnelFile.create({
|
|
data: {
|
|
displayName: name,
|
|
userId: input.userId || null,
|
|
rank: input.rank?.trim() || null,
|
|
department: input.department?.trim() || null,
|
|
statusTags: await gueltigeStatusEtiketten(input.statusTags),
|
|
note: input.note?.trim() || null,
|
|
classification: input.classification,
|
|
createdById: p.userId,
|
|
},
|
|
select: { id: true },
|
|
})
|
|
|
|
await writeAudit(tx, {
|
|
actorId: p.userId,
|
|
action: 'personnel.create',
|
|
targetType: 'PersonnelFile',
|
|
targetId: created.id,
|
|
clearanceAtTime: p.clearanceLevel,
|
|
meta: { displayName: name, classification: input.classification },
|
|
})
|
|
|
|
return created
|
|
})
|
|
}
|
|
|
|
/** Kopfdaten ändern. Die Klassifizierung läuft über `reclassifyPersonnelFile`. */
|
|
export async function updatePersonnelFile(
|
|
p: EffectivePermissions,
|
|
id: string,
|
|
input: {
|
|
displayName?: string
|
|
rank?: string | null
|
|
department?: string | null
|
|
statusTags?: string[]
|
|
note?: string | null
|
|
},
|
|
): Promise<void> {
|
|
const target = await loadVisiblePersonnelFile(p, id)
|
|
if (!target) throw new AccessError('not-found')
|
|
must(canWritePersonnel(p, target.classification))
|
|
|
|
const name = input.displayName?.trim()
|
|
if (name !== undefined && name.length < 2) throw new AccessError('name-required')
|
|
|
|
await db.$transaction(async (tx) => {
|
|
await tx.personnelFile.update({
|
|
where: { id },
|
|
data: {
|
|
...(name !== undefined ? { displayName: name } : {}),
|
|
...(input.rank !== undefined ? { rank: input.rank?.trim() || null } : {}),
|
|
...(input.department !== undefined
|
|
? { department: input.department?.trim() || null }
|
|
: {}),
|
|
...(input.statusTags !== undefined
|
|
? { statusTags: await gueltigeStatusEtiketten(input.statusTags) }
|
|
: {}),
|
|
...(input.note !== undefined ? { note: input.note?.trim() || null } : {}),
|
|
},
|
|
})
|
|
|
|
await writeAudit(tx, {
|
|
actorId: p.userId,
|
|
action: 'personnel.update',
|
|
targetType: 'PersonnelFile',
|
|
targetId: id,
|
|
clearanceAtTime: p.clearanceLevel,
|
|
meta: { fields: Object.keys(input) },
|
|
})
|
|
})
|
|
}
|
|
|
|
/**
|
|
* Klassifizierung ändern. Wie bei der Fallakte ist die Herabstufung der
|
|
* heiklere Fall: sie macht die Akte für Leute sichtbar, die sie vorher nicht
|
|
* sehen durften. Sie braucht eine Begründung und steht im Protokoll.
|
|
*/
|
|
export async function reclassifyPersonnelFile(
|
|
p: EffectivePermissions,
|
|
id: string,
|
|
to: number,
|
|
reason?: string,
|
|
): Promise<void> {
|
|
const target = await loadVisiblePersonnelFile(p, id)
|
|
if (!target) throw new AccessError('not-found')
|
|
if (target.classification === to) return
|
|
if (to < 0 || to > 5) throw new AccessError('clearance-too-low')
|
|
must(canWritePersonnel(p, Math.max(to, target.classification)))
|
|
|
|
const lowering = to < target.classification
|
|
const begruendung = reason?.trim() ?? ''
|
|
if (lowering && begruendung.length < 10) throw new AccessError('reason-required')
|
|
|
|
await db.$transaction(async (tx) => {
|
|
await tx.personnelFile.update({ where: { id }, data: { classification: to } })
|
|
await writeAudit(tx, {
|
|
actorId: p.userId,
|
|
action: 'personnel.reclassify',
|
|
targetType: 'PersonnelFile',
|
|
targetId: id,
|
|
clearanceAtTime: p.clearanceLevel,
|
|
meta: {
|
|
from: target.classification,
|
|
to,
|
|
lowering,
|
|
reason: begruendung || undefined,
|
|
},
|
|
})
|
|
})
|
|
}
|
|
|
|
/**
|
|
* Rückverweise: die Fälle, in denen diese Person vorkommt, nach Rolle getrennt.
|
|
*
|
|
* **Diese Liste läuft durch dieselbe Sichtbarkeitsprüfung wie die Fallliste.**
|
|
* Das ist der häufigste Ort, an dem ein Aktensystem leckt: eine Personalakte
|
|
* der Stufe 1, auf der „3 Fälle" steht, verrät die Existenz von Ermittlungen,
|
|
* die der Leser nicht sehen darf. Deshalb steckt der Filter in der Abfrage und
|
|
* die Zählung entsteht aus dem gefilterten Ergebnis — nie aus einem eigenen
|
|
* `count` ohne Filter. Siehe docs/05-akten-und-vorlagen.md, Rückverweise.
|
|
*/
|
|
export async function loadBacklinks(
|
|
p: EffectivePermissions,
|
|
file: { id: string; userId: string | null },
|
|
) {
|
|
const wenDieAkteMeint: Prisma.CaseParticipantWhereInput[] = [{ personnelFileId: file.id }]
|
|
// Dieselbe Person kann über ihre Personalakte oder über ihr Discord-Konto
|
|
// eingetragen sein. Beides zählt, sonst fehlen Fälle.
|
|
if (file.userId) wenDieAkteMeint.push({ userId: file.userId })
|
|
|
|
const rows = await db.caseParticipant.findMany({
|
|
where: {
|
|
AND: [{ OR: wenDieAkteMeint }, { case: { is: visibleCaseWhere(p) } }],
|
|
},
|
|
select: {
|
|
role: true,
|
|
case: {
|
|
select: {
|
|
id: true,
|
|
caseNumber: true,
|
|
title: true,
|
|
status: true,
|
|
classification: true,
|
|
updatedAt: true,
|
|
},
|
|
},
|
|
},
|
|
orderBy: { createdAt: 'desc' },
|
|
take: 200,
|
|
})
|
|
|
|
// Eine Person kann in einem Fall mehrere Rollen haben; jede Rolle bekommt
|
|
// ihren Eintrag, der Fall steht dann in beiden Gruppen.
|
|
const nachRolle = new Map<string, typeof rows>()
|
|
for (const row of rows) {
|
|
const gruppe = nachRolle.get(row.role) ?? []
|
|
gruppe.push(row)
|
|
nachRolle.set(row.role, gruppe)
|
|
}
|
|
|
|
return {
|
|
total: rows.length,
|
|
byRole: [...nachRolle.entries()].map(([role, eintraege]) => ({ role, eintraege })),
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Filtert auf die Etiketten, die es wirklich gibt.
|
|
*
|
|
* Sonst könnte ein manipuliertes Formular einen erfundenen Status setzen, und
|
|
* die Liste im Adminbereich wäre nur noch eine Empfehlung. Die Reihenfolge
|
|
* kommt aus der Konfiguration, nicht aus der Eingabe: so steht auf jeder Akte
|
|
* dieselbe Abfolge.
|
|
*/
|
|
async function gueltigeStatusEtiketten(gewuenscht: string[] | undefined): Promise<string[]> {
|
|
if (!gewuenscht?.length) return []
|
|
const bekannt = await db.personnelStatusTag.findMany({
|
|
where: { key: { in: gewuenscht }, isActive: true },
|
|
orderBy: { sortOrder: 'asc' },
|
|
select: { key: true },
|
|
})
|
|
return bekannt.map((tag) => tag.key)
|
|
}
|
|
|
|
/** Die Etiketten zur Auswahl, für Formulare und Filter. */
|
|
export async function listStatusTags() {
|
|
return db.personnelStatusTag.findMany({
|
|
where: { isActive: true },
|
|
orderBy: { sortOrder: 'asc' },
|
|
select: { key: true, name: true, colorToken: true },
|
|
})
|
|
}
|
|
|
|
/**
|
|
* Die AfiS-Mitglieder, die an dieser Personalakte hängen.
|
|
*
|
|
* Wer hier steht, liest die Akte unabhängig von seiner Freigabestufe — das ist
|
|
* derselbe ausnahmsweise Zugang wie `CaseParticipant` bei der Fallakte.
|
|
*/
|
|
export async function listAssignments(p: EffectivePermissions, personnelFileId: string) {
|
|
return db.personnelAssignment.findMany({
|
|
where: {
|
|
AND: [{ personnelFileId }, { personnelFile: { is: visiblePersonnelWhere(p) } }],
|
|
},
|
|
orderBy: { createdAt: 'asc' },
|
|
select: {
|
|
id: true,
|
|
role: true,
|
|
createdAt: true,
|
|
user: { select: { id: true, displayName: true } },
|
|
},
|
|
})
|
|
}
|
|
|
|
export async function addAssignment(
|
|
p: EffectivePermissions,
|
|
input: { personnelFileId: string; userId: string; role: AssignmentRoleName },
|
|
): Promise<{ id: string }> {
|
|
const target = await loadVisiblePersonnelFile(p, input.personnelFileId)
|
|
if (!target) throw new AccessError('not-found')
|
|
must(canWritePersonnel(p, target.classification))
|
|
|
|
const mitglied = await db.user.findUnique({
|
|
where: { id: input.userId },
|
|
select: { id: true },
|
|
})
|
|
if (!mitglied) throw new AccessError('not-found')
|
|
|
|
const vorhanden = await db.personnelAssignment.findFirst({
|
|
where: { personnelFileId: target.id, userId: input.userId, role: input.role },
|
|
select: { id: true },
|
|
})
|
|
if (vorhanden) return vorhanden
|
|
|
|
return db.$transaction(async (tx) => {
|
|
const created = await tx.personnelAssignment.create({
|
|
data: {
|
|
personnelFileId: target.id,
|
|
userId: input.userId,
|
|
role: input.role,
|
|
addedById: p.userId,
|
|
},
|
|
select: { id: true },
|
|
})
|
|
|
|
await writeAudit(tx, {
|
|
actorId: p.userId,
|
|
action: 'personnel.assign',
|
|
targetType: 'PersonnelAssignment',
|
|
targetId: created.id,
|
|
clearanceAtTime: p.clearanceLevel,
|
|
meta: {
|
|
personnelFileId: target.id,
|
|
displayName: target.displayName,
|
|
userId: input.userId,
|
|
role: input.role,
|
|
},
|
|
})
|
|
|
|
return created
|
|
})
|
|
}
|
|
|
|
export async function removeAssignment(
|
|
p: EffectivePermissions,
|
|
input: { personnelFileId: string; assignmentId: string },
|
|
): Promise<void> {
|
|
const target = await loadVisiblePersonnelFile(p, input.personnelFileId)
|
|
if (!target) throw new AccessError('not-found')
|
|
must(canWritePersonnel(p, target.classification))
|
|
|
|
const zuordnung = await db.personnelAssignment.findFirst({
|
|
where: { id: input.assignmentId, personnelFileId: target.id },
|
|
select: { id: true, userId: true, role: true },
|
|
})
|
|
if (!zuordnung) throw new AccessError('not-found')
|
|
|
|
await db.$transaction(async (tx) => {
|
|
await tx.personnelAssignment.delete({ where: { id: zuordnung.id } })
|
|
|
|
await writeAudit(tx, {
|
|
actorId: p.userId,
|
|
action: 'personnel.unassign',
|
|
targetType: 'PersonnelAssignment',
|
|
targetId: zuordnung.id,
|
|
clearanceAtTime: p.clearanceLevel,
|
|
meta: {
|
|
personnelFileId: target.id,
|
|
userId: zuordnung.userId,
|
|
role: zuordnung.role,
|
|
},
|
|
})
|
|
})
|
|
}
|