feat(cms): migrate from directus to payloadcms
Some checks failed
Build & Deploy / 🔍 Prepare (push) Successful in 4s
Build & Deploy / 🧪 QA (push) Successful in 2m55s
Build & Deploy / 🏗️ Build (push) Successful in 11m40s
Build & Deploy / 🚀 Deploy (push) Failing after 8s
Build & Deploy / 🩺 Health Check (push) Has been skipped
Build & Deploy / 🔔 Notify (push) Successful in 1s
Some checks failed
Build & Deploy / 🔍 Prepare (push) Successful in 4s
Build & Deploy / 🧪 QA (push) Successful in 2m55s
Build & Deploy / 🏗️ Build (push) Successful in 11m40s
Build & Deploy / 🚀 Deploy (push) Failing after 8s
Build & Deploy / 🩺 Health Check (push) Has been skipped
Build & Deploy / 🔔 Notify (push) Successful in 1s
This commit is contained in:
@@ -1,15 +1,19 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import * as nodemailer from "nodemailer";
|
||||
import directus, { ensureAuthenticated } from "@/lib/directus";
|
||||
import { createItem } from "@directus/sdk";
|
||||
import { getPayload } from "payload";
|
||||
import configPromise from "@payload-config";
|
||||
import { getServerAppServices } from "@/lib/services/create-services.server";
|
||||
import {
|
||||
render,
|
||||
ContactFormNotification,
|
||||
ConfirmationMessage,
|
||||
} from "@mintel/mail";
|
||||
import React from "react";
|
||||
|
||||
export async function POST(req: Request) {
|
||||
const services = getServerAppServices();
|
||||
const logger = services.logger.child({ action: "contact_submission" });
|
||||
|
||||
// Set analytics context from request headers for high-fidelity server-side tracking
|
||||
// This fulfills the "server-side via nextjs proxy" requirement
|
||||
if (services.analytics.setServerContext) {
|
||||
services.analytics.setServerContext({
|
||||
userAgent: req.headers.get("user-agent") || undefined,
|
||||
@@ -41,70 +45,87 @@ export async function POST(req: Request) {
|
||||
if (!message || message.length < 20) {
|
||||
return NextResponse.json({ error: "message_too_short" }, { status: 400 });
|
||||
}
|
||||
|
||||
if (message.length > 4000) {
|
||||
return NextResponse.json({ error: "message_too_long" }, { status: 400 });
|
||||
}
|
||||
|
||||
// 1. Directus save
|
||||
let directusSaved = false;
|
||||
const payload = await getPayload({ config: configPromise });
|
||||
|
||||
// 1. Payload save
|
||||
let payloadSaved = false;
|
||||
try {
|
||||
await ensureAuthenticated();
|
||||
await directus.request(
|
||||
createItem("contact_submissions", {
|
||||
await payload.create({
|
||||
collection: "form-submissions",
|
||||
data: {
|
||||
name,
|
||||
email,
|
||||
company: company || "Nicht angegeben",
|
||||
message,
|
||||
}),
|
||||
);
|
||||
logger.info("Contact submission saved to Directus");
|
||||
directusSaved = true;
|
||||
} catch (directusError) {
|
||||
const errorMessage =
|
||||
directusError instanceof Error
|
||||
? directusError.message
|
||||
: String(directusError);
|
||||
logger.error("Failed to save to Directus", {
|
||||
error: errorMessage,
|
||||
details: directusError,
|
||||
});
|
||||
services.errors.captureException(directusError, {
|
||||
phase: "directus_save",
|
||||
});
|
||||
// We still try to send the email even if Directus fails
|
||||
}
|
||||
|
||||
// 2. Email sending
|
||||
try {
|
||||
const { config } = await import("@/lib/config");
|
||||
const transporter = nodemailer.createTransport({
|
||||
host: config.mail.host,
|
||||
port: config.mail.port,
|
||||
secure: config.mail.port === 465,
|
||||
auth: {
|
||||
user: config.mail.user,
|
||||
pass: config.mail.pass,
|
||||
},
|
||||
});
|
||||
logger.info("Contact submission saved to PayloadCMS");
|
||||
payloadSaved = true;
|
||||
} catch (payloadError) {
|
||||
const errorMessage =
|
||||
payloadError instanceof Error
|
||||
? payloadError.message
|
||||
: String(payloadError);
|
||||
logger.error("Failed to save to Payload", {
|
||||
error: errorMessage,
|
||||
details: payloadError,
|
||||
});
|
||||
services.errors.captureException(payloadError, { phase: "payload_save" });
|
||||
}
|
||||
|
||||
await transporter.sendMail({
|
||||
// 2. Email sending via Payload (which uses configured nodemailer)
|
||||
try {
|
||||
const { config } = await import("@/lib/config");
|
||||
const clientName = "MB Grid Solutions";
|
||||
|
||||
// 2a. Notification to MB Grid
|
||||
const notificationHtml = await render(
|
||||
React.createElement(ContactFormNotification, {
|
||||
name,
|
||||
email,
|
||||
message,
|
||||
company,
|
||||
}),
|
||||
);
|
||||
|
||||
await payload.sendEmail({
|
||||
from: config.mail.from,
|
||||
to: config.mail.recipients.join(",") || "info@mb-grid-solutions.com",
|
||||
to:
|
||||
config.mail.recipients.join(",") ||
|
||||
process.env.CONTACT_RECIPIENT ||
|
||||
"info@mb-grid-solutions.com",
|
||||
replyTo: email,
|
||||
subject: `Kontaktanfrage von ${name}`,
|
||||
text: `
|
||||
Name: ${name}
|
||||
Firma: ${company || "Nicht angegeben"}
|
||||
E-Mail: ${email}
|
||||
Zeitpunkt: ${new Date().toISOString()}
|
||||
|
||||
Nachricht:
|
||||
${message}
|
||||
`,
|
||||
html: notificationHtml,
|
||||
});
|
||||
|
||||
logger.info("Email sent successfully");
|
||||
// 2b. Confirmation to the User
|
||||
try {
|
||||
const confirmationHtml = await render(
|
||||
React.createElement(ConfirmationMessage, {
|
||||
name,
|
||||
clientName,
|
||||
}),
|
||||
);
|
||||
|
||||
await payload.sendEmail({
|
||||
from: config.mail.from,
|
||||
to: email,
|
||||
subject: `Ihre Kontaktanfrage bei ${clientName}`,
|
||||
html: confirmationHtml,
|
||||
});
|
||||
} catch (confirmError) {
|
||||
logger.warn(
|
||||
"Failed to send confirmation email, but notification was sent",
|
||||
{ error: confirmError },
|
||||
);
|
||||
}
|
||||
|
||||
logger.info("Emails sent successfully");
|
||||
|
||||
// Notify success for important leads
|
||||
await services.notifications.notify({
|
||||
@@ -116,18 +137,16 @@ ${message}
|
||||
logger.error("SMTP Error", { error: smtpError });
|
||||
services.errors.captureException(smtpError, { phase: "smtp_send" });
|
||||
|
||||
// If Directus failed AND SMTP failed, then we really have a problem
|
||||
if (!directusSaved) {
|
||||
if (!payloadSaved) {
|
||||
return NextResponse.json(
|
||||
{ error: "Systemfehler (Speicherung und Versand fehlgeschlagen)" },
|
||||
{ status: 500 },
|
||||
);
|
||||
}
|
||||
|
||||
// If Directus was successful, we tell the user "Ok" but we know internally it was a partial failure
|
||||
await services.notifications.notify({
|
||||
title: "🚨 SMTP Fehler (Kontaktformular)",
|
||||
message: `Anfrage von ${name} (${email}) in Directus gespeichert, aber E-Mail-Versand fehlgeschlagen: ${smtpError instanceof Error ? smtpError.message : String(smtpError)}`,
|
||||
message: `Anfrage von ${name} (${email}) in Payload gespeichert, aber E-Mail-Versand fehlgeschlagen: ${smtpError instanceof Error ? smtpError.message : String(smtpError)}`,
|
||||
priority: 8,
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user