Change for Agrar
This commit is contained in:
@@ -1,40 +1,19 @@
|
||||
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 };
|
||||
|
||||
// Wir lesen die PIN aus dem Header (Best Practice für Security)
|
||||
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) {
|
||||
// Spezifische Fehlercodes für das Frontend
|
||||
if (error.message === "Link_NotFound") {
|
||||
return reply.code(404).send({ error: "Link nicht gefunden oder abgelaufen" });
|
||||
}
|
||||
|
||||
if (error.message === "Pin_Required") {
|
||||
return reply.code(401).send({
|
||||
error: "PIN erforderlich",
|
||||
code: "PIN_REQUIRED",
|
||||
requirePin: true
|
||||
});
|
||||
}
|
||||
|
||||
if (error.message === "Pin_Invalid") {
|
||||
return reply.code(403).send({
|
||||
error: "PIN falsch",
|
||||
code: "PIN_INVALID",
|
||||
requirePin: true
|
||||
});
|
||||
}
|
||||
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" });
|
||||
@@ -43,49 +22,42 @@ export default async function publiclinksNonAuthenticatedRoutes(server: FastifyI
|
||||
|
||||
server.post("/workflows/submit/:token", async (req, reply) => {
|
||||
const { token } = req.params as { token: string };
|
||||
// PIN sicher aus dem Header lesen
|
||||
const pin = req.headers['x-public-pin'] as string | undefined;
|
||||
// Der Body enthält { profile, project, service, ... }
|
||||
const payload = req.body;
|
||||
|
||||
console.log(payload)
|
||||
// Payload vom Frontend (enthält nun 'quantity' statt 'startDate/endDate')
|
||||
const body = req.body as any;
|
||||
|
||||
try {
|
||||
// Service aufrufen (führt die 3 Schritte aus: Lieferschein -> Zeit -> History)
|
||||
/**
|
||||
* 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);
|
||||
|
||||
// 201 Created zurückgeben
|
||||
return reply.code(201).send(result);
|
||||
|
||||
} catch (error: any) {
|
||||
console.log(error);
|
||||
server.log.error(error);
|
||||
|
||||
// Fehler-Mapping für saubere HTTP Codes
|
||||
if (error.message === "Link_NotFound") {
|
||||
return reply.code(404).send({ error: "Link ungültig oder nicht aktiv" });
|
||||
}
|
||||
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" });
|
||||
|
||||
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: "Kein Mitarbeiter-Profil gefunden (weder im Link noch in der Eingabe)" });
|
||||
}
|
||||
|
||||
if (error.message === "Project not found" || error.message === "Service not found") {
|
||||
return reply.code(400).send({ error: "Ausgewähltes Projekt oder Leistung existiert nicht mehr." });
|
||||
}
|
||||
|
||||
// Fallback für alle anderen Fehler (z.B. DB Constraints)
|
||||
return reply.code(500).send({
|
||||
error: "Interner Fehler beim Speichern",
|
||||
error: "Fehler beim Speichern",
|
||||
details: error.message
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user