propro-utils 1.5.94 → 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.
|
@@ -77,7 +77,7 @@ const authValidation = (requiredPermissions = []) => {
|
|
|
77
77
|
req.user = user.id;
|
|
78
78
|
next();
|
|
79
79
|
} catch (error) {
|
|
80
|
-
console.log(error);
|
|
80
|
+
console.log(error.message);
|
|
81
81
|
if (error.response && error.response.status) {
|
|
82
82
|
next(new Error(error.response.data.message));
|
|
83
83
|
}
|
|
@@ -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,
|
|
@@ -100,7 +156,7 @@ async function createDefaultFolders(folderSchema, accountId) {
|
|
|
100
156
|
}
|
|
101
157
|
|
|
102
158
|
const DEFAULT_THEME = {
|
|
103
|
-
canvasBackground: '#
|
|
159
|
+
canvasBackground: '#1E1D1D',
|
|
104
160
|
defaultItemWidth: 200,
|
|
105
161
|
defaultColor: '#ffffff',
|
|
106
162
|
fontSize: '16px',
|
|
@@ -117,8 +173,6 @@ const DEFAULT_THEME = {
|
|
|
117
173
|
* @throws {Error} If there's an issue with database operations or invalid input
|
|
118
174
|
*/
|
|
119
175
|
const checkIfUserExists = async accountId => {
|
|
120
|
-
console.log('Starting checkIfUserExists for accountId:', accountId);
|
|
121
|
-
|
|
122
176
|
// Input validation
|
|
123
177
|
if (!accountId || typeof accountId !== 'string') {
|
|
124
178
|
console.warn('Invalid accountId provided:', accountId);
|
|
@@ -126,8 +180,6 @@ const checkIfUserExists = async accountId => {
|
|
|
126
180
|
}
|
|
127
181
|
|
|
128
182
|
try {
|
|
129
|
-
console.log('Loading schemas...');
|
|
130
|
-
// Load all required schemas in parallel
|
|
131
183
|
const schemaResults = await Promise.all([
|
|
132
184
|
ServiceManager.getService('UserSchema'),
|
|
133
185
|
ServiceManager.getService('UserStyleSchema'),
|
|
@@ -138,14 +190,6 @@ const checkIfUserExists = async accountId => {
|
|
|
138
190
|
const [userSchema, userStyleSchema, folderSchema, themeSchema] =
|
|
139
191
|
schemaResults;
|
|
140
192
|
|
|
141
|
-
// Log the status of each schema
|
|
142
|
-
console.log('Schema loading results:', {
|
|
143
|
-
userSchema: userSchema ? 'Loaded' : 'Not available',
|
|
144
|
-
userStyleSchema: userStyleSchema ? 'Loaded' : 'Not available',
|
|
145
|
-
folderSchema: folderSchema ? 'Loaded' : 'Not available',
|
|
146
|
-
themeSchema: themeSchema ? 'Loaded' : 'Not available',
|
|
147
|
-
});
|
|
148
|
-
|
|
149
193
|
if (!userSchema) {
|
|
150
194
|
throw new Error('UserSchema service not available');
|
|
151
195
|
}
|
|
@@ -167,49 +211,23 @@ const checkIfUserExists = async accountId => {
|
|
|
167
211
|
);
|
|
168
212
|
}
|
|
169
213
|
|
|
170
|
-
console.log('Schema loading and validation complete');
|
|
171
|
-
|
|
172
214
|
// Find existing user with populated fields
|
|
173
215
|
const user = await userSchema
|
|
174
216
|
.findOne({ accountId })
|
|
175
217
|
.select('-theme -folderList -userGlobalStyles -mapList')
|
|
176
218
|
.lean();
|
|
177
219
|
|
|
178
|
-
console.log('User lookup result:', user ? 'User found' : 'User not found');
|
|
179
|
-
|
|
180
220
|
if (user) {
|
|
181
|
-
console.log('Updating existing user data...');
|
|
182
221
|
const updates = [];
|
|
183
222
|
|
|
184
223
|
// Handle userGlobalStyles
|
|
185
224
|
if (userStyleSchema) {
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
updates.push(
|
|
189
|
-
createUserGlobalStyles(userStyleSchema, accountId).then(
|
|
190
|
-
async styles => {
|
|
191
|
-
console.log('UserGlobalStyles created successfully');
|
|
192
|
-
await userSchema.updateOne(
|
|
193
|
-
{ _id: user._id },
|
|
194
|
-
{ userGlobalStyles: styles._id }
|
|
195
|
-
);
|
|
196
|
-
return styles;
|
|
197
|
-
}
|
|
198
|
-
)
|
|
199
|
-
);
|
|
200
|
-
} else if (user.userGlobalStyles.styleShortcuts?.length === 0) {
|
|
201
|
-
updates.push(
|
|
202
|
-
userStyleSchema.updateOne(
|
|
203
|
-
{ _id: user.userGlobalStyles._id },
|
|
204
|
-
{ styleShortcuts: defaultUserGlobalStyleShortcuts }
|
|
205
|
-
)
|
|
206
|
-
);
|
|
207
|
-
}
|
|
225
|
+
// Consolidate any duplicate global styles
|
|
226
|
+
await consolidateGlobalStyles(accountId);
|
|
208
227
|
}
|
|
209
228
|
|
|
210
229
|
// Handle folders
|
|
211
230
|
if (folderSchema) {
|
|
212
|
-
console.log('Checking folders...');
|
|
213
231
|
updates.push(
|
|
214
232
|
(async () => {
|
|
215
233
|
const existingFolders = await folderSchema
|
|
@@ -217,7 +235,6 @@ const checkIfUserExists = async accountId => {
|
|
|
217
235
|
.select('name')
|
|
218
236
|
.lean();
|
|
219
237
|
|
|
220
|
-
console.log('Existing folders count:', existingFolders.length);
|
|
221
238
|
const existingFolderNames = new Set(
|
|
222
239
|
existingFolders.map(f => f.name)
|
|
223
240
|
);
|
|
@@ -239,7 +256,6 @@ const checkIfUserExists = async accountId => {
|
|
|
239
256
|
|
|
240
257
|
// Handle themes
|
|
241
258
|
if (themeSchema) {
|
|
242
|
-
console.log('Checking themes...');
|
|
243
259
|
updates.push(
|
|
244
260
|
(async () => {
|
|
245
261
|
const defaultThemeExists = await themeSchema.exists({
|
|
@@ -247,7 +263,6 @@ const checkIfUserExists = async accountId => {
|
|
|
247
263
|
accountId,
|
|
248
264
|
});
|
|
249
265
|
|
|
250
|
-
console.log('Default theme exists:', defaultThemeExists);
|
|
251
266
|
if (!defaultThemeExists) {
|
|
252
267
|
const defaultTheme = await themeSchema.create({
|
|
253
268
|
...DEFAULT_THEME,
|
|
@@ -267,9 +282,7 @@ const checkIfUserExists = async accountId => {
|
|
|
267
282
|
}
|
|
268
283
|
|
|
269
284
|
// Wait for all updates to complete
|
|
270
|
-
console.log('Waiting for all updates to complete...');
|
|
271
285
|
await Promise.all(updates);
|
|
272
|
-
console.log('All updates completed successfully');
|
|
273
286
|
|
|
274
287
|
// Return fresh user data after updates, excluding specified fields
|
|
275
288
|
return userSchema
|
|
@@ -329,7 +342,6 @@ const checkIfUserExists = async accountId => {
|
|
|
329
342
|
await createDefaultFolders(folderSchema, newUser.id);
|
|
330
343
|
}
|
|
331
344
|
|
|
332
|
-
console.log('Operation completed successfully');
|
|
333
345
|
return userSchema
|
|
334
346
|
.findById(newUser._id)
|
|
335
347
|
.select('-theme -folderList -userGlobalStyles -mapList')
|
|
@@ -346,7 +358,44 @@ const checkIfUserExists = async accountId => {
|
|
|
346
358
|
}
|
|
347
359
|
};
|
|
348
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
|
+
|
|
349
397
|
module.exports = {
|
|
350
398
|
getAccountProfile,
|
|
351
399
|
checkIfUserExists,
|
|
400
|
+
updateUserGlobalStyleShortcuts,
|
|
352
401
|
};
|