This commit is contained in:
sebseb7
2025-08-02 08:26:08 +02:00
parent 5c416c77f0
commit da435d2e66
6 changed files with 44 additions and 31 deletions

View File

@@ -1,5 +1,6 @@
class KreditorService {
constructor() {
// API is mounted under /api (see src/index.js). Keep consistent with AuthService.
this.baseURL = '/api';
}
@@ -45,7 +46,8 @@ class KreditorService {
async getAllKreditors() {
try {
const response = await fetch(`${this.baseURL}/data/kreditors`, {
const url = `${this.baseURL}/data/kreditors`;
const response = await fetch(url, {
method: 'GET',
headers: await this.getAuthHeaders(),
});
@@ -53,19 +55,23 @@ class KreditorService {
return await this.handleResponse(response);
} catch (error) {
console.error('Error fetching kreditors:', error);
// Handle network errors
if (error instanceof TypeError && error.message.includes('fetch')) {
throw new Error('FibDash Service nicht erreichbar - Prüfen Sie Ihre Internetverbindung oder versuchen Sie es später erneut');
}
throw error;
}
}
// Convenience: find kreditor by kreditorId (string compare)
async findKreditorByCode(kreditorId) {
const all = await this.getAllKreditors();
return (all || []).find(k => String(k.kreditorId).trim() === String(kreditorId).trim());
}
async getKreditorById(id) {
try {
const response = await fetch(`${this.baseURL}/data/kreditors/${id}`, {
const url = `${this.baseURL}/data/kreditors/${id}`;
const response = await fetch(url, {
method: 'GET',
headers: await this.getAuthHeaders(),
});
@@ -73,19 +79,18 @@ class KreditorService {
return await this.handleResponse(response);
} catch (error) {
console.error('Error fetching kreditor:', error);
// Handle network errors
if (error instanceof TypeError && error.message.includes('fetch')) {
throw new Error('FibDash Service nicht erreichbar - Prüfen Sie Ihre Internetverbindung oder versuchen Sie es später erneut');
}
throw error;
}
}
async createKreditor(kreditorData) {
try {
const response = await fetch(`${this.baseURL}/data/kreditors`, {
const url = `${this.baseURL}/data/kreditors`;
console.debug('[KreditorService] POST', url, kreditorData);
const response = await fetch(url, {
method: 'POST',
headers: await this.getAuthHeaders(),
body: JSON.stringify(kreditorData),
@@ -93,20 +98,23 @@ class KreditorService {
return await this.handleResponse(response);
} catch (error) {
// Map unique constraint errors to a clearer duplicate message when possible
const msg = (error && error.message) || '';
if (/unique|bereits vorhanden|already exists|kreditor/i.test(msg)) {
throw new Error('Kreditor bereits vorhanden');
}
console.error('Error creating kreditor:', error);
// Handle network errors
if (error instanceof TypeError && error.message.includes('fetch')) {
throw new Error('FibDash Service nicht erreichbar - Prüfen Sie Ihre Internetverbindung oder versuchen Sie es später erneut');
}
throw error;
}
}
async updateKreditor(id, kreditorData) {
try {
const response = await fetch(`${this.baseURL}/data/kreditors/${id}`, {
const url = `${this.baseURL}/data/kreditors/${id}`;
const response = await fetch(url, {
method: 'PUT',
headers: await this.getAuthHeaders(),
body: JSON.stringify(kreditorData),
@@ -115,19 +123,17 @@ class KreditorService {
return await this.handleResponse(response);
} catch (error) {
console.error('Error updating kreditor:', error);
// Handle network errors
if (error instanceof TypeError && error.message.includes('fetch')) {
throw new Error('FibDash Service nicht erreichbar - Prüfen Sie Ihre Internetverbindung oder versuchen Sie es später erneut');
}
throw error;
}
}
async deleteKreditor(id) {
try {
const response = await fetch(`${this.baseURL}/data/kreditors/${id}`, {
const url = `${this.baseURL}/data/kreditors/${id}`;
const response = await fetch(url, {
method: 'DELETE',
headers: await this.getAuthHeaders(),
});
@@ -135,12 +141,9 @@ class KreditorService {
return await this.handleResponse(response);
} catch (error) {
console.error('Error deleting kreditor:', error);
// Handle network errors
if (error instanceof TypeError && error.message.includes('fetch')) {
throw new Error('FibDash Service nicht erreichbar - Prüfen Sie Ihre Internetverbindung oder versuchen Sie es später erneut');
}
throw error;
}
}