MCPcopy Create free account
hub / github.com/Useful-Scripts-Extension/useful-script / getStatistics

Function getStatistics

scripts/tiki_totalSpendMoney.js:41–92  ·  view source on GitHub ↗
(orderType)

Source from the content-addressed store, hash-verified

39
40 // https://tiki.vn/api/v2/orders?page=0&limit=10&status=canceled
41 async function getStatistics(orderType) {
42 let pulling = true;
43 let page = 0;
44 let limit = 10;
45
46 let totalDiscount = 0;
47 let totalShip = 0;
48 let totalSpent = 0;
49 let totalItems = 0;
50 let totalOrders = 0;
51
52 while (pulling) {
53 setLoadingText("Đang tải trang thứ " + (page + 1) + "...");
54 let res = await fetch(
55 `https://tiki.vn/api/v2/orders?page=${page}&limit=${limit}${
56 orderType ? "&status=" + orderType : ""
57 }`
58 );
59 let json = await res.json();
60 if (json?.error) throw json.error;
61
62 let orders = json.data;
63 totalOrders += orders.length;
64 for (let i = 0; i < orders.length; i++) {
65 setLoadingText(
66 "Đang tải đơn hàng thứ " + (page * limit + i + 1) + "..."
67 );
68
69 let order = orders[i];
70 let detail = await getOrderDetail(order.id);
71
72 totalSpent += order.grand_total;
73 totalItems += detail.items.reduce(
74 (total, cur) => total + cur.qty,
75 0
76 );
77 totalDiscount += detail.discount_amount;
78 totalShip += detail.shipping_amount;
79 }
80
81 pulling = orders.length >= limit;
82 page++;
83 }
84
85 return {
86 totalDiscount,
87 totalShip,
88 totalSpent,
89 totalItems,
90 totalOrders,
91 };
92 }
93
94 async function getOrderDetail(orderId) {
95 let res = await fetch(

Callers 1

Calls 1

getOrderDetailFunction · 0.70

Tested by

no test coverage detected