@btffamily/chekaam 1.0.4 → 1.0.6

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.
@@ -18,7 +18,13 @@ export declare enum Exchanges {
18
18
  WhatsappMessage = "chekaam.prod.whatsapp.message",
19
19
  AuditCreated = "chekaam.prod.audit.created",
20
20
  InvoiceReminderRequested = "chekaam.prod.invoice.reminder.requested",
21
- InvoiceSendRequested = "chekaam.prod.invoice.send.requested"
21
+ InvoiceSendRequested = "chekaam.prod.invoice.send.requested",
22
+ QuoteRequest = "chekaam.prod.quote.request",
23
+ QuoteReply = "chekaam.prod.quote.reply",
24
+ SalePaid = "chekaam.prod.sale.paid",
25
+ StockOutcome = "chekaam.prod.stock.outcome",
26
+ InvoiceRequest = "chekaam.prod.invoice.request",
27
+ InvoiceCreated = "chekaam.prod.invoice.created"
22
28
  }
23
29
  export declare enum STGSExchanges {
24
30
  NotifyEmail = "chekaam.staging.notify.email",
@@ -40,7 +46,13 @@ export declare enum STGSExchanges {
40
46
  WhatsappMessage = "chekaam.staging.whatsapp.message",
41
47
  AuditCreated = "chekaam.staging.audit.created",
42
48
  InvoiceReminderRequested = "chekaam.staging.invoice.reminder.requested",
43
- InvoiceSendRequested = "chekaam.staging.invoice.send.requested"
49
+ InvoiceSendRequested = "chekaam.staging.invoice.send.requested",
50
+ QuoteRequest = "chekaam.staging.quote.request",
51
+ QuoteReply = "chekaam.staging.quote.reply",
52
+ SalePaid = "chekaam.staging.sale.paid",
53
+ StockOutcome = "chekaam.staging.stock.outcome",
54
+ InvoiceRequest = "chekaam.staging.invoice.request",
55
+ InvoiceCreated = "chekaam.staging.invoice.created"
44
56
  }
45
57
  export declare enum DEVExchanges {
46
58
  NotifyEmail = "chekaam.dev.notify.email",
@@ -62,5 +74,11 @@ export declare enum DEVExchanges {
62
74
  WhatsappMessage = "chekaam.dev.whatsapp.message",
63
75
  AuditCreated = "chekaam.dev.audit.created",
64
76
  InvoiceReminderRequested = "chekaam.dev.invoice.reminder.requested",
65
- InvoiceSendRequested = "chekaam.dev.invoice.send.requested"
77
+ InvoiceSendRequested = "chekaam.dev.invoice.send.requested",
78
+ QuoteRequest = "chekaam.dev.quote.request",
79
+ QuoteReply = "chekaam.dev.quote.reply",
80
+ SalePaid = "chekaam.dev.sale.paid",
81
+ StockOutcome = "chekaam.dev.stock.outcome",
82
+ InvoiceRequest = "chekaam.dev.invoice.request",
83
+ InvoiceCreated = "chekaam.dev.invoice.created"
66
84
  }
@@ -24,6 +24,12 @@ var Exchanges;
24
24
  Exchanges["AuditCreated"] = "chekaam.prod.audit.created";
25
25
  Exchanges["InvoiceReminderRequested"] = "chekaam.prod.invoice.reminder.requested";
26
26
  Exchanges["InvoiceSendRequested"] = "chekaam.prod.invoice.send.requested";
27
+ Exchanges["QuoteRequest"] = "chekaam.prod.quote.request";
28
+ Exchanges["QuoteReply"] = "chekaam.prod.quote.reply";
29
+ Exchanges["SalePaid"] = "chekaam.prod.sale.paid";
30
+ Exchanges["StockOutcome"] = "chekaam.prod.stock.outcome";
31
+ Exchanges["InvoiceRequest"] = "chekaam.prod.invoice.request";
32
+ Exchanges["InvoiceCreated"] = "chekaam.prod.invoice.created";
27
33
  })(Exchanges = exports.Exchanges || (exports.Exchanges = {}));
28
34
  var STGSExchanges;
29
35
  (function (STGSExchanges) {
@@ -47,6 +53,12 @@ var STGSExchanges;
47
53
  STGSExchanges["AuditCreated"] = "chekaam.staging.audit.created";
48
54
  STGSExchanges["InvoiceReminderRequested"] = "chekaam.staging.invoice.reminder.requested";
49
55
  STGSExchanges["InvoiceSendRequested"] = "chekaam.staging.invoice.send.requested";
56
+ STGSExchanges["QuoteRequest"] = "chekaam.staging.quote.request";
57
+ STGSExchanges["QuoteReply"] = "chekaam.staging.quote.reply";
58
+ STGSExchanges["SalePaid"] = "chekaam.staging.sale.paid";
59
+ STGSExchanges["StockOutcome"] = "chekaam.staging.stock.outcome";
60
+ STGSExchanges["InvoiceRequest"] = "chekaam.staging.invoice.request";
61
+ STGSExchanges["InvoiceCreated"] = "chekaam.staging.invoice.created";
50
62
  })(STGSExchanges = exports.STGSExchanges || (exports.STGSExchanges = {}));
