// Was jeder Angemeldete für sich selbst sieht, auch Mitarbeiter am Handy (E27): // eigene Einsätze mit Baustelle und Adresse, später eigene Stunden und Urlaub. Keine Beträge. import type { FastifyInstance } from "fastify"; import { z } from "zod"; import type { Pool } from "./db.js"; import type { Einstellungen } from "./einstellungen.js"; import { anfang, ende } from "../domain/terminplan.js"; import { plusTage } from "../domain/datum.js"; const Tag = z.string().regex(/^\d{4}-\d{2}-\d{2}$/); /** Projekte, an denen ein Kontakt eingeteilt ist (für Ablage und Stunden) */ export async function istEingeteilt(pool: Pool, kontaktId: string | null, projektId: string): Promise { if (!kontaktId) return false; const r = await pool.query(`select 1 from besetzung b join vorgang v on v.id = b.vorgang_id join abschnitt a on a.id = v.abschnitt_id where b.kontakt_id = $1 and a.projekt_id = $2 limit 1`, [kontaktId, projektId]); return !!r.rowCount; } export function registriereIch(app: FastifyInstance, pool: Pool, einstellungen: () => Promise): void { // Eigene Einsätze von heute an, Standard drei Wochen app.get("/api/ich/einsaetze", async (req) => { const q = z.object({ heute: Tag.optional(), tage: z.coerce.number().int().min(1).max(92).default(21) }).parse(req.query); const heute = q.heute ?? new Date().toISOString().slice(0, 10); const bis = plusTage(heute, q.tage - 1); const kontakt = req.wer!.kontakt_id; if (!kontakt) return { heute, bis, einsaetze: [] }; const woche = { tage: (await einstellungen()).arbeitstage }; const r = await pool.query( `select v.id, v.name, v.start, v.dauer, p.id as projekt_id, p.nummer, p.bezeichnung, p.baustelle_strasse, p.baustelle_plz, p.baustelle_ort from besetzung b join vorgang v on v.id = b.vorgang_id join abschnitt a on a.id = v.abschnitt_id join projekt p on p.id = a.projekt_id where b.kontakt_id = $1 and b.freigegeben order by v.start, p.nummer`, [kontakt]); const einsaetze = r.rows .map((v) => ({ ...v, dauer: Number(v.dauer), anfang: anfang({ ...v, dauer: Number(v.dauer) }, woche), ende: ende({ ...v, dauer: Number(v.dauer) }, woche) })) .filter((v) => v.ende >= heute && v.anfang <= bis) .map(({ start: _s, dauer: _d, ...v }) => v); return { heute, bis, arbeitstage: woche.tage, einsaetze }; }); }