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:
@@ -11,9 +11,24 @@ export const tokenHash = (token: string) => createHash("sha256").update(token).d
|
|||||||
|
|
||||||
const storefront = () => process.env.STOREFRONT_URL ?? "http://localhost:4321";
|
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 }) {
|
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);
|
const notification: any = scope.resolve(Modules.NOTIFICATION);
|
||||||
|
try {
|
||||||
await notification.createNotifications({ to: an, channel: "email", template: vorlage, content: inhalt });
|
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) {
|
if (kunde) {
|
||||||
const [vergeben] = await service.listContacts({ customer_id: kunde.id }, { take: 1, relations: ["company"] });
|
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}“.`);
|
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(
|
await mailen(
|
||||||
scope,
|
scope,
|
||||||
kontakt.email,
|
kontakt.email,
|
||||||
@@ -54,16 +61,20 @@ export async function einladen(scope: any, kontaktId: string): Promise<"linked"
|
|||||||
link: `${storefront()}/konto/anmelden?redirect=/konto/firma`,
|
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";
|
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");
|
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(
|
await mailen(
|
||||||
scope,
|
scope,
|
||||||
kontakt.email,
|
kontakt.email,
|
||||||
@@ -75,6 +86,12 @@ export async function einladen(scope: any, kontaktId: string): Promise<"linked"
|
|||||||
tage: GUELTIG_TAGE,
|
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";
|
return "invited";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user