mirror of
https://github.com/R0m1k3/LogiFlow.git
synced 2026-10-15 03:09:32 +02:00
Improve store performance data retrieval and error handling
Refactor database query in `server/storage.ts` to use raw SQL for date and supplier filtering, improving performance and adding try-catch blocks for error handling. Replit-Commit-Author: Agent Replit-Commit-Session-Id: 1e4433b5-47ab-464c-b663-fea2e53367dd Replit-Commit-Checkpoint-Type: full_checkpoint Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/1957c339-2757-4d1f-8e92-e9f71a1ce58e/1e4433b5-47ab-464c-b663-fea2e53367dd/P1V76jG
This commit is contained in:
1 parent
c50ef4cea1
commit
c3150947a3
1 file changed
+56
-44
+56
-44
@@ -3085,52 +3085,64 @@ export class DatabaseStorage implements IStorage {
|
||||
endDate?: Date;
|
||||
supplierIds?: number[];
|
||||
}): Promise<Array<{ groupId: number; storeName: string; orders: number; deliveries: number }>> {
|
||||
const orderConditions: any[] = [];
|
||||
const deliveryConditions: any[] = [];
|
||||
|
||||
try {
|
||||
// Build date filter conditions as strings for raw SQL
|
||||
let orderDateFilter = '1=1';
|
||||
let deliveryDateFilter = '1=1';
|
||||
let supplierFilter = '1=1';
|
||||
|
||||
if (filters.startDate) {
|
||||
const startDate = filters.startDate.toISOString().split('T')[0];
|
||||
orderDateFilter += ` AND planned_date >= '${startDate}'`;
|
||||
deliveryDateFilter += ` AND scheduled_date >= '${startDate}'`;
|
||||
}
|
||||
if (filters.endDate) {
|
||||
const endDate = filters.endDate.toISOString().split('T')[0];
|
||||
orderDateFilter += ` AND planned_date <= '${endDate}'`;
|
||||
deliveryDateFilter += ` AND scheduled_date <= '${endDate}'`;
|
||||
}
|
||||
if (filters.supplierIds?.length) {
|
||||
const supplierIds = filters.supplierIds.join(',');
|
||||
orderDateFilter += ` AND supplier_id IN (${supplierIds})`;
|
||||
deliveryDateFilter += ` AND supplier_id IN (${supplierIds})`;
|
||||
}
|
||||
|
||||
if (filters.startDate) {
|
||||
const startDateStr = filters.startDate.toISOString();
|
||||
orderConditions.push(sql`${orders.plannedDate} >= ${startDateStr}`);
|
||||
deliveryConditions.push(sql`${deliveries.scheduledDate} >= ${startDateStr}`);
|
||||
}
|
||||
if (filters.endDate) {
|
||||
const endDateStr = filters.endDate.toISOString();
|
||||
orderConditions.push(sql`${orders.plannedDate} <= ${endDateStr}`);
|
||||
deliveryConditions.push(sql`${deliveries.scheduledDate} <= ${endDateStr}`);
|
||||
}
|
||||
if (filters.supplierIds?.length) {
|
||||
orderConditions.push(inArray(orders.supplierId, filters.supplierIds));
|
||||
deliveryConditions.push(inArray(deliveries.supplierId, filters.supplierIds));
|
||||
}
|
||||
const result = await db.execute(sql`
|
||||
SELECT
|
||||
g.id as "groupId",
|
||||
g.name as "storeName",
|
||||
COALESCE(o.count, 0) as orders,
|
||||
COALESCE(d.count, 0) as deliveries
|
||||
FROM groups g
|
||||
LEFT JOIN (
|
||||
SELECT group_id, COUNT(*) as count
|
||||
FROM orders
|
||||
WHERE ${sql.raw(orderDateFilter)}
|
||||
GROUP BY group_id
|
||||
) o ON g.id = o.group_id
|
||||
LEFT JOIN (
|
||||
SELECT group_id, COUNT(*) as count
|
||||
FROM deliveries
|
||||
WHERE ${sql.raw(deliveryDateFilter)}
|
||||
GROUP BY group_id
|
||||
) d ON g.id = d.group_id
|
||||
ORDER BY (COALESCE(o.count, 0) + COALESCE(d.count, 0)) DESC
|
||||
`);
|
||||
|
||||
const result = await db.execute(sql`
|
||||
SELECT
|
||||
g.id as "groupId",
|
||||
g.name as "storeName",
|
||||
COALESCE(o.count, 0) as orders,
|
||||
COALESCE(d.count, 0) as deliveries
|
||||
FROM groups g
|
||||
LEFT JOIN (
|
||||
SELECT group_id, COUNT(*) as count
|
||||
FROM orders
|
||||
WHERE ${orderConditions.length ? sql.join(orderConditions, sql` AND `) : sql`1=1`}
|
||||
GROUP BY group_id
|
||||
) o ON g.id = o.group_id
|
||||
LEFT JOIN (
|
||||
SELECT group_id, COUNT(*) as count
|
||||
FROM deliveries
|
||||
WHERE ${deliveryConditions.length ? sql.join(deliveryConditions, sql` AND `) : sql`1=1`}
|
||||
GROUP BY group_id
|
||||
) d ON g.id = d.group_id
|
||||
ORDER BY (COALESCE(o.count, 0) + COALESCE(d.count, 0)) DESC
|
||||
`);
|
||||
|
||||
return result.rows.map((row: any) => ({
|
||||
groupId: row.groupId,
|
||||
storeName: row.storeName,
|
||||
orders: Number(row.orders),
|
||||
deliveries: Number(row.deliveries)
|
||||
}));
|
||||
const mappedResult = result.rows.map((row: any) => ({
|
||||
groupId: row.groupId,
|
||||
storeName: row.storeName,
|
||||
orders: Number(row.orders),
|
||||
deliveries: Number(row.deliveries)
|
||||
}));
|
||||
|
||||
|
||||
return mappedResult;
|
||||
} catch (error) {
|
||||
console.error('Error in getAnalyticsByStore:', error);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user