propro-utils 1.5.95 → 1.5.96

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.
@@ -6,6 +6,7 @@ const ServiceManager = require('../utils/serviceManager');
6
6
  const defaultUserGlobalStyleShortcuts =
7
7
  require('./defaultUserGlobalStyleShortcuts.json').defaultGlobalStyleShortcuts;
8
8
  const defaultFolders = require('./defaultFolders.json').defaultFolders;
9
+ const User = require('../models/user');
9
10
 
10
11
  /**
11
12
  * Retrieves the account profile data from the authentication server and caches it using Redis.
@@ -61,6 +62,54 @@ const getAccountProfile = async (redisClient, userSchema, accountId) => {
61
62
  }
62
63
  };
63
64
 
65
+ /**
66
+ * Consolidates multiple UserGlobalStyles documents for an account into one
67
+ * @param {string} accountId - The account ID to consolidate styles for
68
+ * @returns {Promise<Object>} The consolidated UserGlobalStyles document
69
+ */
70
+ async function consolidateGlobalStyles(accountId) {
71
+ const userStyleSchema = await ServiceManager.getService('UserStyleSchema');
72
+
73
+ // Find all global styles for this account
74
+ const allStyles = await userStyleSchema
75
+ .find({ accountId })
76
+ .sort({ createdAt: 1 });
77
+
78
+ if (allStyles.length <= 1) {
79
+ return allStyles[0];
80
+ }
81
+
82
+ // Use the first document as the main one and merge shortcuts from others
83
+ const mainStyle = allStyles[0];
84
+ const uniqueShortcuts = new Map();
85
+
86
+ // Merge all shortcuts, keeping the latest version of each shortcut by ID
87
+ allStyles.forEach(style => {
88
+ style.styleShortcuts.forEach(shortcut => {
89
+ uniqueShortcuts.set(shortcut.id.toString(), shortcut);
90
+ });
91
+ });
92
+
93
+ // Update the main style document with consolidated shortcuts
94
+ mainStyle.styleShortcuts = Array.from(uniqueShortcuts.values());
95
+ await mainStyle.save();
96
+
97
+ // Delete other style documents
98
+ await userStyleSchema.deleteMany({
99
+ accountId,
100
+ _id: { $ne: mainStyle._id },
101
+ });
102
+
103
+ // Update user reference if needed
104
+ const userSchema = await ServiceManager.getService('UserSchema');
105
+ await userSchema.updateMany(
106
+ { accountId },
107
+ { userGlobalStyles: mainStyle._id }
108
+ );
109
+
110
+ return mainStyle;
111
+ }
112
+
64
113
  /**
65
114
  * Creates a new user global styles document with default style shortcuts.
66
115
  *
@@ -69,6 +118,13 @@ const getAccountProfile = async (redisClient, userSchema, accountId) => {
69
118
  * @returns {Promise<Object>} A promise that resolves to the created user global styles object.
70
119
  */
71
120
  async function createUserGlobalStyles(userStyleSchema, accountId) {
121
+ // First check if any existing styles need to be consolidated
122
+ const existingStyles = await userStyleSchema.find({ accountId });
123
+
124
+ if (existingStyles.length > 0) {
125
+ return await consolidateGlobalStyles(accountId);
126
+ }
127
+
72
128
  return await userStyleSchema.create({
73
129
  id: uuidv4(),
74
130
  styleShortcuts: defaultUserGlobalStyleShortcuts,
@@ -166,26 +222,8 @@ const checkIfUserExists = async accountId => {
166
222
 
167
223
  // Handle userGlobalStyles
168
224
  if (userStyleSchema) {
169
- if (!user.userGlobalStyles) {
170
- updates.push(
171
- createUserGlobalStyles(userStyleSchema, accountId).then(
172
- async styles => {
173
- await userSchema.updateOne(
174
- { _id: user._id },
175
- { userGlobalStyles: styles._id }
176
- );
177
- return styles;
178
- }
179
- )
180
- );
181
- } else if (user.userGlobalStyles.styleShortcuts?.length === 0) {
182
- updates.push(
183
- userStyleSchema.updateOne(
184
- { _id: user.userGlobalStyles._id },
185
- { styleShortcuts: defaultUserGlobalStyleShortcuts }
186
- )
187
- );
188
- }
225
+ // Consolidate any duplicate global styles
226
+ await consolidateGlobalStyles(accountId);
189
227
  }
190
228
 
191
229
  // Handle folders
@@ -320,7 +358,44 @@ const checkIfUserExists = async accountId => {
320
358
  }
321
359
  };
322
360
 
361
+ const updateUserGlobalStyleShortcuts = async (userId, shortcutId, shortcut) => {
362
+ try {
363
+ const user = await User.findOne({ id: userId }).populate(
364
+ 'userGlobalStyles'
365
+ );
366
+ if (!user) {
367
+ throw new Error('User not found');
368
+ }
369
+
370
+ // Ensure we have consolidated styles
371
+ const consolidatedStyles = await consolidateGlobalStyles(user.accountId);
372
+
373
+ // Find the index of the shortcut to update
374
+ const shortcutIndex = consolidatedStyles.styleShortcuts.findIndex(
375
+ s => s.id.toString() === shortcutId
376
+ );
377
+
378
+ if (shortcutIndex === -1) {
379
+ // If the shortcut doesn't exist, add it
380
+ consolidatedStyles.styleShortcuts.push(shortcut);
381
+ } else {
382
+ // If the shortcut exists, update it
383
+ consolidatedStyles.styleShortcuts[shortcutIndex] = {
384
+ ...consolidatedStyles.styleShortcuts[shortcutIndex],
385
+ ...shortcut,
386
+ };
387
+ }
388
+
389
+ await consolidatedStyles.save();
390
+ return consolidatedStyles;
391
+ } catch (error) {
392
+ console.error('Error updating user global style shortcuts:', error);
393
+ throw error;
394
+ }
395
+ };
396
+
323
397
  module.exports = {
324
398
  getAccountProfile,
325
399
  checkIfUserExists,
400
+ updateUserGlobalStyleShortcuts,
326
401
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "propro-utils",
3
- "version": "1.5.95",
3
+ "version": "1.5.96",
4
4
  "description": "Auth middleware for propro-auth",
5
5
  "main": "src/index.js",
6
6
  "scripts": {