propro-utils 1.5.85 → 1.5.89
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 +31 -22
- package/package.json +1 -1
|
@@ -117,12 +117,16 @@ const DEFAULT_THEME = {
|
|
|
117
117
|
* @throws {Error} If there's an issue with database operations or invalid input
|
|
118
118
|
*/
|
|
119
119
|
const checkIfUserExists = async accountId => {
|
|
120
|
+
console.log('Starting checkIfUserExists for accountId:', accountId);
|
|
121
|
+
|
|
120
122
|
// Input validation
|
|
121
123
|
if (!accountId || typeof accountId !== 'string') {
|
|
124
|
+
console.warn('Invalid accountId provided:', accountId);
|
|
122
125
|
throw new Error('Invalid accountId provided');
|
|
123
126
|
}
|
|
124
127
|
|
|
125
128
|
try {
|
|
129
|
+
console.log('Loading schemas...');
|
|
126
130
|
// Load all required schemas in parallel
|
|
127
131
|
const [userSchema, userStyleSchema, folderSchema, themeSchema] =
|
|
128
132
|
await Promise.all([
|
|
@@ -131,6 +135,7 @@ const checkIfUserExists = async accountId => {
|
|
|
131
135
|
ServiceManager.getService('FolderSchema'),
|
|
132
136
|
ServiceManager.getService('ThemeSchema'),
|
|
133
137
|
]);
|
|
138
|
+
console.log('Schemas loaded successfully');
|
|
134
139
|
|
|
135
140
|
if (!userSchema) {
|
|
136
141
|
throw new Error('UserSchema service not available');
|
|
@@ -139,22 +144,23 @@ const checkIfUserExists = async accountId => {
|
|
|
139
144
|
// Find existing user with populated fields
|
|
140
145
|
const user = await userSchema
|
|
141
146
|
.findOne({ accountId })
|
|
142
|
-
.
|
|
143
|
-
.populate({
|
|
144
|
-
path: 'theme',
|
|
145
|
-
model: 'Theme',
|
|
146
|
-
})
|
|
147
|
+
.select('-theme -folderList -userGlobalStyles -mapList')
|
|
147
148
|
.lean();
|
|
148
149
|
|
|
150
|
+
console.log('User lookup result:', user ? 'User found' : 'User not found');
|
|
151
|
+
|
|
149
152
|
if (user) {
|
|
153
|
+
console.log('Updating existing user data...');
|
|
150
154
|
const updates = [];
|
|
151
155
|
|
|
152
156
|
// Handle userGlobalStyles
|
|
153
157
|
if (userStyleSchema) {
|
|
154
158
|
if (!user.userGlobalStyles) {
|
|
159
|
+
console.log('Creating missing userGlobalStyles...');
|
|
155
160
|
updates.push(
|
|
156
161
|
createUserGlobalStyles(userStyleSchema, accountId).then(
|
|
157
162
|
async styles => {
|
|
163
|
+
console.log('UserGlobalStyles created successfully');
|
|
158
164
|
await userSchema.updateOne(
|
|
159
165
|
{ _id: user._id },
|
|
160
166
|
{ userGlobalStyles: styles._id }
|
|
@@ -175,6 +181,7 @@ const checkIfUserExists = async accountId => {
|
|
|
175
181
|
|
|
176
182
|
// Handle folders
|
|
177
183
|
if (folderSchema) {
|
|
184
|
+
console.log('Checking folders...');
|
|
178
185
|
updates.push(
|
|
179
186
|
(async () => {
|
|
180
187
|
const existingFolders = await folderSchema
|
|
@@ -182,6 +189,7 @@ const checkIfUserExists = async accountId => {
|
|
|
182
189
|
.select('name')
|
|
183
190
|
.lean();
|
|
184
191
|
|
|
192
|
+
console.log('Existing folders count:', existingFolders.length);
|
|
185
193
|
const existingFolderNames = new Set(
|
|
186
194
|
existingFolders.map(f => f.name)
|
|
187
195
|
);
|
|
@@ -203,6 +211,7 @@ const checkIfUserExists = async accountId => {
|
|
|
203
211
|
|
|
204
212
|
// Handle themes
|
|
205
213
|
if (themeSchema) {
|
|
214
|
+
console.log('Checking themes...');
|
|
206
215
|
updates.push(
|
|
207
216
|
(async () => {
|
|
208
217
|
const defaultThemeExists = await themeSchema.exists({
|
|
@@ -210,6 +219,7 @@ const checkIfUserExists = async accountId => {
|
|
|
210
219
|
accountId,
|
|
211
220
|
});
|
|
212
221
|
|
|
222
|
+
console.log('Default theme exists:', defaultThemeExists);
|
|
213
223
|
if (!defaultThemeExists) {
|
|
214
224
|
const defaultTheme = await themeSchema.create({
|
|
215
225
|
...DEFAULT_THEME,
|
|
@@ -229,15 +239,15 @@ const checkIfUserExists = async accountId => {
|
|
|
229
239
|
}
|
|
230
240
|
|
|
231
241
|
// Wait for all updates to complete
|
|
242
|
+
console.log('Waiting for all updates to complete...');
|
|
232
243
|
await Promise.all(updates);
|
|
244
|
+
console.log('All updates completed successfully');
|
|
233
245
|
|
|
234
|
-
// Return fresh user data after updates
|
|
235
|
-
return userSchema
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
// model: 'Theme',
|
|
240
|
-
// });
|
|
246
|
+
// Return fresh user data after updates, excluding specified fields
|
|
247
|
+
return userSchema
|
|
248
|
+
.findOne({ accountId })
|
|
249
|
+
.select('-theme -folderList -userGlobalStyles -mapList')
|
|
250
|
+
.lean();
|
|
241
251
|
}
|
|
242
252
|
|
|
243
253
|
// Create new user with all associated data
|
|
@@ -285,25 +295,24 @@ const checkIfUserExists = async accountId => {
|
|
|
285
295
|
userGlobalStyles: userGlobalStyles?._id,
|
|
286
296
|
theme: defaultTheme ? [defaultTheme._id] : undefined,
|
|
287
297
|
});
|
|
288
|
-
console.log('Created user with theme:', newUser.theme);
|
|
289
298
|
|
|
290
299
|
// Create folders if schema available
|
|
291
300
|
if (folderSchema) {
|
|
292
301
|
await createDefaultFolders(folderSchema, newUser.id);
|
|
293
302
|
}
|
|
294
303
|
|
|
295
|
-
|
|
296
|
-
return userSchema
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
// model: 'Theme',
|
|
301
|
-
// });
|
|
304
|
+
console.log('Operation completed successfully');
|
|
305
|
+
return userSchema
|
|
306
|
+
.findById(newUser._id)
|
|
307
|
+
.select('-theme -folderList -userGlobalStyles -mapList')
|
|
308
|
+
.lean();
|
|
302
309
|
} catch (error) {
|
|
303
|
-
console.error('
|
|
310
|
+
console.error('Detailed error in checkIfUserExists:', {
|
|
304
311
|
accountId,
|
|
305
|
-
|
|
312
|
+
errorName: error.name,
|
|
313
|
+
errorMessage: error.message,
|
|
306
314
|
stack: error.stack,
|
|
315
|
+
additionalInfo: error.response?.data, // For axios errors
|
|
307
316
|
});
|
|
308
317
|
throw new Error(`Failed to get or create user: ${error.message}`);
|
|
309
318
|
}
|