Remove CustomerRepository and replace with direct API calls

- Remove `CustomerRepository` and its methods for customer management and caching.
- Refactor customer-related pages (`[id]/page.tsx`, `customers/page.tsx`) to use direct `fetch` API calls.
- Update breadcrumb resolver to fetch data directly from the API.
- Simplify `addCustomer` use case to avoid repository dependency.
This commit is contained in:
2025-07-11 18:38:44 +02:00
parent 328c0537ba
commit 2a95efb75f
5 changed files with 47 additions and 106 deletions

View File

@@ -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<string, Customer> = new Map();
private static lastFetchAll: number | null = null;
private static readonly CACHE_DURATION = 10 * 1000; // seconds in milliseconds
static async getAll(): Promise<Customer[]> {
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<Customer | null> {
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<void> {
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);
}
}

View File

@@ -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<void> {
const {email, name, companyName, street, zip, city, phoneNumbers, notes} = params;
@@ -17,5 +16,15 @@ export async function addCustomer(params: CreateCustomerDto): Promise<void> {
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}`);
}
}