466 lines
15 KiB
TypeScript
466 lines
15 KiB
TypeScript
|
|
import {
|
|
Condo, Family, Payment, AppSettings, User, AuthResponse,
|
|
Ticket, TicketComment, ExtraordinaryExpense, Notice,
|
|
AlertDefinition, NoticeRead, CondoExpense, Document, BrandingConfig
|
|
} from '../types';
|
|
|
|
const API_URL = '/api';
|
|
|
|
async function request<T>(endpoint: string, options: RequestInit = {}): Promise<T> {
|
|
const token = localStorage.getItem('condo_token');
|
|
const headers: HeadersInit = {
|
|
'Content-Type': 'application/json',
|
|
...options.headers as any,
|
|
};
|
|
|
|
if (token) {
|
|
headers['Authorization'] = `Bearer ${token}`;
|
|
}
|
|
|
|
const response = await fetch(`${API_URL}${endpoint}`, {
|
|
...options,
|
|
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);
|
|
}
|
|
|
|
const text = await response.text();
|
|
return text ? JSON.parse(text) : undefined;
|
|
}
|
|
|
|
export const CondoService = {
|
|
// --- 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',
|
|
body: JSON.stringify({ email, password })
|
|
});
|
|
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');
|
|
localStorage.removeItem('active_condo_id');
|
|
localStorage.removeItem('lastViewedTickets');
|
|
localStorage.removeItem('lastViewedExpensesTime');
|
|
|
|
// Usa replace per pulire la history
|
|
window.location.replace('/#/login');
|
|
},
|
|
|
|
getCurrentUser: (): User | null => {
|
|
const u = localStorage.getItem('condo_user');
|
|
return u ? JSON.parse(u) : null;
|
|
},
|
|
|
|
updateProfile: async (data: any): Promise<void> => {
|
|
const res = await request<{success: boolean, user: User}>('/profile', {
|
|
method: 'PUT',
|
|
body: JSON.stringify(data)
|
|
});
|
|
if (res.user) {
|
|
localStorage.setItem('condo_user', JSON.stringify(res.user));
|
|
}
|
|
},
|
|
|
|
// --- Settings ---
|
|
getSettings: async (): Promise<AppSettings> => {
|
|
return request<AppSettings>('/settings');
|
|
},
|
|
|
|
updateSettings: async (settings: AppSettings): Promise<void> => {
|
|
return request('/settings', {
|
|
method: 'PUT',
|
|
body: JSON.stringify(settings)
|
|
});
|
|
},
|
|
|
|
testSmtpConfig: async (config: any): Promise<void> => {
|
|
return request('/settings/smtp-test', {
|
|
method: 'POST',
|
|
body: JSON.stringify(config)
|
|
});
|
|
},
|
|
|
|
getAvailableYears: async (): Promise<number[]> => {
|
|
return request<number[]>('/years');
|
|
},
|
|
|
|
// --- Condos ---
|
|
getCondos: async (): Promise<Condo[]> => {
|
|
return request<Condo[]>('/condos');
|
|
},
|
|
|
|
getActiveCondoId: (): string | undefined => {
|
|
return localStorage.getItem('active_condo_id') || undefined;
|
|
},
|
|
|
|
getActiveCondo: async (): Promise<Condo | undefined> => {
|
|
const id = localStorage.getItem('active_condo_id');
|
|
const condos = await CondoService.getCondos();
|
|
|
|
let match;
|
|
if (id) {
|
|
match = condos.find(c => c.id === id);
|
|
}
|
|
|
|
// 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;
|
|
},
|
|
|
|
setActiveCondo: (id: string) => {
|
|
localStorage.setItem('active_condo_id', id);
|
|
window.dispatchEvent(new Event('condo-updated'));
|
|
window.location.reload();
|
|
},
|
|
|
|
saveCondo: async (condo: Condo): Promise<Condo> => {
|
|
if (condo.id) {
|
|
await request(`/condos/${condo.id}`, {
|
|
method: 'PUT',
|
|
body: JSON.stringify(condo)
|
|
});
|
|
return condo;
|
|
} else {
|
|
return request<Condo>('/condos', {
|
|
method: 'POST',
|
|
body: JSON.stringify(condo)
|
|
});
|
|
}
|
|
},
|
|
|
|
deleteCondo: async (id: string): Promise<void> => {
|
|
return request(`/condos/${id}`, { method: 'DELETE' });
|
|
},
|
|
|
|
// --- Families ---
|
|
getFamilies: async (condoId?: string): Promise<Family[]> => {
|
|
let url = '/families';
|
|
const activeId = condoId || CondoService.getActiveCondoId();
|
|
if (activeId) url += `?condoId=${activeId}`;
|
|
return request<Family[]>(url);
|
|
},
|
|
|
|
addFamily: async (family: any): Promise<Family> => {
|
|
let activeId = CondoService.getActiveCondoId();
|
|
if (!activeId) {
|
|
const condos = await CondoService.getCondos();
|
|
if (condos.length > 0) {
|
|
activeId = condos[0].id;
|
|
localStorage.setItem('active_condo_id', activeId);
|
|
}
|
|
}
|
|
return request<Family>('/families', {
|
|
method: 'POST',
|
|
body: JSON.stringify({ ...family, condoId: activeId })
|
|
});
|
|
},
|
|
|
|
updateFamily: async (family: Family): Promise<void> => {
|
|
return request(`/families/${family.id}`, {
|
|
method: 'PUT',
|
|
body: JSON.stringify(family)
|
|
});
|
|
},
|
|
|
|
deleteFamily: async (id: string): Promise<void> => {
|
|
return request(`/families/${id}`, { method: 'DELETE' });
|
|
},
|
|
|
|
// --- Payments ---
|
|
seedPayments: () => { },
|
|
|
|
getPaymentsByFamily: async (familyId: string): Promise<Payment[]> => {
|
|
return request<Payment[]>(`/payments?familyId=${familyId}`);
|
|
},
|
|
|
|
getCondoPayments: async (condoId: string): Promise<Payment[]> => {
|
|
return request<Payment[]>(`/payments?condoId=${condoId}`);
|
|
},
|
|
|
|
addPayment: async (payment: any): Promise<Payment> => {
|
|
return request<Payment>('/payments', {
|
|
method: 'POST',
|
|
body: JSON.stringify(payment)
|
|
});
|
|
},
|
|
|
|
// --- Users ---
|
|
getUsers: async (condoId?: string): Promise<User[]> => {
|
|
let url = '/users';
|
|
if (condoId) url += `?condoId=${condoId}`;
|
|
return request<User[]>(url);
|
|
},
|
|
|
|
createUser: async (user: any): Promise<void> => {
|
|
return request('/users', {
|
|
method: 'POST',
|
|
body: JSON.stringify(user)
|
|
});
|
|
},
|
|
|
|
updateUser: async (id: string, user: any): Promise<void> => {
|
|
return request(`/users/${id}`, {
|
|
method: 'PUT',
|
|
body: JSON.stringify(user)
|
|
});
|
|
},
|
|
|
|
deleteUser: async (id: string): Promise<void> => {
|
|
return request(`/users/${id}`, { method: 'DELETE' });
|
|
},
|
|
|
|
// --- Alerts ---
|
|
getAlerts: async (condoId?: string): Promise<AlertDefinition[]> => {
|
|
let url = '/alerts';
|
|
if (condoId) url += `?condoId=${condoId}`;
|
|
return request<AlertDefinition[]>(url);
|
|
},
|
|
|
|
saveAlert: async (alert: AlertDefinition): Promise<AlertDefinition> => {
|
|
let activeId = CondoService.getActiveCondoId();
|
|
if (!activeId) {
|
|
const condos = await CondoService.getCondos();
|
|
if (condos.length > 0) activeId = condos[0].id;
|
|
}
|
|
if (alert.id) {
|
|
await request(`/alerts/${alert.id}`, { method: 'PUT', body: JSON.stringify(alert) });
|
|
return alert;
|
|
} else {
|
|
return request('/alerts', {
|
|
method: 'POST',
|
|
body: JSON.stringify({ ...alert, condoId: activeId })
|
|
});
|
|
}
|
|
},
|
|
|
|
deleteAlert: async (id: string): Promise<void> => {
|
|
return request(`/alerts/${id}`, { method: 'DELETE' });
|
|
},
|
|
|
|
// --- Notices (Bacheca) ---
|
|
getNotices: async (condoId?: string): Promise<Notice[]> => {
|
|
let url = '/notices';
|
|
const activeId = condoId || CondoService.getActiveCondoId();
|
|
if (activeId) url += `?condoId=${activeId}`;
|
|
return request<Notice[]>(url);
|
|
},
|
|
|
|
getUnreadNoticesForUser: async (userId: string, condoId: string): Promise<Notice[]> => {
|
|
return request<Notice[]>(`/notices/unread?userId=${userId}&condoId=${condoId}`);
|
|
},
|
|
|
|
getNoticeReadStatus: async (noticeId: string): Promise<NoticeRead[]> => {
|
|
return request<NoticeRead[]>(`/notices/${noticeId}/read-status`);
|
|
},
|
|
|
|
markNoticeAsRead: async (noticeId: string, userId: string): Promise<void> => {
|
|
return request(`/notices/${noticeId}/read`, {
|
|
method: 'POST',
|
|
body: JSON.stringify({ userId })
|
|
});
|
|
},
|
|
|
|
saveNotice: async (notice: Notice): Promise<void> => {
|
|
if (notice.id) {
|
|
return request(`/notices/${notice.id}`, { method: 'PUT', body: JSON.stringify(notice) });
|
|
} else {
|
|
return request('/notices', { method: 'POST', body: JSON.stringify(notice) });
|
|
}
|
|
},
|
|
|
|
deleteNotice: async (id: string): Promise<void> => {
|
|
return request(`/notices/${id}`, { method: 'DELETE' });
|
|
},
|
|
|
|
// --- Tickets ---
|
|
getTickets: async (): Promise<Ticket[]> => {
|
|
let activeId = CondoService.getActiveCondoId();
|
|
if (!activeId) {
|
|
const condos = await CondoService.getCondos();
|
|
if(condos.length > 0) activeId = condos[0].id;
|
|
}
|
|
return request<Ticket[]>(`/tickets?condoId=${activeId}`);
|
|
},
|
|
|
|
createTicket: async (data: any): Promise<void> => {
|
|
let activeId = CondoService.getActiveCondoId();
|
|
if (!activeId) {
|
|
const condos = await CondoService.getCondos();
|
|
if (condos.length > 0) activeId = condos[0].id;
|
|
}
|
|
return request('/tickets', {
|
|
method: 'POST',
|
|
body: JSON.stringify({ ...data, condoId: activeId })
|
|
});
|
|
},
|
|
|
|
updateTicket: async (id: string, data: any): Promise<void> => {
|
|
return request(`/tickets/${id}`, {
|
|
method: 'PUT',
|
|
body: JSON.stringify(data)
|
|
});
|
|
},
|
|
|
|
deleteTicket: async (id: string): Promise<void> => {
|
|
return request(`/tickets/${id}`, { method: 'DELETE' });
|
|
},
|
|
|
|
getTicketComments: async (ticketId: string): Promise<TicketComment[]> => {
|
|
return request<TicketComment[]>(`/tickets/${ticketId}/comments`);
|
|
},
|
|
|
|
addTicketComment: async (ticketId: string, text: string): Promise<void> => {
|
|
return request(`/tickets/${ticketId}/comments`, {
|
|
method: 'POST',
|
|
body: JSON.stringify({ text })
|
|
});
|
|
},
|
|
|
|
getTicketAttachment: async (ticketId: string, attachmentId: string): Promise<any> => {
|
|
return request(`/tickets/${ticketId}/attachments/${attachmentId}`);
|
|
},
|
|
|
|
// --- Extraordinary Expenses ---
|
|
getExpenses: async (condoId?: string): Promise<ExtraordinaryExpense[]> => {
|
|
let url = '/expenses';
|
|
const activeId = condoId || CondoService.getActiveCondoId();
|
|
if (activeId) url += `?condoId=${activeId}`;
|
|
return request<ExtraordinaryExpense[]>(url);
|
|
},
|
|
|
|
getExpenseDetails: async (id: string): Promise<ExtraordinaryExpense> => {
|
|
return request<ExtraordinaryExpense>(`/expenses/${id}`);
|
|
},
|
|
|
|
createExpense: async (data: any): Promise<void> => {
|
|
let activeId = CondoService.getActiveCondoId();
|
|
if (!activeId) {
|
|
const condos = await CondoService.getCondos();
|
|
if (condos.length > 0) activeId = condos[0].id;
|
|
}
|
|
if (!activeId) throw new Error("No active condo");
|
|
return request('/expenses', {
|
|
method: 'POST',
|
|
body: JSON.stringify({ ...data, condoId: activeId })
|
|
});
|
|
},
|
|
|
|
updateExpense: async (id: string, data: any): Promise<void> => {
|
|
return request(`/expenses/${id}`, {
|
|
method: 'PUT',
|
|
body: JSON.stringify(data)
|
|
});
|
|
},
|
|
|
|
deleteExpense: async (id: string): Promise<void> => {
|
|
return request(`/expenses/${id}`, {
|
|
method: 'DELETE'
|
|
});
|
|
},
|
|
|
|
getExpenseAttachment: async (expenseId: string, attachmentId: string): Promise<any> => {
|
|
return request(`/expenses/${expenseId}/attachments/${attachmentId}`);
|
|
},
|
|
|
|
getMyExpenses: async (): Promise<any[]> => {
|
|
const activeId = CondoService.getActiveCondoId();
|
|
return request(`/my-expenses?condoId=${activeId}`);
|
|
},
|
|
|
|
payExpense: async (expenseId: string, amount: number, familyId?: string): Promise<void> => {
|
|
return request(`/expenses/${expenseId}/pay`, {
|
|
method: 'POST',
|
|
body: JSON.stringify({ amount, notes: 'PayPal / Manual Payment', familyId })
|
|
});
|
|
},
|
|
|
|
getExpensePayments: async (expenseId: string, familyId: string): Promise<any[]> => {
|
|
return request<any[]>(`/expenses/${expenseId}/shares/${familyId}/payments`);
|
|
},
|
|
|
|
deleteExpensePayment: async (paymentId: string): Promise<void> => {
|
|
return request(`/expenses/payments/${paymentId}`, { method: 'DELETE' });
|
|
},
|
|
|
|
// --- Condo Ordinary Expenses (Uscite) ---
|
|
getCondoExpenses: async (year?: number): Promise<CondoExpense[]> => {
|
|
const activeId = CondoService.getActiveCondoId();
|
|
let url = `/condo-expenses?condoId=${activeId}`;
|
|
if (year) url += `&year=${year}`;
|
|
return request<CondoExpense[]>(url);
|
|
},
|
|
|
|
saveCondoExpense: async (expense: Partial<CondoExpense>): Promise<void> => {
|
|
const activeId = CondoService.getActiveCondoId();
|
|
const payload = { ...expense, condoId: activeId };
|
|
if (expense.id) {
|
|
return request(`/condo-expenses/${expense.id}`, {
|
|
method: 'PUT',
|
|
body: JSON.stringify(payload)
|
|
});
|
|
} else {
|
|
return request('/condo-expenses', {
|
|
method: 'POST',
|
|
body: JSON.stringify(payload)
|
|
});
|
|
}
|
|
},
|
|
|
|
deleteCondoExpense: async (id: string): Promise<void> => {
|
|
return request(`/condo-expenses/${id}`, { method: 'DELETE' });
|
|
},
|
|
|
|
getCondoExpenseAttachment: async (expenseId: string, attId: string): Promise<any> => {
|
|
return request(`/condo-expenses/${expenseId}/attachments/${attId}`);
|
|
},
|
|
|
|
// --- Documents (Cloud/Local) ---
|
|
getDocuments: async (): Promise<Document[]> => {
|
|
const activeId = CondoService.getActiveCondoId();
|
|
return request<Document[]>(`/documents?condoId=${activeId}`);
|
|
},
|
|
|
|
uploadDocument: async (doc: any): Promise<void> => {
|
|
const activeId = CondoService.getActiveCondoId();
|
|
const settings = await CondoService.getSettings();
|
|
return request('/documents', {
|
|
method: 'POST',
|
|
body: JSON.stringify({ ...doc, condoId: activeId, storageConfig: settings.storageConfig })
|
|
});
|
|
},
|
|
|
|
deleteDocument: async (id: string): Promise<void> => {
|
|
return request(`/documents/${id}`, { method: 'DELETE' });
|
|
},
|
|
|
|
getDocumentDownload: async (id: string): Promise<{fileName: string, fileType: string, data: string}> => {
|
|
return request(`/documents/${id}/download`);
|
|
}
|
|
};
|