@riocrypto/common-server 1.0.1755 → 1.0.1757
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.
|
@@ -10,24 +10,33 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.getOrderFromReference = void 0;
|
|
13
|
-
const user_1 = require("../models/user");
|
|
14
13
|
const order_1 = require("../models/order");
|
|
14
|
+
const user_1 = require("../models/user");
|
|
15
15
|
const getOrderFromReference = (reference, mongoose) => __awaiter(void 0, void 0, void 0, function* () {
|
|
16
16
|
// Extract the last 2 characters (userId part) and the last 4 characters (orderId part)
|
|
17
|
-
const userIdPart = reference.slice(0, 2);
|
|
18
|
-
const orderIdPart = reference.slice(2, 6);
|
|
19
|
-
// Find the user whose ID ends with the extracted userIdPart
|
|
17
|
+
const userIdPart = reference.slice(0, 2).toLocaleLowerCase();
|
|
18
|
+
const orderIdPart = reference.slice(2, 6).toLocaleLowerCase();
|
|
20
19
|
const User = (0, user_1.buildUser)(mongoose);
|
|
21
|
-
const
|
|
20
|
+
const users = yield User.find().exec();
|
|
21
|
+
// Ensure userIdPart is defined and only contains two characters
|
|
22
|
+
if (!userIdPart || userIdPart.length !== 2) {
|
|
23
|
+
throw new Error("Invalid userIdPart. It must be exactly 2 characters long.");
|
|
24
|
+
}
|
|
25
|
+
// Find the user whose id ends with the given part
|
|
26
|
+
const user = users.find((user) => {
|
|
27
|
+
const userIdStr = user.id.toString(); // Convert id to string if it isn't
|
|
28
|
+
return userIdStr.endsWith(userIdPart);
|
|
29
|
+
});
|
|
22
30
|
if (!user) {
|
|
23
31
|
throw new Error("User not found");
|
|
24
32
|
}
|
|
25
33
|
const Order = (0, order_1.buildOrder)(mongoose);
|
|
26
|
-
//
|
|
27
|
-
const
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
34
|
+
// Fetch all orders for the user and filter them by the last 4 characters of their ObjectId
|
|
35
|
+
const orders = yield Order.find({ userId: user.id }).exec();
|
|
36
|
+
const order = orders.find((order) => order.id.endsWith(orderIdPart));
|
|
37
|
+
if (!order) {
|
|
38
|
+
throw new Error("Order not found");
|
|
39
|
+
}
|
|
31
40
|
return order;
|
|
32
41
|
});
|
|
33
42
|
exports.getOrderFromReference = getOrderFromReference;
|