propro-utils 1.5.72 → 1.5.73
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 +78 -245
- package/package.json +1 -1
|
@@ -99,6 +99,14 @@ async function createDefaultFolders(folderSchema, accountId) {
|
|
|
99
99
|
}
|
|
100
100
|
}
|
|
101
101
|
|
|
102
|
+
const DEFAULT_THEME = {
|
|
103
|
+
canvasBackground: '#000000',
|
|
104
|
+
defaultItemWidth: 200,
|
|
105
|
+
defaultColor: '#ffffff',
|
|
106
|
+
fontSize: '16px',
|
|
107
|
+
name: 'Default Theme',
|
|
108
|
+
};
|
|
109
|
+
|
|
102
110
|
/**
|
|
103
111
|
* Checks if a user exists based on the given account ID.
|
|
104
112
|
* If the user does not exist, creates a new user with the given account ID.
|
|
@@ -108,272 +116,97 @@ async function createDefaultFolders(folderSchema, accountId) {
|
|
|
108
116
|
* @returns {Promise<Object>} A promise that resolves to the user object.
|
|
109
117
|
* @throws {Error} If there's an issue with database operations.
|
|
110
118
|
*/
|
|
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
|
-
|
|
140
119
|
const checkIfUserExists = async accountId => {
|
|
141
|
-
const startTime = Date.now();
|
|
142
120
|
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('ThemeSchema').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
|
-
}
|
|
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
|
-
);
|
|
121
|
+
const userSchema = await ServiceManager.getService('UserSchema');
|
|
122
|
+
const userStyleSchema = await ServiceManager.getService('UserStyleSchema');
|
|
123
|
+
const folderSchema = await ServiceManager.getService('FolderSchema');
|
|
124
|
+
const themeSchema = await ServiceManager.getService('ThemeSchema');
|
|
209
125
|
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
});
|
|
215
|
-
} catch (error) {
|
|
216
|
-
debugLog('error', 'Query operations failed', { error: error.message });
|
|
217
|
-
throw error;
|
|
218
|
-
}
|
|
126
|
+
console.log('themeschema', themeSchema);
|
|
127
|
+
let user = await userSchema
|
|
128
|
+
.findOne({ accountId })
|
|
129
|
+
.populate('userGlobalStyles');
|
|
219
130
|
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
defaultTheme = await timeoutPromise(
|
|
226
|
-
Theme.create({
|
|
227
|
-
...DEFAULT_THEME,
|
|
228
|
-
id: uuidv4(),
|
|
229
|
-
accountId,
|
|
230
|
-
}),
|
|
231
|
-
5000,
|
|
232
|
-
'Theme creation'
|
|
131
|
+
if (user) {
|
|
132
|
+
if (!user.userGlobalStyles) {
|
|
133
|
+
user.userGlobalStyles = await createUserGlobalStyles(
|
|
134
|
+
userStyleSchema,
|
|
135
|
+
accountId
|
|
233
136
|
);
|
|
234
|
-
|
|
235
|
-
} else {
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
themeId: defaultTheme._id,
|
|
239
|
-
});
|
|
137
|
+
await user.save();
|
|
138
|
+
} else if (user.userGlobalStyles.styleShortcuts.length === 0) {
|
|
139
|
+
user.userGlobalStyles.styleShortcuts = defaultUserGlobalStyleShortcuts;
|
|
140
|
+
await user.userGlobalStyles.save();
|
|
240
141
|
}
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
if (user) {
|
|
247
|
-
debugLog('user', 'Processing existing user');
|
|
248
|
-
const updates = [];
|
|
142
|
+
// Check if user has any folders
|
|
143
|
+
const userFolders = await folderSchema.find({ user_id: user.id });
|
|
144
|
+
const defaultFolderNames = defaultFolders.map(folder => folder.name);
|
|
145
|
+
const userFolderNames = userFolders.map(folder => folder.name);
|
|
249
146
|
|
|
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
|
-
);
|
|
147
|
+
const foldersToCreate = defaultFolders.filter(
|
|
148
|
+
folder => !userFolderNames.includes(folder.name)
|
|
149
|
+
);
|
|
285
150
|
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
151
|
+
if (foldersToCreate.length > 0) {
|
|
152
|
+
await Promise.all(
|
|
153
|
+
foldersToCreate.map(folder =>
|
|
154
|
+
folderSchema.create({
|
|
155
|
+
...folder,
|
|
156
|
+
user_id: user.id,
|
|
157
|
+
})
|
|
158
|
+
)
|
|
289
159
|
);
|
|
160
|
+
}
|
|
290
161
|
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
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,
|
|
162
|
+
// Check if user has any themes
|
|
163
|
+
const userThemes = await themeSchema.find({
|
|
164
|
+
name: 'Default Theme',
|
|
165
|
+
accountId,
|
|
166
|
+
});
|
|
167
|
+
if (userThemes.length === 0) {
|
|
168
|
+
const defaultTheme = await themeSchema.create({
|
|
169
|
+
...DEFAULT_THEME,
|
|
170
|
+
accountId,
|
|
171
|
+
userId: user.id,
|
|
172
|
+
id: uuidv4(),
|
|
317
173
|
});
|
|
318
|
-
|
|
174
|
+
user.theme = defaultTheme._id;
|
|
175
|
+
await user.save();
|
|
319
176
|
}
|
|
320
177
|
|
|
321
|
-
debugLog('complete', 'Existing user processed', {
|
|
322
|
-
userId: user.id,
|
|
323
|
-
totalTime: Date.now() - startTime,
|
|
324
|
-
});
|
|
325
178
|
return user;
|
|
326
179
|
}
|
|
327
180
|
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
createUserGlobalStyles(userStyleSchema, accountId),
|
|
333
|
-
5000,
|
|
334
|
-
'Global styles creation'
|
|
335
|
-
);
|
|
336
|
-
|
|
337
|
-
const newUserId = uuidv4();
|
|
181
|
+
const userGlobalStyles = await createUserGlobalStyles(
|
|
182
|
+
userStyleSchema,
|
|
183
|
+
accountId
|
|
184
|
+
);
|
|
338
185
|
|
|
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
|
-
);
|
|
186
|
+
user = await userSchema.create({
|
|
187
|
+
accountId,
|
|
188
|
+
id: uuidv4(),
|
|
189
|
+
verified: false,
|
|
190
|
+
userGlobalStyles: userGlobalStyles._id,
|
|
191
|
+
});
|
|
353
192
|
|
|
354
|
-
|
|
355
|
-
|
|
193
|
+
const folders = await createDefaultFolders(folderSchema, user.id);
|
|
194
|
+
console.log('Folders created:', folders);
|
|
195
|
+
user.folders = folders.map(folder => folder._id);
|
|
356
196
|
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
} catch (error) {
|
|
363
|
-
debugLog('error', 'User creation failed', { error: error.message });
|
|
364
|
-
throw error;
|
|
365
|
-
}
|
|
366
|
-
} catch (error) {
|
|
367
|
-
const totalTime = Date.now() - startTime;
|
|
368
|
-
debugLog('error', 'Operation failed', {
|
|
369
|
-
error: error.message,
|
|
370
|
-
stack: error.stack,
|
|
371
|
-
totalTime,
|
|
197
|
+
const defaultTheme = await themeSchema.create({
|
|
198
|
+
...DEFAULT_THEME,
|
|
199
|
+
accountId,
|
|
200
|
+
userId: user.id,
|
|
201
|
+
id: uuidv4(),
|
|
372
202
|
});
|
|
203
|
+
user.theme = defaultTheme._id;
|
|
204
|
+
await user.save();
|
|
373
205
|
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
206
|
+
return user;
|
|
207
|
+
} catch (error) {
|
|
208
|
+
console.error('Error in checkIfUserExists:', error);
|
|
209
|
+
throw new Error('Failed to get or create user');
|
|
377
210
|
}
|
|
378
211
|
};
|
|
379
212
|
|