mirror of
https://github.com/R0m1k3/LogiFlow.git
synced 2026-10-23 06:58:19 +02:00
Merge pull request #213 from R0m1k3/1.1
Filter ticket views and stats by selected store for administrators
This commit is contained in:
2 files changed
+50
-7
No files matched your search
@@ -11,6 +11,7 @@ import { Textarea } from "@/components/ui/textarea";
|
|||||||
import { useAuthUnified } from "@/hooks/useAuthUnified";
|
import { useAuthUnified } from "@/hooks/useAuthUnified";
|
||||||
import { useToast } from "@/hooks/use-toast";
|
import { useToast } from "@/hooks/use-toast";
|
||||||
import { apiRequest } from "@/lib/queryClient";
|
import { apiRequest } from "@/lib/queryClient";
|
||||||
|
import { useStore } from "@/components/Layout";
|
||||||
import {
|
import {
|
||||||
Plus,
|
Plus,
|
||||||
Search,
|
Search,
|
||||||
@@ -52,6 +53,7 @@ export default function SavTickets() {
|
|||||||
const { user } = useAuthUnified();
|
const { user } = useAuthUnified();
|
||||||
const { toast } = useToast();
|
const { toast } = useToast();
|
||||||
const queryClient = useQueryClient();
|
const queryClient = useQueryClient();
|
||||||
|
const { selectedStoreId, storeInitialized } = useStore();
|
||||||
|
|
||||||
const [searchTerm, setSearchTerm] = useState("");
|
const [searchTerm, setSearchTerm] = useState("");
|
||||||
const [statusFilter, setStatusFilter] = useState("all");
|
const [statusFilter, setStatusFilter] = useState("all");
|
||||||
@@ -79,16 +81,22 @@ export default function SavTickets() {
|
|||||||
clientPhone: ""
|
clientPhone: ""
|
||||||
});
|
});
|
||||||
|
|
||||||
// Build query parameters
|
// Build query parameters with store filtering
|
||||||
const queryParams = new URLSearchParams();
|
const queryParams = new URLSearchParams();
|
||||||
if (statusFilter !== 'all') queryParams.append('status', statusFilter);
|
if (statusFilter !== 'all') queryParams.append('status', statusFilter);
|
||||||
if (priorityFilter !== 'all') queryParams.append('priority', priorityFilter);
|
if (priorityFilter !== 'all') queryParams.append('priority', priorityFilter);
|
||||||
if (supplierFilter !== 'all') queryParams.append('supplierId', supplierFilter);
|
if (supplierFilter !== 'all') queryParams.append('supplierId', supplierFilter);
|
||||||
|
|
||||||
|
// Add store filtering for admin users
|
||||||
|
if (selectedStoreId && user?.role === 'admin') {
|
||||||
|
queryParams.append('groupId', selectedStoreId.toString());
|
||||||
|
}
|
||||||
|
|
||||||
const ticketsUrl = `/api/sav/tickets${queryParams.toString() ? '?' + queryParams.toString() : ''}`;
|
const ticketsUrl = `/api/sav/tickets${queryParams.toString() ? '?' + queryParams.toString() : ''}`;
|
||||||
|
|
||||||
const { data: ticketsData = [], isLoading } = useQuery<SavTicketWithRelations[]>({
|
const { data: ticketsData = [], isLoading } = useQuery<SavTicketWithRelations[]>({
|
||||||
queryKey: [ticketsUrl],
|
queryKey: [ticketsUrl, selectedStoreId],
|
||||||
|
enabled: !!user && (user.role !== 'admin' || storeInitialized),
|
||||||
staleTime: 1000 * 60 * 5, // 5 minutes
|
staleTime: 1000 * 60 * 5, // 5 minutes
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -109,7 +117,24 @@ export default function SavTickets() {
|
|||||||
resolvedTickets: number;
|
resolvedTickets: number;
|
||||||
criticalTickets: number;
|
criticalTickets: number;
|
||||||
}>({
|
}>({
|
||||||
queryKey: ['/api/sav/stats'],
|
queryKey: ['/api/sav/stats', selectedStoreId],
|
||||||
|
queryFn: async () => {
|
||||||
|
const params = new URLSearchParams();
|
||||||
|
if (selectedStoreId && user?.role === 'admin') {
|
||||||
|
params.append('groupId', selectedStoreId.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
const response = await fetch(`/api/sav/stats?${params.toString()}`, {
|
||||||
|
credentials: 'include'
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error(`HTTP error! status: ${response.status}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return response.json();
|
||||||
|
},
|
||||||
|
enabled: !!user && (user.role !== 'admin' || storeInitialized),
|
||||||
staleTime: 1000 * 60 * 2, // 2 minutes
|
staleTime: 1000 * 60 * 2, // 2 minutes
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
+22
-4
@@ -2623,12 +2623,21 @@ RÉSUMÉ DU SCAN
|
|||||||
// Get user's group IDs for filtering
|
// Get user's group IDs for filtering
|
||||||
let groupIds: number[] = [];
|
let groupIds: number[] = [];
|
||||||
if (user.role === 'admin') {
|
if (user.role === 'admin') {
|
||||||
// Admin can see all tickets
|
// Admin can see all tickets if no specific store is selected
|
||||||
groupIds = [];
|
// But if a store is selected in the UI, filter by that store
|
||||||
|
const selectedGroupId = req.query.groupId ? parseInt(req.query.groupId) : null;
|
||||||
|
if (selectedGroupId) {
|
||||||
|
groupIds = [selectedGroupId];
|
||||||
|
console.log(`🎫 [SAV] Admin filtering by selected store: ${selectedGroupId}`);
|
||||||
|
} else {
|
||||||
|
groupIds = []; // See all tickets
|
||||||
|
console.log(`🎫 [SAV] Admin viewing all tickets`);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
// Other roles see only their assigned groups
|
// Other roles see only their assigned groups
|
||||||
const userGroups = (user as any).userGroups;
|
const userGroups = (user as any).userGroups;
|
||||||
groupIds = userGroups ? userGroups.map((ug: any) => ug.groupId) : [];
|
groupIds = userGroups ? userGroups.map((ug: any) => ug.groupId) : [];
|
||||||
|
console.log(`🎫 [SAV] User ${user.username} (${user.role}) can see stores:`, groupIds);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse query filters
|
// Parse query filters
|
||||||
@@ -2891,12 +2900,21 @@ RÉSUMÉ DU SCAN
|
|||||||
// Get user's group IDs for filtering
|
// Get user's group IDs for filtering
|
||||||
let groupIds: number[] = [];
|
let groupIds: number[] = [];
|
||||||
if (user.role === 'admin') {
|
if (user.role === 'admin') {
|
||||||
// Admin can see all stats
|
// Admin can see all stats if no specific store is selected
|
||||||
groupIds = [];
|
// But if a store is selected in the UI, filter by that store
|
||||||
|
const selectedGroupId = req.query.groupId ? parseInt(req.query.groupId) : null;
|
||||||
|
if (selectedGroupId) {
|
||||||
|
groupIds = [selectedGroupId];
|
||||||
|
console.log(`📊 [SAV STATS] Admin filtering by selected store: ${selectedGroupId}`);
|
||||||
|
} else {
|
||||||
|
groupIds = []; // See all stats
|
||||||
|
console.log(`📊 [SAV STATS] Admin viewing all stats`);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
// Other roles see only their assigned groups
|
// Other roles see only their assigned groups
|
||||||
const userGroups = (user as any).userGroups;
|
const userGroups = (user as any).userGroups;
|
||||||
groupIds = userGroups ? userGroups.map((ug: any) => ug.groupId) : [];
|
groupIds = userGroups ? userGroups.map((ug: any) => ug.groupId) : [];
|
||||||
|
console.log(`📊 [SAV STATS] User ${user.username} (${user.role}) can see stores:`, groupIds);
|
||||||
}
|
}
|
||||||
|
|
||||||
const stats = await storage.getSavTicketStats(groupIds.length > 0 ? groupIds : undefined);
|
const stats = await storage.getSavTicketStats(groupIds.length > 0 ? groupIds : undefined);
|
||||||
|
|||||||
Reference in new issue
Block a user