@riocrypto/common-server 1.0.2666 → 1.0.2668

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.
@@ -2,7 +2,19 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.generateReference = void 0;
4
4
  const generateReference = (order) => {
5
- const referenceNumber = order.id.slice(-6);
6
- return referenceNumber;
5
+ // MongoDB ObjectId structure (24 hex chars):
6
+ // - chars 0-7: timestamp (4 bytes)
7
+ // - chars 8-17: random value (5 bytes)
8
+ // - chars 18-23: incrementing counter (3 bytes)
9
+ //
10
+ // Using last 6 chars alone caused collisions (~50/month) because the counter
11
+ // portion can overlap across different MongoDB instances/pods.
12
+ //
13
+ // Solution: 2 chars from random portion (8-9) + last 6 chars of order ID.
14
+ // Last 6 chars are kept for easy traceability by finance team.
15
+ // This gives 16^8 = 4.3 billion combinations vs 16^6 = 16.7 million (256x better).
16
+ const randomPrefix = order.id.slice(8, 10);
17
+ const last6 = order.id.slice(-6);
18
+ return (randomPrefix + last6).toUpperCase();
7
19
  };
8
20
  exports.generateReference = generateReference;
@@ -28,10 +28,13 @@ const getBulkPaymentFromReference = (reference, mongoose) => __awaiter(void 0, v
28
28
  ],
29
29
  },
30
30
  });
31
+ // Reference format: 2 chars from random portion (8-9) + last 6 chars of ID
31
32
  const bulkPayment = bulkPayments.find((bulkPayment) => {
32
33
  const bulkPaymentId = bulkPayment.id.toString().toLowerCase();
33
- const last6Chars = bulkPaymentId.slice(-6);
34
- return referenceText.includes(last6Chars);
34
+ const randomPrefix = bulkPaymentId.slice(8, 10);
35
+ const last6 = bulkPaymentId.slice(-6);
36
+ const reference8Chars = randomPrefix + last6;
37
+ return referenceText.includes(reference8Chars);
35
38
  });
36
39
  if (!bulkPayment) {
37
40
  throw new Error("Bulk payment not found");
@@ -47,10 +47,13 @@ const getOrderFromReference = (reference, mongoose) => __awaiter(void 0, void 0,
47
47
  break;
48
48
  }
49
49
  // Check each order ID against the reference
50
+ // Reference format: 2 chars from random portion (8-9) + last 6 chars of order ID
50
51
  for (const order of orders) {
51
52
  const orderId = order._id.toString().toLowerCase();
52
- const last6Chars = orderId.slice(-6);
53
- if (referenceText.includes(last6Chars)) {
53
+ const randomPrefix = orderId.slice(8, 10);
54
+ const last6 = orderId.slice(-6);
55
+ const reference8Chars = randomPrefix + last6;
56
+ if (referenceText.includes(reference8Chars)) {
54
57
  // Found a match, now fetch the full order document
55
58
  foundOrder = yield Order.findById(order._id);
56
59
  break;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@riocrypto/common-server",
3
- "version": "1.0.2666",
3
+ "version": "1.0.2668",
4
4
  "description": "",
5
5
  "main": "./build/index.js",
6
6
  "types": "./build/index.d.ts",