From 29196cfc9fb73c6ef0f5c4362f404bca915b96c7 Mon Sep 17 00:00:00 2001 From: frakarr Date: Wed, 10 Dec 2025 23:43:23 +0100 Subject: [PATCH] Update mockDb.ts --- services/mockDb.ts | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/services/mockDb.ts b/services/mockDb.ts index 502f85f..43a978b 100644 --- a/services/mockDb.ts +++ b/services/mockDb.ts @@ -100,10 +100,20 @@ export const CondoService = { getActiveCondo: async (): Promise => { const id = localStorage.getItem('active_condo_id'); const condos = await CondoService.getCondos(); + + let match; if (id) { - return condos.find(c => c.id === id); + match = condos.find(c => c.id === id); } - return condos.length > 0 ? condos[0] : undefined; + + // Auto-repair: If the stored ID matches nothing in the new DB, but we have condos, default to the first one. + if (!match && condos.length > 0) { + const firstCondoId = condos[0].id; + localStorage.setItem('active_condo_id', firstCondoId); + return condos[0]; + } + + return match; }, setActiveCondo: (id: string) => { @@ -143,7 +153,10 @@ export const CondoService = { let activeId = CondoService.getActiveCondoId(); if (!activeId) { const condos = await CondoService.getCondos(); - if (condos.length > 0) activeId = condos[0].id; + if (condos.length > 0) { + activeId = condos[0].id; + localStorage.setItem('active_condo_id', activeId); + } } return request('/families', { method: 'POST', @@ -236,7 +249,7 @@ export const CondoService = { // Notices getNotices: async (condoId?: string): Promise => { let url = '/notices'; - const activeId = condoId || CondoService.getActiveCondoId(); + const activeId = CondoService.getActiveCondoId(); if (activeId) url += `?condoId=${activeId}`; return request(url); }, @@ -270,13 +283,16 @@ export const CondoService = { // Tickets getTickets: async (): 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(`/tickets?condoId=${activeId}`); }, createTicket: async (data: any): Promise => { 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;