propro-utils 1.5.62 → 1.5.63
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 +103 -58
- package/package.json +1 -1
- package/src/server/index.js +3 -1
|
@@ -100,35 +100,68 @@ async function createDefaultFolders(folderSchema, accountId) {
|
|
|
100
100
|
}
|
|
101
101
|
}
|
|
102
102
|
|
|
103
|
+
const debugLog = (area, message, data = null) => {
|
|
104
|
+
const timestamp = new Date().toISOString();
|
|
105
|
+
console.log(`[${timestamp}] [${area}] ${message}`, data ? data : '');
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
const DEFAULT_THEME = {
|
|
109
|
+
canvasBackground: '#000000',
|
|
110
|
+
defaultItemWidth: 200,
|
|
111
|
+
defaultColor: '#ffffff',
|
|
112
|
+
fontSize: '16px',
|
|
113
|
+
name: 'Default Theme',
|
|
114
|
+
};
|
|
115
|
+
|
|
103
116
|
const checkIfUserExists = async accountId => {
|
|
104
117
|
try {
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
const
|
|
118
|
+
debugLog('init', `Starting user check for accountId: ${accountId}`);
|
|
119
|
+
|
|
120
|
+
// Parallel service initialization
|
|
121
|
+
const [userSchema, userStyleSchema, folderSchema, Theme] =
|
|
122
|
+
await Promise.all([
|
|
123
|
+
ServiceManager.getService('UserSchema'),
|
|
124
|
+
ServiceManager.getService('UserStyleSchema'),
|
|
125
|
+
ServiceManager.getService('FolderSchema'),
|
|
126
|
+
ServiceManager.getService('Theme'),
|
|
127
|
+
]);
|
|
128
|
+
|
|
129
|
+
debugLog('services', 'All services initialized');
|
|
130
|
+
|
|
131
|
+
// Find user and theme in parallel
|
|
132
|
+
const [user, existingTheme] = await Promise.all([
|
|
133
|
+
userSchema.findOne({ accountId }).populate('userGlobalStyles'),
|
|
134
|
+
Theme.findOne({
|
|
135
|
+
name: 'Default Theme',
|
|
136
|
+
accountId,
|
|
137
|
+
}),
|
|
138
|
+
]);
|
|
139
|
+
|
|
140
|
+
debugLog('query', 'Initial queries complete', {
|
|
141
|
+
userFound: !!user,
|
|
142
|
+
themeFound: !!existingTheme,
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
// Create or use existing theme
|
|
146
|
+
const defaultTheme =
|
|
147
|
+
existingTheme ||
|
|
148
|
+
(await Theme.create({
|
|
149
|
+
...DEFAULT_THEME,
|
|
150
|
+
id: uuidv4(),
|
|
151
|
+
accountId,
|
|
152
|
+
}));
|
|
109
153
|
|
|
110
|
-
|
|
111
|
-
.findOne({ accountId })
|
|
112
|
-
.populate('userGlobalStyles');
|
|
154
|
+
debugLog('theme', 'Theme ready', { themeId: defaultTheme._id });
|
|
113
155
|
|
|
114
156
|
if (user) {
|
|
157
|
+
debugLog('user', 'Existing user found, updating if needed');
|
|
158
|
+
|
|
159
|
+
const updates = [];
|
|
160
|
+
|
|
161
|
+
// Collect necessary updates
|
|
115
162
|
if (!user.theme) {
|
|
116
|
-
let defaultTheme = await Theme.findOne({
|
|
117
|
-
name: 'Default Theme',
|
|
118
|
-
accountId,
|
|
119
|
-
});
|
|
120
|
-
if (!defaultTheme) {
|
|
121
|
-
defaultTheme = await Theme.create({
|
|
122
|
-
canvasBackground: '#000000',
|
|
123
|
-
defaultItemWidth: 200,
|
|
124
|
-
defaultColor: '#ffffff',
|
|
125
|
-
fontSize: '16px',
|
|
126
|
-
name: 'Default Theme',
|
|
127
|
-
id: uuidv4(),
|
|
128
|
-
});
|
|
129
|
-
}
|
|
130
163
|
user.theme = defaultTheme._id;
|
|
131
|
-
|
|
164
|
+
updates.push('theme');
|
|
132
165
|
}
|
|
133
166
|
|
|
134
167
|
if (!user.userGlobalStyles) {
|
|
@@ -136,21 +169,27 @@ const checkIfUserExists = async accountId => {
|
|
|
136
169
|
userStyleSchema,
|
|
137
170
|
accountId
|
|
138
171
|
);
|
|
139
|
-
|
|
172
|
+
updates.push('userGlobalStyles');
|
|
140
173
|
} else if (user.userGlobalStyles.styleShortcuts.length === 0) {
|
|
141
174
|
user.userGlobalStyles.styleShortcuts = defaultUserGlobalStyleShortcuts;
|
|
142
175
|
await user.userGlobalStyles.save();
|
|
143
176
|
}
|
|
144
177
|
|
|
145
|
-
|
|
146
|
-
const
|
|
147
|
-
|
|
148
|
-
|
|
178
|
+
// Check folders only if needed
|
|
179
|
+
const userFolders = await folderSchema.find(
|
|
180
|
+
{ user_id: user.id },
|
|
181
|
+
{ name: 1 }
|
|
182
|
+
);
|
|
183
|
+
const userFolderNames = new Set(userFolders.map(f => f.name));
|
|
149
184
|
const foldersToCreate = defaultFolders.filter(
|
|
150
|
-
|
|
185
|
+
f => !userFolderNames.has(f.name)
|
|
151
186
|
);
|
|
152
187
|
|
|
153
188
|
if (foldersToCreate.length > 0) {
|
|
189
|
+
debugLog(
|
|
190
|
+
'folders',
|
|
191
|
+
`Creating ${foldersToCreate.length} missing folders`
|
|
192
|
+
);
|
|
154
193
|
await Promise.all(
|
|
155
194
|
foldersToCreate.map(folder =>
|
|
156
195
|
folderSchema.create({
|
|
@@ -161,49 +200,55 @@ const checkIfUserExists = async accountId => {
|
|
|
161
200
|
);
|
|
162
201
|
}
|
|
163
202
|
|
|
203
|
+
// Save user only if there were updates
|
|
204
|
+
if (updates.length > 0) {
|
|
205
|
+
debugLog('update', `Saving user updates: ${updates.join(', ')}`);
|
|
206
|
+
await user.save();
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
debugLog('complete', 'Existing user processed successfully');
|
|
164
210
|
return user;
|
|
165
211
|
}
|
|
166
212
|
|
|
167
|
-
|
|
168
|
-
name: 'Default Theme',
|
|
169
|
-
accountId,
|
|
170
|
-
});
|
|
171
|
-
if (!defaultTheme) {
|
|
172
|
-
defaultTheme = await Theme.create({
|
|
173
|
-
canvasBackground: '#000000',
|
|
174
|
-
defaultItemWidth: 200,
|
|
175
|
-
defaultColor: '#ffffff',
|
|
176
|
-
fontSize: '16px',
|
|
177
|
-
name: 'Default Theme',
|
|
178
|
-
id: uuidv4(),
|
|
179
|
-
});
|
|
180
|
-
}
|
|
213
|
+
debugLog('create', 'Creating new user');
|
|
181
214
|
|
|
215
|
+
// Create new user with all required relations
|
|
182
216
|
const userGlobalStyles = await createUserGlobalStyles(
|
|
183
217
|
userStyleSchema,
|
|
184
218
|
accountId
|
|
185
219
|
);
|
|
186
220
|
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
221
|
+
const [newUser, newFolders] = await Promise.all([
|
|
222
|
+
userSchema.create({
|
|
223
|
+
accountId,
|
|
224
|
+
id: uuidv4(),
|
|
225
|
+
verified: false,
|
|
226
|
+
userGlobalStyles: userGlobalStyles._id,
|
|
227
|
+
theme: defaultTheme._id,
|
|
228
|
+
}),
|
|
229
|
+
createDefaultFolders(folderSchema, uuidv4()), // Pass the new user ID
|
|
230
|
+
]);
|
|
231
|
+
|
|
232
|
+
newUser.folders = newFolders.map(folder => folder._id);
|
|
233
|
+
await newUser.save();
|
|
234
|
+
|
|
235
|
+
debugLog('complete', 'New user created successfully', {
|
|
236
|
+
userId: newUser.id,
|
|
193
237
|
});
|
|
194
|
-
|
|
195
|
-
const folders = await createDefaultFolders(folderSchema, user.id);
|
|
196
|
-
console.log('Folders created:', folders);
|
|
197
|
-
user.folders = folders.map(folder => folder._id);
|
|
198
|
-
await user.save();
|
|
199
|
-
|
|
200
|
-
return user;
|
|
238
|
+
return newUser;
|
|
201
239
|
} catch (error) {
|
|
202
|
-
|
|
203
|
-
|
|
240
|
+
debugLog('error', 'Operation failed', {
|
|
241
|
+
error: error.message,
|
|
242
|
+
stack: error.stack,
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
// Throw error with original cause and context
|
|
246
|
+
throw new Error('Failed to get or create user', {
|
|
247
|
+
cause: error,
|
|
248
|
+
accountId,
|
|
249
|
+
});
|
|
204
250
|
}
|
|
205
251
|
};
|
|
206
|
-
|
|
207
252
|
module.exports = {
|
|
208
253
|
getAccountProfile,
|
|
209
254
|
checkIfUserExists,
|
package/package.json
CHANGED
package/src/server/index.js
CHANGED
|
@@ -8,6 +8,7 @@ const {
|
|
|
8
8
|
clearAuthCookies,
|
|
9
9
|
} = require('./middleware/cookieUtils');
|
|
10
10
|
const { checkIfUserExists } = require('../../middlewares/account_info');
|
|
11
|
+
const UserService = require('../../middlewares/userService');
|
|
11
12
|
const authValidation = require('../../middlewares/access_token');
|
|
12
13
|
const axios = require('axios');
|
|
13
14
|
const { Router, query } = require('express');
|
|
@@ -84,7 +85,8 @@ class AuthMiddleware {
|
|
|
84
85
|
|
|
85
86
|
const { account, tokens } = response.data;
|
|
86
87
|
console.log('account:', account);
|
|
87
|
-
const user = await checkIfUserExists(account.accountId);
|
|
88
|
+
// const user = await checkIfUserExists(account.accountId);
|
|
89
|
+
const user = await UserService.checkIfUserExists(account.accountId);
|
|
88
90
|
console.log('user:', user);
|
|
89
91
|
|
|
90
92
|
if (returnTokens === 'true') {
|