From c7d8f99442f90282d1e28c51ffbcbd27dbf7d0d3 Mon Sep 17 00:00:00 2001 From: frakarr Date: Thu, 11 Dec 2025 21:13:58 +0100 Subject: [PATCH] Update api.ts --- services/api.ts | 100 +++++++++++++++++++----------------------------- 1 file changed, 40 insertions(+), 60 deletions(-) diff --git a/services/api.ts b/services/api.ts index 67b77ba..c77c5da 100644 --- a/services/api.ts +++ b/services/api.ts @@ -2,7 +2,7 @@ import { Condo, Family, Payment, AppSettings, User, AuthResponse, Ticket, TicketComment, ExtraordinaryExpense, Notice, - AlertDefinition, NoticeRead + AlertDefinition, NoticeRead, CondoExpense } from '../types'; const API_URL = '/api'; @@ -28,7 +28,6 @@ async function request(endpoint: string, options: RequestInit = {}): Promise< throw new Error(errorText || response.statusText); } - // Handle empty responses const text = await response.text(); return text ? JSON.parse(text) : undefined; } @@ -43,18 +42,15 @@ export const CondoService = { localStorage.setItem('condo_token', data.token); localStorage.setItem('condo_user', JSON.stringify(data.user)); }, - logout: () => { localStorage.removeItem('condo_token'); localStorage.removeItem('condo_user'); window.location.href = '/#/login'; }, - getCurrentUser: (): User | null => { const u = localStorage.getItem('condo_user'); return u ? JSON.parse(u) : null; }, - updateProfile: async (data: any): Promise => { const res = await request<{success: boolean, user: User}>('/profile', { method: 'PUT', @@ -69,21 +65,18 @@ export const CondoService = { getSettings: async (): Promise => { return request('/settings'); }, - updateSettings: async (settings: AppSettings): Promise => { return request('/settings', { method: 'PUT', body: JSON.stringify(settings) }); }, - testSmtpConfig: async (config: any): Promise => { return request('/settings/smtp-test', { method: 'POST', body: JSON.stringify(config) }); }, - getAvailableYears: async (): Promise => { return request('/years'); }, @@ -92,36 +85,28 @@ export const CondoService = { getCondos: async (): Promise => { return request('/condos'); }, - getActiveCondoId: (): string | undefined => { return localStorage.getItem('active_condo_id') || undefined; }, - getActiveCondo: async (): Promise => { const id = localStorage.getItem('active_condo_id'); const condos = await CondoService.getCondos(); - let match; if (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) { const firstCondoId = condos[0].id; localStorage.setItem('active_condo_id', firstCondoId); return condos[0]; } - return match; }, - setActiveCondo: (id: string) => { localStorage.setItem('active_condo_id', id); window.dispatchEvent(new Event('condo-updated')); window.location.reload(); }, - saveCondo: async (condo: Condo): Promise => { if (condo.id) { await request(`/condos/${condo.id}`, { @@ -136,7 +121,6 @@ export const CondoService = { }); } }, - deleteCondo: async (id: string): Promise => { return request(`/condos/${id}`, { method: 'DELETE' }); }, @@ -144,14 +128,10 @@ export const CondoService = { // Families getFamilies: async (condoId?: string): Promise => { 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(); if (activeId) url += `?condoId=${activeId}`; return request(url); }, - addFamily: async (family: any): Promise => { let activeId = CondoService.getActiveCondoId(); if (!activeId) { @@ -166,29 +146,24 @@ export const CondoService = { body: JSON.stringify({ ...family, condoId: activeId }) }); }, - updateFamily: async (family: Family): Promise => { return request(`/families/${family.id}`, { method: 'PUT', body: JSON.stringify(family) }); }, - deleteFamily: async (id: string): Promise => { return request(`/families/${id}`, { method: 'DELETE' }); }, // Payments - seedPayments: () => { /* No-op for real backend */ }, - + seedPayments: () => { }, getPaymentsByFamily: async (familyId: string): Promise => { return request(`/payments?familyId=${familyId}`); }, - getCondoPayments: async (condoId: string): Promise => { return request(`/payments?condoId=${condoId}`); }, - addPayment: async (payment: any): Promise => { return request('/payments', { method: 'POST', @@ -202,32 +177,28 @@ export const CondoService = { if (condoId) url += `?condoId=${condoId}`; return request(url); }, - createUser: async (user: any): Promise => { return request('/users', { method: 'POST', body: JSON.stringify(user) }); }, - updateUser: async (id: string, user: any): Promise => { return request(`/users/${id}`, { method: 'PUT', body: JSON.stringify(user) }); }, - deleteUser: async (id: string): Promise => { return request(`/users/${id}`, { method: 'DELETE' }); }, - // Alerts + // Alerts, Notices, Tickets, Extraordinary... getAlerts: async (condoId?: string): Promise => { let url = '/alerts'; if (condoId) url += `?condoId=${condoId}`; return request(url); }, - saveAlert: async (alert: AlertDefinition): Promise => { let activeId = CondoService.getActiveCondoId(); if (!activeId) { @@ -244,34 +215,27 @@ export const CondoService = { }); } }, - deleteAlert: async (id: string): Promise => { return request(`/alerts/${id}`, { method: 'DELETE' }); }, - - // Notices getNotices: async (condoId?: string): Promise => { let url = '/notices'; const activeId = condoId || CondoService.getActiveCondoId(); if (activeId) url += `?condoId=${activeId}`; return request(url); }, - getUnreadNoticesForUser: async (userId: string, condoId: string): Promise => { return request(`/notices/unread?userId=${userId}&condoId=${condoId}`); }, - getNoticeReadStatus: async (noticeId: string): Promise => { return request(`/notices/${noticeId}/read-status`); }, - markNoticeAsRead: async (noticeId: string, userId: string): Promise => { return request(`/notices/${noticeId}/read`, { method: 'POST', body: JSON.stringify({ userId }) }); }, - saveNotice: async (notice: Notice): Promise => { if (notice.id) { 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) }); } }, - deleteNotice: async (id: string): Promise => { return request(`/notices/${id}`, { method: 'DELETE' }); }, - - // Tickets getTickets: async (): Promise => { let activeId = CondoService.getActiveCondoId(); - // If no active condo, try to self-heal first or let backend handle fallback if (!activeId) { const condos = await CondoService.getCondos(); if(condos.length > 0) activeId = condos[0].id; } return request(`/tickets?condoId=${activeId}`); }, - createTicket: async (data: any): Promise => { let activeId = CondoService.getActiveCondoId(); if (!activeId) { @@ -306,45 +265,36 @@ export const CondoService = { body: JSON.stringify({ ...data, condoId: activeId }) }); }, - updateTicket: async (id: string, data: any): Promise => { return request(`/tickets/${id}`, { method: 'PUT', body: JSON.stringify(data) }); }, - deleteTicket: async (id: string): Promise => { return request(`/tickets/${id}`, { method: 'DELETE' }); }, - getTicketComments: async (ticketId: string): Promise => { return request(`/tickets/${ticketId}/comments`); }, - addTicketComment: async (ticketId: string, text: string): Promise => { return request(`/tickets/${ticketId}/comments`, { method: 'POST', body: JSON.stringify({ text }) }); }, - getTicketAttachment: async (ticketId: string, attachmentId: string): Promise => { return request(`/tickets/${ticketId}/attachments/${attachmentId}`); }, - - // Extraordinary Expenses getExpenses: async (condoId?: string): Promise => { let url = '/expenses'; const activeId = condoId || CondoService.getActiveCondoId(); if (activeId) url += `?condoId=${activeId}`; return request(url); }, - getExpenseDetails: async (id: string): Promise => { return request(`/expenses/${id}`); }, - createExpense: async (data: any): Promise => { let activeId = CondoService.getActiveCondoId(); if (!activeId) { @@ -357,33 +307,63 @@ export const CondoService = { body: JSON.stringify({ ...data, condoId: activeId }) }); }, - updateExpense: async (id: string, data: any): Promise => { return request(`/expenses/${id}`, { method: 'PUT', body: JSON.stringify(data) }); }, - deleteExpense: async (id: string): Promise => { return request(`/expenses/${id}`, { method: 'DELETE' }); }, - getExpenseAttachment: async (expenseId: string, attachmentId: string): Promise => { return request(`/expenses/${expenseId}/attachments/${attachmentId}`); }, - getMyExpenses: async (): Promise => { const activeId = CondoService.getActiveCondoId(); return request(`/my-expenses?condoId=${activeId}`); }, - - payExpense: async (expenseId: string, amount: number): Promise => { + payExpense: async (expenseId: string, amount: number, familyId?: string): Promise => { return request(`/expenses/${expenseId}/pay`, { 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 => { + return request(`/expenses/${expenseId}/shares/${familyId}/payments`); + }, + deleteExpensePayment: async (paymentId: string): Promise => { + return request(`/expenses/payments/${paymentId}`, { method: 'DELETE' }); + }, + + // --- CONDO EXPENSES (ORDINARY/SUPPLIERS) --- + getCondoExpenses: async (year?: number): Promise => { + const activeId = CondoService.getActiveCondoId(); + let url = `/condo-expenses?condoId=${activeId}`; + if (year) url += `&year=${year}`; + return request(url); + }, + saveCondoExpense: async (expense: Partial): Promise => { + 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 => { + return request(`/condo-expenses/${id}`, { method: 'DELETE' }); + }, + getCondoExpenseAttachment: async (expenseId: string, attId: string): Promise => { + return request(`/condo-expenses/${expenseId}/attachments/${attId}`); } };