mirror of
https://github.com/R0m1k3/LogiFlow.git
synced 2026-10-11 17:27:31 +02:00
Add debugging tools to help diagnose order display issues
Introduce new API endpoints and enhanced logging in `routes.ts` and `storage.ts` to allow for granular inspection of customer order data and filtering logic, aiding in the diagnosis of production discrepancies. Replit-Commit-Author: Agent Replit-Commit-Session-Id: a3de0820-9397-41b2-b000-7931ee9c29de Replit-Commit-Checkpoint-Type: full_checkpoint Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/1957c339-2757-4d1f-8e92-e9f71a1ce58e/a3de0820-9397-41b2-b000-7931ee9c29de/mKF47ul
This commit is contained in:
1 parent
f21d807968
commit
7613b257cc
2 files changed
+121
-9
No files matched your search
+93
-2
@@ -1395,6 +1395,75 @@ export async function registerRoutes(app: Express): Promise<Server> {
|
||||
}
|
||||
});
|
||||
|
||||
// DEBUG ROUTE for production diagnosis
|
||||
app.get('/api/debug/customer-orders-raw', isAuthenticated, async (req: any, res) => {
|
||||
try {
|
||||
const user = await storage.getUserWithGroups(req.user.claims ? req.user.claims.sub : req.user.id);
|
||||
if (!user || user.role !== 'admin') {
|
||||
return res.status(403).json({ message: "Admin access required" });
|
||||
}
|
||||
|
||||
console.log('🔍 [DEBUG] Raw customer orders analysis started');
|
||||
|
||||
// Get all customer orders without filtering
|
||||
const allOrders = await storage.getCustomerOrders();
|
||||
console.log('🔍 [DEBUG] Total orders in DB:', allOrders.length);
|
||||
|
||||
// Group by store and analyze dates
|
||||
const analysis = allOrders.reduce((acc, order) => {
|
||||
const groupId = order.groupId;
|
||||
const groupName = order.group?.name || 'Unknown';
|
||||
|
||||
if (!acc[groupId]) {
|
||||
acc[groupId] = {
|
||||
groupId,
|
||||
groupName,
|
||||
orders: [],
|
||||
count: 0,
|
||||
latestDate: null,
|
||||
oldestDate: null
|
||||
};
|
||||
}
|
||||
|
||||
acc[groupId].orders.push({
|
||||
id: order.id,
|
||||
customerName: order.customerName,
|
||||
createdAt: order.createdAt,
|
||||
status: order.status
|
||||
});
|
||||
acc[groupId].count++;
|
||||
|
||||
const orderDate = new Date(order.createdAt);
|
||||
if (!acc[groupId].latestDate || orderDate > new Date(acc[groupId].latestDate)) {
|
||||
acc[groupId].latestDate = order.createdAt;
|
||||
}
|
||||
if (!acc[groupId].oldestDate || orderDate < new Date(acc[groupId].oldestDate)) {
|
||||
acc[groupId].oldestDate = order.createdAt;
|
||||
}
|
||||
|
||||
return acc;
|
||||
}, {} as Record<number, any>);
|
||||
|
||||
console.log('🔍 [DEBUG] Analysis by group:', analysis);
|
||||
|
||||
res.json({
|
||||
totalOrders: allOrders.length,
|
||||
analysis: Object.values(analysis),
|
||||
sampleOrders: allOrders.slice(0, 10).map(order => ({
|
||||
id: order.id,
|
||||
groupId: order.groupId,
|
||||
groupName: order.group?.name,
|
||||
customerName: order.customerName,
|
||||
createdAt: order.createdAt,
|
||||
status: order.status
|
||||
}))
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Error in debug route:", error);
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// Customer Orders routes
|
||||
app.get('/api/customer-orders', isAuthenticated, async (req: any, res) => {
|
||||
try {
|
||||
@@ -1417,9 +1486,31 @@ export async function registerRoutes(app: Express): Promise<Server> {
|
||||
}
|
||||
}
|
||||
|
||||
console.log('Customer Orders API called with:', { groupIds, userRole: user.role });
|
||||
console.log('🔍 [PRODUCTION DEBUG] Customer Orders API called with:', {
|
||||
groupIds,
|
||||
userRole: user.role,
|
||||
userId: user.id,
|
||||
userGroups: user.userGroups?.map(ug => ({ groupId: ug.groupId, groupName: ug.group?.name }))
|
||||
});
|
||||
const customerOrders = await storage.getCustomerOrders(groupIds);
|
||||
console.log('Customer Orders returned:', customerOrders.length, 'items');
|
||||
console.log('🔍 [PRODUCTION DEBUG] Customer Orders returned:', customerOrders.length, 'items');
|
||||
|
||||
// Debug: Group orders by groupId to see distribution
|
||||
const ordersByGroup = customerOrders.reduce((acc, order) => {
|
||||
const groupId = order.groupId || 'null';
|
||||
if (!acc[groupId]) acc[groupId] = [];
|
||||
acc[groupId].push(order);
|
||||
return acc;
|
||||
}, {} as Record<string, any[]>);
|
||||
|
||||
console.log('🔍 [PRODUCTION DEBUG] Orders distribution by group:',
|
||||
Object.entries(ordersByGroup).map(([groupId, orders]) => ({
|
||||
groupId,
|
||||
count: orders.length,
|
||||
latestDate: orders[0]?.createdAt,
|
||||
groupName: orders[0]?.group?.name
|
||||
}))
|
||||
);
|
||||
res.json(customerOrders);
|
||||
} catch (error) {
|
||||
console.error("Error fetching customer orders:", error);
|
||||
|
||||
+28
-7
@@ -1324,6 +1324,8 @@ export class DatabaseStorage implements IStorage {
|
||||
|
||||
// Real implementations for production operations
|
||||
async getCustomerOrders(groupIds?: number[]): Promise<CustomerOrderWithRelations[]> {
|
||||
console.log('🔍 [PRODUCTION DEBUG] getCustomerOrders called with groupIds:', groupIds);
|
||||
|
||||
let query = db
|
||||
.select({
|
||||
customerOrder: customerOrders,
|
||||
@@ -1336,15 +1338,34 @@ export class DatabaseStorage implements IStorage {
|
||||
|
||||
if (groupIds && groupIds.length > 0) {
|
||||
query = query.where(inArray(customerOrders.groupId, groupIds));
|
||||
console.log('🔍 [PRODUCTION DEBUG] Filtering by groupIds:', groupIds);
|
||||
}
|
||||
|
||||
const results = await query.orderBy(desc(customerOrders.createdAt));
|
||||
return results.map((row: any) => ({
|
||||
...row.customerOrder,
|
||||
supplier: row.supplier,
|
||||
group: row.group,
|
||||
creator: row.creator || { id: row.customerOrder.createdBy, username: 'unknown' }
|
||||
}));
|
||||
try {
|
||||
const results = await query.orderBy(desc(customerOrders.createdAt));
|
||||
console.log('🔍 [PRODUCTION DEBUG] Raw SQL results count:', results.length);
|
||||
|
||||
// Debug: Log some sample data
|
||||
if (results.length > 0) {
|
||||
console.log('🔍 [PRODUCTION DEBUG] Sample results (first 3):');
|
||||
results.slice(0, 3).forEach((row, idx) => {
|
||||
console.log(` [${idx}] Order ID: ${row.customerOrder?.id}, GroupId: ${row.customerOrder?.groupId}, Customer: ${row.customerOrder?.customerName}, Created: ${row.customerOrder?.createdAt}`);
|
||||
});
|
||||
}
|
||||
|
||||
const mappedResults = results.map((row: any) => ({
|
||||
...row.customerOrder,
|
||||
supplier: row.supplier,
|
||||
group: row.group,
|
||||
creator: row.creator || { id: row.customerOrder.createdBy, username: 'unknown' }
|
||||
}));
|
||||
|
||||
console.log('🔍 [PRODUCTION DEBUG] Final mapped results count:', mappedResults.length);
|
||||
return mappedResults;
|
||||
} catch (error) {
|
||||
console.error('❌ [PRODUCTION ERROR] getCustomerOrders failed:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async getCustomerOrder(id: number): Promise<CustomerOrderWithRelations | undefined> {
|
||||
|
||||
Reference in new issue
Block a user