propro-utils 1.5.86 → 1.5.90
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 +55 -9
- package/package.json +1 -1
|
@@ -117,40 +117,78 @@ 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
|
|
131
|
+
const schemaResults = await Promise.all([
|
|
132
|
+
ServiceManager.getService('UserSchema'),
|
|
133
|
+
ServiceManager.getService('UserStyleSchema'),
|
|
134
|
+
ServiceManager.getService('FolderSchema'),
|
|
135
|
+
ServiceManager.getService('ThemeSchema'),
|
|
136
|
+
]);
|
|
137
|
+
|
|
127
138
|
const [userSchema, userStyleSchema, folderSchema, themeSchema] =
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
139
|
+
schemaResults;
|
|
140
|
+
|
|
141
|
+
// Log the status of each schema
|
|
142
|
+
console.log('Schema loading results:', {
|
|
143
|
+
userSchema: userSchema ? 'Loaded' : 'Not available',
|
|
144
|
+
userStyleSchema: userStyleSchema ? 'Loaded' : 'Not available',
|
|
145
|
+
folderSchema: folderSchema ? 'Loaded' : 'Not available',
|
|
146
|
+
themeSchema: themeSchema ? 'Loaded' : 'Not available',
|
|
147
|
+
});
|
|
134
148
|
|
|
135
149
|
if (!userSchema) {
|
|
136
150
|
throw new Error('UserSchema service not available');
|
|
137
151
|
}
|
|
138
152
|
|
|
153
|
+
// Optional schemas warning
|
|
154
|
+
if (!userStyleSchema) {
|
|
155
|
+
console.warn(
|
|
156
|
+
'UserStyleSchema service not available - style features will be skipped'
|
|
157
|
+
);
|
|
158
|
+
}
|
|
159
|
+
if (!folderSchema) {
|
|
160
|
+
console.warn(
|
|
161
|
+
'FolderSchema service not available - folder features will be skipped'
|
|
162
|
+
);
|
|
163
|
+
}
|
|
164
|
+
if (!themeSchema) {
|
|
165
|
+
console.warn(
|
|
166
|
+
'ThemeSchema service not available - theme features will be skipped'
|
|
167
|
+
);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
console.log('Schema loading and validation complete');
|
|
171
|
+
|
|
139
172
|
// Find existing user with populated fields
|
|
140
173
|
const user = await userSchema
|
|
141
174
|
.findOne({ accountId })
|
|
142
175
|
.select('-theme -folderList -userGlobalStyles -mapList')
|
|
143
176
|
.lean();
|
|
144
177
|
|
|
178
|
+
console.log('User lookup result:', user ? 'User found' : 'User not found');
|
|
179
|
+
|
|
145
180
|
if (user) {
|
|
181
|
+
console.log('Updating existing user data...');
|
|
146
182
|
const updates = [];
|
|
147
183
|
|
|
148
184
|
// Handle userGlobalStyles
|
|
149
185
|
if (userStyleSchema) {
|
|
150
186
|
if (!user.userGlobalStyles) {
|
|
187
|
+
console.log('Creating missing userGlobalStyles...');
|
|
151
188
|
updates.push(
|
|
152
189
|
createUserGlobalStyles(userStyleSchema, accountId).then(
|
|
153
190
|
async styles => {
|
|
191
|
+
console.log('UserGlobalStyles created successfully');
|
|
154
192
|
await userSchema.updateOne(
|
|
155
193
|
{ _id: user._id },
|
|
156
194
|
{ userGlobalStyles: styles._id }
|
|
@@ -171,6 +209,7 @@ const checkIfUserExists = async accountId => {
|
|
|
171
209
|
|
|
172
210
|
// Handle folders
|
|
173
211
|
if (folderSchema) {
|
|
212
|
+
console.log('Checking folders...');
|
|
174
213
|
updates.push(
|
|
175
214
|
(async () => {
|
|
176
215
|
const existingFolders = await folderSchema
|
|
@@ -178,6 +217,7 @@ const checkIfUserExists = async accountId => {
|
|
|
178
217
|
.select('name')
|
|
179
218
|
.lean();
|
|
180
219
|
|
|
220
|
+
console.log('Existing folders count:', existingFolders.length);
|
|
181
221
|
const existingFolderNames = new Set(
|
|
182
222
|
existingFolders.map(f => f.name)
|
|
183
223
|
);
|
|
@@ -199,6 +239,7 @@ const checkIfUserExists = async accountId => {
|
|
|
199
239
|
|
|
200
240
|
// Handle themes
|
|
201
241
|
if (themeSchema) {
|
|
242
|
+
console.log('Checking themes...');
|
|
202
243
|
updates.push(
|
|
203
244
|
(async () => {
|
|
204
245
|
const defaultThemeExists = await themeSchema.exists({
|
|
@@ -206,6 +247,7 @@ const checkIfUserExists = async accountId => {
|
|
|
206
247
|
accountId,
|
|
207
248
|
});
|
|
208
249
|
|
|
250
|
+
console.log('Default theme exists:', defaultThemeExists);
|
|
209
251
|
if (!defaultThemeExists) {
|
|
210
252
|
const defaultTheme = await themeSchema.create({
|
|
211
253
|
...DEFAULT_THEME,
|
|
@@ -225,7 +267,9 @@ const checkIfUserExists = async accountId => {
|
|
|
225
267
|
}
|
|
226
268
|
|
|
227
269
|
// Wait for all updates to complete
|
|
270
|
+
console.log('Waiting for all updates to complete...');
|
|
228
271
|
await Promise.all(updates);
|
|
272
|
+
console.log('All updates completed successfully');
|
|
229
273
|
|
|
230
274
|
// Return fresh user data after updates, excluding specified fields
|
|
231
275
|
return userSchema
|
|
@@ -285,16 +329,18 @@ const checkIfUserExists = async accountId => {
|
|
|
285
329
|
await createDefaultFolders(folderSchema, newUser.id);
|
|
286
330
|
}
|
|
287
331
|
|
|
288
|
-
|
|
332
|
+
console.log('Operation completed successfully');
|
|
289
333
|
return userSchema
|
|
290
334
|
.findById(newUser._id)
|
|
291
335
|
.select('-theme -folderList -userGlobalStyles -mapList')
|
|
292
336
|
.lean();
|
|
293
337
|
} catch (error) {
|
|
294
|
-
console.error('
|
|
338
|
+
console.error('Detailed error in checkIfUserExists:', {
|
|
295
339
|
accountId,
|
|
296
|
-
|
|
340
|
+
errorName: error.name,
|
|
341
|
+
errorMessage: error.message,
|
|
297
342
|
stack: error.stack,
|
|
343
|
+
additionalInfo: error.response?.data, // For axios errors
|
|
298
344
|
});
|
|
299
345
|
throw new Error(`Failed to get or create user: ${error.message}`);
|
|
300
346
|
}
|