Kundenansicht P5: Kundenart beim Anlegen Pflicht (ab Schalter pilot), beim Ändern optional
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
8d3eb73db7
commit
5c21023e28
3 files changed
+93
-2
No files matched your search
@@ -14,6 +14,8 @@ import {
|
||||
Query,
|
||||
} from './appwriteAdmin.js'
|
||||
import { forgetPasswordVersion } from './passwordVersion.js'
|
||||
import { KUNDENARTEN, kundenartGespeichert } from './kundenart.js'
|
||||
import { neueAnsichtModus } from './kundenansicht/schalter.js'
|
||||
|
||||
function readPortalPassword(customer) {
|
||||
if (customer?.portalPassword) return customer.portalPassword
|
||||
@@ -27,6 +29,17 @@ function notesWithoutPortalPassword(notes) {
|
||||
return (notes || '').replace(/^__portalPassword__:[^\n]*\n?/, '').trim()
|
||||
}
|
||||
|
||||
/** Fehler fuer eine fehlende oder ungueltige Kundenart (400 kundenart_fehlt); die Route reicht `code` durch. */
|
||||
function kundenartFehler() {
|
||||
const error = new Error('Kundenart ist erforderlich (unternehmen oder privat).')
|
||||
error.status = 400
|
||||
error.code = 'kundenart_fehlt'
|
||||
return error
|
||||
}
|
||||
|
||||
/** Als "nicht mitgeschickt" gelten undefined, null und ''. */
|
||||
const kundenartFehlt = (wert) => wert === undefined || wert === null || wert === ''
|
||||
|
||||
function sanitizeCustomer(customer, { portalPassword } = {}) {
|
||||
const storedPassword = portalPassword || readPortalPassword(customer)
|
||||
return {
|
||||
@@ -41,6 +54,7 @@ function sanitizeCustomer(customer, { portalPassword } = {}) {
|
||||
appwriteUserId: customer.appwriteUserId || '',
|
||||
customerStatus: customer.customerStatus || '',
|
||||
archived: Boolean(customer.archived),
|
||||
kundenart: kundenartGespeichert(customer),
|
||||
portalPassword: storedPassword || '',
|
||||
}
|
||||
}
|
||||
@@ -73,6 +87,7 @@ export async function createCustomerWithPortalAccess(payload) {
|
||||
email,
|
||||
phone = '',
|
||||
password,
|
||||
kundenart,
|
||||
} = payload || {}
|
||||
|
||||
if (!name?.trim()) {
|
||||
@@ -90,6 +105,9 @@ export async function createCustomerWithPortalAccess(payload) {
|
||||
error.status = 400
|
||||
throw error
|
||||
}
|
||||
// Kundenart (V4): Pflicht nur ab Schalter pilot (bei off legt das WOMS Kunden wie heute ohne Kundenart an);
|
||||
// ein mitgeschickter, ungueltiger Wert ist in jedem Modus ein Fehler. Vor jedem Schreibzugriff.
|
||||
if (kundenartFehlt(kundenart) ? neueAnsichtModus() !== 'off' : !KUNDENARTEN.includes(kundenart)) throw kundenartFehler()
|
||||
|
||||
const existing = await getCustomerByEmail(email.trim())
|
||||
if (existing) {
|
||||
@@ -117,6 +135,7 @@ export async function createCustomerWithPortalAccess(payload) {
|
||||
portalAccessEnabled: true,
|
||||
appwriteUserId: appwriteUser.$id,
|
||||
customerStatus: 'active',
|
||||
...(kundenartFehlt(kundenart) ? {} : { kundenart }),
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
})
|
||||
@@ -150,6 +169,7 @@ export async function updateCustomerWithPortalAccess(customerId, payload) {
|
||||
email,
|
||||
phone,
|
||||
password,
|
||||
kundenart,
|
||||
} = payload || {}
|
||||
|
||||
const nextEmail = (email ?? customer.email ?? '').trim()
|
||||
@@ -163,6 +183,8 @@ export async function updateCustomerWithPortalAccess(customerId, payload) {
|
||||
error.status = 400
|
||||
throw error
|
||||
}
|
||||
// Kundenart optional: nur wenn mitgeschickt, dann gueltig (V4)
|
||||
if (kundenart !== undefined && !KUNDENARTEN.includes(kundenart)) throw kundenartFehler()
|
||||
|
||||
const normalizedPassword =
|
||||
typeof password === 'string' && password.trim().length > 0 ? password : undefined
|
||||
@@ -216,6 +238,7 @@ export async function updateCustomerWithPortalAccess(customerId, payload) {
|
||||
...(location !== undefined ? { location: String(location).trim() } : {}),
|
||||
email: nextEmail,
|
||||
...(phone !== undefined ? { phone: String(phone).trim() } : {}),
|
||||
...(kundenart !== undefined ? { kundenart } : {}),
|
||||
...(nextPortalPassword ? { portalPassword: nextPortalPassword } : {}),
|
||||
...(notesChanged ? { notes: cleanedNotes } : {}),
|
||||
// CRM-Stufe (lead/customer/lost) nicht ueberschreiben - nur setzen, wenn leer
|
||||
|
||||
Reference in new issue
Block a user