propro-utils 1.5.69 → 1.5.70
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 +49 -248
- package/package.json +1 -1
|
@@ -99,35 +99,6 @@ async function createDefaultFolders(folderSchema, accountId) {
|
|
|
99
99
|
}
|
|
100
100
|
}
|
|
101
101
|
|
|
102
|
-
const debugLog = (area, message, data = null) => {
|
|
103
|
-
const timestamp = new Date().toISOString();
|
|
104
|
-
console.log(
|
|
105
|
-
`[${timestamp}] [${area}] ${message}`,
|
|
106
|
-
data ? JSON.stringify(data, null, 2) : ''
|
|
107
|
-
);
|
|
108
|
-
};
|
|
109
|
-
|
|
110
|
-
const DEFAULT_THEME = {
|
|
111
|
-
canvasBackground: '#000000',
|
|
112
|
-
defaultItemWidth: 200,
|
|
113
|
-
defaultColor: '#ffffff',
|
|
114
|
-
fontSize: '16px',
|
|
115
|
-
name: 'Default Theme',
|
|
116
|
-
};
|
|
117
|
-
|
|
118
|
-
const timeoutPromise = (promise, timeout, name) => {
|
|
119
|
-
return Promise.race([
|
|
120
|
-
promise,
|
|
121
|
-
new Promise((_, reject) =>
|
|
122
|
-
setTimeout(
|
|
123
|
-
() =>
|
|
124
|
-
reject(new Error(`${name} operation timed out after ${timeout}ms`)),
|
|
125
|
-
timeout
|
|
126
|
-
)
|
|
127
|
-
),
|
|
128
|
-
]);
|
|
129
|
-
};
|
|
130
|
-
|
|
131
102
|
/**
|
|
132
103
|
* Checks if a user exists based on the given account ID.
|
|
133
104
|
* If the user does not exist, creates a new user with the given account ID.
|
|
@@ -138,242 +109,72 @@ const timeoutPromise = (promise, timeout, name) => {
|
|
|
138
109
|
* @throws {Error} If there's an issue with database operations.
|
|
139
110
|
*/
|
|
140
111
|
const checkIfUserExists = async accountId => {
|
|
141
|
-
const startTime = Date.now();
|
|
142
112
|
try {
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
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('Theme').catch(e => {
|
|
164
|
-
throw new Error(`Failed to get Theme: ${e.message}`);
|
|
165
|
-
}),
|
|
166
|
-
]),
|
|
167
|
-
5000,
|
|
168
|
-
'Service initialization'
|
|
169
|
-
);
|
|
170
|
-
|
|
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
|
-
}
|
|
113
|
+
const userSchema = await ServiceManager.getService('UserSchema');
|
|
114
|
+
const userStyleSchema = await ServiceManager.getService('UserStyleSchema');
|
|
115
|
+
const folderSchema = await ServiceManager.getService('FolderSchema');
|
|
116
|
+
const themeSchema = await ServiceManager.getService('ThemeSchema');
|
|
181
117
|
|
|
182
|
-
|
|
118
|
+
console.log('themeschema', themeSchema);
|
|
119
|
+
let user = await userSchema
|
|
120
|
+
.findOne({ accountId })
|
|
121
|
+
.populate('userGlobalStyles');
|
|
183
122
|
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
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'
|
|
123
|
+
if (user) {
|
|
124
|
+
if (!user.userGlobalStyles) {
|
|
125
|
+
user.userGlobalStyles = await createUserGlobalStyles(
|
|
126
|
+
userStyleSchema,
|
|
127
|
+
accountId
|
|
233
128
|
);
|
|
234
|
-
|
|
235
|
-
} else {
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
themeId: defaultTheme._id,
|
|
239
|
-
});
|
|
129
|
+
await user.save();
|
|
130
|
+
} else if (user.userGlobalStyles.styleShortcuts.length === 0) {
|
|
131
|
+
user.userGlobalStyles.styleShortcuts = defaultUserGlobalStyleShortcuts;
|
|
132
|
+
await user.userGlobalStyles.save();
|
|
240
133
|
}
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
if (user) {
|
|
247
|
-
debugLog('user', 'Processing existing user');
|
|
248
|
-
const updates = [];
|
|
134
|
+
// Check if user has any folders
|
|
135
|
+
const userFolders = await folderSchema.find({ user_id: user.id });
|
|
136
|
+
const defaultFolderNames = defaultFolders.map(folder => folder.name);
|
|
137
|
+
const userFolderNames = userFolders.map(folder => folder.name);
|
|
249
138
|
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
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
|
-
);
|
|
139
|
+
const foldersToCreate = defaultFolders.filter(
|
|
140
|
+
folder => !userFolderNames.includes(folder.name)
|
|
141
|
+
);
|
|
285
142
|
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
143
|
+
if (foldersToCreate.length > 0) {
|
|
144
|
+
await Promise.all(
|
|
145
|
+
foldersToCreate.map(folder =>
|
|
146
|
+
folderSchema.create({
|
|
147
|
+
...folder,
|
|
148
|
+
user_id: user.id,
|
|
149
|
+
})
|
|
150
|
+
)
|
|
289
151
|
);
|
|
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;
|
|
319
152
|
}
|
|
320
153
|
|
|
321
|
-
debugLog('complete', 'Existing user processed', {
|
|
322
|
-
userId: user.id,
|
|
323
|
-
totalTime: Date.now() - startTime,
|
|
324
|
-
});
|
|
325
154
|
return user;
|
|
326
155
|
}
|
|
327
156
|
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
createUserGlobalStyles(userStyleSchema, accountId),
|
|
333
|
-
5000,
|
|
334
|
-
'Global styles creation'
|
|
335
|
-
);
|
|
336
|
-
|
|
337
|
-
const newUserId = uuidv4();
|
|
157
|
+
const userGlobalStyles = await createUserGlobalStyles(
|
|
158
|
+
userStyleSchema,
|
|
159
|
+
accountId
|
|
160
|
+
);
|
|
338
161
|
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
userGlobalStyles: userGlobalStyles._id,
|
|
346
|
-
theme: defaultTheme._id,
|
|
347
|
-
}),
|
|
348
|
-
createDefaultFolders(folderSchema, newUserId),
|
|
349
|
-
]),
|
|
350
|
-
5000,
|
|
351
|
-
'User and folders creation'
|
|
352
|
-
);
|
|
162
|
+
user = await userSchema.create({
|
|
163
|
+
accountId,
|
|
164
|
+
id: uuidv4(),
|
|
165
|
+
verified: false,
|
|
166
|
+
userGlobalStyles: userGlobalStyles._id,
|
|
167
|
+
});
|
|
353
168
|
|
|
354
|
-
|
|
355
|
-
|
|
169
|
+
const folders = await createDefaultFolders(folderSchema, user.id);
|
|
170
|
+
console.log('Folders created:', folders);
|
|
171
|
+
user.folders = folders.map(folder => folder._id);
|
|
172
|
+
await user.save();
|
|
356
173
|
|
|
357
|
-
|
|
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
|
-
}
|
|
174
|
+
return user;
|
|
366
175
|
} catch (error) {
|
|
367
|
-
|
|
368
|
-
|
|
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
|
-
});
|
|
176
|
+
console.error('Error in checkIfUserExists:', error);
|
|
177
|
+
throw new Error('Failed to get or create user');
|
|
377
178
|
}
|
|
378
179
|
};
|
|
379
180
|
|