mirror of
https://github.com/R0m1k3/LogiFlow.git
synced 2026-10-24 07:28:19 +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;
|
endDate?: Date;
|
||||||
supplierIds?: number[];
|
supplierIds?: number[];
|
||||||
}): Promise<Array<{ groupId: number; storeName: string; orders: number; deliveries: 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 result = await db.execute(sql`
|
||||||
const startDateStr = filters.startDate.toISOString();
|
SELECT
|
||||||
orderConditions.push(sql`${orders.plannedDate} >= ${startDateStr}`);
|
g.id as "groupId",
|
||||||
deliveryConditions.push(sql`${deliveries.scheduledDate} >= ${startDateStr}`);
|
g.name as "storeName",
|
||||||
}
|
COALESCE(o.count, 0) as orders,
|
||||||
if (filters.endDate) {
|
COALESCE(d.count, 0) as deliveries
|
||||||
const endDateStr = filters.endDate.toISOString();
|
FROM groups g
|
||||||
orderConditions.push(sql`${orders.plannedDate} <= ${endDateStr}`);
|
LEFT JOIN (
|
||||||
deliveryConditions.push(sql`${deliveries.scheduledDate} <= ${endDateStr}`);
|
SELECT group_id, COUNT(*) as count
|
||||||
}
|
FROM orders
|
||||||
if (filters.supplierIds?.length) {
|
WHERE ${sql.raw(orderDateFilter)}
|
||||||
orderConditions.push(inArray(orders.supplierId, filters.supplierIds));
|
GROUP BY group_id
|
||||||
deliveryConditions.push(inArray(deliveries.supplierId, filters.supplierIds));
|
) 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`
|
const mappedResult = result.rows.map((row: any) => ({
|
||||||
SELECT
|
groupId: row.groupId,
|
||||||
g.id as "groupId",
|
storeName: row.storeName,
|
||||||
g.name as "storeName",
|
orders: Number(row.orders),
|
||||||
COALESCE(o.count, 0) as orders,
|
deliveries: Number(row.deliveries)
|
||||||
COALESCE(d.count, 0) as deliveries
|
}));
|
||||||
FROM groups g
|
|
||||||
LEFT JOIN (
|
|
||||||
SELECT group_id, COUNT(*) as count
|
return mappedResult;
|
||||||
FROM orders
|
} catch (error) {
|
||||||
WHERE ${orderConditions.length ? sql.join(orderConditions, sql` AND `) : sql`1=1`}
|
console.error('Error in getAnalyticsByStore:', error);
|
||||||
GROUP BY group_id
|
return [];
|
||||||
) 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)
|
|
||||||
}));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user