@symbo.ls/sdk 2.34.35 → 3.1.2

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.
Files changed (187) hide show
  1. package/README.md +2 -143
  2. package/dist/cjs/config/environment.js +30 -98
  3. package/dist/cjs/index.js +24 -144
  4. package/dist/cjs/services/AIService.js +155 -0
  5. package/dist/cjs/services/AuthService.js +305 -738
  6. package/dist/cjs/services/BaseService.js +6 -158
  7. package/dist/cjs/services/BasedService.js +1185 -0
  8. package/dist/cjs/services/CoreService.js +1751 -0
  9. package/dist/cjs/services/SocketIOService.js +307 -0
  10. package/dist/cjs/services/SocketService.js +161 -0
  11. package/dist/cjs/services/SymstoryService.js +571 -0
  12. package/dist/cjs/services/index.js +16 -64
  13. package/dist/cjs/utils/TokenManager.js +30 -78
  14. package/dist/cjs/utils/basedQuerys.js +181 -0
  15. package/dist/cjs/utils/services.js +103 -301
  16. package/dist/cjs/utils/symstoryClient.js +259 -0
  17. package/dist/cjs/utils/validation.js +3 -0
  18. package/dist/esm/config/environment.js +30 -98
  19. package/dist/esm/index.js +8797 -49416
  20. package/dist/esm/services/AIService.js +185 -0
  21. package/dist/esm/services/AuthService.js +386 -1493
  22. package/dist/esm/services/BaseService.js +6 -757
  23. package/dist/esm/services/BasedService.js +5278 -0
  24. package/dist/esm/services/CoreService.js +2264 -0
  25. package/dist/esm/services/SocketIOService.js +470 -0
  26. package/dist/esm/services/SocketService.js +191 -0
  27. package/dist/esm/services/SymstoryService.js +7041 -0
  28. package/dist/esm/services/index.js +8690 -49015
  29. package/dist/esm/utils/TokenManager.js +30 -78
  30. package/dist/esm/utils/basedQuerys.js +163 -0
  31. package/dist/esm/utils/services.js +103 -301
  32. package/dist/esm/utils/symstoryClient.js +370 -0
  33. package/dist/esm/utils/validation.js +7 -4
  34. package/dist/node/config/environment.js +30 -98
  35. package/dist/node/index.js +32 -175
  36. package/dist/node/services/AIService.js +136 -0
  37. package/dist/node/services/AuthService.js +310 -742
  38. package/dist/node/services/BaseService.js +6 -148
  39. package/dist/node/services/BasedService.js +1156 -0
  40. package/dist/node/services/CoreService.js +1722 -0
  41. package/dist/node/services/SocketIOService.js +278 -0
  42. package/dist/node/services/SocketService.js +142 -0
  43. package/dist/node/services/SymstoryService.js +542 -0
  44. package/dist/node/services/index.js +16 -64
  45. package/dist/node/utils/TokenManager.js +30 -78
  46. package/dist/node/utils/basedQuerys.js +162 -0
  47. package/dist/node/utils/services.js +103 -301
  48. package/dist/node/utils/symstoryClient.js +230 -0
  49. package/dist/node/utils/validation.js +3 -0
  50. package/package.json +16 -35
  51. package/src/config/environment.js +28 -99
  52. package/src/index.js +36 -181
  53. package/src/services/AIService.js +150 -0
  54. package/src/services/AuthService.js +328 -874
  55. package/src/services/BaseService.js +6 -166
  56. package/src/services/BasedService.js +1301 -0
  57. package/src/services/CoreService.js +1943 -0
  58. package/src/services/SocketIOService.js +334 -0
  59. package/src/services/SocketService.js +168 -0
  60. package/src/services/SymstoryService.js +649 -0
  61. package/src/services/index.js +13 -80
  62. package/src/utils/TokenManager.js +33 -88
  63. package/src/utils/basedQuerys.js +164 -0
  64. package/src/utils/services.js +107 -326
  65. package/src/utils/symstoryClient.js +252 -0
  66. package/src/utils/validation.js +3 -0
  67. package/dist/cjs/services/AdminService.js +0 -351
  68. package/dist/cjs/services/BranchService.js +0 -484
  69. package/dist/cjs/services/CollabService.js +0 -743
  70. package/dist/cjs/services/DnsService.js +0 -340
  71. package/dist/cjs/services/FeatureFlagService.js +0 -175
  72. package/dist/cjs/services/FileService.js +0 -201
  73. package/dist/cjs/services/IntegrationService.js +0 -538
  74. package/dist/cjs/services/MetricsService.js +0 -62
  75. package/dist/cjs/services/PaymentService.js +0 -271
  76. package/dist/cjs/services/PlanService.js +0 -426
  77. package/dist/cjs/services/ProjectService.js +0 -1207
  78. package/dist/cjs/services/PullRequestService.js +0 -503
  79. package/dist/cjs/services/ScreenshotService.js +0 -304
  80. package/dist/cjs/services/SubscriptionService.js +0 -396
  81. package/dist/cjs/services/TrackingService.js +0 -661
  82. package/dist/cjs/services/WaitlistService.js +0 -148
  83. package/dist/cjs/state/RootStateManager.js +0 -65
  84. package/dist/cjs/state/rootEventBus.js +0 -74
  85. package/dist/cjs/utils/CollabClient.js +0 -223
  86. package/dist/cjs/utils/changePreprocessor.js +0 -199
  87. package/dist/cjs/utils/jsonDiff.js +0 -145
  88. package/dist/cjs/utils/ordering.js +0 -309
  89. package/dist/esm/services/AdminService.js +0 -1132
  90. package/dist/esm/services/BranchService.js +0 -1265
  91. package/dist/esm/services/CollabService.js +0 -26838
  92. package/dist/esm/services/DnsService.js +0 -1121
  93. package/dist/esm/services/FeatureFlagService.js +0 -956
  94. package/dist/esm/services/FileService.js +0 -982
  95. package/dist/esm/services/IntegrationService.js +0 -1319
  96. package/dist/esm/services/MetricsService.js +0 -843
  97. package/dist/esm/services/PaymentService.js +0 -1052
  98. package/dist/esm/services/PlanService.js +0 -1207
  99. package/dist/esm/services/ProjectService.js +0 -2526
  100. package/dist/esm/services/PullRequestService.js +0 -1284
  101. package/dist/esm/services/ScreenshotService.js +0 -1085
  102. package/dist/esm/services/SubscriptionService.js +0 -1177
  103. package/dist/esm/services/TrackingService.js +0 -18343
  104. package/dist/esm/services/WaitlistService.js +0 -929
  105. package/dist/esm/state/RootStateManager.js +0 -90
  106. package/dist/esm/state/rootEventBus.js +0 -56
  107. package/dist/esm/utils/CollabClient.js +0 -18901
  108. package/dist/esm/utils/changePreprocessor.js +0 -542
  109. package/dist/esm/utils/jsonDiff.js +0 -7011
  110. package/dist/esm/utils/ordering.js +0 -291
  111. package/dist/node/services/AdminService.js +0 -332
  112. package/dist/node/services/BranchService.js +0 -465
  113. package/dist/node/services/CollabService.js +0 -724
  114. package/dist/node/services/DnsService.js +0 -321
  115. package/dist/node/services/FeatureFlagService.js +0 -156
  116. package/dist/node/services/FileService.js +0 -182
  117. package/dist/node/services/IntegrationService.js +0 -519
  118. package/dist/node/services/MetricsService.js +0 -43
  119. package/dist/node/services/PaymentService.js +0 -252
  120. package/dist/node/services/PlanService.js +0 -407
  121. package/dist/node/services/ProjectService.js +0 -1188
  122. package/dist/node/services/PullRequestService.js +0 -484
  123. package/dist/node/services/ScreenshotService.js +0 -285
  124. package/dist/node/services/SubscriptionService.js +0 -377
  125. package/dist/node/services/TrackingService.js +0 -632
  126. package/dist/node/services/WaitlistService.js +0 -129
  127. package/dist/node/state/RootStateManager.js +0 -36
  128. package/dist/node/state/rootEventBus.js +0 -55
  129. package/dist/node/utils/CollabClient.js +0 -194
  130. package/dist/node/utils/changePreprocessor.js +0 -180
  131. package/dist/node/utils/jsonDiff.js +0 -116
  132. package/dist/node/utils/ordering.js +0 -290
  133. package/src/services/AdminService.js +0 -374
  134. package/src/services/BranchService.js +0 -536
  135. package/src/services/CollabService.js +0 -900
  136. package/src/services/DnsService.js +0 -366
  137. package/src/services/FeatureFlagService.js +0 -174
  138. package/src/services/FileService.js +0 -213
  139. package/src/services/IntegrationService.js +0 -548
  140. package/src/services/MetricsService.js +0 -40
  141. package/src/services/PaymentService.js +0 -287
  142. package/src/services/PlanService.js +0 -468
  143. package/src/services/ProjectService.js +0 -1366
  144. package/src/services/PullRequestService.js +0 -537
  145. package/src/services/ScreenshotService.js +0 -258
  146. package/src/services/SubscriptionService.js +0 -425
  147. package/src/services/TrackingService.js +0 -853
  148. package/src/services/WaitlistService.js +0 -130
  149. package/src/services/tests/BranchService/createBranch.test.js +0 -153
  150. package/src/services/tests/BranchService/deleteBranch.test.js +0 -173
  151. package/src/services/tests/BranchService/getBranchChanges.test.js +0 -146
  152. package/src/services/tests/BranchService/listBranches.test.js +0 -87
  153. package/src/services/tests/BranchService/mergeBranch.test.js +0 -210
  154. package/src/services/tests/BranchService/publishVersion.test.js +0 -183
  155. package/src/services/tests/BranchService/renameBranch.test.js +0 -240
  156. package/src/services/tests/BranchService/resetBranch.test.js +0 -152
  157. package/src/services/tests/FeatureFlagService/adminFeatureFlags.test.js +0 -67
  158. package/src/services/tests/FeatureFlagService/getFeatureFlags.test.js +0 -75
  159. package/src/services/tests/FileService/createFileFormData.test.js +0 -74
  160. package/src/services/tests/FileService/getFileUrl.test.js +0 -69
  161. package/src/services/tests/FileService/updateProjectIcon.test.js +0 -109
  162. package/src/services/tests/FileService/uploadDocument.test.js +0 -36
  163. package/src/services/tests/FileService/uploadFile.test.js +0 -78
  164. package/src/services/tests/FileService/uploadFileWithValidation.test.js +0 -114
  165. package/src/services/tests/FileService/uploadImage.test.js +0 -36
  166. package/src/services/tests/FileService/uploadMultipleFiles.test.js +0 -111
  167. package/src/services/tests/FileService/validateFile.test.js +0 -63
  168. package/src/services/tests/PlanService/createPlan.test.js +0 -104
  169. package/src/services/tests/PlanService/createPlanWithValidation.test.js +0 -523
  170. package/src/services/tests/PlanService/deletePlan.test.js +0 -92
  171. package/src/services/tests/PlanService/getActivePlans.test.js +0 -123
  172. package/src/services/tests/PlanService/getAdminPlans.test.js +0 -84
  173. package/src/services/tests/PlanService/getPlan.test.js +0 -50
  174. package/src/services/tests/PlanService/getPlanByKey.test.js +0 -109
  175. package/src/services/tests/PlanService/getPlanWithValidation.test.js +0 -85
  176. package/src/services/tests/PlanService/getPlans.test.js +0 -53
  177. package/src/services/tests/PlanService/getPlansByPriceRange.test.js +0 -109
  178. package/src/services/tests/PlanService/getPlansWithValidation.test.js +0 -48
  179. package/src/services/tests/PlanService/initializePlans.test.js +0 -75
  180. package/src/services/tests/PlanService/updatePlan.test.js +0 -111
  181. package/src/services/tests/PlanService/updatePlanWithValidation.test.js +0 -556
  182. package/src/state/RootStateManager.js +0 -76
  183. package/src/state/rootEventBus.js +0 -67
  184. package/src/utils/CollabClient.js +0 -248
  185. package/src/utils/changePreprocessor.js +0 -239
  186. package/src/utils/jsonDiff.js +0 -144
  187. package/src/utils/ordering.js +0 -271
