@riocrypto/common-server 1.0.1754 → 1.0.1756
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.
|
@@ -1 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
import { OrderDoc } from "../models/order";
|
|
2
|
+
export declare const generateReference: (order: OrderDoc) => string;
|
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.generateReference = void 0;
|
|
4
|
-
const generateReference = (
|
|
4
|
+
const generateReference = (order) => {
|
|
5
5
|
// Validate that userId and orderId are valid MongoDB ObjectIDs
|
|
6
6
|
const mongoIdRegex = /^[a-f\d]{24}$/i;
|
|
7
|
-
if (!mongoIdRegex.test(userId) || !mongoIdRegex.test(
|
|
7
|
+
if (!mongoIdRegex.test(order.userId) || !mongoIdRegex.test(order.id)) {
|
|
8
8
|
throw new Error("Invalid MongoDB ObjectID format");
|
|
9
9
|
}
|
|
10
|
-
const userIdLast2 = userId.slice(-2);
|
|
11
|
-
const orderIdLast4 =
|
|
10
|
+
const userIdLast2 = order.userId.slice(-2);
|
|
11
|
+
const orderIdLast4 = order.id.slice(-4);
|
|
12
12
|
const referenceNumber = userIdLast2 + orderIdLast4;
|
|
13
13
|
return referenceNumber;
|
|
14
14
|
};
|
|
@@ -16,18 +16,20 @@ const getOrderFromReference = (reference, mongoose) => __awaiter(void 0, void 0,
|
|
|
16
16
|
// Extract the last 2 characters (userId part) and the last 4 characters (orderId part)
|
|
17
17
|
const userIdPart = reference.slice(0, 2);
|
|
18
18
|
const orderIdPart = reference.slice(2, 6);
|
|
19
|
-
//
|
|
19
|
+
// Fetch all users and filter them by the last 2 characters of their ObjectId
|
|
20
20
|
const User = (0, user_1.buildUser)(mongoose);
|
|
21
|
-
const
|
|
21
|
+
const users = yield User.find().exec();
|
|
22
|
+
const user = users.find((user) => user.id.endsWith(userIdPart));
|
|
22
23
|
if (!user) {
|
|
23
24
|
throw new Error("User not found");
|
|
24
25
|
}
|
|
25
26
|
const Order = (0, order_1.buildOrder)(mongoose);
|
|
26
|
-
//
|
|
27
|
-
const
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
27
|
+
// Fetch all orders for the user and filter them by the last 4 characters of their ObjectId
|
|
28
|
+
const orders = yield Order.find({ userId: user._id }).exec();
|
|
29
|
+
const order = orders.find((order) => order.id.endsWith(orderIdPart));
|
|
30
|
+
if (!order) {
|
|
31
|
+
throw new Error("Order not found");
|
|
32
|
+
}
|
|
31
33
|
return order;
|
|
32
34
|
});
|
|
33
35
|
exports.getOrderFromReference = getOrderFromReference;
|