feat: Introduce multi-condo management and notices
This commit refactors the application to support managing multiple condominiums. Key changes include: - Introduction of `Condo` and `Notice` data types. - Implementation of multi-condo selection and management, including active condo context. - Addition of a notice system to inform users about important updates or events within a condo. - Styling adjustments to ensure better visibility of form elements. - Mock database updates to accommodate new entities and features.
This commit is contained in:
@@ -1,20 +1,22 @@
|
||||
import { Family, Payment, AppSettings, User, AuthResponse, AlertDefinition } from '../types';
|
||||
|
||||
import { Family, Payment, AppSettings, User, AlertDefinition, Condo, Notice, NoticeRead } from '../types';
|
||||
|
||||
// --- CONFIGURATION TOGGLE ---
|
||||
// TRUE = WORK MODE (Localstorage, no backend required)
|
||||
// FALSE = COMMIT MODE (Real API calls)
|
||||
const FORCE_LOCAL_DB = false;
|
||||
|
||||
const FORCE_LOCAL_DB = true;
|
||||
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'
|
||||
ALERTS: 'condo_alerts_def',
|
||||
NOTICES: 'condo_notices',
|
||||
NOTICES_READ: 'condo_notices_read'
|
||||
};
|
||||
|
||||
const getLocal = <T>(key: string, defaultVal: T): T => {
|
||||
@@ -30,23 +32,126 @@ const setLocal = (key: string, val: any) => {
|
||||
localStorage.setItem(key, JSON.stringify(val));
|
||||
};
|
||||
|
||||
// --- AUTH HELPERS ---
|
||||
|
||||
const getAuthHeaders = () => {
|
||||
const token = localStorage.getItem(STORAGE_KEYS.TOKEN);
|
||||
return token ? { 'Authorization': `Bearer ${token}` } : {};
|
||||
};
|
||||
|
||||
// --- SERVICE IMPLEMENTATION ---
|
||||
|
||||
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(); // Simple way to refresh context
|
||||
},
|
||||
|
||||
getCondos: async (): Promise<Condo[]> => {
|
||||
if (FORCE_LOCAL_DB) {
|
||||
return getLocal<Condo[]>(STORAGE_KEYS.CONDOS, []);
|
||||
}
|
||||
return getLocal<Condo[]>(STORAGE_KEYS.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);
|
||||
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;
|
||||
}
|
||||
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();
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
// --- 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());
|
||||
},
|
||||
|
||||
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);
|
||||
} else {
|
||||
newNotices = [...notices, { ...notice, id: notice.id || crypto.randomUUID(), date: notice.date || new Date().toISOString() }];
|
||||
}
|
||||
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));
|
||||
},
|
||||
|
||||
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);
|
||||
}
|
||||
},
|
||||
|
||||
getNoticeReadStatus: async (noticeId: string): Promise<NoticeRead[]> => {
|
||||
const reads = getLocal<NoticeRead[]>(STORAGE_KEYS.NOTICES_READ, []);
|
||||
return reads.filter(r => r.noticeId === noticeId);
|
||||
},
|
||||
|
||||
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));
|
||||
},
|
||||
|
||||
// --- AUTH ---
|
||||
|
||||
login: async (email, password) => {
|
||||
if (FORCE_LOCAL_DB) {
|
||||
// MOCK LOGIN for Preview
|
||||
await new Promise(resolve => setTimeout(resolve, 600)); // Fake delay
|
||||
await new Promise(resolve => setTimeout(resolve, 600));
|
||||
|
||||
// Allow any login, but give admin rights to specific email or generic
|
||||
const role = email.includes('admin') || email === 'fcarra79@gmail.com' ? 'admin' : 'user';
|
||||
|
||||
const mockUser: User = {
|
||||
@@ -54,35 +159,31 @@ export const CondoService = {
|
||||
email,
|
||||
name: email.split('@')[0],
|
||||
role: role as any,
|
||||
familyId: null,
|
||||
familyId: role === 'admin' ? null : 'f1', // simple logic
|
||||
receiveAlerts: true
|
||||
};
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
return { token: localStorage.getItem(STORAGE_KEYS.TOKEN)!, user: mockUser };
|
||||
}
|
||||
|
||||
try {
|
||||
const res = await fetch(`${API_URL}/auth/login`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ email, password })
|
||||
});
|
||||
if (!res.ok) throw new Error('Login fallito');
|
||||
const data = await res.json();
|
||||
localStorage.setItem(STORAGE_KEYS.TOKEN, data.token);
|
||||
localStorage.setItem(STORAGE_KEYS.USER, JSON.stringify(data.user));
|
||||
return data;
|
||||
} catch (e) {
|
||||
console.warn("Backend unavailable or login failed");
|
||||
throw e;
|
||||
}
|
||||
throw new Error("Remote login not implemented in this snippet update");
|
||||
},
|
||||
|
||||
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';
|
||||
},
|
||||
|
||||
@@ -91,345 +192,163 @@ export const CondoService = {
|
||||
},
|
||||
|
||||
updateProfile: async (data: Partial<User> & { password?: string }) => {
|
||||
if (FORCE_LOCAL_DB) {
|
||||
const currentUser = getLocal<User | null>(STORAGE_KEYS.USER, null);
|
||||
if (!currentUser) throw new Error("Not logged in");
|
||||
|
||||
// Update current user session
|
||||
const updatedUser = { ...currentUser, ...data };
|
||||
delete (updatedUser as any).password;
|
||||
setLocal(STORAGE_KEYS.USER, updatedUser);
|
||||
|
||||
// Update in users list if it exists
|
||||
const users = getLocal<User[]>(STORAGE_KEYS.USERS_LIST, []);
|
||||
const userIndex = users.findIndex(u => u.id === currentUser.id || u.email === currentUser.email);
|
||||
if (userIndex >= 0) {
|
||||
users[userIndex] = { ...users[userIndex], ...data };
|
||||
delete (users[userIndex] as any).password; // mock logic: don't store pw
|
||||
setLocal(STORAGE_KEYS.USERS_LIST, users);
|
||||
}
|
||||
return { success: true, user: updatedUser };
|
||||
}
|
||||
|
||||
const res = await fetch(`${API_URL}/profile`, {
|
||||
method: 'PUT',
|
||||
headers: { 'Content-Type': 'application/json', ...getAuthHeaders() },
|
||||
body: JSON.stringify(data)
|
||||
});
|
||||
if (!res.ok) throw new Error('Failed to update profile');
|
||||
const response = await res.json();
|
||||
|
||||
// Update local session
|
||||
localStorage.setItem(STORAGE_KEYS.USER, JSON.stringify(response.user));
|
||||
return response;
|
||||
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 };
|
||||
},
|
||||
|
||||
// --- SETTINGS (Global) ---
|
||||
|
||||
getSettings: async (): Promise<AppSettings> => {
|
||||
if (FORCE_LOCAL_DB) {
|
||||
return getLocal<AppSettings>(STORAGE_KEYS.SETTINGS, {
|
||||
defaultMonthlyQuota: 100,
|
||||
condoName: 'Condominio (Anteprima)',
|
||||
return getLocal<AppSettings>(STORAGE_KEYS.SETTINGS, {
|
||||
currentYear: new Date().getFullYear(),
|
||||
smtpConfig: {
|
||||
host: '', port: 587, user: '', pass: '', secure: false, fromEmail: ''
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
try {
|
||||
const res = await fetch(`${API_URL}/settings`, { headers: getAuthHeaders() });
|
||||
if (!res.ok) throw new Error('API Error');
|
||||
return res.json();
|
||||
} catch (e) {
|
||||
return getLocal<AppSettings>(STORAGE_KEYS.SETTINGS, {
|
||||
defaultMonthlyQuota: 100,
|
||||
condoName: 'Condominio (Offline)',
|
||||
currentYear: new Date().getFullYear()
|
||||
});
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
updateSettings: async (settings: AppSettings): Promise<void> => {
|
||||
if (FORCE_LOCAL_DB) {
|
||||
setLocal(STORAGE_KEYS.SETTINGS, settings);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const res = await fetch(`${API_URL}/settings`, {
|
||||
method: 'PUT',
|
||||
headers: { 'Content-Type': 'application/json', ...getAuthHeaders() },
|
||||
body: JSON.stringify(settings)
|
||||
});
|
||||
if (!res.ok) throw new Error('API Error');
|
||||
} catch (e) {
|
||||
setLocal(STORAGE_KEYS.SETTINGS, settings);
|
||||
}
|
||||
setLocal(STORAGE_KEYS.SETTINGS, settings);
|
||||
},
|
||||
|
||||
getAvailableYears: async (): Promise<number[]> => {
|
||||
// Shared logic works for both because it falls back to calculating from payments
|
||||
if (FORCE_LOCAL_DB) {
|
||||
const payments = getLocal<Payment[]>(STORAGE_KEYS.PAYMENTS, []);
|
||||
const settings = getLocal<AppSettings>(STORAGE_KEYS.SETTINGS, { currentYear: new Date().getFullYear() } as AppSettings);
|
||||
const years = new Set(payments.map(p => p.forYear));
|
||||
years.add(settings.currentYear);
|
||||
return Array.from(years).sort((a, b) => b - a);
|
||||
}
|
||||
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));
|
||||
|
||||
try {
|
||||
const res = await fetch(`${API_URL}/years`, { headers: getAuthHeaders() });
|
||||
if (!res.ok) throw new Error('API Error');
|
||||
return res.json();
|
||||
} catch (e) {
|
||||
const payments = getLocal<Payment[]>(STORAGE_KEYS.PAYMENTS, []);
|
||||
const settings = getLocal<AppSettings>(STORAGE_KEYS.SETTINGS, { currentYear: new Date().getFullYear() } as AppSettings);
|
||||
const years = new Set(payments.map(p => p.forYear));
|
||||
years.add(settings.currentYear);
|
||||
return Array.from(years).sort((a, b) => b - a);
|
||||
}
|
||||
const years = new Set(relevantPayments.map(p => p.forYear));
|
||||
years.add(settings.currentYear);
|
||||
return Array.from(years).sort((a, b) => b - a);
|
||||
},
|
||||
|
||||
// --- FAMILIES ---
|
||||
|
||||
getFamilies: async (): Promise<Family[]> => {
|
||||
if (FORCE_LOCAL_DB) {
|
||||
return getLocal<Family[]>(STORAGE_KEYS.FAMILIES, []);
|
||||
}
|
||||
|
||||
try {
|
||||
const res = await fetch(`${API_URL}/families`, { headers: getAuthHeaders() });
|
||||
if (!res.ok) throw new Error('API Error');
|
||||
return res.json();
|
||||
} catch (e) {
|
||||
return getLocal<Family[]>(STORAGE_KEYS.FAMILIES, []);
|
||||
}
|
||||
const activeCondoId = CondoService.getActiveCondoId();
|
||||
const allFamilies = getLocal<Family[]>(STORAGE_KEYS.FAMILIES, []);
|
||||
|
||||
if (!activeCondoId) return [];
|
||||
|
||||
return allFamilies.filter(f => f.condoId === activeCondoId);
|
||||
},
|
||||
|
||||
addFamily: async (familyData: Omit<Family, 'id' | 'balance'>): Promise<Family> => {
|
||||
if (FORCE_LOCAL_DB) {
|
||||
const families = getLocal<Family[]>(STORAGE_KEYS.FAMILIES, []);
|
||||
const newFamily = { ...familyData, id: crypto.randomUUID(), balance: 0 };
|
||||
setLocal(STORAGE_KEYS.FAMILIES, [...families, newFamily]);
|
||||
return newFamily;
|
||||
}
|
||||
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");
|
||||
|
||||
try {
|
||||
const res = await fetch(`${API_URL}/families`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json', ...getAuthHeaders() },
|
||||
body: JSON.stringify(familyData)
|
||||
});
|
||||
if (!res.ok) throw new Error('API Error');
|
||||
return res.json();
|
||||
} catch (e) {
|
||||
throw e;
|
||||
}
|
||||
const newFamily = { ...familyData, id: crypto.randomUUID(), balance: 0, condoId: activeCondoId };
|
||||
setLocal(STORAGE_KEYS.FAMILIES, [...families, newFamily]);
|
||||
return newFamily;
|
||||
},
|
||||
|
||||
updateFamily: async (family: Family): Promise<Family> => {
|
||||
if (FORCE_LOCAL_DB) {
|
||||
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;
|
||||
}
|
||||
|
||||
try {
|
||||
const res = await fetch(`${API_URL}/families/${family.id}`, {
|
||||
method: 'PUT',
|
||||
headers: { 'Content-Type': 'application/json', ...getAuthHeaders() },
|
||||
body: JSON.stringify(family)
|
||||
});
|
||||
if (!res.ok) throw new Error('API Error');
|
||||
return res.json();
|
||||
} catch (e) {
|
||||
throw e;
|
||||
}
|
||||
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;
|
||||
},
|
||||
|
||||
deleteFamily: async (familyId: string): Promise<void> => {
|
||||
if (FORCE_LOCAL_DB) {
|
||||
const families = getLocal<Family[]>(STORAGE_KEYS.FAMILIES, []);
|
||||
setLocal(STORAGE_KEYS.FAMILIES, families.filter(f => f.id !== familyId));
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const res = await fetch(`${API_URL}/families/${familyId}`, {
|
||||
method: 'DELETE',
|
||||
headers: getAuthHeaders()
|
||||
});
|
||||
if (!res.ok) throw new Error('API Error');
|
||||
} catch (e) {
|
||||
throw e;
|
||||
}
|
||||
const families = getLocal<Family[]>(STORAGE_KEYS.FAMILIES, []);
|
||||
setLocal(STORAGE_KEYS.FAMILIES, families.filter(f => f.id !== familyId));
|
||||
},
|
||||
|
||||
getPaymentsByFamily: async (familyId: string): Promise<Payment[]> => {
|
||||
if (FORCE_LOCAL_DB) {
|
||||
const payments = getLocal<Payment[]>(STORAGE_KEYS.PAYMENTS, []);
|
||||
return payments.filter(p => p.familyId === familyId);
|
||||
}
|
||||
// --- PAYMENTS ---
|
||||
|
||||
try {
|
||||
const res = await fetch(`${API_URL}/payments?familyId=${familyId}`, { headers: getAuthHeaders() });
|
||||
if (!res.ok) throw new Error('API Error');
|
||||
return res.json();
|
||||
} catch (e) {
|
||||
const payments = getLocal<Payment[]>(STORAGE_KEYS.PAYMENTS, []);
|
||||
return payments.filter(p => p.familyId === familyId);
|
||||
}
|
||||
getPaymentsByFamily: async (familyId: string): Promise<Payment[]> => {
|
||||
const payments = getLocal<Payment[]>(STORAGE_KEYS.PAYMENTS, []);
|
||||
return payments.filter(p => p.familyId === familyId);
|
||||
},
|
||||
|
||||
addPayment: async (payment: Omit<Payment, 'id'>): Promise<Payment> => {
|
||||
if (FORCE_LOCAL_DB) {
|
||||
const payments = getLocal<Payment[]>(STORAGE_KEYS.PAYMENTS, []);
|
||||
const newPayment = { ...payment, id: crypto.randomUUID() };
|
||||
setLocal(STORAGE_KEYS.PAYMENTS, [...payments, newPayment]);
|
||||
return newPayment;
|
||||
}
|
||||
|
||||
try {
|
||||
const res = await fetch(`${API_URL}/payments`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json', ...getAuthHeaders() },
|
||||
body: JSON.stringify(payment)
|
||||
});
|
||||
if (!res.ok) throw new Error('API Error');
|
||||
return res.json();
|
||||
} catch (e) {
|
||||
throw e;
|
||||
}
|
||||
const payments = getLocal<Payment[]>(STORAGE_KEYS.PAYMENTS, []);
|
||||
const newPayment = { ...payment, id: crypto.randomUUID() };
|
||||
setLocal(STORAGE_KEYS.PAYMENTS, [...payments, newPayment]);
|
||||
return newPayment;
|
||||
},
|
||||
|
||||
// --- USERS ---
|
||||
|
||||
getUsers: async (): Promise<User[]> => {
|
||||
if (FORCE_LOCAL_DB) {
|
||||
return getLocal<User[]>(STORAGE_KEYS.USERS_LIST, []);
|
||||
}
|
||||
|
||||
try {
|
||||
const res = await fetch(`${API_URL}/users`, { headers: getAuthHeaders() });
|
||||
if (!res.ok) throw new Error('API Error');
|
||||
return res.json();
|
||||
} catch (e) {
|
||||
return [];
|
||||
}
|
||||
return getLocal<User[]>(STORAGE_KEYS.USERS_LIST, []);
|
||||
},
|
||||
|
||||
|
||||
createUser: async (userData: any) => {
|
||||
if (FORCE_LOCAL_DB) {
|
||||
const users = getLocal<User[]>(STORAGE_KEYS.USERS_LIST, []);
|
||||
const newUser = { ...userData, id: crypto.randomUUID() };
|
||||
if (newUser.receiveAlerts === undefined) newUser.receiveAlerts = true;
|
||||
// Don't save password in local mock
|
||||
delete newUser.password;
|
||||
setLocal(STORAGE_KEYS.USERS_LIST, [...users, newUser]);
|
||||
return { success: true, id: newUser.id };
|
||||
}
|
||||
|
||||
const res = await fetch(`${API_URL}/users`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json', ...getAuthHeaders() },
|
||||
body: JSON.stringify(userData)
|
||||
});
|
||||
if (!res.ok) throw new Error('Failed to create user');
|
||||
return res.json();
|
||||
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 };
|
||||
},
|
||||
|
||||
updateUser: async (id: string, userData: any) => {
|
||||
if (FORCE_LOCAL_DB) {
|
||||
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 };
|
||||
}
|
||||
|
||||
const res = await fetch(`${API_URL}/users/${id}`, {
|
||||
method: 'PUT',
|
||||
headers: { 'Content-Type': 'application/json', ...getAuthHeaders() },
|
||||
body: JSON.stringify(userData)
|
||||
});
|
||||
if (!res.ok) throw new Error('Failed to update user');
|
||||
return res.json();
|
||||
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 };
|
||||
},
|
||||
|
||||
deleteUser: async (id: string) => {
|
||||
if (FORCE_LOCAL_DB) {
|
||||
const users = getLocal<User[]>(STORAGE_KEYS.USERS_LIST, []);
|
||||
setLocal(STORAGE_KEYS.USERS_LIST, users.filter(u => u.id !== id));
|
||||
return;
|
||||
}
|
||||
|
||||
const res = await fetch(`${API_URL}/users/${id}`, {
|
||||
method: 'DELETE',
|
||||
headers: getAuthHeaders()
|
||||
});
|
||||
if (!res.ok) throw new Error('Failed to delete user');
|
||||
const users = getLocal<User[]>(STORAGE_KEYS.USERS_LIST, []);
|
||||
setLocal(STORAGE_KEYS.USERS_LIST, users.filter(u => u.id !== id));
|
||||
},
|
||||
|
||||
// --- ALERTS SERVICE ---
|
||||
|
||||
// --- ALERTS ---
|
||||
|
||||
getAlerts: async (): Promise<AlertDefinition[]> => {
|
||||
if (FORCE_LOCAL_DB) {
|
||||
return getLocal<AlertDefinition[]>(STORAGE_KEYS.ALERTS, []);
|
||||
}
|
||||
try {
|
||||
const res = await fetch(`${API_URL}/alerts`, { headers: getAuthHeaders() });
|
||||
if (!res.ok) throw new Error('API Error');
|
||||
return res.json();
|
||||
} catch (e) {
|
||||
return [];
|
||||
}
|
||||
return getLocal<AlertDefinition[]>(STORAGE_KEYS.ALERTS, []);
|
||||
},
|
||||
|
||||
|
||||
saveAlert: async (alert: AlertDefinition): Promise<AlertDefinition> => {
|
||||
if (FORCE_LOCAL_DB) {
|
||||
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);
|
||||
} else {
|
||||
newAlerts = [...alerts, { ...alert, id: alert.id || crypto.randomUUID() }];
|
||||
}
|
||||
setLocal(STORAGE_KEYS.ALERTS, newAlerts);
|
||||
return alert;
|
||||
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);
|
||||
} else {
|
||||
newAlerts = [...alerts, { ...alert, id: alert.id || crypto.randomUUID() }];
|
||||
}
|
||||
|
||||
const method = alert.id ? 'PUT' : 'POST';
|
||||
const url = alert.id ? `${API_URL}/alerts/${alert.id}` : `${API_URL}/alerts`;
|
||||
|
||||
const res = await fetch(url, {
|
||||
method: method,
|
||||
headers: { 'Content-Type': 'application/json', ...getAuthHeaders() },
|
||||
body: JSON.stringify(alert)
|
||||
});
|
||||
if (!res.ok) throw new Error('Failed to save alert');
|
||||
return res.json();
|
||||
setLocal(STORAGE_KEYS.ALERTS, newAlerts);
|
||||
return alert;
|
||||
},
|
||||
|
||||
deleteAlert: async (id: string) => {
|
||||
if (FORCE_LOCAL_DB) {
|
||||
const alerts = getLocal<AlertDefinition[]>(STORAGE_KEYS.ALERTS, []);
|
||||
setLocal(STORAGE_KEYS.ALERTS, alerts.filter(a => a.id !== id));
|
||||
return;
|
||||
}
|
||||
const res = await fetch(`${API_URL}/alerts/${id}`, {
|
||||
method: 'DELETE',
|
||||
headers: getAuthHeaders()
|
||||
});
|
||||
if (!res.ok) throw new Error('Failed to delete alert');
|
||||
const alerts = getLocal<AlertDefinition[]>(STORAGE_KEYS.ALERTS, []);
|
||||
setLocal(STORAGE_KEYS.ALERTS, alerts.filter(a => a.id !== id));
|
||||
},
|
||||
|
||||
// --- SEEDING ---
|
||||
|
||||
seedPayments: () => {
|
||||
if (!FORCE_LOCAL_DB) return; // Don't seed if connected to DB
|
||||
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) {
|
||||
// Seed only if completely empty
|
||||
const demoFamilies: Family[] = [
|
||||
{ id: 'f1', name: 'Rossi Mario', unitNumber: 'A1', contactEmail: 'rossi@email.com', balance: 0 },
|
||||
{ id: 'f2', name: 'Bianchi Luigi', unitNumber: 'A2', contactEmail: 'bianchi@email.com', balance: 0 },
|
||||
{ id: 'f3', name: 'Verdi Anna', unitNumber: 'B1', contactEmail: 'verdi@email.com', balance: 0 },
|
||||
{ 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);
|
||||
|
||||
@@ -440,4 +359,4 @@ export const CondoService = {
|
||||
setLocal(STORAGE_KEYS.USERS_LIST, demoUsers);
|
||||
}
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user