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