51
63
  var DEVExchanges;
52
64
  (function (DEVExchanges) {
@@ -70,4 +82,10 @@ var DEVExchanges;
70
82
  DEVExchanges["AuditCreated"] = "chekaam.dev.audit.created";
71
83
  DEVExchanges["InvoiceReminderRequested"] = "chekaam.dev.invoice.reminder.requested";
72
84
  DEVExchanges["InvoiceSendRequested"] = "chekaam.dev.invoice.send.requested";
85
+ DEVExchanges["QuoteRequest"] = "chekaam.dev.quote.request";
86
+ DEVExchanges["QuoteReply"] = "chekaam.dev.quote.reply";
87
+ DEVExchanges["SalePaid"] = "chekaam.dev.sale.paid";
88
+ DEVExchanges["StockOutcome"] = "chekaam.dev.stock.outcome";
89
+ DEVExchanges["InvoiceRequest"] = "chekaam.dev.invoice.request";
90
+ DEVExchanges["InvoiceCreated"] = "chekaam.dev.invoice.created";
73
91
  })(DEVExchanges = exports.DEVExchanges || (exports.DEVExchanges = {}));
@@ -1,4 +1,4 @@
1
1
  import { IPermitOptions, IRedisConnOptions, IVerifyJWT } from '../utils/interfaces.util';
2
2
  export declare const protect: (token: string, secret: string) => IVerifyJWT;
3
- export declare const authorize: (roles: Array<string>, userRoles: Array<any>, authType: string, authDB: string, options: IRedisConnOptions) => Promise<boolean>;
3
+ export declare const authorize: (roles: Array<string>, userRoles: Array<any>, authType: string, authDB: string, _options: IRedisConnOptions) => Promise<boolean>;
4
4
  export declare const permit: (data: IPermitOptions) => Promise<boolean>;
