propro-utils 1.5.50 → 1.5.52

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.
@@ -84,14 +84,20 @@ async function createUserGlobalStyles(userStyleSchema, accountId) {
84
84
  * @returns {Promise<Array>} A promise that resolves to an array of created folder objects.
85
85
  */
86
86
  async function createDefaultFolders(folderSchema, accountId) {
87
- const folderPromises = defaultFolders.map(folder =>
88
- folderSchema.create({
89
- ...folder,
90
- id: uuidv4(),
91
- user_id: accountId,
92
- })
93
- );
94
- return Promise.all(folderPromises);
87
+ try {
88
+ console.log('Creating default folders for user:', accountId);
89
+ const folderPromises = defaultFolders.map(folder =>
90
+ folderSchema.create({
91
+ ...folder,
92
+ id: uuidv4(),
93
+ user_id: accountId,
94
+ })
95
+ );
96
+ return Promise.all(folderPromises);
97
+ } catch (error) {
98
+ console.error('Error in createDefaultFolders:', error);
99
+ throw new Error('Failed to create default folders');
100
+ }
95
101
  }
96
102
 
97
103
  /**
@@ -105,6 +111,13 @@ async function createDefaultFolders(folderSchema, accountId) {
105
111
  */
106
112
  const checkIfUserExists = async accountId => {
107
113
  try {
114
+ console.log('Checking if user exists:', accountId);
115
+ console.log('defaultFolders', defaultFolders);
116
+ console.log('defaultFolders.length', defaultFolders.length);
117
+ console.log(
118
+ 'defaultUserGlobalStyleShortcuts.length',
119
+ defaultUserGlobalStyleShortcuts.length
120
+ );
108
121
  const userSchema = await ServiceManager.getService('UserSchema');
109
122
  const userStyleSchema = await ServiceManager.getService('UserStyleSchema');
110
123
  const folderSchema = await ServiceManager.getService('FolderSchema');
@@ -125,9 +138,24 @@ const checkIfUserExists = async accountId => {
125
138
  await user.userGlobalStyles.save();
126
139
  }
127
140
  // Check if user has any folders
128
- const userFolders = await folderSchema.find({ accountId });
129
- if (userFolders.length === 0) {
130
- await createDefaultFolders(folderSchema, accountId);
141
+ const userFolders = await folderSchema.find({ user_id: accountId });
142
+ const defaultFolderNames = defaultFolders.map(folder => folder.name);
143
+ const userFolderNames = userFolders.map(folder => folder.name);
144
+
145
+ const foldersToCreate = defaultFolders.filter(
146
+ folder => !userFolderNames.includes(folder.name)
147
+ );
148
+
149
+ if (foldersToCreate.length > 0) {
150
+ await Promise.all(
151
+ foldersToCreate.map(folder =>
152
+ folderSchema.create({
153
+ ...folder,
154
+ id: uuidv4(),
155
+ user_id: accountId,
156
+ })
157
+ )
158
+ );
131
159
  }
132
160
  return user;
133
161
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "propro-utils",
3
- "version": "1.5.50",
3
+ "version": "1.5.52",
4
4
  "description": "Auth middleware for propro-auth",
5
5
  "main": "src/index.js",
6
6
  "scripts": {