import { FastifyRequest, FastifyReply, FastifyInstance } from 'fastify'; import { publicLinkService } from '../../modules/publiclinks.service'; import dayjs from 'dayjs'; // Falls nicht installiert: npm install dayjs export default async function publiclinksNonAuthenticatedRoutes(server: FastifyInstance) { server.get("/workflows/context/:token", async (req, reply) => { const { token } = req.params as { token: string }; const pin = req.headers['x-public-pin'] as string | undefined; try { const context = await publicLinkService.getLinkContext(server, token, pin); return reply.send(context); } catch (error: any) { if (error.message === "Link_NotFound") return reply.code(404).send({ error: "Link nicht gefunden" }); if (error.message === "Pin_Required") return reply.code(401).send({ error: "PIN erforderlich", requirePin: true }); if (error.message === "Pin_Invalid") return reply.code(403).send({ error: "PIN falsch", requirePin: true }); server.log.error(error); return reply.code(500).send({ error: "Interner Server Fehler" }); } }); server.post("/workflows/submit/:token", async (req, reply) => { const { token } = req.params as { token: string }; const pin = req.headers['x-public-pin'] as string | undefined; // Payload vom Frontend (enthält nun 'quantity' statt 'startDate/endDate') const body = req.body as any; try { /** * TRANSFORMATION * Wenn das Backend weiterhin Zeiträume erwartet, bauen wir diese hier: * Wir nehmen 'jetzt' als Ende und rechnen die 'quantity' (Stunden) zurück. */ const quantity = parseFloat(body.quantity) || 0; const payload = { ...body, // Wir generieren Start/Ende, damit der Service kompatibel bleibt endDate: new Date(), startDate: dayjs().subtract(quantity, 'hour').toDate() }; // Service aufrufen const result = await publicLinkService.submitFormData(server, token, payload, pin); return reply.code(201).send(result); } catch (error: any) { server.log.error(error); if (error.message === "Link_NotFound") return reply.code(404).send({ error: "Link ungültig" }); if (error.message === "Pin_Required") return reply.code(401).send({ error: "PIN erforderlich" }); if (error.message === "Pin_Invalid") return reply.code(403).send({ error: "PIN ist falsch" }); if (error.message === "Profile_Missing") return reply.code(400).send({ error: "Profil fehlt" }); return reply.code(500).send({ error: "Fehler beim Speichern", details: error.message }); } }); }