Update mockDb.ts
This commit is contained in:
@@ -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, CondoExpense, Document
|
AlertDefinition, NoticeRead, CondoExpense, Document, BrandingConfig
|
||||||
} from '../types';
|
} from '../types';
|
||||||
|
|
||||||
const API_URL = '/api';
|
const API_URL = '/api';
|
||||||
@@ -23,18 +23,30 @@ async function request<T>(endpoint: string, options: RequestInit = {}): Promise<
|
|||||||
headers,
|
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) {
|
if (!response.ok) {
|
||||||
const errorText = await response.text();
|
const errorText = await response.text();
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const CondoService = {
|
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> => {
|
login: async (email: string, password: string): Promise<void> => {
|
||||||
const data = await request<AuthResponse>('/auth/login', {
|
const data = await request<AuthResponse>('/auth/login', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
@@ -45,9 +57,15 @@ export const CondoService = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
logout: () => {
|
logout: () => {
|
||||||
|
// Pulizia completa per evitare stati inconsistenti
|
||||||
localStorage.removeItem('condo_token');
|
localStorage.removeItem('condo_token');
|
||||||
localStorage.removeItem('condo_user');
|
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 => {
|
getCurrentUser: (): User | null => {
|
||||||
@@ -65,7 +83,7 @@ export const CondoService = {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
// Settings
|
// --- Settings ---
|
||||||
getSettings: async (): Promise<AppSettings> => {
|
getSettings: async (): Promise<AppSettings> => {
|
||||||
return request<AppSettings>('/settings');
|
return request<AppSettings>('/settings');
|
||||||
},
|
},
|
||||||
@@ -88,7 +106,7 @@ export const CondoService = {
|
|||||||
return request<number[]>('/years');
|
return request<number[]>('/years');
|
||||||
},
|
},
|
||||||
|
|
||||||
// Condos
|
// --- Condos ---
|
||||||
getCondos: async (): Promise<Condo[]> => {
|
getCondos: async (): Promise<Condo[]> => {
|
||||||
return request<Condo[]>('/condos');
|
return request<Condo[]>('/condos');
|
||||||
},
|
},
|
||||||
@@ -106,13 +124,12 @@ export const CondoService = {
|
|||||||
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.
|
// Logica Fallback: Se non c'è un ID salvato o l'ID non è valido, prendi il primo
|
||||||
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;
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -141,7 +158,7 @@ export const CondoService = {
|
|||||||
return request(`/condos/${id}`, { method: 'DELETE' });
|
return request(`/condos/${id}`, { method: 'DELETE' });
|
||||||
},
|
},
|
||||||
|
|
||||||
// Families
|
// --- Families ---
|
||||||
getFamilies: async (condoId?: string): Promise<Family[]> => {
|
getFamilies: async (condoId?: string): Promise<Family[]> => {
|
||||||
let url = '/families';
|
let url = '/families';
|
||||||
const activeId = condoId || CondoService.getActiveCondoId();
|
const activeId = condoId || CondoService.getActiveCondoId();
|
||||||
@@ -175,8 +192,8 @@ export const CondoService = {
|
|||||||
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}`);
|
||||||
@@ -193,7 +210,7 @@ export const CondoService = {
|
|||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
// Users
|
// --- Users ---
|
||||||
getUsers: async (condoId?: string): Promise<User[]> => {
|
getUsers: async (condoId?: string): Promise<User[]> => {
|
||||||
let url = '/users';
|
let url = '/users';
|
||||||
if (condoId) url += `?condoId=${condoId}`;
|
if (condoId) url += `?condoId=${condoId}`;
|
||||||
@@ -218,7 +235,7 @@ export const CondoService = {
|
|||||||
return request(`/users/${id}`, { method: 'DELETE' });
|
return request(`/users/${id}`, { method: 'DELETE' });
|
||||||
},
|
},
|
||||||
|
|
||||||
// Alerts
|
// --- Alerts ---
|
||||||
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}`;
|
||||||
@@ -246,10 +263,10 @@ export const CondoService = {
|
|||||||
return request(`/alerts/${id}`, { method: 'DELETE' });
|
return request(`/alerts/${id}`, { method: 'DELETE' });
|
||||||
},
|
},
|
||||||
|
|
||||||
// Notices
|
// --- Notices (Bacheca) ---
|
||||||
getNotices: async (condoId?: string): Promise<Notice[]> => {
|
getNotices: async (condoId?: string): Promise<Notice[]> => {
|
||||||
let url = '/notices';
|
let url = '/notices';
|
||||||
const activeId = 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);
|
||||||
},
|
},
|
||||||
@@ -281,7 +298,7 @@ export const CondoService = {
|
|||||||
return request(`/notices/${id}`, { method: 'DELETE' });
|
return request(`/notices/${id}`, { method: 'DELETE' });
|
||||||
},
|
},
|
||||||
|
|
||||||
// Tickets
|
// --- Tickets ---
|
||||||
getTickets: async (): Promise<Ticket[]> => {
|
getTickets: async (): Promise<Ticket[]> => {
|
||||||
let activeId = CondoService.getActiveCondoId();
|
let activeId = CondoService.getActiveCondoId();
|
||||||
if (!activeId) {
|
if (!activeId) {
|
||||||
@@ -329,7 +346,7 @@ export const CondoService = {
|
|||||||
return request(`/tickets/${ticketId}/attachments/${attachmentId}`);
|
return request(`/tickets/${ticketId}/attachments/${attachmentId}`);
|
||||||
},
|
},
|
||||||
|
|
||||||
// Extraordinary Expenses
|
// --- 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();
|
||||||
@@ -377,7 +394,6 @@ export const CondoService = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
payExpense: async (expenseId: string, amount: number, familyId?: string): Promise<void> => {
|
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`, {
|
return request(`/expenses/${expenseId}/pay`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
body: JSON.stringify({ amount, notes: 'PayPal / Manual Payment', familyId })
|
body: JSON.stringify({ amount, notes: 'PayPal / Manual Payment', familyId })
|
||||||
@@ -392,7 +408,7 @@ export const CondoService = {
|
|||||||
return request(`/expenses/payments/${paymentId}`, { method: 'DELETE' });
|
return request(`/expenses/payments/${paymentId}`, { method: 'DELETE' });
|
||||||
},
|
},
|
||||||
|
|
||||||
// --- CONDO EXPENSES (ORDINARY/SUPPLIERS) ---
|
// --- Condo Ordinary Expenses (Uscite) ---
|
||||||
getCondoExpenses: async (year?: number): Promise<CondoExpense[]> => {
|
getCondoExpenses: async (year?: number): Promise<CondoExpense[]> => {
|
||||||
const activeId = CondoService.getActiveCondoId();
|
const activeId = CondoService.getActiveCondoId();
|
||||||
let url = `/condo-expenses?condoId=${activeId}`;
|
let url = `/condo-expenses?condoId=${activeId}`;
|
||||||
@@ -424,7 +440,7 @@ export const CondoService = {
|
|||||||
return request(`/condo-expenses/${expenseId}/attachments/${attId}`);
|
return request(`/condo-expenses/${expenseId}/attachments/${attId}`);
|
||||||
},
|
},
|
||||||
|
|
||||||
// --- DOCUMENTS ---
|
// --- Documents (Cloud/Local) ---
|
||||||
getDocuments: async (): Promise<Document[]> => {
|
getDocuments: async (): Promise<Document[]> => {
|
||||||
const activeId = CondoService.getActiveCondoId();
|
const activeId = CondoService.getActiveCondoId();
|
||||||
return request<Document[]>(`/documents?condoId=${activeId}`);
|
return request<Document[]>(`/documents?condoId=${activeId}`);
|
||||||
|
|||||||
Reference in New Issue
Block a user