refactor: Migrate to API endpoint and use real DB connection

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.
This commit is contained in:
2025-12-07 01:45:12 +01:00
parent 3f954c65b1
commit 1641b931e8
5 changed files with 406 additions and 625 deletions

View File

@@ -2,39 +2,40 @@
import { Family, Payment, AppSettings, User, AlertDefinition, Condo, Notice, NoticeRead } from '../types';
// --- CONFIGURATION TOGGLE ---
const FORCE_LOCAL_DB = true;
const FORCE_LOCAL_DB = false;
const API_URL = '/api';
const STORAGE_KEYS = {
SETTINGS: 'condo_settings',
CONDOS: 'condo_list',
ACTIVE_CONDO_ID: 'condo_active_id',
FAMILIES: 'condo_families',
PAYMENTS: 'condo_payments',
TOKEN: 'condo_auth_token',
USER: 'condo_user_info',
USERS_LIST: 'condo_users_list',
ALERTS: 'condo_alerts_def',
NOTICES: 'condo_notices',
NOTICES_READ: 'condo_notices_read'
};
const getLocal = <T>(key: string, defaultVal: T): T => {
try {
const item = localStorage.getItem(key);
return item ? JSON.parse(item) : defaultVal;
} catch {
return defaultVal;
}
};
const setLocal = (key: string, val: any) => {
localStorage.setItem(key, JSON.stringify(val));
ACTIVE_CONDO_ID: 'condo_active_id',
};
const getAuthHeaders = () => {
const token = localStorage.getItem(STORAGE_KEYS.TOKEN);
return token ? { 'Authorization': `Bearer ${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 = {
@@ -47,316 +48,245 @@ export const CondoService = {
setActiveCondo: (condoId: string) => {
localStorage.setItem(STORAGE_KEYS.ACTIVE_CONDO_ID, condoId);
window.location.reload(); // Simple way to refresh context
window.location.reload();
},
getCondos: async (): Promise<Condo[]> => {
if (FORCE_LOCAL_DB) {
return getLocal<Condo[]>(STORAGE_KEYS.CONDOS, []);
}
return getLocal<Condo[]>(STORAGE_KEYS.CONDOS, []);
return request<Condo[]>('/condos');
},
getActiveCondo: async (): Promise<Condo | undefined> => {
const condos = await CondoService.getCondos();
const activeId = CondoService.getActiveCondoId();
if (!activeId && condos.length > 0) {
CondoService.setActiveCondo(condos[0].id);
// 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 (FORCE_LOCAL_DB) {
const condos = getLocal<Condo[]>(STORAGE_KEYS.CONDOS, []);
const index = condos.findIndex(c => c.id === condo.id);
let newCondos;
if (index >= 0) {
newCondos = condos.map(c => c.id === condo.id ? condo : c);
} else {
newCondos = [...condos, { ...condo, id: condo.id || crypto.randomUUID() }];
}
setLocal(STORAGE_KEYS.CONDOS, newCondos);
if (newCondos.length === 1) {
localStorage.setItem(STORAGE_KEYS.ACTIVE_CONDO_ID, newCondos[0].id);
}
return 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)
});
}
return condo;
},
deleteCondo: async (id: string) => {
if (FORCE_LOCAL_DB) {
const condos = getLocal<Condo[]>(STORAGE_KEYS.CONDOS, []);
setLocal(STORAGE_KEYS.CONDOS, condos.filter(c => c.id !== id));
if (localStorage.getItem(STORAGE_KEYS.ACTIVE_CONDO_ID) === id) {
localStorage.removeItem(STORAGE_KEYS.ACTIVE_CONDO_ID);
window.location.reload();
}
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[]> => {
const allNotices = getLocal<Notice[]>(STORAGE_KEYS.NOTICES, []);
if (!condoId) return allNotices;
return allNotices.filter(n => n.condoId === condoId).sort((a,b) => new Date(b.date).getTime() - new Date(a.date).getTime());
let url = '/notices';
if (condoId) url += `?condoId=${condoId}`;
return request<Notice[]>(url);
},
saveNotice: async (notice: Notice): Promise<Notice> => {
const notices = getLocal<Notice[]>(STORAGE_KEYS.NOTICES, []);
const index = notices.findIndex(n => n.id === notice.id);
let newNotices;
if (index >= 0) {
newNotices = notices.map(n => n.id === notice.id ? notice : n);
if (!notice.id) {
return request<Notice>('/notices', {
method: 'POST',
body: JSON.stringify(notice)
});
} else {
newNotices = [...notices, { ...notice, id: notice.id || crypto.randomUUID(), date: notice.date || new Date().toISOString() }];
return request<Notice>(`/notices/${notice.id}`, {
method: 'PUT',
body: JSON.stringify(notice)
});
}
setLocal(STORAGE_KEYS.NOTICES, newNotices);
return notice;
},
deleteNotice: async (id: string) => {
const notices = getLocal<Notice[]>(STORAGE_KEYS.NOTICES, []);
setLocal(STORAGE_KEYS.NOTICES, notices.filter(n => n.id !== id));
await request(`/notices/${id}`, { method: 'DELETE' });
},
markNoticeAsRead: async (noticeId: string, userId: string) => {
const reads = getLocal<NoticeRead[]>(STORAGE_KEYS.NOTICES_READ, []);
if (!reads.find(r => r.noticeId === noticeId && r.userId === userId)) {
reads.push({ noticeId, userId, readAt: new Date().toISOString() });
setLocal(STORAGE_KEYS.NOTICES_READ, reads);
}
await request(`/notices/${noticeId}/read`, {
method: 'POST',
body: JSON.stringify({ userId })
});
},
getNoticeReadStatus: async (noticeId: string): Promise<NoticeRead[]> => {
const reads = getLocal<NoticeRead[]>(STORAGE_KEYS.NOTICES_READ, []);
return reads.filter(r => r.noticeId === noticeId);
return request<NoticeRead[]>(`/notices/${noticeId}/reads`);
},
getUnreadNoticesForUser: async (userId: string, condoId: string): Promise<Notice[]> => {
const notices = await CondoService.getNotices(condoId);
const reads = getLocal<NoticeRead[]>(STORAGE_KEYS.NOTICES_READ, []);
const userReadIds = reads.filter(r => r.userId === userId).map(r => r.noticeId);
return notices.filter(n => n.active && !userReadIds.includes(n.id));
return request<Notice[]>(`/notices/unread?userId=${userId}&condoId=${condoId}`);
},
// --- AUTH ---
login: async (email, password) => {
if (FORCE_LOCAL_DB) {
await new Promise(resolve => setTimeout(resolve, 600));
const data = await request<{token: string, user: User}>('/auth/login', {
method: 'POST',
body: JSON.stringify({ email, password })
});
const role = email.includes('admin') || email === 'fcarra79@gmail.com' ? 'admin' : 'user';
const mockUser: User = {
id: 'local-user-' + Math.random().toString(36).substr(2, 9),
email,
name: email.split('@')[0],
role: role as any,
familyId: role === 'admin' ? null : 'f1', // simple logic
receiveAlerts: true
};
localStorage.setItem(STORAGE_KEYS.TOKEN, data.token);
localStorage.setItem(STORAGE_KEYS.USER, JSON.stringify(data.user));
localStorage.setItem(STORAGE_KEYS.TOKEN, 'mock-local-token-' + Date.now());
localStorage.setItem(STORAGE_KEYS.USER, JSON.stringify(mockUser));
// Post-login check: if user has a family, set active condo to that family's condo
if (mockUser.familyId) {
const families = getLocal<Family[]>(STORAGE_KEYS.FAMILIES, []);
const fam = families.find(f => f.id === mockUser.familyId);
if (fam) {
localStorage.setItem(STORAGE_KEYS.ACTIVE_CONDO_ID, fam.condoId);
}
// 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 { token: localStorage.getItem(STORAGE_KEYS.TOKEN)!, user: mockUser };
}
throw new Error("Remote login not implemented in this snippet update");
return data;
},
logout: () => {
localStorage.removeItem(STORAGE_KEYS.TOKEN);
localStorage.removeItem(STORAGE_KEYS.USER);
// Do NOT clear active condo ID, nice for UX to remember where admin was
window.location.href = '#/login';
},
getCurrentUser: (): User | null => {
return getLocal<User | null>(STORAGE_KEYS.USER, null);
const u = localStorage.getItem(STORAGE_KEYS.USER);
return u ? JSON.parse(u) : null;
},
updateProfile: async (data: Partial<User> & { password?: string }) => {
const currentUser = getLocal<User | null>(STORAGE_KEYS.USER, null);
if (!currentUser) throw new Error("Not logged in");
const updatedUser = { ...currentUser, ...data };
delete (updatedUser as any).password;
setLocal(STORAGE_KEYS.USER, updatedUser);
return { success: true, user: updatedUser };
return request<{success: true, user: User}>('/profile', {
method: 'PUT',
body: JSON.stringify(data)
});
},
// --- SETTINGS (Global) ---
getSettings: async (): Promise<AppSettings> => {
return getLocal<AppSettings>(STORAGE_KEYS.SETTINGS, {
currentYear: new Date().getFullYear(),
smtpConfig: {
host: '', port: 587, user: '', pass: '', secure: false, fromEmail: ''
}
});
return request<AppSettings>('/settings');
},
updateSettings: async (settings: AppSettings): Promise<void> => {
setLocal(STORAGE_KEYS.SETTINGS, settings);
await request('/settings', {
method: 'PUT',
body: JSON.stringify(settings)
});
},
getAvailableYears: async (): Promise<number[]> => {
const payments = getLocal<Payment[]>(STORAGE_KEYS.PAYMENTS, []);
const settings = getLocal<AppSettings>(STORAGE_KEYS.SETTINGS, { currentYear: new Date().getFullYear() } as AppSettings);
const activeCondoId = CondoService.getActiveCondoId();
const families = getLocal<Family[]>(STORAGE_KEYS.FAMILIES, []);
const condoFamilyIds = families.filter(f => f.condoId === activeCondoId).map(f => f.id);
const relevantPayments = payments.filter(p => condoFamilyIds.includes(p.familyId));
const years = new Set(relevantPayments.map(p => p.forYear));
years.add(settings.currentYear);
return Array.from(years).sort((a, b) => b - a);
return request<number[]>('/years');
},
// --- FAMILIES ---
getFamilies: async (): Promise<Family[]> => {
const activeCondoId = CondoService.getActiveCondoId();
const allFamilies = getLocal<Family[]>(STORAGE_KEYS.FAMILIES, []);
if (!activeCondoId) return [];
return allFamilies.filter(f => f.condoId === activeCondoId);
// 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 families = getLocal<Family[]>(STORAGE_KEYS.FAMILIES, []);
const activeCondoId = CondoService.getActiveCondoId();
if (!activeCondoId) throw new Error("Nessun condominio selezionato");
const newFamily = { ...familyData, id: crypto.randomUUID(), balance: 0, condoId: activeCondoId };
setLocal(STORAGE_KEYS.FAMILIES, [...families, newFamily]);
return newFamily;
return request<Family>('/families', {
method: 'POST',
body: JSON.stringify({ ...familyData, condoId: activeCondoId })
});
},
updateFamily: async (family: Family): Promise<Family> => {
const families = getLocal<Family[]>(STORAGE_KEYS.FAMILIES, []);
const updated = families.map(f => f.id === family.id ? family : f);
setLocal(STORAGE_KEYS.FAMILIES, updated);
return family;
return request<Family>(`/families/${family.id}`, {
method: 'PUT',
body: JSON.stringify(family)
});
},
deleteFamily: async (familyId: string): Promise<void> => {
const families = getLocal<Family[]>(STORAGE_KEYS.FAMILIES, []);
setLocal(STORAGE_KEYS.FAMILIES, families.filter(f => f.id !== familyId));
await request(`/families/${familyId}`, { method: 'DELETE' });
},
// --- PAYMENTS ---
getPaymentsByFamily: async (familyId: string): Promise<Payment[]> => {
const payments = getLocal<Payment[]>(STORAGE_KEYS.PAYMENTS, []);
return payments.filter(p => p.familyId === familyId);
return request<Payment[]>(`/payments?familyId=${familyId}`);
},
addPayment: async (payment: Omit<Payment, 'id'>): Promise<Payment> => {
const payments = getLocal<Payment[]>(STORAGE_KEYS.PAYMENTS, []);
const newPayment = { ...payment, id: crypto.randomUUID() };
setLocal(STORAGE_KEYS.PAYMENTS, [...payments, newPayment]);
return newPayment;
return request<Payment>('/payments', {
method: 'POST',
body: JSON.stringify(payment)
});
},
// --- USERS ---
getUsers: async (): Promise<User[]> => {
return getLocal<User[]>(STORAGE_KEYS.USERS_LIST, []);
return request<User[]>('/users');
},
createUser: async (userData: any) => {
const users = getLocal<User[]>(STORAGE_KEYS.USERS_LIST, []);
const newUser = { ...userData, id: crypto.randomUUID() };
delete newUser.password;
setLocal(STORAGE_KEYS.USERS_LIST, [...users, newUser]);
return { success: true, id: newUser.id };
return request('/users', {
method: 'POST',
body: JSON.stringify(userData)
});
},
updateUser: async (id: string, userData: any) => {
const users = getLocal<User[]>(STORAGE_KEYS.USERS_LIST, []);
const updatedUsers = users.map(u => u.id === id ? { ...u, ...userData, id } : u);
setLocal(STORAGE_KEYS.USERS_LIST, updatedUsers);
return { success: true };
return request(`/users/${id}`, {
method: 'PUT',
body: JSON.stringify(userData)
});
},
deleteUser: async (id: string) => {
const users = getLocal<User[]>(STORAGE_KEYS.USERS_LIST, []);
setLocal(STORAGE_KEYS.USERS_LIST, users.filter(u => u.id !== id));
await request(`/users/${id}`, { method: 'DELETE' });
},
// --- ALERTS ---
getAlerts: async (): Promise<AlertDefinition[]> => {
return getLocal<AlertDefinition[]>(STORAGE_KEYS.ALERTS, []);
return request<AlertDefinition[]>('/alerts');
},
saveAlert: async (alert: AlertDefinition): Promise<AlertDefinition> => {
const alerts = getLocal<AlertDefinition[]>(STORAGE_KEYS.ALERTS, []);
const existingIndex = alerts.findIndex(a => a.id === alert.id);
let newAlerts;
if (existingIndex >= 0) {
newAlerts = alerts.map(a => a.id === alert.id ? alert : a);
if (!alert.id) {
return request<AlertDefinition>('/alerts', {
method: 'POST',
body: JSON.stringify(alert)
});
} else {
newAlerts = [...alerts, { ...alert, id: alert.id || crypto.randomUUID() }];
return request<AlertDefinition>(`/alerts/${alert.id}`, {
method: 'PUT',
body: JSON.stringify(alert)
});
}
setLocal(STORAGE_KEYS.ALERTS, newAlerts);
return alert;
},
deleteAlert: async (id: string) => {
const alerts = getLocal<AlertDefinition[]>(STORAGE_KEYS.ALERTS, []);
setLocal(STORAGE_KEYS.ALERTS, alerts.filter(a => a.id !== id));
await request(`/alerts/${id}`, { method: 'DELETE' });
},
// --- SEEDING ---
seedPayments: () => {
if (!FORCE_LOCAL_DB) return;
const condos = getLocal<Condo[]>(STORAGE_KEYS.CONDOS, []);
if (condos.length === 0) {
const demoCondos: Condo[] = [
{ id: 'c1', name: 'Residenza i Pini', address: 'Via Roma 10, Milano', defaultMonthlyQuota: 100 },
{ id: 'c2', name: 'Condominio Parco Vittoria', address: 'Corso Italia 50, Torino', defaultMonthlyQuota: 85 }
];
setLocal(STORAGE_KEYS.CONDOS, demoCondos);
localStorage.setItem(STORAGE_KEYS.ACTIVE_CONDO_ID, 'c1');
}
const families = getLocal<Family[]>(STORAGE_KEYS.FAMILIES, []);
if (families.length === 0) {
const demoFamilies: Family[] = [
{ id: 'f1', condoId: 'c1', name: 'Rossi Mario', unitNumber: 'A1', contactEmail: 'rossi@email.com', balance: 0 },
{ id: 'f2', condoId: 'c1', name: 'Bianchi Luigi', unitNumber: 'A2', contactEmail: 'bianchi@email.com', balance: 0 },
{ id: 'f3', condoId: 'c2', name: 'Verdi Anna', unitNumber: 'B1', contactEmail: 'verdi@email.com', balance: 0 },
{ id: 'f4', condoId: 'c2', name: 'Neri Paolo', unitNumber: 'B2', contactEmail: 'neri@email.com', balance: 0 },
];
setLocal(STORAGE_KEYS.FAMILIES, demoFamilies);
const demoUsers: User[] = [
{ id: 'u1', email: 'admin@condo.it', name: 'Amministratore', role: 'admin', phone: '3331234567', familyId: null, receiveAlerts: true },
{ id: 'u2', email: 'rossi@email.com', name: 'Mario Rossi', role: 'user', phone: '', familyId: 'f1', receiveAlerts: true }
];
setLocal(STORAGE_KEYS.USERS_LIST, demoUsers);
}
// No-op in remote mode
}
};