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