diff --git a/services/mockDb.ts b/services/mockDb.ts index 8d72fe8..502f85f 100644 --- a/services/mockDb.ts +++ b/services/mockDb.ts @@ -140,7 +140,11 @@ export const CondoService = { }, addFamily: async (family: any): Promise => { - const activeId = CondoService.getActiveCondoId(); + let activeId = CondoService.getActiveCondoId(); + if (!activeId) { + const condos = await CondoService.getCondos(); + if (condos.length > 0) activeId = condos[0].id; + } return request('/families', { method: 'POST', body: JSON.stringify({ ...family, condoId: activeId }) @@ -209,7 +213,11 @@ export const CondoService = { }, saveAlert: async (alert: AlertDefinition): Promise => { - const activeId = CondoService.getActiveCondoId(); + 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; @@ -267,7 +275,12 @@ export const CondoService = { }, createTicket: async (data: any): Promise => { - const activeId = CondoService.getActiveCondoId(); + let activeId = CondoService.getActiveCondoId(); + // Robustness: If no activeId (e.g. standard user), fetch condos and use the first one + 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 }) @@ -313,7 +326,11 @@ export const CondoService = { }, createExpense: async (data: any): Promise => { - const activeId = CondoService.getActiveCondoId(); + 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',