propro-utils 1.3.28 → 1.3.30

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,8 +1,20 @@
1
1
  require("dotenv").config();
2
2
  const axios = require("axios");
3
3
  const {getOrSetCache} = require("../utils/redis");
4
-
5
- const getAccountProfile = async (redisClient, accountId) => {
4
+ const { v4: uuidv4 } = require('uuid');
5
+ const { Schema } = require('mongoose');
6
+ const { Client } = require('redis');
7
+ /**
8
+ * Retrieves the account profile data from the authentication server and caches it using Redis.
9
+ * If the profile data is not found in the cache, it fetches it from the authentication server and stores it in the cache.
10
+ *
11
+ * @param {Client} redisClient - Redis client instance
12
+ * @param {Schema} userSchema - User schema/model object
13
+ * @param {string} accountId - ID of the account
14
+ * @returns {Object} - Account profile data
15
+ * @throws {Error} - If there is an error retrieving the account profile data or validating the token
16
+ */
17
+ const getAccountProfile = async (redisClient, userSchema, accountId) => {
6
18
  try {
7
19
  const accessToken = req.cookies['x-access-token'] || req.headers.authorization?.split(" ")[1];
8
20
 
@@ -32,10 +44,32 @@ const getAccountProfile = async (redisClient, accountId) => {
32
44
 
33
45
  } catch (error) {
34
46
  if (error.response && error.response.status) {
35
- next(new Error(error.response.data.message));
47
+ throw new Error(error.response.data.message);
36
48
  }
37
49
  throw new Error('Error validating token');
38
50
  }
39
51
  };
40
52
 
41
- module.exports = getAccountProfile;
53
+
54
+ /**
55
+ * Checks if a user exists based on the given account ID.
56
+ * If the user does not exist, creates a new user with the given account ID.
57
+ *
58
+ * @param {Schema} userSchema - The user schema to perform the operations on.
59
+ * @param {string} accountId - The account ID of the user to check/create.
60
+ * @returns {Promise<void>} - A promise that resolves once the check/create operation is done.
61
+ */
62
+ const checkIfUserExists = async (userSchema, accountId) => {
63
+ const user = await userSchema.findOne({ accountId
64
+ });
65
+ if (!user) {
66
+ await userSchema.create({
67
+ accountId: accountId
68
+ });
69
+ }
70
+ };
71
+
72
+ module.exports = {
73
+ getAccountProfile,
74
+ checkIfUserExists
75
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "propro-utils",
3
- "version": "1.3.28",
3
+ "version": "1.3.30",
4
4
  "description": "Auth middleware for propro-auth",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -61,10 +61,12 @@
61
61
  "axios": "^1.6.1",
62
62
  "dotenv": "^16.4.1",
63
63
  "express-rate-limit": "^7.1.4",
64
+ "mongoose": "^8.1.1",
64
65
  "nodemailer": "^6.9.7",
65
66
  "nodemailer-mailgun-transport": "^2.1.5",
66
67
  "querystring": "^0.2.1",
67
68
  "react-email": "^1.9.5",
68
- "redis": "^4.6.12"
69
+ "redis": "^4.6.12",
70
+ "uuid": "^9.0.1"
69
71
  }
70
72
  }
@@ -49,7 +49,7 @@ function proproAuthMiddleware(options = {}) {
49
49
 
50
50
  console.log("code", code);
51
51
 
52
- const {tokens, redirectUrl} = await exchangeToken(
52
+ const {tokens, account, redirectUrl} = await exchangeToken(
53
53
  authUrl,
54
54
  code,
55
55
  clientId,