Fix customer order creation for employees assigned to different stores

Correctly assigns the customer order's store based on the employee's assigned store, preventing orders from being misdirected.

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/z5Nx2ao
This commit is contained in:
michaelschal committed 2025-08-12 15:04:05 +00:00
1 parent 00e3c98550
commit 8939b40bcc
4 files changed
+142 -6

No files matched your search

+38 -4
View File
@@ -1950,9 +1950,37 @@ RÉSUMÉ DU SCAN
return res.status(404).json({ message: "User not found" });
}
// Fix groupId BEFORE validation - use user's assigned group or fallback
let finalGroupId = req.body.groupId;
if (!finalGroupId || finalGroupId === undefined || finalGroupId === null || finalGroupId === '') {
if (user.userGroups?.[0]?.groupId) {
finalGroupId = user.userGroups[0].groupId;
console.log("🔧 Customer Order Backend Fix: Using user's assigned group:", finalGroupId);
} else {
finalGroupId = 1; // Emergency fallback
console.log("🚨 Customer Order Backend Fix: Using emergency fallback groupId:", finalGroupId);
}
}
console.log("🔍 GroupId resolution debug:", {
originalGroupId: req.body.groupId,
finalGroupId,
typeOfFinal: typeof finalGroupId,
userGroups: user.userGroups?.map(ug => ({groupId: ug.groupId, groupName: ug.group?.name}))
});
// Prepare body with valid groupId for validation
const bodyWithGroupId = {
...req.body,
groupId: finalGroupId
};
// Use frontend schema and map to backend fields
const frontendData = insertCustomerOrderFrontendSchema.parse(req.body);
console.log("Frontend data parsed:", frontendData);
console.log("🔍 Pre-parse bodyWithGroupId:", bodyWithGroupId);
console.log("🔍 GroupId value and type:", { groupId: bodyWithGroupId.groupId, type: typeof bodyWithGroupId.groupId });
const frontendData = insertCustomerOrderFrontendSchema.parse(bodyWithGroupId);
console.log("Frontend data parsed with fixed groupId:", frontendData);
// Map frontend fields to backend schema
const backendData = {
@@ -1965,7 +1993,7 @@ RÉSUMÉ DU SCAN
gencode: frontendData.gencode || "",
quantity: frontendData.quantity,
supplierId: frontendData.supplierId || 1, // Default supplier
groupId: frontendData.groupId,
groupId: finalGroupId, // Use fixed groupId
deposit: frontendData.deposit,
isPromotionalPrice: frontendData.isPromotionalPrice,
notes: frontendData.notes,
@@ -1975,7 +2003,13 @@ RÉSUMÉ DU SCAN
console.log("Backend data mapped:", backendData);
// REMOVED: All role restrictions - tous les rôles peuvent créer des commandes client
console.log("Creating customer order - no role restrictions:", { userId, userRole: user.role, groupId: backendData.groupId });
console.log("Creating customer order - no role restrictions:", {
userId,
userRole: user.role,
userGroups: user.userGroups?.map(ug => ({groupId: ug.groupId, groupName: ug.group?.name})),
originalGroupId: req.body.groupId,
finalGroupId: backendData.groupId
});
const customerOrder = await storage.createCustomerOrder(backendData);
res.status(201).json(customerOrder);