@riocrypto/common-server 1.0.2461 → 1.0.2463
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -20,23 +20,47 @@ const getOrderFromReference = (reference, mongoose) => __awaiter(void 0, void 0,
|
|
|
20
20
|
// Convert reference to lowercase to ensure case-insensitive comparison
|
|
21
21
|
const referenceText = reference.toLowerCase();
|
|
22
22
|
const Order = (0, order_1.buildOrder)(mongoose);
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
23
|
+
// Define statuses to exclude (completed, cancelled, and any failed status)
|
|
24
|
+
const excludedStatuses = Object.values(common_1.OrderStatus).filter((status) => {
|
|
25
|
+
const statusLower = status.toLowerCase();
|
|
26
|
+
return (statusLower === "complete" ||
|
|
27
|
+
statusLower === "cancelled" ||
|
|
28
|
+
statusLower === "expired" ||
|
|
29
|
+
statusLower === "refundcomplete" ||
|
|
30
|
+
statusLower.includes("failed"));
|
|
31
31
|
});
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
32
|
+
// Process in batches to avoid memory issues
|
|
33
|
+
const batchSize = 1000;
|
|
34
|
+
let skip = 0;
|
|
35
|
+
let foundOrder = null;
|
|
36
|
+
while (!foundOrder) {
|
|
37
|
+
// Query only active orders (exclude completed/cancelled/failed) in batches
|
|
38
|
+
const orders = yield Order.find({
|
|
39
|
+
status: { $nin: excludedStatuses },
|
|
40
|
+
})
|
|
41
|
+
.select("_id") // Only select the ID field to minimize memory usage
|
|
42
|
+
.skip(skip)
|
|
43
|
+
.limit(batchSize)
|
|
44
|
+
.lean(); // Use lean() for better performance
|
|
45
|
+
// If no more orders, break the loop
|
|
46
|
+
if (orders.length === 0) {
|
|
47
|
+
break;
|
|
48
|
+
}
|
|
49
|
+
// Check each order ID against the reference
|
|
50
|
+
for (const order of orders) {
|
|
51
|
+
const orderId = order._id.toString().toLowerCase();
|
|
52
|
+
const last6Chars = orderId.slice(-6);
|
|
53
|
+
if (referenceText.includes(last6Chars)) {
|
|
54
|
+
// Found a match, now fetch the full order document
|
|
55
|
+
foundOrder = yield Order.findById(order._id);
|
|
56
|
+
break;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
skip += batchSize;
|
|
60
|
+
}
|
|
61
|
+
if (!foundOrder) {
|
|
38
62
|
throw new Error("Order not found");
|
|
39
63
|
}
|
|
40
|
-
return
|
|
64
|
+
return foundOrder;
|
|
41
65
|
});
|
|
42
66
|
exports.getOrderFromReference = getOrderFromReference;
|