@@ -11,16 +11,11 @@ const auth_service_1 = __importDefault(require("../services/auth.service"));
11
11
  const checkRole = (roleIds, roles) => {
12
12
  let flag = false;
13
13
  let mappedRoles = roles.map((x) => {
14
- if (x._id || x.id || x.name) {
15
- return x._id;
16
- }
17
- else {
18
- return x;
19
- }
14
+ return x?._id ?? x?.id ?? x;
20
15
  });
21
16
  for (let i = 0; i < roleIds.length; i++) {
22
17
  for (let j = 0; j < mappedRoles.length; j++) {
23
- if (roleIds[i] !== undefined && roleIds[i].toString() === mappedRoles[j].toString()) {
18
+ if (roleIds[i] != null && mappedRoles[j] != null && roleIds[i].toString() === mappedRoles[j].toString()) {
24
19
  flag = true;
25
20
  }
26
21
  }
@@ -45,30 +40,14 @@ const protect = (token, secret) => {
45
40
  return result;
46
41
  };
47
42
  exports.protect = protect;
48
- const authorize = async (roles, userRoles, authType, authDB, options) => {
49
- let result = false;
50
- try {
51
- const conn = await (0, db_mw_1.connectDB)(authType, authDB);
52
- const allRoles = await (0, role_mw_1.getRoles)({ conn, roles });
53
- // get authorized IDs //
54
- const roleIds = allRoles.map((role) => {
55
- if (role !== undefined && role !== null) {
56
- return role._id;
57
- }
58
- });
59
- // check if user roles matches the authorized roles
60
- const flag = checkRole(roleIds, userRoles);
61
- if (flag) {
62
- result = true;
63
- }
64
- else {
65
- result = false;
66
- }
43
+ const authorize = async (roles, userRoles, authType, authDB, _options) => {
44
+ if (!Array.isArray(roles) || !roles.length || !Array.isArray(userRoles) || !userRoles.length) {
45
+ return false;
67
46
  }
68
- catch (err) {
69
- result = false;
70
- }
71
- return result;
47
+ const conn = await (0, db_mw_1.connectDB)(authType, authDB);
48
+ const allRoles = await (0, role_mw_1.getRoles)({ conn, roles });
49
+ const roleIds = allRoles.map((role) => role._id);
50
+ return checkRole(roleIds, userRoles);
72
51
  };
73
52
  exports.authorize = authorize;
74
53
  const permit = async (data) => {
@@ -1,6 +1,6 @@
1
- import { Model } from 'mongoose';
1
+ import { Connection, Collection } from 'mongoose';
2
2
  export declare const connectDB: (authType: string, authDB: string) => Promise<any>;
3
3
  export declare const getModel: (data: {
4
- conn: any;
4
+ conn: Connection;
5
5
  name: string;
6
- }) => Promise<Model<any>>;
6
+ }) => Promise<Collection>;
@@ -5,39 +5,60 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.getModel = exports.connectDB = void 0;
7
7
  const mongoose_1 = __importDefault(require("mongoose"));
8
- let connection = null;
8
+ const connections = new Map();
9
9
  const options = {
10
- autoIndex: true,
11
- maxPoolSize: 50000,
12
- wtimeoutMS: 60000,
13
- connectTimeoutMS: 60000,
14
- socketTimeoutMS: 60000,
15
- serverSelectionTimeoutMS: 60000,
16
- family: 4,
10
+ autoIndex: false,
11
+ maxPoolSize: 10,
12
+ connectTimeoutMS: 10000,
13
+ serverSelectionTimeoutMS: 10000,
14
+ family: 4
17
15
  };
18
16
  const cert = `${__dirname.split('middleware')[0]}src/ca-certificate.crt`;
19
17
  // console.log(__dirname);
20
18
  const connectDB = async (authType, authDB) => {
19
+ if (!['development', 'production', 'staging', 'cloud'].includes(authType)) {
20
+ throw new Error('Authentication type is required');
21
+ }
22
+ if (!authDB) {
23
+ throw new Error('Authentication database URI is required');
24
+ }
25
+ const key = JSON.stringify([authType, authDB]);
26
+ const existing = connections.get(key);
27
+ if (existing) {
28
+ return existing;
29
+ }
30
+ const settings = { ...options };
31
+ if (authType === 'cloud') {
32
+ settings.tls = true;
33
+ settings.tlsCAFile = cert;
34
+ }
35
+ const connection = mongoose_1.default.createConnection(authDB, settings);
36
+ const pending = connection.asPromise();
37
+ connections.set(key, pending);
38
+ connection.once('close', () => {
39
+ if (connections.get(key) === pending) {
40
+ connections.delete(key);
41
+ }
42
+ });
21
43
  try {
22
- if (authType === 'development' || authType === 'production' || authType === 'staging') {
23
- connection = mongoose_1.default.createConnection(authDB, options);
44
+ return await pending;
45
+ }
46
+ catch (error) {
47
+ if (connections.get(key) === pending) {
48
+ connections.delete(key);
24
49
  }
25
- else if (authType === 'cloud') {
26
- const cloudDBString = authDB + `&tls=true&tlsCAFile=${cert}`;
27
- connection = mongoose_1.default.createConnection(cloudDBString, options);
50
+ try {
51
+ await connection.destroy();
28
52
  }
29
- else {
30
- throw new Error(`Authentication type is required`);
53
+ catch {
54
+ // Preserve the original connection failure if cleanup also fails.
31
55
  }
32
- return connection;
33
- }
34
- catch (err) {
35
- throw new Error(`${err}`);
56
+ throw error;
36
57
  }
37
58
  };
38
59
  exports.connectDB = connectDB;
39
60
  const getModel = async (data) => {
40
61
  const { conn, name } = data;
41
- return await conn.collection(name.toLowerCase());
62
+ return conn.collection(name.toLowerCase());
42
63
  };
43
64
  exports.getModel = getModel;
@@ -14,10 +14,10 @@ class redisWrapper {
14
14
  // console.log(colors.yellow.inverse('Connected to REDIS'));
15
15
  }
16
16
  async keepData(data, exp) {
17
- const parsed = JSON.stringify(data.value);
18
- this.client.set(data.key, parsed, {
19
- EX: exp,
20
- NX: false
17
+ const { key, value } = data;
18
+ const parsed = JSON.stringify(value);
19
+ await this.client.set(key, parsed, {
20
+ EX: exp
21
21
  });
22
22
  }
23
23
  async fetchData(key) {
@@ -1,4 +1,5 @@
1
+ import { Connection } from 'mongoose';
1
2
  export declare const getRoles: (data: {
2
- conn: any;
3
+ conn: Connection;
3
4
  roles: Array<string>;
4
5
  }) => Promise<Array<any>>;
@@ -3,17 +3,15 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getRoles = void 0;
4
4
  const db_mw_1 = require("./db.mw");
5
5
  const getRoles = async (data) => {
6
- let result = [];
7
6
  const { conn, roles } = data;
7
+ if (!roles.length) {
8
+ return [];
9
+ }
8
10
  const Role = await (0, db_mw_1.getModel)({ conn, name: 'roles' });
9
- if (Role) {
10
- for (let i = 0; i < roles.length; i++) {
11
- let role = await Role.findOne({ name: roles[i] });
12
- if (role) {
13
- result.push(role);
14
- }
11
+ return Role.find({
12
+ name: {
13
+ $in: roles
15
14
  }
16
- }
17
- return result;
15
+ }).toArray();
18
16
  };
19
17
  exports.getRoles = getRoles;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@btffamily/chekaam",
3
- "version": "1.0.4",
3
+ "version": "1.0.6",
4
4
  "description": "common node.js codebase for chekaam platform",
5
5
  "main": "./build/index.js",
6
6
  "types": "./build/index.d.ts",
@@ -40,7 +40,7 @@
40
40
  "express": "^4.17.1",
41
41
  "jsonwebtoken": "^8.5.1",
42
42
  "moment": "^2.30.1",
43
- "mongoose": "^8.14.2",
43
+ "mongoose": "^8.24.6",
44
44
  "node-nats-streaming": "^0.3.2",
45
45
  "redis": "^4.0.6",
46
46
  "slugify": "^1.6.6",