From 8b25be4723e32788a4d4ba3849199ac3a82b88e0 Mon Sep 17 00:00:00 2001 From: torbinus Date: Thu, 5 Nov 2026 12:54:13 +0100 Subject: [PATCH] Einladung ins Firmenportal gilt erst als verschickt, wenn die Mail raus ist Ohne Mailserver schrieb Medusa die Mail nur ins Log, im Admin stand trotzdem "Eingeladen". Co-Authored-By: Claude Opus 5.5 --- backend/apps/backend/src/lib/b2b-einladung.ts | 47 +++++++++++++------ 1 file changed, 32 insertions(+), 15 deletions(-) diff --git a/backend/apps/backend/src/lib/b2b-einladung.ts b/backend/apps/backend/src/lib/b2b-einladung.ts index e2336cc..c03b1ae 100644 --- a/backend/apps/backend/src/lib/b2b-einladung.ts +++ b/backend/apps/backend/src/lib/b2b-einladung.ts @@ -11,9 +11,24 @@ export const tokenHash = (token: string) => createHash("sha256").update(token).d const storefront = () => process.env.STOREFRONT_URL ?? "http://localhost:4321"; +/** + * Schickt die Mail und wirft, wenn sie nicht wirklich rausgeht. Ohne + * Mailserver schreibt Medusa sie nur ins Log und meldet trotzdem Erfolg — + * im Admin stünde dann "Eingeladen", obwohl nie etwas ankommt. + */ async function mailen(scope: any, an: string, vorlage: string, inhalt: { subject: string; html: string; text: string }) { + if (!process.env.SMTP_HOST || !process.env.MAIL_FROM) { + throw new Eingabefehler( + "Auf diesem System ist kein Mailserver eingerichtet (SMTP_HOST / MAIL_FROM fehlen). Die Einladung wurde nicht verschickt." + ); + } const notification: any = scope.resolve(Modules.NOTIFICATION); - await notification.createNotifications({ to: an, channel: "email", template: vorlage, content: inhalt }); + try { + await notification.createNotifications({ to: an, channel: "email", template: vorlage, content: inhalt }); + } catch (e) { + const grund = e instanceof Error ? e.message.split("\n").pop() : String(e); + throw new Eingabefehler(`Die Mail ließ sich nicht verschicken: ${grund}`); + } } /** @@ -36,14 +51,6 @@ export async function einladen(scope: any, kontaktId: string): Promise<"linked" if (kunde) { const [vergeben] = await service.listContacts({ customer_id: kunde.id }, { take: 1, relations: ["company"] }); if (vergeben) throw new Eingabefehler(`Dieses Konto gehört schon zu „${vergeben.company?.name}“.`); - await service.updateContacts({ - id: kontakt.id, - customer_id: kunde.id, - invited_at: jetzt, - accepted_at: jetzt, - invite_token_hash: null, - invite_expires_at: null, - }); await mailen( scope, kontakt.email, @@ -54,16 +61,20 @@ export async function einladen(scope: any, kontaktId: string): Promise<"linked" link: `${storefront()}/konto/anmelden?redirect=/konto/firma`, }) ); + await service.updateContacts({ + id: kontakt.id, + customer_id: kunde.id, + invited_at: jetzt, + accepted_at: jetzt, + invite_token_hash: null, + invite_expires_at: null, + }); return "linked"; } + // Erst die Mail, dann speichern: Kommt sie nicht raus, bleibt der Status, + // wie er war, statt "Eingeladen" zu behaupten. const token = randomBytes(32).toString("base64url"); - await service.updateContacts({ - id: kontakt.id, - invite_token_hash: tokenHash(token), - invite_expires_at: new Date(jetzt.getTime() + GUELTIG_TAGE * 86400000), - invited_at: jetzt, - }); await mailen( scope, kontakt.email, @@ -75,6 +86,12 @@ export async function einladen(scope: any, kontaktId: string): Promise<"linked" tage: GUELTIG_TAGE, }) ); + await service.updateContacts({ + id: kontakt.id, + invite_token_hash: tokenHash(token), + invite_expires_at: new Date(jetzt.getTime() + GUELTIG_TAGE * 86400000), + invited_at: jetzt, + }); return "invited"; }