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 <noreply@anthropic.com>
This commit is contained in:
torbinus
2026-11-05 12:54:13 +01:00
parent 9b368f9b7a
commit 8b25be4723
+31 -14
View File
@@ -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);
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";
}