trihub-ernaehrung-backseatDevs/src/server/services/contact-email.js

52 lines
2.0 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import { createMailTransport } from "./email-service.js";
import { BookingError } from "./booking-service.js";
import { contactNotice } from "../../shared/contact.js";
export function createContactEmailService(makeTransport) {
// Dieser Ablauf bleibt auch bei extern konfiguriertem Buchungs-SMTP lokal.
const { transport, from } = createMailTransport(
{
SMTP_HOST: "127.0.0.1",
SMTP_PORT: "1025",
SMTP_FROM: "Tri-Hub Simulation <kontakt@tri-hub.de>",
},
makeTransport,
);
return async ({ email, advisor, reason }) => {
const results = await Promise.allSettled(
[
{
to: email,
text: `Deine Kontaktanfrage an ${advisor.name} ist in der Simulation eingegangen.`,
},
{
to: advisor.email,
text: `Hallo ${advisor.name}, eine simulierte Kontaktanfrage von ${email} ist eingegangen.`,
},
].map(async ({ to, text }) => {
const result = await transport.sendMail({
from,
to,
subject:
"Eingangsbestätigung – Tri-Hub Kontakt (Simulation)",
text: `${text}\n\nKontaktgrund: ${reason}\n\n${contactNotice}`,
});
if (
!result.accepted?.some(
(address) => address.toLowerCase() === to.toLowerCase(),
)
) {
throw new Error("SMTP hat den Empfänger nicht angenommen.");
}
}),
);
if (results.some((result) => result.status === "rejected")) {
throw new BookingError(
502,
"CONTACT_MAIL_FAILED",
"Nicht beide Bestätigungen konnten bestätigt werden. Bitte Mailpit prüfen, bevor du erneut sendest; eine Nachricht kann bereits eingegangen sein.",
);
}
};
}