-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget-graph-revenue.ts
60 lines (51 loc) · 1.53 KB
/
get-graph-revenue.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import prismadb from "@/lib/prismadb";
interface GraphData {
name: string;
total: number;
}
export const getGraphRevenue = async (storeId: string): Promise<GraphData[]> => {
const paidOrders = await prismadb.order.findMany({
where: {
storeId,
isPaid: true,
},
include: {
orderItems: {
include: {
product: true,
},
},
},
});
const monthlyRevenue: { [key: number]: number } = {};
// Grouping the orders by month and summing the revenue
for (const order of paidOrders) {
const month = order.createdAt.getMonth(); // 0 for Jan, 1 for Feb, ...
let revenueForOrder = 0;
for (const item of order.orderItems) {
revenueForOrder += item.product.price.toNumber();
}
// Adding the revenue for this order to the respective month
monthlyRevenue[month] = (monthlyRevenue[month] || 0) + revenueForOrder;
}
// Converting the grouped data into the format expected by the graph
const graphData: GraphData[] = [
{ name: "Jan", total: 0 },
{ name: "Feb", total: 0 },
{ name: "Mar", total: 0 },
{ name: "Apr", total: 0 },
{ name: "May", total: 0 },
{ name: "Jun", total: 0 },
{ name: "Jul", total: 0 },
{ name: "Aug", total: 0 },
{ name: "Sep", total: 0 },
{ name: "Oct", total: 0 },
{ name: "Nov", total: 0 },
{ name: "Dec", total: 0 },
];
// Filling in the revenue data
for (const month in monthlyRevenue) {
graphData[parseInt(month)].total = monthlyRevenue[parseInt(month)];
}
return graphData;
};