diff --git a/client/src/components/SalesHistory.tsx b/client/src/components/SalesHistory.tsx index e83f678..6527e7f 100644 --- a/client/src/components/SalesHistory.tsx +++ b/client/src/components/SalesHistory.tsx @@ -33,8 +33,8 @@ export default function SalesHistory({ canDelete = false }: SalesHistoryProps) { if (startDate) params.append('startDate', startDate); if (endDate) params.append('endDate', endDate); - // Add storeId for admin users - if (user?.role === 'admin' && selectedStoreId) { + // Always add storeId for queries + if (selectedStoreId) { params.append('storeId', selectedStoreId.toString()); } @@ -42,7 +42,9 @@ export default function SalesHistory({ canDelete = false }: SalesHistoryProps) { const response = await apiRequest(`/api/sales${queryString ? `?${queryString}` : ''}`); return response.json(); }, - enabled: !!selectedStoreId, // Only run query when store is selected + enabled: !!selectedStoreId && !!user, // Only run query when store is selected and user is loaded + refetchOnMount: true, // Always refetch when component mounts + refetchOnWindowFocus: false, // Don't refetch on window focus }); const sales = Array.isArray(salesData) ? salesData : []; diff --git a/server/routes.ts b/server/routes.ts index c7d34a1..2880a18 100644 --- a/server/routes.ts +++ b/server/routes.ts @@ -149,16 +149,17 @@ export async function registerRoutes(app: Express): Promise { // Determine which store to query let targetStoreId: number; - if (user.role === 'admin') { - if (storeId) { - // Admin querying a specific store - targetStoreId = parseInt(storeId as string); - } else { - // Admin without specific store - should not happen with the new frontend - return res.status(400).json({ error: "Store ID required for admin queries" }); + + if (storeId) { + // storeId provided in query + targetStoreId = parseInt(storeId as string); + + // For non-admin users, verify they can only access their own store + if (user.role !== 'admin' && targetStoreId !== user.storeId) { + return res.status(403).json({ error: "Access denied to this store" }); } } else { - // Non-admin users can only query their own store + // No storeId provided - use user's assigned store for non-admin if (!user.storeId) { return res.status(400).json({ error: "User has no assigned store" }); }