@riocrypto/common-server 1.0.1753 → 1.0.1755

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.
@@ -0,0 +1,2 @@
1
+ import { OrderDoc } from "../models/order";
2
+ export declare const generateReference: (order: OrderDoc) => string;
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.generateReference = void 0;
4
+ const generateReference = (order) => {
5
+ // Validate that userId and orderId are valid MongoDB ObjectIDs
6
+ const mongoIdRegex = /^[a-f\d]{24}$/i;
7
+ if (!mongoIdRegex.test(order.userId) || !mongoIdRegex.test(order.id)) {
8
+ throw new Error("Invalid MongoDB ObjectID format");
9
+ }
10
+ const userIdLast2 = order.userId.slice(-2);
11
+ const orderIdLast4 = order.id.slice(-4);
12
+ const referenceNumber = userIdLast2 + orderIdLast4;
13
+ return referenceNumber;
14
+ };
15
+ exports.generateReference = generateReference;
@@ -0,0 +1,3 @@
1
+ import { Mongoose } from "mongoose";
2
+ import { OrderDoc } from "../models/order";
3
+ export declare const getOrderFromReference: (reference: string, mongoose: Mongoose) => Promise<OrderDoc | null>;
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.getOrderFromReference = void 0;
13
+ const user_1 = require("../models/user");
14
+ const order_1 = require("../models/order");
15
+ const getOrderFromReference = (reference, mongoose) => __awaiter(void 0, void 0, void 0, function* () {
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
20
+ const User = (0, user_1.buildUser)(mongoose);
21
+ const user = yield User.findOne({ _id: { $regex: userIdPart + "$" } }).exec();
22
+ if (!user) {
23
+ throw new Error("User not found");
24
+ }
25
+ const Order = (0, order_1.buildOrder)(mongoose);
26
+ // Find the order whose ID ends with the extracted orderIdPart and belongs to the user
27
+ const order = yield Order.findOne({
28
+ _id: { $regex: orderIdPart + "$" },
29
+ userId: user._id,
30
+ }).exec();
31
+ return order;
32
+ });
33
+ exports.getOrderFromReference = getOrderFromReference;
package/build/index.d.ts CHANGED
@@ -90,3 +90,5 @@ export * from "./helpers/sanitize-user-doc";
90
90
  export * from "./helpers/get-currency-api-exchange-rates";
91
91
  export * from "./helpers/get-tax-rate";
92
92
  export * from "./helpers/get-asset-price";
93
+ export * from "./helpers/generate-payment-reference";
94
+ export * from "./helpers/get-order-from-reference";
package/build/index.js CHANGED
@@ -106,3 +106,5 @@ __exportStar(require("./helpers/sanitize-user-doc"), exports);
106
106
  __exportStar(require("./helpers/get-currency-api-exchange-rates"), exports);
107
107
  __exportStar(require("./helpers/get-tax-rate"), exports);
108
108
  __exportStar(require("./helpers/get-asset-price"), exports);
109
+ __exportStar(require("./helpers/generate-payment-reference"), exports);
110
+ __exportStar(require("./helpers/get-order-from-reference"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@riocrypto/common-server",
3
- "version": "1.0.1753",
3
+ "version": "1.0.1755",
4
4
  "description": "",
5
5
  "main": "./build/index.js",
6
6
  "types": "./build/index.d.ts",