manual-order: fehlenden Kunden automatisch anlegen (Login-Konto per E-Mail)

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
s4luorth
2026-09-07 07:58:57 +02:00
parent bc295fea01
commit be1669a44f

View File

@@ -1638,14 +1638,22 @@ const handler = (router, { services, getSchema, logger }) => {
let quote = null;
try { quote = await calculatePrice(schema, { ...body, quantity: entries.length }); } catch { /* egal */ }
// Kunde optional verknüpfen (per E-Mail oder ID).
// Kunde per E-Mail verknüpfen – existiert er noch nicht, automatisch anlegen.
let customerId = null, customerEmail = null;
if (body.customer_email && isEmail(norm(body.customer_email))) {
const u = (await new UsersService({ schema, accountability: null }).readByQuery({
filter: { email: { _eq: norm(body.customer_email) } }, limit: 1, fields: ['id', 'email'],
}))?.[0];
const kundeMail = norm(body.customer_email);
const users = new UsersService({ schema, accountability: null });
const u = (await users.readByQuery({ filter: { email: { _eq: kundeMail } }, limit: 1, fields: ['id', 'email'] }))?.[0];
if (u) { customerId = u.id; customerEmail = u.email; }
else customerEmail = norm(body.customer_email);
else {
try {
const role = (await new RolesService({ schema, accountability: null }).readByQuery({
filter: { name: { _eq: 'Kunde' } }, limit: 1, fields: ['id'],
}))?.[0];
customerId = await users.createOne({ email: kundeMail, password: zufallsPasswort(), role: role?.id ?? null, status: 'active' });
customerEmail = kundeMail;
} catch (e) { logger.warn(`[skrift-orders] manual-order Kunde anlegen: ${e.message}`); customerEmail = kundeMail; }
}
}
let formatId = null;