propro-utils 1.5.95 → 1.5.97

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