@hastehaul/common 3.1.0 → 3.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,27 +1,4 @@
1
1
  "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
25
2
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
3
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
4
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -31,9 +8,12 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
31
8
  step((generator = generator.apply(thisArg, _arguments || [])).next());
32
9
  });
33
10
  };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
34
14
  Object.defineProperty(exports, "__esModule", { value: true });
35
15
  exports.currentUser = void 0;
36
- const jsonwebtoken_1 = __importStar(require("jsonwebtoken"));
16
+ const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
37
17
  /**
38
18
  * Middleware function to extract the current user from the request's Authorization header.
39
19
  *
@@ -51,23 +31,16 @@ const currentUser = (req, res, next) => __awaiter(void 0, void 0, void 0, functi
51
31
  return next();
52
32
  }
53
33
  try {
34
+ const jwtKey = (_a = process.env.JWT_KEY) !== null && _a !== void 0 ? _a : "";
35
+ console.log("=== jwtKey ===", jwtKey);
54
36
  const token = bearerToken.split(' ')[1];
55
37
  if (token) {
56
- // ! over here we should get the JWT_KEY from the db or use the => kubectl create secret generic jwt-secret --from-literal=jwt=secret
57
- const jwtKey = (_a = process.env.JWT_KEY) !== null && _a !== void 0 ? _a : "";
58
38
  const result = jsonwebtoken_1.default.verify(token, jwtKey.split('-').join(''));
59
39
  req.currentUser = result;
60
40
  }
61
41
  }
62
42
  catch (err) {
63
- if (err instanceof jsonwebtoken_1.TokenExpiredError) {
64
- // If the token is expired, you may consider refreshing the token or taking appropriate actions.
65
- // Uncomment the following line if you want to handle token expiration using the AuthController.
66
- // await AuthController.refreshToken(req, res);
67
- }
68
- else {
69
- console.error('Error verifying token:', err);
70
- }
43
+ console.error('Error verifying token:', err);
71
44
  }
72
45
  next();
73
46
  });
@@ -1,3 +1,4 @@
1
+ import { Model, Document } from 'mongoose';
1
2
  type Setting = {
2
3
  key: string;
3
4
  value: any;
@@ -16,4 +17,5 @@ export declare function isPointInGeoJSON(latLng: number[], fileName: string): Pr
16
17
  properties: null;
17
18
  }>;
18
19
  export declare function clearGeoJsonCache(): void;
20
+ export declare function generateCode<T extends Document>(model: Model<T>, filterProperty: keyof T): Promise<string>;
19
21
  export {};
@@ -20,6 +20,7 @@ exports.getValueFromSettings = getValueFromSettings;
20
20
  exports.loadAsset = loadAsset;
21
21
  exports.isPointInGeoJSON = isPointInGeoJSON;
22
22
  exports.clearGeoJsonCache = clearGeoJsonCache;
23
+ exports.generateCode = generateCode;
23
24
  const boolean_point_in_polygon_1 = __importDefault(require("@turf/boolean-point-in-polygon"));
24
25
  const helpers_1 = require("@turf/helpers");
25
26
  const moment_1 = __importDefault(require("moment"));
@@ -108,3 +109,24 @@ function isPointInGeoJSON(latLng, fileName) {
108
109
  function clearGeoJsonCache() {
109
110
  cachedGeoJsonData = null;
110
111
  }
112
+ function generateCode(model, filterProperty) {
113
+ return __awaiter(this, void 0, void 0, function* () {
114
+ const batchSize = 10;
115
+ const prefixes = ['R'];
116
+ let totalDocuments = yield model.countDocuments();
117
+ let batch = Array.from({ length: batchSize }, (_, i) => {
118
+ const prefix = prefixes[Math.floor(Math.random() * prefixes.length)];
119
+ const code = (totalDocuments + i + 1).toString().padStart(4, '0'); // Adjust to 5 digits to fit prefix
120
+ return prefix + code;
121
+ });
122
+ let query = { [filterProperty]: { $in: batch } };
123
+ let foundCode = yield model.find({ query });
124
+ let foundSet = new Set(foundCode.map((document) => document[filterProperty]));
125
+ for (let code of batch) {
126
+ if (!foundSet.has(code)) {
127
+ return code;
128
+ }
129
+ }
130
+ return generateCode(model, filterProperty);
131
+ });
132
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hastehaul/common",
3
- "version": "3.1.0",
3
+ "version": "3.3.0",
4
4
  "description": "",
5
5
  "main": "./build/index.js",
6
6
  "types": "./build/index.d.ts",
@@ -36,6 +36,7 @@
36
36
  "ioredis": "^5.3.2",
37
37
  "jsonwebtoken": "^9.0.1",
38
38
  "moment": "^2.29.4",
39
+ "mongoose": "^8.5.2",
39
40
  "nats": "^2.17.0",
40
41
  "socket.io": "^4.7.1"
41
42
  }