Enable store selection for admins and associate sales with specific stores

Implements store selection and association functionality using useStoreContext hook and updates sale creation and retrieval APIs.

Replit-Commit-Author: Agent
Replit-Commit-Session-Id: 91318273-c764-4fd4-be04-bdc12c38af32
Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/0715008c-7244-40f4-befc-26c014852236/d304bad8-e4eb-4558-b9d6-38704ba052e8.jpg
This commit is contained in:
michaelschal committed 2025-07-09 19:04:37 +00:00
1 parent a5336a5919
commit 4384818f63
5 files changed
+216 -7

No files matched your search

+11 -1
View File
@@ -8,6 +8,8 @@ import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@
import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from '@/components/ui/form';
import { Alert, AlertDescription } from '@/components/ui/alert';
import { useToast } from '@/hooks/use-toast';
import { useAuth } from '@/hooks/useAuth';
import { useStoreContext } from '@/hooks/useStoreContext';
import { apiRequest } from '@/lib/queryClient';
import { validateRequiredFields, validateEAN13, ARTICLE_CATEGORY_MAPPING, IDENTITY_TYPES } from '@/lib/validation';
import { Package, User, Users, Save, X } from 'lucide-react';
@@ -32,6 +34,8 @@ interface FormData {
export default function NewSaleForm() {
const { toast } = useToast();
const queryClient = useQueryClient();
const { user } = useAuth();
const { selectedStoreId } = useStoreContext();
const [isSubmitting, setIsSubmitting] = useState(false);
const form = useForm<FormData>({
@@ -58,9 +62,15 @@ export default function NewSaleForm() {
const createSaleMutation = useMutation({
mutationFn: async (saleData: any) => {
const dataToSend = {
...saleData,
// Include storeId for admin users
...(user?.role === 'admin' && selectedStoreId && { storeId: selectedStoreId })
};
const response = await apiRequest('/api/sales', {
method: 'POST',
body: JSON.stringify(saleData),
body: JSON.stringify(dataToSend),
});
return response.json();
},
+12 -1
View File
@@ -6,6 +6,8 @@ import { Input } from '@/components/ui/input';
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from '@/components/ui/table';
import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogTrigger } from '@/components/ui/dialog';
import { useToast } from '@/hooks/use-toast';
import { useStoreContext } from '@/hooks/useStoreContext';
import { useAuth } from '@/hooks/useAuth';
import { apiRequest } from '@/lib/queryClient';
import type { Sale } from '@shared/schema';
import { History, Download, Trash2, Eye, Filter } from 'lucide-react';
@@ -17,21 +19,30 @@ interface SalesHistoryProps {
export default function SalesHistory({ canDelete = false }: SalesHistoryProps) {
const { toast } = useToast();
const queryClient = useQueryClient();
const { user } = useAuth();
const { selectedStoreId } = useStoreContext();
const [selectedSale, setSelectedSale] = useState<Sale | null>(null);
const [startDate, setStartDate] = useState('');
const [endDate, setEndDate] = useState('');
// Query for sales
const { data: salesData, isLoading } = useQuery<Sale[]>({
queryKey: ['/api/sales', startDate, endDate],
queryKey: ['/api/sales', startDate, endDate, selectedStoreId],
queryFn: async () => {
const params = new URLSearchParams();
if (startDate) params.append('startDate', startDate);
if (endDate) params.append('endDate', endDate);
// Add storeId for admin users
if (user?.role === 'admin' && selectedStoreId) {
params.append('storeId', selectedStoreId.toString());
}
const queryString = params.toString();
const response = await apiRequest(`/api/sales${queryString ? `?${queryString}` : ''}`);
return response.json();
},
enabled: !!selectedStoreId, // Only run query when store is selected
});
const sales = Array.isArray(salesData) ? salesData : [];