fix talkback function

This commit is contained in:
Erza Bytyci
2026-05-19 06:29:43 +00:00
parent 9c89cda001
commit e2968974f7
2 changed files with 67 additions and 36 deletions
+7 -3
View File
@@ -1,5 +1,5 @@
import { z } from "zod"; import { z } from "zod";
import { sendEmail } from "@/backend/mailService"; import sendEmail from "@/backend/mailService";
const talkbackSchema = z.object({ const talkbackSchema = z.object({
email: z email: z
@@ -34,7 +34,11 @@ export async function POST(req) {
const { email, subject, message } = result.data; const { email, subject, message } = result.data;
await sendEmail(email, subject, message); await sendEmail({
to: email,
subject,
text: message,
});
return Response.json({ return Response.json({
message: "Nachricht erfolgreich gesendet.", message: "Nachricht erfolgreich gesendet.",
@@ -47,4 +51,4 @@ export async function POST(req) {
{ status: 500 } { status: 500 }
); );
} }
} };
+60 -33
View File
@@ -1,38 +1,65 @@
import axios from "axios"; const PATE_URL = "https://api-toska.apps.ocp-prod-0.k8s.it.helsinki.fi/pate/";
const isDevelopment = process.env.NODE_ENV === "development";
const pateClient = axios.create({ const isCI = process.env.CI === "true";
baseURL: "https://api-toska.apps.ocp-prod-0.k8s.it.helsinki.fi/pate/",
params: {
token: process.env.PATE_TOKEN,
},
});
const template = {
from: "Gradesa",
};
const settings = { const settings = {
color: "orange", hideToska: false,
header: "Sent by Gradesa", disableToska: true,
dryrun: false, color: "orange",
hideToska: true, header: "GRADESA",
disableToska: true, headerFontColor: "white",
dryrun: false,
}; };
export async function sendEmail(to, subject, body) { const sendEmail = async ({ to, subject, text }) => {
if (!process.env.PATE_TOKEN) { if (!to || !subject || !text) {
throw new Error("PATE token missing"); throw new Error("Missing email fields: to, subject and text are required");
} }
return pateClient.post("/", { const mail = {
template, template: {
emails: [ from: "GRADESA",
{ text,
to, },
subject, emails: [
body, {
}, to,
], subject,
settings, },
}); ],
} settings,
};
if (isCI || isDevelopment) {
console.info("Skipping email sending in CI/development", {
to,
subject,
nodeEnv: process.env.NODE_ENV,
});
return;
}
if (!process.env.PATE_TOKEN) {
throw new Error("PATE_TOKEN missing");
}
try {
const response = await fetch(`${PATE_URL}?token=${process.env.PATE_TOKEN}`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(mail),
});
if (!response.ok) {
const errorBody = await response.text();
throw new Error(`PATE email sending failed with status ${response.status}: ${errorBody}`);
}
} catch (error) {
console.error("PATE fetch error:", error);
throw error;
}
};
export default sendEmail;