Update mockDb.ts
This commit is contained in:
@@ -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<T>(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<BrandingConfig> => {
|
||||
// Timestamp per evitare caching browser
|
||||
return request<BrandingConfig>(`/public/branding?t=${Date.now()}`);
|
||||
},
|
||||
|
||||
// --- Auth & User ---
|
||||
login: async (email: string, password: string): Promise<void> => {
|
||||
const data = await request<AuthResponse>('/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<AppSettings> => {
|
||||
return request<AppSettings>('/settings');
|
||||
},
|
||||
@@ -88,7 +106,7 @@ export const CondoService = {
|
||||
return request<number[]>('/years');
|
||||
},
|
||||
|
||||
// Condos
|
||||
// --- Condos ---
|
||||
getCondos: async (): Promise<Condo[]> => {
|
||||
return request<Condo[]>('/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<Family[]> => {
|
||||
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<Payment[]> => {
|
||||
return request<Payment[]>(`/payments?familyId=${familyId}`);
|
||||
@@ -193,7 +210,7 @@ export const CondoService = {
|
||||
});
|
||||
},
|
||||
|
||||
// Users
|
||||
// --- Users ---
|
||||
getUsers: async (condoId?: string): Promise<User[]> => {
|
||||
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<AlertDefinition[]> => {
|
||||
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<Notice[]> => {
|
||||
let url = '/notices';
|
||||
const activeId = CondoService.getActiveCondoId();
|
||||
const activeId = condoId || CondoService.getActiveCondoId();
|
||||
if (activeId) url += `?condoId=${activeId}`;
|
||||
return request<Notice[]>(url);
|
||||
},
|
||||
@@ -281,7 +298,7 @@ export const CondoService = {
|
||||
return request(`/notices/${id}`, { method: 'DELETE' });
|
||||
},
|
||||
|
||||
// Tickets
|
||||
// --- Tickets ---
|
||||
getTickets: async (): Promise<Ticket[]> => {
|
||||
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<ExtraordinaryExpense[]> => {
|
||||
let url = '/expenses';
|
||||
const activeId = condoId || CondoService.getActiveCondoId();
|
||||
@@ -377,7 +394,6 @@ export const CondoService = {
|
||||
},
|
||||
|
||||
payExpense: async (expenseId: string, amount: number, familyId?: string): Promise<void> => {
|
||||
// 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<CondoExpense[]> => {
|
||||
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<Document[]> => {
|
||||
const activeId = CondoService.getActiveCondoId();
|
||||
return request<Document[]>(`/documents?condoId=${activeId}`);
|
||||
@@ -432,7 +448,7 @@ export const CondoService = {
|
||||
|
||||
uploadDocument: async (doc: any): Promise<void> => {
|
||||
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 })
|
||||
|
||||
Reference in New Issue
Block a user