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 ", }, 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.", ); } }; }