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 { sendEmail } from "@/backend/mailService";
import sendEmail from "@/backend/mailService";
const talkbackSchema = z.object({
email: z
@@ -34,7 +34,11 @@ export async function POST(req) {
const { email, subject, message } = result.data;
await sendEmail(email, subject, message);
await sendEmail({
to: email,
subject,
text: message,
});
return Response.json({
message: "Nachricht erfolgreich gesendet.",
@@ -47,4 +51,4 @@ export async function POST(req) {
{ status: 500 }
);
}
}
};
+60 -33
View File
@@ -1,38 +1,65 @@
import axios from "axios";
const pateClient = axios.create({
baseURL: "https://api-toska.apps.ocp-prod-0.k8s.it.helsinki.fi/pate/",
params: {
token: process.env.PATE_TOKEN,
},
});
const template = {
from: "Gradesa",
};
const PATE_URL = "https://api-toska.apps.ocp-prod-0.k8s.it.helsinki.fi/pate/";
const isDevelopment = process.env.NODE_ENV === "development";
const isCI = process.env.CI === "true";
const settings = {
color: "orange",
header: "Sent by Gradesa",
dryrun: false,
hideToska: true,
disableToska: true,
hideToska: false,
disableToska: true,
color: "orange",
header: "GRADESA",
headerFontColor: "white",
dryrun: false,
};
export async function sendEmail(to, subject, body) {
if (!process.env.PATE_TOKEN) {
throw new Error("PATE token missing");
}
const sendEmail = async ({ to, subject, text }) => {
if (!to || !subject || !text) {
throw new Error("Missing email fields: to, subject and text are required");
}
return pateClient.post("/", {
template,
emails: [
{
to,
subject,
body,
},
],
settings,
});
}
const mail = {
template: {
from: "GRADESA",
text,
},
emails: [
{
to,
subject,
},
],
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;