@@ -0,0 +1,252 @@
1
+ import config from '../config/environment.js'
2
+
3
+ const DEFAULT_OPTIONS = {
4
+ baseUrl: config.baseUrl
5
+ }
6
+
7
+ class SymstoryClient {
8
+ /**
9
+ * Creates an instance of SymstoryClient.
10
+ * @param {string} appKey - The application key.
11
+ * @param {object} [options={}] - The options for the client.
12
+ */
13
+ constructor (appKey, options = {}) {
14
+ if (!appKey) {
15
+ throw new Error('AppKey is required')
16
+ }
17
+ this.appKey = appKey
18
+ this.options = { ...DEFAULT_OPTIONS, ...options }
19
+ this.headers = {
20
+ 'Content-Type': 'application/json',
21
+ ...this.options.headers,
22
+ 'X-AppKey': appKey
23
+ }
24
+ }
25
+
26
+ /**
27
+ * Makes a request to the Symstory service.
28
+ * @param {string} [path=''] - The request path.
29
+ * @param {object} [options={}] - The request options.
30
+ * @returns {Promise<any>} - The response data.
31
+ */
32
+ async request (path = '', options = {}) {
33
+ const url = `${this.options.baseUrl}/symstory/${this.appKey}${path}`
34
+ const response = await fetch(url, {
35
+ ...options,
36
+ headers: { ...this.headers, ...options.headers }
37
+ })
38
+
39
+ if (!response.ok) {
40
+ const error = await response.json()
41
+ throw new Error(error.message || error.error || 'Request failed')
42
+ }
43
+
44
+ return response.status === 204 ? null : response.json()
45
+ }
46
+
47
+ /**
48
+ * Fetches project data.
49
+ * @param {object} query - The query object to filter data.
50
+ * @param {string|null} [branch=null] - The branch name.
51
+ * @param {string|null} [version=null] - The version number.
52
+ * @returns {Promise<any>} - The project data.
53
+ */
54
+ get (query, branch = null, version = null) {
55
+ const params = new URLSearchParams({
56
+ ...(branch && { branch }),
57
+ ...(version ? { version } : { cacheId: Math.random() }),
58
+ ...(query && { query: JSON.stringify(query) })
59
+ })
60
+ return this.request(`?${params}`)
61
+ }
62
+
63
+ /**
64
+ * Sets a value at the specified path.
65
+ * @param {string} path - The path where the value should be set.
66
+ * @param {any} value - The value to set.
67
+ * @returns {Promise<any>} - The response data.
68
+ */
69
+ set (path, value) {
70
+ return this.request('', {
71
+ method: 'POST',
72
+ body: JSON.stringify({ changes: [['update', path, value]] })
73
+ })
74
+ }
75
+
76
+ /**
77
+ * Updates project data.
78
+ * @param {Array} changes - The changes to apply.
79
+ * @param {object} [options={}] - The update options.
80
+ * @param {string} [options.type='patch'] - The type of update.
81
+ * @param {string} [options.message=''] - A message describing the update.
82
+ * @param {string} [options.branch='main'] - The branch name.
83
+ * @returns {Promise<any>} - The response data.
84
+ */
85
+ async update (
86
+ changes,
87
+ { type = 'patch', message = '', branch = 'main' } = {}
88
+ ) {
89
+ return await this.request('', {
90
+ method: 'POST',
91
+ body: JSON.stringify({ changes, type, message, branch })
92
+ })
93
+ }
94
+
95
+ /**
96
+ * Retrieves all branches of the project.
97
+ * @returns {Promise<any>} - The branches data.
98
+ */
99
+ getBranches () {
100
+ return this.request('/branches')
101
+ }
102
+
103
+ /**
104
+ * Creates a new branch.
105
+ * @param {string} branch - The name of the new branch.
106
+ * @param {object} [options={}] - The branch creation options.
107
+ * @param {string} [options.message] - A message describing the branch creation.
108
+ * @param {string} [options.source='main'] - The source branch.
109
+ * @param {string} [options.version] - The version number.
110
+ * @returns {Promise<any>} - The response data.
111
+ */
112
+ createBranch (branch, { message, source = 'main', version } = {}) {
113
+ return this.request('/branch', {
114
+ method: 'POST',
115
+ body: JSON.stringify({ branch, message, source, version })
116
+ })
117
+ }
118
+
119
+ /**
120
+ * Edit an existing branch. (For now only supports branch renaming)
121
+ * @param {string} branch - The current name of the branch to edit.
122
+ * @param {object} [options={}] - The branch edit options.
123
+ * @param {string} [options.name] - New name for the branch
124
+ * @returns {Promise<any>} - The response data.
125
+ */
126
+ editBranch (branch, { name } = {}) {
127
+ return this.request('/branch', {
128
+ method: 'PATCH',
129
+ body: JSON.stringify({ branch, name })
130
+ })
131
+ }
132
+
133
+ /**
134
+ * Delete an existing branch.
135
+ * @param {string} branch - The name of the branch to delete.
136
+ * @returns {Promise<any>} - The response data.
137
+ */
138
+ deleteBranch (branch) {
139
+ return this.request('/branch', {
140
+ method: 'DELETE',
141
+ body: JSON.stringify({ branch })
142
+ })
143
+ }
144
+
145
+ /**
146
+ * Merges a branch into the target branch.
147
+ * @param {string} target - The target branch.
148
+ * @param {object} [options={}] - The merge options.
149
+ * @param {string} [options.message] - A message describing the merge.
150
+ * @param {string} [options.source='main'] - The source branch.
151
+ * @param {string} [options.type='patch'] - The type of merge.
152
+ * @param {string} [options.version] - The version number.
153
+ * @param {boolean} [options.commit='false'] - Whether to commit the merge.
154
+ * @param {Array} [options.changes] - The changes to apply during the merge.
155
+ * @returns {Promise<any>} - The response data.
156
+ */
157
+ mergeBranch (
158
+ target,
159
+ {
160
+ message,
161
+ source = 'main',
162
+ type = 'patch',
163
+ version,
164
+ commit = 'false',
165
+ changes
166
+ } = {}
167
+ ) {
168
+ return this.request('/merge', {
169
+ method: 'POST',
170
+ body: JSON.stringify({
171
+ target,
172
+ source,
173
+ message,
174
+ type,
175
+ version,
176
+ commit,
177
+ changes
178
+ })
179
+ })
180
+ }
181
+
182
+ /**
183
+ * Restores an older version of the project.
184
+ * @param {string} version - The version number to restore.
185
+ * @param {object} [options={}] - The restore options.
186
+ * @param {string} [options.branch='main'] - The branch name.
187
+ * @param {string} [options.type='patch'] - The type of restore.
188
+ * @param {string} [options.message] - A message describing the restore.
189
+ * @returns {Promise<any>} - The response data.
190
+ */
191
+ restoreVersion (version, { branch = 'main', type = 'patch', message } = {}) {
192
+ return this.request('/restore', {
193
+ method: 'POST',
194
+ body: JSON.stringify({ branch, version, type, message })
195
+ })
196
+ }
197
+
198
+ /**
199
+ * Publishes an existing version of the project.
200
+ * @param {string} version - The version ID/number to publish.
201
+ * @param {object} [options={}] - The publishing options.
202
+ * @param {string} [options.branch='main'] - The branch name. (Only if version number is provided)
203
+ * @returns {Promise<any>} - The response data.
204
+ */
205
+ publishVersion (version, { branch = 'main' } = {}) {
206
+ return this.request('/publish', {
207
+ method: 'POST',
208
+ body: JSON.stringify({ version })
209
+ })
210
+ }
211
+
212
+ /**
213
+ * Retrieves all changes after a specific version.
214
+ * @param {object} [options={}] - The changes options.
215
+ * @param {string} [options.versionId] - The version ID to publish.
216
+ * @param {string} [options.versionValue] - The version ID to publish. (alternative to versionId)
217
+ * @param {string} [options.branch] - The branch to publish (Only in combination to versionValue)
218
+ * @returns {Promise<any>} - The changes data.
219
+ */
220
+ getChanges ({ versionId, versionValue, branch } = {}) {
221
+ return this.request(
222
+ '/changes?' +
223
+ new URLSearchParams({
224
+ ...(versionId ? { versionId } : {}),
225
+ ...(versionValue ? { versionValue } : {}),
226
+ ...(branch ? { branch } : {})
227
+ }).toString(),
228
+ {}
229
+ )
230
+ }
231
+ }
232
+
233
+ export default {
234
+ /**
235
+ * Creates a new SymstoryClient instance.
236
+ * @param {string} appKey - The application key.
237
+ * @param {object} options - The options for the client.
238
+ * @returns {SymstoryClient} - The SymstoryClient instance.
239
+ */
240
+ create: (appKey, options) => new SymstoryClient(appKey, options),
241
+
242
+ /**
243
+ * Initializes the Symstory client.
244
+ * @param {string} appKey - The application key.
245
+ * @param {object} options - The options for the client.
246
+ * @returns {SymstoryClient} - The initialized SymstoryClient instance.
247
+ */
248
+ init (appKey, options) {
249
+ this.client = this.create(appKey, options)
250
+ return this
251
+ }
252
+ }
@@ -185,6 +185,9 @@ export const validateParams = {
185
185
  if (!data.key) {
186
186
  throw new Error('Data must contain a key property')
187
187
  }
188
+ if (!data.value) {
189
+ throw new Error('Data must contain a value property')
190
+ }
188
191
 
189
192
  // Check key against reserved keywords for code-related types
190
193
  if (CODE_TYPES.includes(type)) {
@@ -1,351 +0,0 @@
1
- var __defProp = Object.defineProperty;
2
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
- var __getOwnPropNames = Object.getOwnPropertyNames;
4
- var __hasOwnProp = Object.prototype.hasOwnProperty;
5
- var __export = (target, all) => {
6
- for (var name in all)
7
- __defProp(target, name, { get: all[name], enumerable: true });
8
- };
9
- var __copyProps = (to, from, except, desc) => {
10
- if (from && typeof from === "object" || typeof from === "function") {
11
- for (let key of __getOwnPropNames(from))
12
- if (!__hasOwnProp.call(to, key) && key !== except)
13
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
- }
15
- return to;
16
- };
17
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
- var AdminService_exports = {};
19
- __export(AdminService_exports, {
20
- AdminService: () => AdminService
21
- });
22
- module.exports = __toCommonJS(AdminService_exports);
23
- var import_BaseService = require("./BaseService.js");
24
- class AdminService extends import_BaseService.BaseService {
25
- // ==================== ADMIN METHODS ====================
26
- /**
27
- * Get admin users list with comprehensive filtering and search capabilities
28
- * Requires admin or super_admin global role
29
- */
30
- async getAdminUsers(params = {}) {
31
- this._requireReady("getAdminUsers");
32
- const {
33
- emails,
34
- ids,
35
- query,
36
- status,
37
- page = 1,
38
- limit = 50,
39
- sort = { field: "createdAt", order: "desc" }
40
- } = params;
41
- const queryParams = new URLSearchParams();
42
- if (emails) {
43
- queryParams.append("emails", emails);
44
- }
45
- if (ids) {
46
- queryParams.append("ids", ids);
47
- }
48
- if (query) {
49
- queryParams.append("query", query);
50
- }
51
- if (status) {
52
- queryParams.append("status", status);
53
- }
54
- if (page) {
55
- queryParams.append("page", page.toString());
56
- }
57
- if (limit) {
58
- queryParams.append("limit", limit.toString());
59
- }
60
- if (sort && sort.field) {
61
- queryParams.append("sort[field]", sort.field);
62
- queryParams.append("sort[order]", sort.order || "desc");
63
- }
64
- const queryString = queryParams.toString();
65
- const url = `/users/admin/users${queryString ? `?${queryString}` : ""}`;
66
- try {
67
- const response = await this._request(url, {
68
- method: "GET",
69
- methodName: "getAdminUsers"
70
- });
71
- if (response.success) {
72
- return response.data;
73
- }
74
- throw new Error(response.message);
75
- } catch (error) {
76
- throw new Error(`Failed to get admin users: ${error.message}`, { cause: error });
77
- }
78
- }
79
- /**
80
- * Assign projects to a specific user
81
- * Requires admin or super_admin global role
82
- */
83
- async assignProjectsToUser(userId, options = {}) {
84
- this._requireReady("assignProjectsToUser");
85
- if (!userId) {
86
- throw new Error("User ID is required");
87
- }
88
- const { projectIds, role = "guest" } = options;
89
- const requestBody = {
90
- userId,
91
- role
92
- };
93
- if (projectIds && Array.isArray(projectIds)) {
94
- requestBody.projectIds = projectIds;
95
- }
96
- try {
97
- const response = await this._request("/assign-projects", {
98
- method: "POST",
99
- body: JSON.stringify(requestBody),
100
- methodName: "assignProjectsToUser"
101
- });
102
- if (response.success) {
103
- return response.data;
104
- }
105
- throw new Error(response.message);
106
- } catch (error) {
107
- throw new Error(`Failed to assign projects to user: ${error.message}`, { cause: error });
108
- }
109
- }
110
- /**
111
- * Update user information (admin only)
112
- */
113
- async updateUser(userId, userData) {
114
- var _a;
115
- this._requireReady("updateUser");
116
- if (!userId) {
117
- throw new Error("User ID is required");
118
- }
119
- if (!userData || typeof userData !== "object" || Object.keys(userData).length === 0) {
120
- throw new Error("userData must be a non-empty object");
121
- }
122
- try {
123
- const response = await this._request(`/users/${userId}`, {
124
- method: "PATCH",
125
- body: JSON.stringify(userData),
126
- methodName: "updateUser"
127
- });
128
- if (response.success) {
129
- return response.data;
130
- }
131
- throw new Error(response.message);
132
- } catch (error) {
133
- if ((_a = error.message) == null ? void 0 : _a.includes("Duplicate")) {
134
- throw new Error("Username already exists");
135
- }
136
- throw new Error(`Failed to update user: ${error.message}`, { cause: error });
137
- }
138
- }
139
- // ==================== ADMIN HELPER METHODS ====================
140
- /**
141
- * Helper method for admin users search
142
- */
143
- async searchAdminUsers(searchQuery, options = {}) {
144
- return await this.getAdminUsers({
145
- query: searchQuery,
146
- ...options
147
- });
148
- }
149
- /**
150
- * Helper method to get admin users by email list
151
- */
152
- async getAdminUsersByEmails(emails, options = {}) {
153
- const emailList = Array.isArray(emails) ? emails.join(",") : emails;
154
- return await this.getAdminUsers({
155
- emails: emailList,
156
- ...options
157
- });
158
- }
159
- /**
160
- * Helper method to get admin users by ID list
161
- */
162
- async getAdminUsersByIds(ids, options = {}) {
163
- const idList = Array.isArray(ids) ? ids.join(",") : ids;
164
- return await this.getAdminUsers({
165
- ids: idList,
166
- ...options
167
- });
168
- }
169
- /**
170
- * Helper method to assign specific projects to a user with a specific role
171
- */
172
- async assignSpecificProjectsToUser(userId, projectIds, role = "guest") {
173
- if (!Array.isArray(projectIds) || projectIds.length === 0) {
174
- throw new Error("Project IDs must be a non-empty array");
175
- }
176
- return await this.assignProjectsToUser(userId, {
177
- projectIds,
178
- role
179
- });
180
- }
181
- /**
182
- * Helper method to assign all projects to a user with a specific role
183
- */
184
- async assignAllProjectsToUser(userId, role = "guest") {
185
- return await this.assignProjectsToUser(userId, {
186
- role
187
- });
188
- }
189
- /**
190
- * Helper method to validate user data for updates
191
- */
192
- validateUserData(userData) {
193
- const errors = [];
194
- if (!userData || typeof userData !== "object") {
195
- errors.push("User data must be an object");
196
- return { isValid: false, errors };
197
- }
198
- if (userData.email && typeof userData.email !== "string") {
199
- errors.push("Email must be a string");
200
- } else if (userData.email && !this._isValidEmail(userData.email)) {
201
- errors.push("Email must be a valid email address");
202
- }
203
- if (userData.username && typeof userData.username !== "string") {
204
- errors.push("Username must be a string");
205
- } else if (userData.username && userData.username.length < 3) {
206
- errors.push("Username must be at least 3 characters long");
207
- }
208
- if (userData.role && !["admin", "user", "guest"].includes(userData.role)) {
209
- errors.push("Role must be one of: admin, user, guest");
210
- }
211
- if (userData.status && !["active", "inactive", "suspended"].includes(userData.status)) {
212
- errors.push("Status must be one of: active, inactive, suspended");
213
- }
214
- return {
215
- isValid: errors.length === 0,
216
- errors
217
- };
218
- }
219
- /**
220
- * Helper method to update user with validation
221
- */
222
- async updateUserWithValidation(userId, userData) {
223
- const validation = this.validateUserData(userData);
224
- if (!validation.isValid) {
225
- throw new Error(`Validation failed: ${validation.errors.join(", ")}`);
226
- }
227
- return await this.updateUser(userId, userData);
228
- }
229
- /**
230
- * Helper method to get user statistics
231
- */
232
- async getUserStats() {
233
- try {
234
- const users = await this.getAdminUsers({ limit: 1e3 });
235
- const stats = {
236
- total: users.length,
237
- active: 0,
238
- inactive: 0,
239
- suspended: 0,
240
- admins: 0,
241
- users: 0,
242
- guests: 0
243
- };
244
- users.forEach((user) => {
245
- if (user.status === "active") {
246
- stats.active++;
247
- }
248
- if (user.status === "inactive") {
249
- stats.inactive++;
250
- }
251
- if (user.status === "suspended") {
252
- stats.suspended++;
253
- }
254
- if (user.role === "admin") {
255
- stats.admins++;
256
- }
257
- if (user.role === "user") {
258
- stats.users++;
259
- }
260
- if (user.role === "guest") {
261
- stats.guests++;
262
- }
263
- });
264
- return stats;
265
- } catch (error) {
266
- throw new Error(`Failed to get user stats: ${error.message}`, { cause: error });
267
- }
268
- }
269
- /**
270
- * Helper method to bulk update users
271
- */
272
- async bulkUpdateUsers(userUpdates) {
273
- if (!Array.isArray(userUpdates) || userUpdates.length === 0) {
274
- throw new Error("User updates must be a non-empty array");
275
- }
276
- const updatePromises = userUpdates.map(async (update) => {
277
- try {
278
- const result = await this.updateUser(update.userId, update.userData);
279
- return {
280
- userId: update.userId,
281
- success: true,
282
- data: result
283
- };
284
- } catch (error) {
285
- return {
286
- userId: update.userId,
287
- success: false,
288
- error: error.message
289
- };
290
- }
291
- });
292
- const results = await Promise.all(updatePromises);
293
- return {
294
- total: results.length,
295
- successful: results.filter((r) => r.success).length,
296
- failed: results.filter((r) => !r.success).length,
297
- results
298
- };
299
- }
300
- /**
301
- * Helper method to get users by role
302
- */
303
- async getUsersByRole(role, options = {}) {
304
- const users = await this.getAdminUsers(options);
305
- return users.filter((user) => user.role === role);
306
- }
307
- /**
308
- * Helper method to get users by status
309
- */
310
- async getUsersByStatus(status, options = {}) {
311
- const users = await this.getAdminUsers(options);
312
- return users.filter((user) => user.status === status);
313
- }
314
- /**
315
- * Helper method to activate a user
316
- */
317
- async activateUser(userId) {
318
- return await this.updateUser(userId, { status: "active" });
319
- }
320
- /**
321
- * Helper method to deactivate a user
322
- */
323
- async deactivateUser(userId) {
324
- return await this.updateUser(userId, { status: "inactive" });
325
- }
326
- /**
327
- * Helper method to suspend a user
328
- */
329
- async suspendUser(userId) {
330
- return await this.updateUser(userId, { status: "suspended" });
331
- }
332
- /**
333
- * Helper method to promote user to admin
334
- */
335
- async promoteToAdmin(userId) {
336
- return await this.updateUser(userId, { role: "admin" });
337
- }
338
- /**
339
- * Helper method to demote user from admin
340
- */
341
- async demoteFromAdmin(userId) {
342
- return await this.updateUser(userId, { role: "user" });
343
- }
344
- /**
345
- * Private helper to validate email format
346
- */
347
- _isValidEmail(email) {
348
- const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/u;
349
- return emailRegex.test(email);
350
- }
351
- }