diff --git a/src/hooks/useCustomers.js b/src/hooks/useCustomers.js index c920f9f..55cde48 100644 --- a/src/hooks/useCustomers.js +++ b/src/hooks/useCustomers.js @@ -1,5 +1,9 @@ import { useState, useEffect, useCallback } from 'react' import { databases, DATABASE_ID, COLLECTIONS, ID, Query, isDemoMode } from '../lib/appwrite' +import { + createCustomerWithPortalAccess, + updateCustomerWithPortalAccess, +} from '../lib/customerAdminApi' const DEMO_MODE = isDemoMode @@ -50,20 +54,18 @@ export function useCustomers() { const createCustomer = async (data) => { if (DEMO_MODE) { - const newCustomer = { ...data, $id: Date.now().toString() } + const { password: _pw, ...rest } = data + const newCustomer = { ...rest, $id: Date.now().toString() } setCustomers(prev => [...prev, newCustomer]) return { success: true, data: newCustomer } } try { - const response = await databases.createDocument( - DATABASE_ID, - COLLECTIONS.CUSTOMERS, - ID.unique(), - data - ) - setCustomers(prev => [...prev, response]) - return { success: true, data: response } + const { password, ...fields } = data + const result = await createCustomerWithPortalAccess({ ...fields, password }) + const customer = result.customer + setCustomers(prev => [...prev, customer]) + return { success: true, data: customer } } catch (err) { return { success: false, error: err.message } } @@ -71,19 +73,20 @@ export function useCustomers() { const updateCustomer = async (id, data) => { if (DEMO_MODE) { - setCustomers(prev => prev.map(c => c.$id === id ? { ...c, ...data } : c)) + const { password: _pw, ...rest } = data + setCustomers(prev => prev.map(c => c.$id === id ? { ...c, ...rest } : c)) return { success: true } } try { - const response = await databases.updateDocument( - DATABASE_ID, - COLLECTIONS.CUSTOMERS, - id, - data - ) - setCustomers(prev => prev.map(c => c.$id === id ? response : c)) - return { success: true, data: response } + const { password, ...fields } = data + const payload = { ...fields } + if (password) payload.password = password + + const result = await updateCustomerWithPortalAccess(id, payload) + const customer = result.customer + setCustomers(prev => prev.map(c => c.$id === id ? customer : c)) + return { success: true, data: customer } } catch (err) { return { success: false, error: err.message } } diff --git a/src/lib/customerAdminApi.js b/src/lib/customerAdminApi.js new file mode 100644 index 0000000..1787525 --- /dev/null +++ b/src/lib/customerAdminApi.js @@ -0,0 +1,32 @@ +import { account } from './appwrite' + +const PROJECT_ADMIN_URL = + import.meta.env.VITE_PROJECT_ADMIN_URL || 'https://project.webklar.com' + +async function adminFetch(path, { method = 'POST', body } = {}) { + const jwt = await account.createJWT() + const response = await fetch(`${PROJECT_ADMIN_URL}${path}`, { + method, + headers: { + Authorization: `Bearer ${jwt.jwt}`, + 'Content-Type': 'application/json', + }, + body: body ? JSON.stringify(body) : undefined, + }) + const data = await response.json().catch(() => ({})) + if (!response.ok) { + throw new Error(data.error || `API-Fehler ${response.status}`) + } + return data +} + +export async function createCustomerWithPortalAccess(payload) { + return adminFetch('/api/admin/customers', { method: 'POST', body: payload }) +} + +export async function updateCustomerWithPortalAccess(customerId, payload) { + return adminFetch(`/api/admin/customers/${customerId}`, { + method: 'PATCH', + body: payload, + }) +} diff --git a/src/pages/AdminPage.jsx b/src/pages/AdminPage.jsx index 515aaae..ba44e68 100644 --- a/src/pages/AdminPage.jsx +++ b/src/pages/AdminPage.jsx @@ -27,7 +27,22 @@ export default function AdminPage() { const [saving, setSaving] = useState(false) const [saveMessage, setSaveMessage] = useState('') const [editingCustomer, setEditingCustomer] = useState(null) - const [customerForm, setCustomerForm] = useState({ code: '', name: '', location: '', email: '', phone: '' }) + const [customerForm, setCustomerForm] = useState({ + code: '', + name: '', + location: '', + email: '', + phone: '', + password: '', + }) + const emptyCustomerForm = { + code: '', + name: '', + location: '', + email: '', + phone: '', + password: '', + } const [editingEmployee, setEditingEmployee] = useState(null) const [employeeForm, setEmployeeForm] = useState({ userId: '', displayName: '', email: '', shortcode: '' }) const [adminSection, setAdminSection] = useState('config') @@ -384,6 +399,7 @@ export default function AdminPage() {