Update api.ts

This commit is contained in:
2025-12-11 21:13:58 +01:00
committed by GitHub
parent 116fad793c
commit c7d8f99442

View File

@@ -2,7 +2,7 @@
import { import {
Condo, Family, Payment, AppSettings, User, AuthResponse, Condo, Family, Payment, AppSettings, User, AuthResponse,
Ticket, TicketComment, ExtraordinaryExpense, Notice, Ticket, TicketComment, ExtraordinaryExpense, Notice,
AlertDefinition, NoticeRead AlertDefinition, NoticeRead, CondoExpense
} from '../types'; } from '../types';
const API_URL = '/api'; const API_URL = '/api';
@@ -28,7 +28,6 @@ async function request<T>(endpoint: string, options: RequestInit = {}): Promise<
throw new Error(errorText || response.statusText); throw new Error(errorText || response.statusText);
} }
// Handle empty responses
const text = await response.text(); const text = await response.text();
return text ? JSON.parse(text) : undefined; return text ? JSON.parse(text) : undefined;
} }
@@ -43,18 +42,15 @@ export const CondoService = {
localStorage.setItem('condo_token', data.token); localStorage.setItem('condo_token', data.token);
localStorage.setItem('condo_user', JSON.stringify(data.user)); localStorage.setItem('condo_user', JSON.stringify(data.user));
}, },
logout: () => { logout: () => {
localStorage.removeItem('condo_token'); localStorage.removeItem('condo_token');
localStorage.removeItem('condo_user'); localStorage.removeItem('condo_user');
window.location.href = '/#/login'; window.location.href = '/#/login';
}, },
getCurrentUser: (): User | null => { getCurrentUser: (): User | null => {
const u = localStorage.getItem('condo_user'); const u = localStorage.getItem('condo_user');
return u ? JSON.parse(u) : null; return u ? JSON.parse(u) : null;
}, },
updateProfile: async (data: any): Promise<void> => { updateProfile: async (data: any): Promise<void> => {
const res = await request<{success: boolean, user: User}>('/profile', { const res = await request<{success: boolean, user: User}>('/profile', {
method: 'PUT', method: 'PUT',
@@ -69,21 +65,18 @@ export const CondoService = {
getSettings: async (): Promise<AppSettings> => { getSettings: async (): Promise<AppSettings> => {
return request<AppSettings>('/settings'); return request<AppSettings>('/settings');
}, },
updateSettings: async (settings: AppSettings): Promise<void> => { updateSettings: async (settings: AppSettings): Promise<void> => {
return request('/settings', { return request('/settings', {
method: 'PUT', method: 'PUT',
body: JSON.stringify(settings) body: JSON.stringify(settings)
}); });
}, },
testSmtpConfig: async (config: any): Promise<void> => { testSmtpConfig: async (config: any): Promise<void> => {
return request('/settings/smtp-test', { return request('/settings/smtp-test', {
method: 'POST', method: 'POST',
body: JSON.stringify(config) body: JSON.stringify(config)
}); });
}, },
getAvailableYears: async (): Promise<number[]> => { getAvailableYears: async (): Promise<number[]> => {
return request<number[]>('/years'); return request<number[]>('/years');
}, },
@@ -92,36 +85,28 @@ export const CondoService = {
getCondos: async (): Promise<Condo[]> => { getCondos: async (): Promise<Condo[]> => {
return request<Condo[]>('/condos'); return request<Condo[]>('/condos');
}, },
getActiveCondoId: (): string | undefined => { getActiveCondoId: (): string | undefined => {
return localStorage.getItem('active_condo_id') || undefined; return localStorage.getItem('active_condo_id') || undefined;
}, },
getActiveCondo: async (): Promise<Condo | undefined> => { getActiveCondo: async (): Promise<Condo | undefined> => {
const id = localStorage.getItem('active_condo_id'); const id = localStorage.getItem('active_condo_id');
const condos = await CondoService.getCondos(); const condos = await CondoService.getCondos();
let match; let match;
if (id) { if (id) {
match = condos.find(c => c.id === id); match = condos.find(c => c.id === id);
} }
// Auto-repair: If the stored ID matches nothing in the new DB, but we have condos, default to the first one.
if (!match && condos.length > 0) { if (!match && condos.length > 0) {
const firstCondoId = condos[0].id; const firstCondoId = condos[0].id;
localStorage.setItem('active_condo_id', firstCondoId); localStorage.setItem('active_condo_id', firstCondoId);
return condos[0]; return condos[0];
} }
return match; return match;
}, },
setActiveCondo: (id: string) => { setActiveCondo: (id: string) => {
localStorage.setItem('active_condo_id', id); localStorage.setItem('active_condo_id', id);
window.dispatchEvent(new Event('condo-updated')); window.dispatchEvent(new Event('condo-updated'));
window.location.reload(); window.location.reload();
}, },
saveCondo: async (condo: Condo): Promise<Condo> => { saveCondo: async (condo: Condo): Promise<Condo> => {
if (condo.id) { if (condo.id) {
await request(`/condos/${condo.id}`, { await request(`/condos/${condo.id}`, {
@@ -136,7 +121,6 @@ export const CondoService = {
}); });
} }
}, },
deleteCondo: async (id: string): Promise<void> => { deleteCondo: async (id: string): Promise<void> => {
return request(`/condos/${id}`, { method: 'DELETE' }); return request(`/condos/${id}`, { method: 'DELETE' });
}, },
@@ -144,14 +128,10 @@ export const CondoService = {
// Families // Families
getFamilies: async (condoId?: string): Promise<Family[]> => { getFamilies: async (condoId?: string): Promise<Family[]> => {
let url = '/families'; let url = '/families';
// Use explicit ID if provided, otherwise fetch active from storage
// WARNING: If storage is stale, this returns empty. The App Layout calls getActiveCondo() which fixes storage.
// But to be safe, we rely on the component passing the condoId usually.
const activeId = condoId || CondoService.getActiveCondoId(); const activeId = condoId || CondoService.getActiveCondoId();
if (activeId) url += `?condoId=${activeId}`; if (activeId) url += `?condoId=${activeId}`;
return request<Family[]>(url); return request<Family[]>(url);
}, },
addFamily: async (family: any): Promise<Family> => { addFamily: async (family: any): Promise<Family> => {
let activeId = CondoService.getActiveCondoId(); let activeId = CondoService.getActiveCondoId();
if (!activeId) { if (!activeId) {
@@ -166,29 +146,24 @@ export const CondoService = {
body: JSON.stringify({ ...family, condoId: activeId }) body: JSON.stringify({ ...family, condoId: activeId })
}); });
}, },
updateFamily: async (family: Family): Promise<void> => { updateFamily: async (family: Family): Promise<void> => {
return request(`/families/${family.id}`, { return request(`/families/${family.id}`, {
method: 'PUT', method: 'PUT',
body: JSON.stringify(family) body: JSON.stringify(family)
}); });
}, },
deleteFamily: async (id: string): Promise<void> => { deleteFamily: async (id: string): Promise<void> => {
return request(`/families/${id}`, { method: 'DELETE' }); return request(`/families/${id}`, { method: 'DELETE' });
}, },
// Payments // Payments
seedPayments: () => { /* No-op for real backend */ }, seedPayments: () => { },
getPaymentsByFamily: async (familyId: string): Promise<Payment[]> => { getPaymentsByFamily: async (familyId: string): Promise<Payment[]> => {
return request<Payment[]>(`/payments?familyId=${familyId}`); return request<Payment[]>(`/payments?familyId=${familyId}`);
}, },
getCondoPayments: async (condoId: string): Promise<Payment[]> => { getCondoPayments: async (condoId: string): Promise<Payment[]> => {
return request<Payment[]>(`/payments?condoId=${condoId}`); return request<Payment[]>(`/payments?condoId=${condoId}`);
}, },
addPayment: async (payment: any): Promise<Payment> => { addPayment: async (payment: any): Promise<Payment> => {
return request<Payment>('/payments', { return request<Payment>('/payments', {
method: 'POST', method: 'POST',
@@ -202,32 +177,28 @@ export const CondoService = {
if (condoId) url += `?condoId=${condoId}`; if (condoId) url += `?condoId=${condoId}`;
return request<User[]>(url); return request<User[]>(url);
}, },
createUser: async (user: any): Promise<void> => { createUser: async (user: any): Promise<void> => {
return request('/users', { return request('/users', {
method: 'POST', method: 'POST',
body: JSON.stringify(user) body: JSON.stringify(user)
}); });
}, },
updateUser: async (id: string, user: any): Promise<void> => { updateUser: async (id: string, user: any): Promise<void> => {
return request(`/users/${id}`, { return request(`/users/${id}`, {
method: 'PUT', method: 'PUT',
body: JSON.stringify(user) body: JSON.stringify(user)
}); });
}, },
deleteUser: async (id: string): Promise<void> => { deleteUser: async (id: string): Promise<void> => {
return request(`/users/${id}`, { method: 'DELETE' }); return request(`/users/${id}`, { method: 'DELETE' });
}, },
// Alerts // Alerts, Notices, Tickets, Extraordinary...
getAlerts: async (condoId?: string): Promise<AlertDefinition[]> => { getAlerts: async (condoId?: string): Promise<AlertDefinition[]> => {
let url = '/alerts'; let url = '/alerts';
if (condoId) url += `?condoId=${condoId}`; if (condoId) url += `?condoId=${condoId}`;
return request<AlertDefinition[]>(url); return request<AlertDefinition[]>(url);
}, },
saveAlert: async (alert: AlertDefinition): Promise<AlertDefinition> => { saveAlert: async (alert: AlertDefinition): Promise<AlertDefinition> => {
let activeId = CondoService.getActiveCondoId(); let activeId = CondoService.getActiveCondoId();
if (!activeId) { if (!activeId) {
@@ -244,34 +215,27 @@ export const CondoService = {
}); });
} }
}, },
deleteAlert: async (id: string): Promise<void> => { deleteAlert: async (id: string): Promise<void> => {
return request(`/alerts/${id}`, { method: 'DELETE' }); return request(`/alerts/${id}`, { method: 'DELETE' });
}, },
// Notices
getNotices: async (condoId?: string): Promise<Notice[]> => { getNotices: async (condoId?: string): Promise<Notice[]> => {
let url = '/notices'; let url = '/notices';
const activeId = condoId || CondoService.getActiveCondoId(); const activeId = condoId || CondoService.getActiveCondoId();
if (activeId) url += `?condoId=${activeId}`; if (activeId) url += `?condoId=${activeId}`;
return request<Notice[]>(url); return request<Notice[]>(url);
}, },
getUnreadNoticesForUser: async (userId: string, condoId: string): Promise<Notice[]> => { getUnreadNoticesForUser: async (userId: string, condoId: string): Promise<Notice[]> => {
return request<Notice[]>(`/notices/unread?userId=${userId}&condoId=${condoId}`); return request<Notice[]>(`/notices/unread?userId=${userId}&condoId=${condoId}`);
}, },
getNoticeReadStatus: async (noticeId: string): Promise<NoticeRead[]> => { getNoticeReadStatus: async (noticeId: string): Promise<NoticeRead[]> => {
return request<NoticeRead[]>(`/notices/${noticeId}/read-status`); return request<NoticeRead[]>(`/notices/${noticeId}/read-status`);
}, },
markNoticeAsRead: async (noticeId: string, userId: string): Promise<void> => { markNoticeAsRead: async (noticeId: string, userId: string): Promise<void> => {
return request(`/notices/${noticeId}/read`, { return request(`/notices/${noticeId}/read`, {
method: 'POST', method: 'POST',
body: JSON.stringify({ userId }) body: JSON.stringify({ userId })
}); });
}, },
saveNotice: async (notice: Notice): Promise<void> => { saveNotice: async (notice: Notice): Promise<void> => {
if (notice.id) { if (notice.id) {
return request(`/notices/${notice.id}`, { method: 'PUT', body: JSON.stringify(notice) }); return request(`/notices/${notice.id}`, { method: 'PUT', body: JSON.stringify(notice) });
@@ -279,22 +243,17 @@ export const CondoService = {
return request('/notices', { method: 'POST', body: JSON.stringify(notice) }); return request('/notices', { method: 'POST', body: JSON.stringify(notice) });
} }
}, },
deleteNotice: async (id: string): Promise<void> => { deleteNotice: async (id: string): Promise<void> => {
return request(`/notices/${id}`, { method: 'DELETE' }); return request(`/notices/${id}`, { method: 'DELETE' });
}, },
// Tickets
getTickets: async (): Promise<Ticket[]> => { getTickets: async (): Promise<Ticket[]> => {
let activeId = CondoService.getActiveCondoId(); let activeId = CondoService.getActiveCondoId();
// If no active condo, try to self-heal first or let backend handle fallback
if (!activeId) { if (!activeId) {
const condos = await CondoService.getCondos(); const condos = await CondoService.getCondos();
if(condos.length > 0) activeId = condos[0].id; if(condos.length > 0) activeId = condos[0].id;
} }
return request<Ticket[]>(`/tickets?condoId=${activeId}`); return request<Ticket[]>(`/tickets?condoId=${activeId}`);
}, },
createTicket: async (data: any): Promise<void> => { createTicket: async (data: any): Promise<void> => {
let activeId = CondoService.getActiveCondoId(); let activeId = CondoService.getActiveCondoId();
if (!activeId) { if (!activeId) {
@@ -306,45 +265,36 @@ export const CondoService = {
body: JSON.stringify({ ...data, condoId: activeId }) body: JSON.stringify({ ...data, condoId: activeId })
}); });
}, },
updateTicket: async (id: string, data: any): Promise<void> => { updateTicket: async (id: string, data: any): Promise<void> => {
return request(`/tickets/${id}`, { return request(`/tickets/${id}`, {
method: 'PUT', method: 'PUT',
body: JSON.stringify(data) body: JSON.stringify(data)
}); });
}, },
deleteTicket: async (id: string): Promise<void> => { deleteTicket: async (id: string): Promise<void> => {
return request(`/tickets/${id}`, { method: 'DELETE' }); return request(`/tickets/${id}`, { method: 'DELETE' });
}, },
getTicketComments: async (ticketId: string): Promise<TicketComment[]> => { getTicketComments: async (ticketId: string): Promise<TicketComment[]> => {
return request<TicketComment[]>(`/tickets/${ticketId}/comments`); return request<TicketComment[]>(`/tickets/${ticketId}/comments`);
}, },
addTicketComment: async (ticketId: string, text: string): Promise<void> => { addTicketComment: async (ticketId: string, text: string): Promise<void> => {
return request(`/tickets/${ticketId}/comments`, { return request(`/tickets/${ticketId}/comments`, {
method: 'POST', method: 'POST',
body: JSON.stringify({ text }) body: JSON.stringify({ text })
}); });
}, },
getTicketAttachment: async (ticketId: string, attachmentId: string): Promise<any> => { getTicketAttachment: async (ticketId: string, attachmentId: string): Promise<any> => {
return request(`/tickets/${ticketId}/attachments/${attachmentId}`); return request(`/tickets/${ticketId}/attachments/${attachmentId}`);
}, },
// Extraordinary Expenses
getExpenses: async (condoId?: string): Promise<ExtraordinaryExpense[]> => { getExpenses: async (condoId?: string): Promise<ExtraordinaryExpense[]> => {
let url = '/expenses'; let url = '/expenses';
const activeId = condoId || CondoService.getActiveCondoId(); const activeId = condoId || CondoService.getActiveCondoId();
if (activeId) url += `?condoId=${activeId}`; if (activeId) url += `?condoId=${activeId}`;
return request<ExtraordinaryExpense[]>(url); return request<ExtraordinaryExpense[]>(url);
}, },
getExpenseDetails: async (id: string): Promise<ExtraordinaryExpense> => { getExpenseDetails: async (id: string): Promise<ExtraordinaryExpense> => {
return request<ExtraordinaryExpense>(`/expenses/${id}`); return request<ExtraordinaryExpense>(`/expenses/${id}`);
}, },
createExpense: async (data: any): Promise<void> => { createExpense: async (data: any): Promise<void> => {
let activeId = CondoService.getActiveCondoId(); let activeId = CondoService.getActiveCondoId();
if (!activeId) { if (!activeId) {
@@ -357,33 +307,63 @@ export const CondoService = {
body: JSON.stringify({ ...data, condoId: activeId }) body: JSON.stringify({ ...data, condoId: activeId })
}); });
}, },
updateExpense: async (id: string, data: any): Promise<void> => { updateExpense: async (id: string, data: any): Promise<void> => {
return request(`/expenses/${id}`, { return request(`/expenses/${id}`, {
method: 'PUT', method: 'PUT',
body: JSON.stringify(data) body: JSON.stringify(data)
}); });
}, },
deleteExpense: async (id: string): Promise<void> => { deleteExpense: async (id: string): Promise<void> => {
return request(`/expenses/${id}`, { return request(`/expenses/${id}`, {
method: 'DELETE' method: 'DELETE'
}); });
}, },
getExpenseAttachment: async (expenseId: string, attachmentId: string): Promise<any> => { getExpenseAttachment: async (expenseId: string, attachmentId: string): Promise<any> => {
return request(`/expenses/${expenseId}/attachments/${attachmentId}`); return request(`/expenses/${expenseId}/attachments/${attachmentId}`);
}, },
getMyExpenses: async (): Promise<any[]> => { getMyExpenses: async (): Promise<any[]> => {
const activeId = CondoService.getActiveCondoId(); const activeId = CondoService.getActiveCondoId();
return request(`/my-expenses?condoId=${activeId}`); return request(`/my-expenses?condoId=${activeId}`);
}, },
payExpense: async (expenseId: string, amount: number, familyId?: string): Promise<void> => {
payExpense: async (expenseId: string, amount: number): Promise<void> => {
return request(`/expenses/${expenseId}/pay`, { return request(`/expenses/${expenseId}/pay`, {
method: 'POST', method: 'POST',
body: JSON.stringify({ amount, notes: 'PayPal Payment' }) body: JSON.stringify({ amount, notes: 'PayPal / Manual Payment', familyId })
});
},
getExpensePayments: async (expenseId: string, familyId: string): Promise<any[]> => {
return request<any[]>(`/expenses/${expenseId}/shares/${familyId}/payments`);
},
deleteExpensePayment: async (paymentId: string): Promise<void> => {
return request(`/expenses/payments/${paymentId}`, { method: 'DELETE' });
},
// --- CONDO EXPENSES (ORDINARY/SUPPLIERS) ---
getCondoExpenses: async (year?: number): Promise<CondoExpense[]> => {
const activeId = CondoService.getActiveCondoId();
let url = `/condo-expenses?condoId=${activeId}`;
if (year) url += `&year=${year}`;
return request<CondoExpense[]>(url);
},
saveCondoExpense: async (expense: Partial<CondoExpense>): Promise<void> => {
const activeId = CondoService.getActiveCondoId();
const payload = { ...expense, condoId: activeId };
if (expense.id) {
return request(`/condo-expenses/${expense.id}`, {
method: 'PUT',
body: JSON.stringify(payload)
});
} else {
return request('/condo-expenses', {
method: 'POST',
body: JSON.stringify(payload)
}); });
} }
},
deleteCondoExpense: async (id: string): Promise<void> => {
return request(`/condo-expenses/${id}`, { method: 'DELETE' });
},
getCondoExpenseAttachment: async (expenseId: string, attId: string): Promise<any> => {
return request(`/condo-expenses/${expenseId}/attachments/${attId}`);
}
}; };