Files
FEDEO/backend/src/routes/publiclinks/publiclinks-non-authenticated.ts
florianfederspiel 2aed851224
All checks were successful
Build and Push Docker Images / build-backend (push) Successful in 32s
Build and Push Docker Images / build-frontend (push) Successful in 1m10s
Change for Agrar
2026-01-26 22:04:27 +01:00

63 lines
2.9 KiB
TypeScript

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
});
}
});
}