This commit shifts the application's data fetching mechanism from local storage mocks to a dedicated API endpoint. It also refactors the database connection logic to utilize a connection pool for improved performance and scalability. Key changes include: - Disabling `FORCE_LOCAL_DB` in `mockDb.ts` and implementing a generic `request` function for API calls. - Centralizing authentication headers in `mockDb.ts`. - Modifying `server/db.js` to use `pg` and `mysql2/promise` pools and a unified `executeQuery` function. - Updating `server/server.js` to use the database pool for queries. - Configuring Vite's development server to proxy API requests to the backend.
293 lines
8.6 KiB
TypeScript
293 lines
8.6 KiB
TypeScript
|
|
import { Family, Payment, AppSettings, User, AlertDefinition, Condo, Notice, NoticeRead } from '../types';
|
|
|
|
// --- CONFIGURATION TOGGLE ---
|
|
const FORCE_LOCAL_DB = false;
|
|
const API_URL = '/api';
|
|
|
|
const STORAGE_KEYS = {
|
|
TOKEN: 'condo_auth_token',
|
|
USER: 'condo_user_info',
|
|
ACTIVE_CONDO_ID: 'condo_active_id',
|
|
};
|
|
|
|
const getAuthHeaders = () => {
|
|
const token = localStorage.getItem(STORAGE_KEYS.TOKEN);
|
|
return token ? { 'Authorization': `Bearer ${token}`, 'Content-Type': 'application/json' } : { 'Content-Type': 'application/json' };
|
|
};
|
|
|
|
const request = async <T>(endpoint: string, options: RequestInit = {}): Promise<T> => {
|
|
const response = await fetch(`${API_URL}${endpoint}`, {
|
|
...options,
|
|
headers: {
|
|
...getAuthHeaders(),
|
|
...options.headers,
|
|
},
|
|
});
|
|
|
|
if (response.status === 401) {
|
|
CondoService.logout();
|
|
throw new Error("Unauthorized");
|
|
}
|
|
|
|
if (!response.ok) {
|
|
const errText = await response.text();
|
|
throw new Error(errText || `API Error: ${response.status}`);
|
|
}
|
|
|
|
return response.json();
|
|
};
|
|
|
|
export const CondoService = {
|
|
|
|
// --- CONDO CONTEXT MANAGEMENT ---
|
|
|
|
getActiveCondoId: (): string | null => {
|
|
return localStorage.getItem(STORAGE_KEYS.ACTIVE_CONDO_ID);
|
|
},
|
|
|
|
setActiveCondo: (condoId: string) => {
|
|
localStorage.setItem(STORAGE_KEYS.ACTIVE_CONDO_ID, condoId);
|
|
window.location.reload();
|
|
},
|
|
|
|
getCondos: async (): Promise<Condo[]> => {
|
|
return request<Condo[]>('/condos');
|
|
},
|
|
|
|
getActiveCondo: async (): Promise<Condo | undefined> => {
|
|
const condos = await CondoService.getCondos();
|
|
const activeId = CondoService.getActiveCondoId();
|
|
if (!activeId && condos.length > 0) {
|
|
// Do not reload here, just set it silently or let the UI handle it
|
|
localStorage.setItem(STORAGE_KEYS.ACTIVE_CONDO_ID, condos[0].id);
|
|
return condos[0];
|
|
}
|
|
return condos.find(c => c.id === activeId);
|
|
},
|
|
|
|
saveCondo: async (condo: Condo): Promise<Condo> => {
|
|
// If no ID, it's a creation
|
|
if (!condo.id || condo.id.length < 5) { // Simple check if it's a new ID request
|
|
return request<Condo>('/condos', {
|
|
method: 'POST',
|
|
body: JSON.stringify(condo)
|
|
});
|
|
} else {
|
|
return request<Condo>(`/condos/${condo.id}`, {
|
|
method: 'PUT',
|
|
body: JSON.stringify(condo)
|
|
});
|
|
}
|
|
},
|
|
|
|
deleteCondo: async (id: string) => {
|
|
await request(`/condos/${id}`, { method: 'DELETE' });
|
|
if (localStorage.getItem(STORAGE_KEYS.ACTIVE_CONDO_ID) === id) {
|
|
localStorage.removeItem(STORAGE_KEYS.ACTIVE_CONDO_ID);
|
|
}
|
|
},
|
|
|
|
// --- NOTICES (BACHECA) ---
|
|
|
|
getNotices: async (condoId?: string): Promise<Notice[]> => {
|
|
let url = '/notices';
|
|
if (condoId) url += `?condoId=${condoId}`;
|
|
return request<Notice[]>(url);
|
|
},
|
|
|
|
saveNotice: async (notice: Notice): Promise<Notice> => {
|
|
if (!notice.id) {
|
|
return request<Notice>('/notices', {
|
|
method: 'POST',
|
|
body: JSON.stringify(notice)
|
|
});
|
|
} else {
|
|
return request<Notice>(`/notices/${notice.id}`, {
|
|
method: 'PUT',
|
|
body: JSON.stringify(notice)
|
|
});
|
|
}
|
|
},
|
|
|
|
deleteNotice: async (id: string) => {
|
|
await request(`/notices/${id}`, { method: 'DELETE' });
|
|
},
|
|
|
|
markNoticeAsRead: async (noticeId: string, userId: string) => {
|
|
await request(`/notices/${noticeId}/read`, {
|
|
method: 'POST',
|
|
body: JSON.stringify({ userId })
|
|
});
|
|
},
|
|
|
|
getNoticeReadStatus: async (noticeId: string): Promise<NoticeRead[]> => {
|
|
return request<NoticeRead[]>(`/notices/${noticeId}/reads`);
|
|
},
|
|
|
|
getUnreadNoticesForUser: async (userId: string, condoId: string): Promise<Notice[]> => {
|
|
return request<Notice[]>(`/notices/unread?userId=${userId}&condoId=${condoId}`);
|
|
},
|
|
|
|
// --- AUTH ---
|
|
|
|
login: async (email, password) => {
|
|
const data = await request<{token: string, user: User}>('/auth/login', {
|
|
method: 'POST',
|
|
body: JSON.stringify({ email, password })
|
|
});
|
|
|
|
localStorage.setItem(STORAGE_KEYS.TOKEN, data.token);
|
|
localStorage.setItem(STORAGE_KEYS.USER, JSON.stringify(data.user));
|
|
|
|
// Set active condo if user belongs to a family
|
|
if (data.user.familyId) {
|
|
// We need to fetch family to get condoId.
|
|
// For simplicity, we trust the flow or fetch families next.
|
|
// In a real app, login might return condoId directly.
|
|
try {
|
|
const families = await CondoService.getFamilies(); // This will filter by user perms
|
|
const fam = families.find(f => f.id === data.user.familyId);
|
|
if (fam) {
|
|
localStorage.setItem(STORAGE_KEYS.ACTIVE_CONDO_ID, fam.condoId);
|
|
}
|
|
} catch (e) { console.error("Could not set active condo on login", e); }
|
|
}
|
|
|
|
return data;
|
|
},
|
|
|
|
logout: () => {
|
|
localStorage.removeItem(STORAGE_KEYS.TOKEN);
|
|
localStorage.removeItem(STORAGE_KEYS.USER);
|
|
window.location.href = '#/login';
|
|
},
|
|
|
|
getCurrentUser: (): User | null => {
|
|
const u = localStorage.getItem(STORAGE_KEYS.USER);
|
|
return u ? JSON.parse(u) : null;
|
|
},
|
|
|
|
updateProfile: async (data: Partial<User> & { password?: string }) => {
|
|
return request<{success: true, user: User}>('/profile', {
|
|
method: 'PUT',
|
|
body: JSON.stringify(data)
|
|
});
|
|
},
|
|
|
|
// --- SETTINGS (Global) ---
|
|
|
|
getSettings: async (): Promise<AppSettings> => {
|
|
return request<AppSettings>('/settings');
|
|
},
|
|
|
|
updateSettings: async (settings: AppSettings): Promise<void> => {
|
|
await request('/settings', {
|
|
method: 'PUT',
|
|
body: JSON.stringify(settings)
|
|
});
|
|
},
|
|
|
|
getAvailableYears: async (): Promise<number[]> => {
|
|
return request<number[]>('/years');
|
|
},
|
|
|
|
// --- FAMILIES ---
|
|
|
|
getFamilies: async (): Promise<Family[]> => {
|
|
const activeCondoId = CondoService.getActiveCondoId();
|
|
// Pass condoId to filter on server if needed, or filter client side.
|
|
// The server `getFamilies` endpoint handles filtering based on user role.
|
|
// However, if we are admin, we want ALL families, but usually filtered by the UI for the active condo.
|
|
// Let's get all allowed families from server.
|
|
return request<Family[]>('/families');
|
|
},
|
|
|
|
addFamily: async (familyData: Omit<Family, 'id' | 'balance' | 'condoId'>): Promise<Family> => {
|
|
const activeCondoId = CondoService.getActiveCondoId();
|
|
if (!activeCondoId) throw new Error("Nessun condominio selezionato");
|
|
|
|
return request<Family>('/families', {
|
|
method: 'POST',
|
|
body: JSON.stringify({ ...familyData, condoId: activeCondoId })
|
|
});
|
|
},
|
|
|
|
updateFamily: async (family: Family): Promise<Family> => {
|
|
return request<Family>(`/families/${family.id}`, {
|
|
method: 'PUT',
|
|
body: JSON.stringify(family)
|
|
});
|
|
},
|
|
|
|
deleteFamily: async (familyId: string): Promise<void> => {
|
|
await request(`/families/${familyId}`, { method: 'DELETE' });
|
|
},
|
|
|
|
// --- PAYMENTS ---
|
|
|
|
getPaymentsByFamily: async (familyId: string): Promise<Payment[]> => {
|
|
return request<Payment[]>(`/payments?familyId=${familyId}`);
|
|
},
|
|
|
|
addPayment: async (payment: Omit<Payment, 'id'>): Promise<Payment> => {
|
|
return request<Payment>('/payments', {
|
|
method: 'POST',
|
|
body: JSON.stringify(payment)
|
|
});
|
|
},
|
|
|
|
// --- USERS ---
|
|
|
|
getUsers: async (): Promise<User[]> => {
|
|
return request<User[]>('/users');
|
|
},
|
|
|
|
createUser: async (userData: any) => {
|
|
return request('/users', {
|
|
method: 'POST',
|
|
body: JSON.stringify(userData)
|
|
});
|
|
},
|
|
|
|
updateUser: async (id: string, userData: any) => {
|
|
return request(`/users/${id}`, {
|
|
method: 'PUT',
|
|
body: JSON.stringify(userData)
|
|
});
|
|
},
|
|
|
|
deleteUser: async (id: string) => {
|
|
await request(`/users/${id}`, { method: 'DELETE' });
|
|
},
|
|
|
|
// --- ALERTS ---
|
|
|
|
getAlerts: async (): Promise<AlertDefinition[]> => {
|
|
return request<AlertDefinition[]>('/alerts');
|
|
},
|
|
|
|
saveAlert: async (alert: AlertDefinition): Promise<AlertDefinition> => {
|
|
if (!alert.id) {
|
|
return request<AlertDefinition>('/alerts', {
|
|
method: 'POST',
|
|
body: JSON.stringify(alert)
|
|
});
|
|
} else {
|
|
return request<AlertDefinition>(`/alerts/${alert.id}`, {
|
|
method: 'PUT',
|
|
body: JSON.stringify(alert)
|
|
});
|
|
}
|
|
},
|
|
|
|
deleteAlert: async (id: string) => {
|
|
await request(`/alerts/${id}`, { method: 'DELETE' });
|
|
},
|
|
|
|
// --- SEEDING ---
|
|
seedPayments: () => {
|
|
// No-op in remote mode
|
|
}
|
|
};
|