propro-utils 1.5.70 → 1.5.72
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.
- package/middlewares/account_info.js +248 -49
- package/package.json +1 -1
|
@@ -108,73 +108,272 @@ async function createDefaultFolders(folderSchema, accountId) {
|
|
|
108
108
|
* @returns {Promise<Object>} A promise that resolves to the user object.
|
|
109
109
|
* @throws {Error} If there's an issue with database operations.
|
|
110
110
|
*/
|
|
111
|
+
const debugLog = (area, message, data = null) => {
|
|
112
|
+
const timestamp = new Date().toISOString();
|
|
113
|
+
console.log(
|
|
114
|
+
`[${timestamp}] [${area}] ${message}`,
|
|
115
|
+
data ? JSON.stringify(data, null, 2) : ''
|
|
116
|
+
);
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
const DEFAULT_THEME = {
|
|
120
|
+
canvasBackground: '#000000',
|
|
121
|
+
defaultItemWidth: 200,
|
|
122
|
+
defaultColor: '#ffffff',
|
|
123
|
+
fontSize: '16px',
|
|
124
|
+
name: 'Default Theme',
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
const timeoutPromise = (promise, timeout, name) => {
|
|
128
|
+
return Promise.race([
|
|
129
|
+
promise,
|
|
130
|
+
new Promise((_, reject) =>
|
|
131
|
+
setTimeout(
|
|
132
|
+
() =>
|
|
133
|
+
reject(new Error(`${name} operation timed out after ${timeout}ms`)),
|
|
134
|
+
timeout
|
|
135
|
+
)
|
|
136
|
+
),
|
|
137
|
+
]);
|
|
138
|
+
};
|
|
139
|
+
|
|
111
140
|
const checkIfUserExists = async accountId => {
|
|
141
|
+
const startTime = Date.now();
|
|
112
142
|
try {
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
143
|
+
if (!accountId) {
|
|
144
|
+
throw new Error('AccountId is required');
|
|
145
|
+
}
|
|
146
|
+
debugLog('init', `Starting user check for accountId: ${accountId}`);
|
|
117
147
|
|
|
118
|
-
|
|
119
|
-
let
|
|
120
|
-
|
|
121
|
-
|
|
148
|
+
// Initialize services with timeout and individual error handling
|
|
149
|
+
let services;
|
|
150
|
+
try {
|
|
151
|
+
debugLog('services', 'Initializing services...');
|
|
152
|
+
services = await timeoutPromise(
|
|
153
|
+
Promise.all([
|
|
154
|
+
ServiceManager.getService('UserSchema').catch(e => {
|
|
155
|
+
throw new Error(`Failed to get UserSchema: ${e.message}`);
|
|
156
|
+
}),
|
|
157
|
+
ServiceManager.getService('UserStyleSchema').catch(e => {
|
|
158
|
+
throw new Error(`Failed to get UserStyleSchema: ${e.message}`);
|
|
159
|
+
}),
|
|
160
|
+
ServiceManager.getService('FolderSchema').catch(e => {
|
|
161
|
+
throw new Error(`Failed to get FolderSchema: ${e.message}`);
|
|
162
|
+
}),
|
|
163
|
+
ServiceManager.getService('ThemeSchema').catch(e => {
|
|
164
|
+
throw new Error(`Failed to get Theme: ${e.message}`);
|
|
165
|
+
}),
|
|
166
|
+
]),
|
|
167
|
+
5000,
|
|
168
|
+
'Service initialization'
|
|
169
|
+
);
|
|
122
170
|
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
171
|
+
debugLog('services', 'Services initialized successfully', {
|
|
172
|
+
serviceCount: services.length,
|
|
173
|
+
initTime: Date.now() - startTime,
|
|
174
|
+
});
|
|
175
|
+
} catch (error) {
|
|
176
|
+
debugLog('error', 'Service initialization failed', {
|
|
177
|
+
error: error.message,
|
|
178
|
+
});
|
|
179
|
+
throw error;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
const [userSchema, userStyleSchema, folderSchema, Theme] = services;
|
|
183
|
+
|
|
184
|
+
// Find user and theme with timeout
|
|
185
|
+
let user, existingTheme;
|
|
186
|
+
try {
|
|
187
|
+
debugLog('query', 'Starting user and theme queries');
|
|
188
|
+
[user, existingTheme] = await timeoutPromise(
|
|
189
|
+
Promise.all([
|
|
190
|
+
userSchema
|
|
191
|
+
.findOne({ accountId })
|
|
192
|
+
.populate('userGlobalStyles')
|
|
193
|
+
.exec()
|
|
194
|
+
.catch(e => {
|
|
195
|
+
throw new Error(`Failed to find user: ${e.message}`);
|
|
196
|
+
}),
|
|
197
|
+
Theme.findOne({
|
|
198
|
+
name: 'Default Theme',
|
|
199
|
+
accountId,
|
|
200
|
+
})
|
|
201
|
+
.exec()
|
|
202
|
+
.catch(e => {
|
|
203
|
+
throw new Error(`Failed to find theme: ${e.message}`);
|
|
204
|
+
}),
|
|
205
|
+
]),
|
|
206
|
+
5000,
|
|
207
|
+
'Initial queries'
|
|
208
|
+
);
|
|
209
|
+
|
|
210
|
+
debugLog('query', 'Queries completed', {
|
|
211
|
+
userFound: !!user,
|
|
212
|
+
themeFound: !!existingTheme,
|
|
213
|
+
queryTime: Date.now() - startTime,
|
|
214
|
+
});
|
|
215
|
+
} catch (error) {
|
|
216
|
+
debugLog('error', 'Query operations failed', { error: error.message });
|
|
217
|
+
throw error;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
// Create or use existing theme
|
|
221
|
+
let defaultTheme;
|
|
222
|
+
try {
|
|
223
|
+
if (!existingTheme) {
|
|
224
|
+
debugLog('theme', 'Creating new theme');
|
|
225
|
+
defaultTheme = await timeoutPromise(
|
|
226
|
+
Theme.create({
|
|
227
|
+
...DEFAULT_THEME,
|
|
228
|
+
id: uuidv4(),
|
|
229
|
+
accountId,
|
|
230
|
+
}),
|
|
231
|
+
5000,
|
|
232
|
+
'Theme creation'
|
|
128
233
|
);
|
|
129
|
-
|
|
130
|
-
} else
|
|
131
|
-
|
|
132
|
-
|
|
234
|
+
debugLog('theme', 'New theme created', { themeId: defaultTheme._id });
|
|
235
|
+
} else {
|
|
236
|
+
defaultTheme = existingTheme;
|
|
237
|
+
debugLog('theme', 'Using existing theme', {
|
|
238
|
+
themeId: defaultTheme._id,
|
|
239
|
+
});
|
|
133
240
|
}
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
241
|
+
} catch (error) {
|
|
242
|
+
debugLog('error', 'Theme operation failed', { error: error.message });
|
|
243
|
+
throw error;
|
|
244
|
+
}
|
|
138
245
|
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
246
|
+
if (user) {
|
|
247
|
+
debugLog('user', 'Processing existing user');
|
|
248
|
+
const updates = [];
|
|
142
249
|
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
250
|
+
try {
|
|
251
|
+
// Theme update if needed
|
|
252
|
+
if (!user.theme) {
|
|
253
|
+
debugLog('update', 'Adding theme to user');
|
|
254
|
+
user.theme = defaultTheme._id;
|
|
255
|
+
updates.push('theme');
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
// Global styles update if needed
|
|
259
|
+
if (!user.userGlobalStyles) {
|
|
260
|
+
debugLog('update', 'Creating user global styles');
|
|
261
|
+
user.userGlobalStyles = await timeoutPromise(
|
|
262
|
+
createUserGlobalStyles(userStyleSchema, accountId),
|
|
263
|
+
5000,
|
|
264
|
+
'Global styles creation'
|
|
265
|
+
);
|
|
266
|
+
updates.push('userGlobalStyles');
|
|
267
|
+
} else if (user.userGlobalStyles.styleShortcuts.length === 0) {
|
|
268
|
+
debugLog('update', 'Updating empty style shortcuts');
|
|
269
|
+
user.userGlobalStyles.styleShortcuts =
|
|
270
|
+
defaultUserGlobalStyleShortcuts;
|
|
271
|
+
await timeoutPromise(
|
|
272
|
+
user.userGlobalStyles.save(),
|
|
273
|
+
5000,
|
|
274
|
+
'Style shortcuts update'
|
|
275
|
+
);
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
// Folder checks
|
|
279
|
+
debugLog('folders', 'Checking user folders');
|
|
280
|
+
const userFolders = await timeoutPromise(
|
|
281
|
+
folderSchema.find({ user_id: user.id }, { name: 1 }),
|
|
282
|
+
5000,
|
|
283
|
+
'Folder query'
|
|
284
|
+
);
|
|
285
|
+
|
|
286
|
+
const userFolderNames = new Set(userFolders.map(f => f.name));
|
|
287
|
+
const foldersToCreate = defaultFolders.filter(
|
|
288
|
+
f => !userFolderNames.has(f.name)
|
|
151
289
|
);
|
|
290
|
+
|
|
291
|
+
if (foldersToCreate.length > 0) {
|
|
292
|
+
debugLog('folders', 'Creating missing folders', {
|
|
293
|
+
count: foldersToCreate.length,
|
|
294
|
+
});
|
|
295
|
+
await timeoutPromise(
|
|
296
|
+
Promise.all(
|
|
297
|
+
foldersToCreate.map(folder =>
|
|
298
|
+
folderSchema.create({
|
|
299
|
+
...folder,
|
|
300
|
+
user_id: user.id,
|
|
301
|
+
})
|
|
302
|
+
)
|
|
303
|
+
),
|
|
304
|
+
5000,
|
|
305
|
+
'Folder creation'
|
|
306
|
+
);
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
// Save user if there were updates
|
|
310
|
+
if (updates.length > 0) {
|
|
311
|
+
debugLog('update', 'Saving user updates', { updates });
|
|
312
|
+
await timeoutPromise(user.save(), 5000, 'User save');
|
|
313
|
+
}
|
|
314
|
+
} catch (error) {
|
|
315
|
+
debugLog('error', 'User update operations failed', {
|
|
316
|
+
error: error.message,
|
|
317
|
+
});
|
|
318
|
+
throw error;
|
|
152
319
|
}
|
|
153
320
|
|
|
321
|
+
debugLog('complete', 'Existing user processed', {
|
|
322
|
+
userId: user.id,
|
|
323
|
+
totalTime: Date.now() - startTime,
|
|
324
|
+
});
|
|
154
325
|
return user;
|
|
155
326
|
}
|
|
156
327
|
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
328
|
+
// Create new user
|
|
329
|
+
try {
|
|
330
|
+
debugLog('create', 'Creating new user');
|
|
331
|
+
const userGlobalStyles = await timeoutPromise(
|
|
332
|
+
createUserGlobalStyles(userStyleSchema, accountId),
|
|
333
|
+
5000,
|
|
334
|
+
'Global styles creation'
|
|
335
|
+
);
|
|
161
336
|
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
337
|
+
const newUserId = uuidv4();
|
|
338
|
+
|
|
339
|
+
const [newUser, newFolders] = await timeoutPromise(
|
|
340
|
+
Promise.all([
|
|
341
|
+
userSchema.create({
|
|
342
|
+
accountId,
|
|
343
|
+
id: newUserId,
|
|
344
|
+
verified: false,
|
|
345
|
+
userGlobalStyles: userGlobalStyles._id,
|
|
346
|
+
theme: defaultTheme._id,
|
|
347
|
+
}),
|
|
348
|
+
createDefaultFolders(folderSchema, newUserId),
|
|
349
|
+
]),
|
|
350
|
+
5000,
|
|
351
|
+
'User and folders creation'
|
|
352
|
+
);
|
|
168
353
|
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
user.folders = folders.map(folder => folder._id);
|
|
172
|
-
await user.save();
|
|
354
|
+
newUser.folders = newFolders.map(folder => folder._id);
|
|
355
|
+
await timeoutPromise(newUser.save(), 5000, 'New user save');
|
|
173
356
|
|
|
174
|
-
|
|
357
|
+
debugLog('complete', 'New user created', {
|
|
358
|
+
userId: newUser.id,
|
|
359
|
+
totalTime: Date.now() - startTime,
|
|
360
|
+
});
|
|
361
|
+
return newUser;
|
|
362
|
+
} catch (error) {
|
|
363
|
+
debugLog('error', 'User creation failed', { error: error.message });
|
|
364
|
+
throw error;
|
|
365
|
+
}
|
|
175
366
|
} catch (error) {
|
|
176
|
-
|
|
177
|
-
|
|
367
|
+
const totalTime = Date.now() - startTime;
|
|
368
|
+
debugLog('error', 'Operation failed', {
|
|
369
|
+
error: error.message,
|
|
370
|
+
stack: error.stack,
|
|
371
|
+
totalTime,
|
|
372
|
+
});
|
|
373
|
+
|
|
374
|
+
throw new Error(`Failed to get or create user: ${error.message}`, {
|
|
375
|
+
cause: error,
|
|
376
|
+
});
|
|
178
377
|
}
|
|
179
378
|
};
|
|
180
379
|
|