import {CreateCustomerDto} from "@/services/customers/dtos/createCustomer.dto"; import {UUID} from "node:crypto"; export async function addCustomer(params: CreateCustomerDto): Promise { const {email, name, companyName, street, zip, city, phoneNumbers, notes} = params; const payload: CreateCustomerDto = { email, name, companyName, street, zip, city, phoneNumbers, notes: notes.map(({text}) => ({text})), }; const res = await fetch('/api/customers', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(payload), }); const contentType = res.headers.get("content-type") ?? ""; const isJson = contentType.includes("application/json"); const rawBody = isJson ? await res.json() : await res.text(); console.log(`[api /api/customers] Response:`, res.status, rawBody); if (!res.ok) { const errorMessage = isJson ? (rawBody?.message ?? rawBody?.errors?.join(", ")) ?? "Unbekannter Fehler" : String(rawBody); // Log correlation ID if available for debugging if (isJson && rawBody?.correlationId) { console.error(`[api /api/customers] Error occurred [${rawBody.correlationId}]:`, errorMessage); } throw new Error(errorMessage); } const response = rawBody as UUID; console.log(response); }