diff --git a/internal_frontend/app/customers/[id]/page.tsx b/internal_frontend/app/customers/[id]/page.tsx index 51bec34..c5d276b 100644 --- a/internal_frontend/app/customers/[id]/page.tsx +++ b/internal_frontend/app/customers/[id]/page.tsx @@ -11,7 +11,6 @@ import CustomerInformationContent from "@/components/customers/details/sub/Conta import CustomerPhoneNumberContent from "@/components/customers/details/sub/CustomerPhoneNumberContent"; import CustomerNotesContent from "@/components/customers/details/sub/CustomerNotesContent"; import {Customer} from "@/services/customers/entities/customer"; -import {CustomerRepository} from "@/services/customers/repositories/customerRepository"; export default function CustomerDetailPage() { const router = useRouter(); @@ -33,16 +32,22 @@ export default function CustomerDetailPage() { setLoading(true); setError(null); - CustomerRepository.getById(id) - .then((result) => { + fetch(`/api/customers/${id}`) + .then(async (response) => { if (!isMounted) return; - if (!result) { + if (response.status === 404) { setError("Kunde nicht gefunden"); setCustomer(null); - } else { - setCustomer(result); + return; } + + if (!response.ok) { + throw new Error(`Failed to fetch customer: ${response.statusText}`); + } + + const result = await response.json(); + setCustomer(result); }) .catch((error) => { if (!isMounted) return; @@ -164,4 +169,4 @@ export default function CustomerDetailPage() { ); -} \ No newline at end of file +} diff --git a/internal_frontend/app/customers/page.tsx b/internal_frontend/app/customers/page.tsx index 5dfdf96..cd96151 100644 --- a/internal_frontend/app/customers/page.tsx +++ b/internal_frontend/app/customers/page.tsx @@ -24,7 +24,6 @@ import {ArrowRight} from "lucide-react"; import {NewCustomerModal} from "@/components/customers/modal/NewCustomerModal"; import {Customer} from "@/services/customers/entities/customer"; import Link from "next/link"; -import {CustomerRepository} from "@/services/customers/repositories/customerRepository"; export default function CustomersPage() { const [customers, setCustomers] = useState([]); @@ -35,7 +34,13 @@ export default function CustomersPage() { useEffect(() => { setLoading(true); - CustomerRepository.getAll() + fetch('/api/customers') + .then(async (response) => { + if (!response.ok) { + throw new Error(`Failed to fetch customers: ${response.statusText}`); + } + return response.json(); + }) .then((data) => { setCustomers(data); }) @@ -176,4 +181,4 @@ export default function CustomersPage() { ); -} \ No newline at end of file +} diff --git a/internal_frontend/lib/navigation/breadcrumb-map.ts b/internal_frontend/lib/navigation/breadcrumb-map.ts index 7ccdeab..b740a38 100644 --- a/internal_frontend/lib/navigation/breadcrumb-map.ts +++ b/internal_frontend/lib/navigation/breadcrumb-map.ts @@ -1,5 +1,3 @@ -import {CustomerRepository} from "@/services/customers/repositories/customerRepository"; - export const breadcrumbMap: Record = { 'dashboard': 'Dashboard', 'settings': 'Settings', @@ -10,11 +8,21 @@ export const breadcrumbMap: Record = { export const breadcrumbResolvers: Record Promise> = { "customers": async (id: string) => { - const customer = await CustomerRepository.getById(id); - if (!customer) return `ID: ${id}`; + try { + const response = await fetch(`/api/customers/${id}`); + if (!response.ok) { + return `ID: ${id}`; + } - if (customer.companyName) return `Firma: ${customer.companyName}`; - if (customer.name) return `Name: ${customer.name}`; - return `ID: ${id}`; + const customer = await response.json(); + if (!customer) return `ID: ${id}`; + + if (customer.companyName) return `Firma: ${customer.companyName}`; + if (customer.name) return `Name: ${customer.name}`; + return `ID: ${id}`; + } catch (error) { + console.error('Error fetching customer for breadcrumb:', error); + return `ID: ${id}`; + } }, -}; \ No newline at end of file +}; diff --git a/internal_frontend/services/customers/repositories/customerRepository.ts b/internal_frontend/services/customers/repositories/customerRepository.ts deleted file mode 100644 index 90f5010..0000000 --- a/internal_frontend/services/customers/repositories/customerRepository.ts +++ /dev/null @@ -1,86 +0,0 @@ -import {Customer} from "@/services/customers/entities/customer"; -import {CreateCustomerDto} from "@/services/customers/dtos/createCustomer.dto"; - -export class CustomerRepository { - private static readonly customerCache: Map = new Map(); - private static lastFetchAll: number | null = null; - private static readonly CACHE_DURATION = 10 * 1000; // seconds in milliseconds - - static async getAll(): Promise { - const now = Date.now(); - if (this.lastFetchAll && (now - this.lastFetchAll < this.CACHE_DURATION)) { - return Array.from(this.customerCache.values()); - } - - console.log('Cache expired or not initialized, fetching fresh data from API'); - - const response = await fetch('/api/customers'); - if (!response.ok) { - console.error('Failed to fetch customers:', response.status, response.statusText); - return Promise.reject(new Error(`Failed to fetch customers: ${response.statusText}`)); - } - - const customers: Customer[] = await response.json(); - this.customerCache.clear(); - customers.forEach((customer: Customer) => this.customerCache.set(customer.id, customer)); - this.lastFetchAll = now; - return customers; - } - - static async getById(id: string): Promise { - const cachedCustomer = this.customerCache.get(id); - if (cachedCustomer) { - return cachedCustomer; - } - - console.log(`Cache miss for customer ${id}, fetching from API`); - - try { - const response = await fetch(`/api/customers/${id}`); - if (!response.ok) { - if (response.status === 404) { - console.log(`Customer ${id} not found`); - return null; - } - console.error('Failed to fetch customer:', response.status, response.statusText); - return Promise.reject(new Error(`Failed to fetch customer: ${response.statusText}`)); - } - - const customer: Customer = await response.json(); - this.customerCache.set(id, customer); - return customer; - } catch (error) { - console.error('Error fetching customer:', error); - return Promise.reject(error); - } - } - - static async create(payload: CreateCustomerDto): Promise { - const response = await fetch('/api/customers', { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - }, - body: JSON.stringify(payload) - }); - - if (!response.ok) { - console.error('Failed to create customer:', response.status, response.statusText); - return Promise.reject(new Error(`Failed to create customer: ${response.statusText}`)); - } - - console.log('Cache invalidated after creating new customer'); - this.lastFetchAll = null; // Invalidate the cache after creating new customer - } - - static clearCache(): void { - console.log('Cache manually cleared'); - this.customerCache.clear(); - this.lastFetchAll = null; - } - - static updateCache(customer: Customer): void { - console.log(`Cache updated for customer ${customer.id}`); - this.customerCache.set(customer.id, customer); - } -} \ No newline at end of file diff --git a/internal_frontend/services/customers/usecases/addCustomer.ts b/internal_frontend/services/customers/usecases/addCustomer.ts index d124836..5b67721 100644 --- a/internal_frontend/services/customers/usecases/addCustomer.ts +++ b/internal_frontend/services/customers/usecases/addCustomer.ts @@ -1,7 +1,6 @@ "use server"; import {CreateCustomerDto} from "@/services/customers/dtos/createCustomer.dto"; -import {CustomerRepository} from "@/services/customers/repositories/customerRepository"; export async function addCustomer(params: CreateCustomerDto): Promise { const {email, name, companyName, street, zip, city, phoneNumbers, notes} = params; @@ -17,5 +16,15 @@ export async function addCustomer(params: CreateCustomerDto): Promise { notes: notes.map(({text}) => ({text})), }; - await CustomerRepository.create(payload); + const response = await fetch('/api/customers', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify(payload) + }); + + if (!response.ok) { + throw new Error(`Failed to create customer: ${response.statusText}`); + } }