Fix customer order creation for employees by correctly assigning store groups

Correctly assign `groupId` in the customer order form's default values to ensure orders are associated with the correct store for employees, resolving an issue where orders were appearing in the wrong location.

Replit-Commit-Author: Agent
Replit-Commit-Session-Id: b163d4c0-de5e-4f4e-a9c0-aed4c7049718
Replit-Commit-Checkpoint-Type: full_checkpoint
Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/1957c339-2757-4d1f-8e92-e9f71a1ce58e/b163d4c0-de5e-4f4e-a9c0-aed4c7049718/Yyj5QkL
This commit is contained in:
michaelschal committed 2025-08-12 15:38:10 +00:00
1 parent 27972c0bce
commit 311286646f
3 files changed
+64 -80

No files matched your search

+22 -1
View File
@@ -66,6 +66,27 @@ export function CustomerOrderForm({
queryKey: ['/api/suppliers'],
});
// Calculate correct groupId with PRIORITY for user's assigned group
const getDefaultGroupId = () => {
if (order?.groupId) return order.groupId;
// PRIORITY 1: User's assigned group (works for employees assigned to specific stores)
if (user?.userGroups?.[0]?.groupId) {
console.log("🎯 CustomerOrderForm defaultValue: Using user's assigned group:", user.userGroups[0].groupId);
return user.userGroups[0].groupId;
}
// PRIORITY 2: Admin selected store (only if no assigned group)
if (user?.role === 'admin' && selectedStoreId) {
console.log("🎯 CustomerOrderForm defaultValue: Using admin selected store:", selectedStoreId);
return selectedStoreId;
}
// FALLBACK: Default to 1
console.log("🚨 CustomerOrderForm defaultValue: Using fallback groupId 1");
return 1;
};
const form = useForm<CustomerOrderFormData>({
resolver: zodResolver(customerOrderFormSchema),
defaultValues: {
@@ -81,7 +102,7 @@ export function CustomerOrderForm({
deposit: order?.deposit || 0,
isPromotionalPrice: order?.isPromotionalPrice || false,
customerNotified: order?.customerNotified || false,
groupId: order?.groupId || (user?.userGroups?.[0]?.groupId || (user?.role === 'admin' && selectedStoreId ? selectedStoreId : 1)), // PRIORITÉ: groupe utilisateur assigné
groupId: getDefaultGroupId(), // ✅ PRIORITÉ CORRECTE APPLIQUÉE
},
});