propro-utils 1.5.67 → 1.5.69
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 -47
- package/package.json +1 -1
- package/src/server/index.js +9 -3
|
@@ -99,6 +99,35 @@ 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
|
+
|
|
102
131
|
/**
|
|
103
132
|
* Checks if a user exists based on the given account ID.
|
|
104
133
|
* If the user does not exist, creates a new user with the given account ID.
|
|
@@ -109,70 +138,242 @@ async function createDefaultFolders(folderSchema, accountId) {
|
|
|
109
138
|
* @throws {Error} If there's an issue with database operations.
|
|
110
139
|
*/
|
|
111
140
|
const checkIfUserExists = async accountId => {
|
|
141
|
+
const startTime = Date.now();
|
|
112
142
|
try {
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
143
|
+
if (!accountId) {
|
|
144
|
+
throw new Error('AccountId is required');
|
|
145
|
+
}
|
|
146
|
+
debugLog('init', `Starting user check for accountId: ${accountId}`);
|
|
116
147
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
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
|
+
);
|
|
120
170
|
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
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'
|
|
126
233
|
);
|
|
127
|
-
|
|
128
|
-
} else
|
|
129
|
-
|
|
130
|
-
|
|
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
|
+
});
|
|
131
240
|
}
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
241
|
+
} catch (error) {
|
|
242
|
+
debugLog('error', 'Theme operation failed', { error: error.message });
|
|
243
|
+
throw error;
|
|
244
|
+
}
|
|
136
245
|
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
246
|
+
if (user) {
|
|
247
|
+
debugLog('user', 'Processing existing user');
|
|
248
|
+
const updates = [];
|
|
140
249
|
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
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)
|
|
149
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;
|
|
150
319
|
}
|
|
151
320
|
|
|
321
|
+
debugLog('complete', 'Existing user processed', {
|
|
322
|
+
userId: user.id,
|
|
323
|
+
totalTime: Date.now() - startTime,
|
|
324
|
+
});
|
|
152
325
|
return user;
|
|
153
326
|
}
|
|
154
327
|
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
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
|
+
);
|
|
159
336
|
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
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
|
+
);
|
|
166
353
|
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
user.folders = folders.map(folder => folder._id);
|
|
170
|
-
await user.save();
|
|
354
|
+
newUser.folders = newFolders.map(folder => folder._id);
|
|
355
|
+
await timeoutPromise(newUser.save(), 5000, 'New user save');
|
|
171
356
|
|
|
172
|
-
|
|
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
|
+
}
|
|
173
366
|
} catch (error) {
|
|
174
|
-
|
|
175
|
-
|
|
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
|
+
});
|
|
176
377
|
}
|
|
177
378
|
};
|
|
178
379
|
|
package/package.json
CHANGED
package/src/server/index.js
CHANGED
|
@@ -33,7 +33,13 @@ const upload = multer().single('file');
|
|
|
33
33
|
* @returns {Function} - Express middleware function.
|
|
34
34
|
*/
|
|
35
35
|
class AuthMiddleware {
|
|
36
|
-
constructor(
|
|
36
|
+
constructor(
|
|
37
|
+
options = {},
|
|
38
|
+
userSchema,
|
|
39
|
+
userStyleSchema,
|
|
40
|
+
themeSchema,
|
|
41
|
+
redisClient
|
|
42
|
+
) {
|
|
37
43
|
this.options = {
|
|
38
44
|
secret: options.secret || 'RESTFULAPIs',
|
|
39
45
|
authUrl: options.authUrl || process.env.AUTH_URL,
|
|
@@ -47,6 +53,7 @@ class AuthMiddleware {
|
|
|
47
53
|
this.userSchema = userSchema;
|
|
48
54
|
this.redisClient = redisClient;
|
|
49
55
|
this.userStyleSchema = userStyleSchema;
|
|
56
|
+
this.themeSchema = themeSchema;
|
|
50
57
|
this.router = Router();
|
|
51
58
|
this.initializeRoutes();
|
|
52
59
|
}
|
|
@@ -84,8 +91,7 @@ class AuthMiddleware {
|
|
|
84
91
|
|
|
85
92
|
const { account, tokens } = response.data;
|
|
86
93
|
console.log('account:', account);
|
|
87
|
-
|
|
88
|
-
const user = await UserService.checkIfUserExists(account.accountId);
|
|
94
|
+
const user = await checkIfUserExists(account.accountId);
|
|
89
95
|
console.log('user:', user);
|
|
90
96
|
|
|
91
97
|
if (returnTokens === 'true') {
|