From a6b3a67175e413a8510bff253a3a996e234de59e Mon Sep 17 00:00:00 2001 From: frakarr Date: Fri, 9 Jan 2026 20:59:53 +0100 Subject: [PATCH] Update mockDb.ts --- services/mockDb.ts | 60 +++++++++++++++++++++++++++++----------------- 1 file changed, 38 insertions(+), 22 deletions(-) diff --git a/services/mockDb.ts b/services/mockDb.ts index 38722d0..7c099b3 100644 --- a/services/mockDb.ts +++ b/services/mockDb.ts @@ -2,7 +2,7 @@ import { Condo, Family, Payment, AppSettings, User, AuthResponse, Ticket, TicketComment, ExtraordinaryExpense, Notice, - AlertDefinition, NoticeRead, CondoExpense, Document + AlertDefinition, NoticeRead, CondoExpense, Document, BrandingConfig } from '../types'; const API_URL = '/api'; @@ -23,18 +23,30 @@ async function request(endpoint: string, options: RequestInit = {}): Promise< headers, }); + // CORREZIONE VITALE: Se il token è scaduto (401), forza il logout immediato + if (response.status === 401) { + console.warn("Sessione scaduta. Logout in corso..."); + CondoService.logout(); + throw new Error("Sessione scaduta"); + } + if (!response.ok) { const errorText = await response.text(); throw new Error(errorText || response.statusText); } - // Handle empty responses const text = await response.text(); return text ? JSON.parse(text) : undefined; } export const CondoService = { - // Auth & User + // --- Public Branding --- + getPublicBranding: async (): Promise => { + // Timestamp per evitare caching browser + return request(`/public/branding?t=${Date.now()}`); + }, + + // --- Auth & User --- login: async (email: string, password: string): Promise => { const data = await request('/auth/login', { method: 'POST', @@ -43,11 +55,17 @@ export const CondoService = { localStorage.setItem('condo_token', data.token); localStorage.setItem('condo_user', JSON.stringify(data.user)); }, - + logout: () => { + // Pulizia completa per evitare stati inconsistenti localStorage.removeItem('condo_token'); localStorage.removeItem('condo_user'); - window.location.href = '/#/login'; + localStorage.removeItem('active_condo_id'); + localStorage.removeItem('lastViewedTickets'); + localStorage.removeItem('lastViewedExpensesTime'); + + // Usa replace per pulire la history + window.location.replace('/#/login'); }, getCurrentUser: (): User | null => { @@ -65,7 +83,7 @@ export const CondoService = { } }, - // Settings + // --- Settings --- getSettings: async (): Promise => { return request('/settings'); }, @@ -88,7 +106,7 @@ export const CondoService = { return request('/years'); }, - // Condos + // --- Condos --- getCondos: async (): Promise => { return request('/condos'); }, @@ -106,13 +124,12 @@ export const CondoService = { 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. + // Logica Fallback: Se non c'è un ID salvato o l'ID non è valido, prendi il primo if (!match && condos.length > 0) { const firstCondoId = condos[0].id; localStorage.setItem('active_condo_id', firstCondoId); return condos[0]; } - return match; }, @@ -141,7 +158,7 @@ export const CondoService = { return request(`/condos/${id}`, { method: 'DELETE' }); }, - // Families + // --- Families --- getFamilies: async (condoId?: string): Promise => { let url = '/families'; const activeId = condoId || CondoService.getActiveCondoId(); @@ -175,8 +192,8 @@ export const CondoService = { return request(`/families/${id}`, { method: 'DELETE' }); }, - // Payments - seedPayments: () => { /* No-op for real backend */ }, + // --- Payments --- + seedPayments: () => { }, getPaymentsByFamily: async (familyId: string): Promise => { return request(`/payments?familyId=${familyId}`); @@ -193,7 +210,7 @@ export const CondoService = { }); }, - // Users + // --- Users --- getUsers: async (condoId?: string): Promise => { let url = '/users'; if (condoId) url += `?condoId=${condoId}`; @@ -218,7 +235,7 @@ export const CondoService = { return request(`/users/${id}`, { method: 'DELETE' }); }, - // Alerts + // --- Alerts --- getAlerts: async (condoId?: string): Promise => { let url = '/alerts'; if (condoId) url += `?condoId=${condoId}`; @@ -246,10 +263,10 @@ export const CondoService = { return request(`/alerts/${id}`, { method: 'DELETE' }); }, - // Notices + // --- Notices (Bacheca) --- getNotices: async (condoId?: string): Promise => { let url = '/notices'; - const activeId = CondoService.getActiveCondoId(); + const activeId = condoId || CondoService.getActiveCondoId(); if (activeId) url += `?condoId=${activeId}`; return request(url); }, @@ -281,7 +298,7 @@ export const CondoService = { return request(`/notices/${id}`, { method: 'DELETE' }); }, - // Tickets + // --- Tickets --- getTickets: async (): Promise => { let activeId = CondoService.getActiveCondoId(); if (!activeId) { @@ -329,7 +346,7 @@ export const CondoService = { return request(`/tickets/${ticketId}/attachments/${attachmentId}`); }, - // Extraordinary Expenses + // --- Extraordinary Expenses --- getExpenses: async (condoId?: string): Promise => { let url = '/expenses'; const activeId = condoId || CondoService.getActiveCondoId(); @@ -377,7 +394,6 @@ export const CondoService = { }, payExpense: async (expenseId: string, amount: number, familyId?: string): Promise => { - // familyId is optional: if passed, it's an admin recording a payment for someone else. return request(`/expenses/${expenseId}/pay`, { method: 'POST', body: JSON.stringify({ amount, notes: 'PayPal / Manual Payment', familyId }) @@ -392,7 +408,7 @@ export const CondoService = { return request(`/expenses/payments/${paymentId}`, { method: 'DELETE' }); }, - // --- CONDO EXPENSES (ORDINARY/SUPPLIERS) --- + // --- Condo Ordinary Expenses (Uscite) --- getCondoExpenses: async (year?: number): Promise => { const activeId = CondoService.getActiveCondoId(); let url = `/condo-expenses?condoId=${activeId}`; @@ -424,7 +440,7 @@ export const CondoService = { return request(`/condo-expenses/${expenseId}/attachments/${attId}`); }, - // --- DOCUMENTS --- + // --- Documents (Cloud/Local) --- getDocuments: async (): Promise => { const activeId = CondoService.getActiveCondoId(); return request(`/documents?condoId=${activeId}`); @@ -432,7 +448,7 @@ export const CondoService = { uploadDocument: async (doc: any): Promise => { const activeId = CondoService.getActiveCondoId(); - const settings = await CondoService.getSettings(); + const settings = await CondoService.getSettings(); return request('/documents', { method: 'POST', body: JSON.stringify({ ...doc, condoId: activeId, storageConfig: settings.storageConfig })