mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-21 09:31:13 -05:00
Fix: Apply filters and sorting to count query
This commit is contained in:
@@ -197,7 +197,74 @@ export function useModerationQueueManager(config: ModerationQueueManagerConfig):
|
|||||||
setLoadingState("refreshing");
|
setLoadingState("refreshing");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Build base query
|
// Helper function to apply filters consistently to both count and data queries
|
||||||
|
const applyFilters = (query: any) => {
|
||||||
|
let filteredQuery = query;
|
||||||
|
|
||||||
|
// CRITICAL: Multi-level ordering
|
||||||
|
// Level 1: Always sort by escalated first (descending)
|
||||||
|
filteredQuery = filteredQuery.order('escalated', { ascending: false });
|
||||||
|
|
||||||
|
// Level 2: Apply user-selected sort (use debounced config)
|
||||||
|
filteredQuery = filteredQuery.order(
|
||||||
|
sort.debouncedConfig.field,
|
||||||
|
{ ascending: sort.debouncedConfig.direction === 'asc' }
|
||||||
|
);
|
||||||
|
|
||||||
|
// Level 3: Tertiary sort by created_at (if not already primary)
|
||||||
|
if (sort.debouncedConfig.field !== 'created_at') {
|
||||||
|
filteredQuery = filteredQuery.order('created_at', { ascending: true });
|
||||||
|
}
|
||||||
|
|
||||||
|
// Apply tab-based status filtering
|
||||||
|
const tab = filters.activeTab;
|
||||||
|
const statusFilter = filters.debouncedStatusFilter;
|
||||||
|
const entityFilter = filters.debouncedEntityFilter;
|
||||||
|
|
||||||
|
if (tab === "mainQueue") {
|
||||||
|
if (statusFilter === "all") {
|
||||||
|
filteredQuery = filteredQuery.in("status", ["pending", "flagged", "partially_approved"]);
|
||||||
|
} else if (statusFilter === "pending") {
|
||||||
|
filteredQuery = filteredQuery.in("status", ["pending", "partially_approved"]);
|
||||||
|
} else {
|
||||||
|
filteredQuery = filteredQuery.eq("status", statusFilter);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (statusFilter === "all") {
|
||||||
|
filteredQuery = filteredQuery.in("status", ["approved", "rejected"]);
|
||||||
|
} else {
|
||||||
|
filteredQuery = filteredQuery.eq("status", statusFilter);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Apply entity type filter
|
||||||
|
if (entityFilter === "photos") {
|
||||||
|
filteredQuery = filteredQuery.eq("submission_type", "photo");
|
||||||
|
} else if (entityFilter === "submissions") {
|
||||||
|
filteredQuery = filteredQuery.neq("submission_type", "photo");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Apply access control
|
||||||
|
if (!isAdmin && !isSuperuser) {
|
||||||
|
const now = new Date().toISOString();
|
||||||
|
filteredQuery = filteredQuery.or(
|
||||||
|
`assigned_to.is.null,locked_until.lt.${now},assigned_to.eq.${user.id}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return filteredQuery;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Get total count using the same filter logic
|
||||||
|
let countQuery = supabase
|
||||||
|
.from("content_submissions")
|
||||||
|
.select("*", { count: "exact", head: true });
|
||||||
|
|
||||||
|
countQuery = applyFilters(countQuery);
|
||||||
|
const { count } = await countQuery;
|
||||||
|
pagination.setTotalCount(count || 0);
|
||||||
|
|
||||||
|
// Build data query with full select
|
||||||
let submissionsQuery = supabase
|
let submissionsQuery = supabase
|
||||||
.from("content_submissions")
|
.from("content_submissions")
|
||||||
.select(
|
.select(
|
||||||
@@ -223,64 +290,8 @@ export function useModerationQueueManager(config: ModerationQueueManagerConfig):
|
|||||||
`,
|
`,
|
||||||
);
|
);
|
||||||
|
|
||||||
// CRITICAL: Multi-level ordering
|
// Apply the same filters to data query
|
||||||
// Level 1: Always sort by escalated first (descending)
|
submissionsQuery = applyFilters(submissionsQuery);
|
||||||
submissionsQuery = submissionsQuery.order('escalated', { ascending: false });
|
|
||||||
|
|
||||||
// Level 2: Apply user-selected sort (use debounced config)
|
|
||||||
submissionsQuery = submissionsQuery.order(
|
|
||||||
sort.debouncedConfig.field,
|
|
||||||
{ ascending: sort.debouncedConfig.direction === 'asc' }
|
|
||||||
);
|
|
||||||
|
|
||||||
// Level 3: Tertiary sort by created_at (if not already primary)
|
|
||||||
if (sort.debouncedConfig.field !== 'created_at') {
|
|
||||||
submissionsQuery = submissionsQuery.order('created_at', { ascending: true });
|
|
||||||
}
|
|
||||||
|
|
||||||
// Apply tab-based status filtering
|
|
||||||
const tab = filters.activeTab;
|
|
||||||
const statusFilter = filters.debouncedStatusFilter;
|
|
||||||
const entityFilter = filters.debouncedEntityFilter;
|
|
||||||
|
|
||||||
if (tab === "mainQueue") {
|
|
||||||
if (statusFilter === "all") {
|
|
||||||
submissionsQuery = submissionsQuery.in("status", ["pending", "flagged", "partially_approved"]);
|
|
||||||
} else if (statusFilter === "pending") {
|
|
||||||
submissionsQuery = submissionsQuery.in("status", ["pending", "partially_approved"]);
|
|
||||||
} else {
|
|
||||||
submissionsQuery = submissionsQuery.eq("status", statusFilter);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (statusFilter === "all") {
|
|
||||||
submissionsQuery = submissionsQuery.in("status", ["approved", "rejected"]);
|
|
||||||
} else {
|
|
||||||
submissionsQuery = submissionsQuery.eq("status", statusFilter);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Apply entity type filter
|
|
||||||
if (entityFilter === "photos") {
|
|
||||||
submissionsQuery = submissionsQuery.eq("submission_type", "photo");
|
|
||||||
} else if (entityFilter === "submissions") {
|
|
||||||
submissionsQuery = submissionsQuery.neq("submission_type", "photo");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Apply access control
|
|
||||||
if (!isAdmin && !isSuperuser) {
|
|
||||||
const now = new Date().toISOString();
|
|
||||||
submissionsQuery = submissionsQuery.or(
|
|
||||||
`assigned_to.is.null,locked_until.lt.${now},assigned_to.eq.${user.id}`,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get total count
|
|
||||||
const { count } = await supabase
|
|
||||||
.from("content_submissions")
|
|
||||||
.select("*", { count: "exact", head: true })
|
|
||||||
.match(submissionsQuery as any);
|
|
||||||
|
|
||||||
pagination.setTotalCount(count || 0);
|
|
||||||
|
|
||||||
// Apply pagination
|
// Apply pagination
|
||||||
const startIndex = pagination.startIndex;
|
const startIndex = pagination.startIndex;
|
||||||
|
|||||||
Reference in New Issue
Block a user