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

54 lines
2.3 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 { advisorDisplayName } from "../../shared/berater.js";
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 Ernährung <noreply@tri-hub.de>",
},
makeTransport,
);
return async ({ email, advisor, reason }) => {
const results = await Promise.allSettled(
[
{
to: email,
text: `Deine Kontaktanfrage an ${advisorDisplayName(advisor)} ist in der Simulation eingegangen. ${advisorDisplayName(advisor)} wird sich bei dir melden.`,
closing: "Viele Grüße\nTri-Hub Ernährung",
},
{
to: advisor.email,
text: `Hallo ${advisorDisplayName(advisor)}, bitte setze dich mit ${email} in Kontakt.`,
},
].map(async ({ to, text, closing }) => {
const result = await transport.sendMail({
from,
to,
subject:
"Eingangsbestätigung – Tri-Hub Kontakt (Simulation)",
text: `${text}\n\nKontaktgrund: ${reason}\n\n${contactNotice}${closing ? `\n\n${closing}` : ""}`,
});
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.",
);
}
};
}