@memberjunction/actions-bizapps-lms 5.3.0 → 5.4.0

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 (194) hide show
  1. package/.turbo/turbo-build.log +1 -1
  2. package/CHANGELOG.md +26 -0
  3. package/LEARNWORLDS_SIDECAR_PLAN.md +845 -0
  4. package/dist/base/base-lms.action.d.ts +26 -4
  5. package/dist/base/base-lms.action.d.ts.map +1 -1
  6. package/dist/base/base-lms.action.js +73 -17
  7. package/dist/base/base-lms.action.js.map +1 -1
  8. package/dist/providers/learnworlds/actions/attach-tags.action.d.ts +26 -0
  9. package/dist/providers/learnworlds/actions/attach-tags.action.d.ts.map +1 -0
  10. package/dist/providers/learnworlds/actions/attach-tags.action.js +79 -0
  11. package/dist/providers/learnworlds/actions/attach-tags.action.js.map +1 -0
  12. package/dist/providers/learnworlds/actions/create-user.action.d.ts +24 -1
  13. package/dist/providers/learnworlds/actions/create-user.action.d.ts.map +1 -1
  14. package/dist/providers/learnworlds/actions/create-user.action.js +166 -142
  15. package/dist/providers/learnworlds/actions/create-user.action.js.map +1 -1
  16. package/dist/providers/learnworlds/actions/detach-tags.action.d.ts +26 -0
  17. package/dist/providers/learnworlds/actions/detach-tags.action.d.ts.map +1 -0
  18. package/dist/providers/learnworlds/actions/detach-tags.action.js +79 -0
  19. package/dist/providers/learnworlds/actions/detach-tags.action.js.map +1 -0
  20. package/dist/providers/learnworlds/actions/enroll-user.action.d.ts +31 -3
  21. package/dist/providers/learnworlds/actions/enroll-user.action.d.ts.map +1 -1
  22. package/dist/providers/learnworlds/actions/enroll-user.action.js +179 -125
  23. package/dist/providers/learnworlds/actions/enroll-user.action.js.map +1 -1
  24. package/dist/providers/learnworlds/actions/get-bulk-data.action.d.ts +79 -0
  25. package/dist/providers/learnworlds/actions/get-bulk-data.action.d.ts.map +1 -0
  26. package/dist/providers/learnworlds/actions/get-bulk-data.action.js +359 -0
  27. package/dist/providers/learnworlds/actions/get-bulk-data.action.js.map +1 -0
  28. package/dist/providers/learnworlds/actions/get-bundles.action.d.ts +34 -0
  29. package/dist/providers/learnworlds/actions/get-bundles.action.d.ts.map +1 -0
  30. package/dist/providers/learnworlds/actions/get-bundles.action.js +107 -0
  31. package/dist/providers/learnworlds/actions/get-bundles.action.js.map +1 -0
  32. package/dist/providers/learnworlds/actions/get-certificates.action.d.ts +33 -2
  33. package/dist/providers/learnworlds/actions/get-certificates.action.d.ts.map +1 -1
  34. package/dist/providers/learnworlds/actions/get-certificates.action.js +311 -244
  35. package/dist/providers/learnworlds/actions/get-certificates.action.js.map +1 -1
  36. package/dist/providers/learnworlds/actions/get-course-analytics.action.d.ts +14 -14
  37. package/dist/providers/learnworlds/actions/get-course-analytics.action.d.ts.map +1 -1
  38. package/dist/providers/learnworlds/actions/get-course-analytics.action.js +234 -226
  39. package/dist/providers/learnworlds/actions/get-course-analytics.action.js.map +1 -1
  40. package/dist/providers/learnworlds/actions/get-course-details.action.d.ts +42 -6
  41. package/dist/providers/learnworlds/actions/get-course-details.action.d.ts.map +1 -1
  42. package/dist/providers/learnworlds/actions/get-course-details.action.js +182 -148
  43. package/dist/providers/learnworlds/actions/get-course-details.action.js.map +1 -1
  44. package/dist/providers/learnworlds/actions/get-courses.action.d.ts +29 -82
  45. package/dist/providers/learnworlds/actions/get-courses.action.d.ts.map +1 -1
  46. package/dist/providers/learnworlds/actions/get-courses.action.js +188 -207
  47. package/dist/providers/learnworlds/actions/get-courses.action.js.map +1 -1
  48. package/dist/providers/learnworlds/actions/get-quiz-results.action.d.ts +22 -2
  49. package/dist/providers/learnworlds/actions/get-quiz-results.action.d.ts.map +1 -1
  50. package/dist/providers/learnworlds/actions/get-quiz-results.action.js +316 -294
  51. package/dist/providers/learnworlds/actions/get-quiz-results.action.js.map +1 -1
  52. package/dist/providers/learnworlds/actions/get-user-details.action.d.ts +23 -59
  53. package/dist/providers/learnworlds/actions/get-user-details.action.d.ts.map +1 -1
  54. package/dist/providers/learnworlds/actions/get-user-details.action.js +113 -114
  55. package/dist/providers/learnworlds/actions/get-user-details.action.js.map +1 -1
  56. package/dist/providers/learnworlds/actions/get-user-enrollments.action.d.ts +43 -5
  57. package/dist/providers/learnworlds/actions/get-user-enrollments.action.d.ts.map +1 -1
  58. package/dist/providers/learnworlds/actions/get-user-enrollments.action.js +191 -147
  59. package/dist/providers/learnworlds/actions/get-user-enrollments.action.js.map +1 -1
  60. package/dist/providers/learnworlds/actions/get-user-progress.action.d.ts +21 -92
  61. package/dist/providers/learnworlds/actions/get-user-progress.action.d.ts.map +1 -1
  62. package/dist/providers/learnworlds/actions/get-user-progress.action.js +145 -166
  63. package/dist/providers/learnworlds/actions/get-user-progress.action.js.map +1 -1
  64. package/dist/providers/learnworlds/actions/get-users.action.d.ts +14 -28
  65. package/dist/providers/learnworlds/actions/get-users.action.d.ts.map +1 -1
  66. package/dist/providers/learnworlds/actions/get-users.action.js +122 -143
  67. package/dist/providers/learnworlds/actions/get-users.action.js.map +1 -1
  68. package/dist/providers/learnworlds/actions/index.d.ts +7 -0
  69. package/dist/providers/learnworlds/actions/index.d.ts.map +1 -1
  70. package/dist/providers/learnworlds/actions/index.js +7 -0
  71. package/dist/providers/learnworlds/actions/index.js.map +1 -1
  72. package/dist/providers/learnworlds/actions/onboard-learner.action.d.ts +53 -0
  73. package/dist/providers/learnworlds/actions/onboard-learner.action.d.ts.map +1 -0
  74. package/dist/providers/learnworlds/actions/onboard-learner.action.js +236 -0
  75. package/dist/providers/learnworlds/actions/onboard-learner.action.js.map +1 -0
  76. package/dist/providers/learnworlds/actions/sso-login.action.d.ts +35 -0
  77. package/dist/providers/learnworlds/actions/sso-login.action.d.ts.map +1 -0
  78. package/dist/providers/learnworlds/actions/sso-login.action.js +101 -0
  79. package/dist/providers/learnworlds/actions/sso-login.action.js.map +1 -0
  80. package/dist/providers/learnworlds/actions/update-user-progress.action.d.ts +12 -1
  81. package/dist/providers/learnworlds/actions/update-user-progress.action.d.ts.map +1 -1
  82. package/dist/providers/learnworlds/actions/update-user-progress.action.js +167 -210
  83. package/dist/providers/learnworlds/actions/update-user-progress.action.js.map +1 -1
  84. package/dist/providers/learnworlds/actions/update-user.action.d.ts +39 -0
  85. package/dist/providers/learnworlds/actions/update-user.action.d.ts.map +1 -0
  86. package/dist/providers/learnworlds/actions/update-user.action.js +168 -0
  87. package/dist/providers/learnworlds/actions/update-user.action.js.map +1 -0
  88. package/dist/providers/learnworlds/index.d.ts +1 -0
  89. package/dist/providers/learnworlds/index.d.ts.map +1 -1
  90. package/dist/providers/learnworlds/index.js +2 -0
  91. package/dist/providers/learnworlds/index.js.map +1 -1
  92. package/dist/providers/learnworlds/interfaces/bundle.types.d.ts +16 -0
  93. package/dist/providers/learnworlds/interfaces/bundle.types.d.ts.map +1 -0
  94. package/dist/providers/learnworlds/interfaces/bundle.types.js +2 -0
  95. package/dist/providers/learnworlds/interfaces/bundle.types.js.map +1 -0
  96. package/dist/providers/learnworlds/interfaces/certificate.types.d.ts +66 -0
  97. package/dist/providers/learnworlds/interfaces/certificate.types.d.ts.map +1 -0
  98. package/dist/providers/learnworlds/interfaces/certificate.types.js +2 -0
  99. package/dist/providers/learnworlds/interfaces/certificate.types.js.map +1 -0
  100. package/dist/providers/learnworlds/interfaces/common.types.d.ts +221 -0
  101. package/dist/providers/learnworlds/interfaces/common.types.d.ts.map +1 -0
  102. package/dist/providers/learnworlds/interfaces/common.types.js +2 -0
  103. package/dist/providers/learnworlds/interfaces/common.types.js.map +1 -0
  104. package/dist/providers/learnworlds/interfaces/course.types.d.ts +393 -0
  105. package/dist/providers/learnworlds/interfaces/course.types.d.ts.map +1 -0
  106. package/dist/providers/learnworlds/interfaces/course.types.js +2 -0
  107. package/dist/providers/learnworlds/interfaces/course.types.js.map +1 -0
  108. package/dist/providers/learnworlds/interfaces/enrollment.types.d.ts +165 -0
  109. package/dist/providers/learnworlds/interfaces/enrollment.types.d.ts.map +1 -0
  110. package/dist/providers/learnworlds/interfaces/enrollment.types.js +2 -0
  111. package/dist/providers/learnworlds/interfaces/enrollment.types.js.map +1 -0
  112. package/dist/providers/learnworlds/interfaces/index.d.ts +12 -0
  113. package/dist/providers/learnworlds/interfaces/index.d.ts.map +1 -0
  114. package/dist/providers/learnworlds/interfaces/index.js +13 -0
  115. package/dist/providers/learnworlds/interfaces/index.js.map +1 -0
  116. package/dist/providers/learnworlds/interfaces/onboarding.types.d.ts +39 -0
  117. package/dist/providers/learnworlds/interfaces/onboarding.types.d.ts.map +1 -0
  118. package/dist/providers/learnworlds/interfaces/onboarding.types.js +2 -0
  119. package/dist/providers/learnworlds/interfaces/onboarding.types.js.map +1 -0
  120. package/dist/providers/learnworlds/interfaces/quiz.types.d.ts +108 -0
  121. package/dist/providers/learnworlds/interfaces/quiz.types.d.ts.map +1 -0
  122. package/dist/providers/learnworlds/interfaces/quiz.types.js +2 -0
  123. package/dist/providers/learnworlds/interfaces/quiz.types.js.map +1 -0
  124. package/dist/providers/learnworlds/interfaces/sso.types.d.ts +18 -0
  125. package/dist/providers/learnworlds/interfaces/sso.types.d.ts.map +1 -0
  126. package/dist/providers/learnworlds/interfaces/sso.types.js +2 -0
  127. package/dist/providers/learnworlds/interfaces/sso.types.js.map +1 -0
  128. package/dist/providers/learnworlds/interfaces/sync.types.d.ts +44 -0
  129. package/dist/providers/learnworlds/interfaces/sync.types.d.ts.map +1 -0
  130. package/dist/providers/learnworlds/interfaces/sync.types.js +2 -0
  131. package/dist/providers/learnworlds/interfaces/sync.types.js.map +1 -0
  132. package/dist/providers/learnworlds/interfaces/tag.types.d.ts +17 -0
  133. package/dist/providers/learnworlds/interfaces/tag.types.d.ts.map +1 -0
  134. package/dist/providers/learnworlds/interfaces/tag.types.js +2 -0
  135. package/dist/providers/learnworlds/interfaces/tag.types.js.map +1 -0
  136. package/dist/providers/learnworlds/interfaces/user.types.d.ts +198 -0
  137. package/dist/providers/learnworlds/interfaces/user.types.d.ts.map +1 -0
  138. package/dist/providers/learnworlds/interfaces/user.types.js +2 -0
  139. package/dist/providers/learnworlds/interfaces/user.types.js.map +1 -0
  140. package/dist/providers/learnworlds/learnworlds-base.action.d.ts +111 -7
  141. package/dist/providers/learnworlds/learnworlds-base.action.d.ts.map +1 -1
  142. package/dist/providers/learnworlds/learnworlds-base.action.js +293 -56
  143. package/dist/providers/learnworlds/learnworlds-base.action.js.map +1 -1
  144. package/package.json +8 -7
  145. package/src/__tests__/attach-tags.action.test.ts +137 -0
  146. package/src/__tests__/create-user.action.test.ts +311 -0
  147. package/src/__tests__/detach-tags.action.test.ts +136 -0
  148. package/src/__tests__/enroll-user.action.test.ts +283 -0
  149. package/src/__tests__/get-bulk-data.action.test.ts +649 -0
  150. package/src/__tests__/get-bundles.action.test.ts +215 -0
  151. package/src/__tests__/lms.test.ts +275 -184
  152. package/src/__tests__/onboard-learner.action.test.ts +455 -0
  153. package/src/__tests__/sso-login.action.test.ts +256 -0
  154. package/src/__tests__/update-user.action.test.ts +350 -0
  155. package/src/base/base-lms.action.ts +226 -163
  156. package/src/providers/learnworlds/actions/attach-tags.action.ts +90 -0
  157. package/src/providers/learnworlds/actions/create-user.action.ts +291 -235
  158. package/src/providers/learnworlds/actions/detach-tags.action.ts +90 -0
  159. package/src/providers/learnworlds/actions/enroll-user.action.ts +326 -220
  160. package/src/providers/learnworlds/actions/get-bulk-data.action.ts +438 -0
  161. package/src/providers/learnworlds/actions/get-bundles.action.ts +139 -0
  162. package/src/providers/learnworlds/actions/get-certificates.action.ts +499 -309
  163. package/src/providers/learnworlds/actions/get-course-analytics.action.ts +470 -337
  164. package/src/providers/learnworlds/actions/get-course-details.action.ts +395 -249
  165. package/src/providers/learnworlds/actions/get-courses.action.ts +508 -565
  166. package/src/providers/learnworlds/actions/get-quiz-results.action.ts +606 -381
  167. package/src/providers/learnworlds/actions/get-user-details.action.ts +335 -349
  168. package/src/providers/learnworlds/actions/get-user-enrollments.action.ts +328 -228
  169. package/src/providers/learnworlds/actions/get-user-progress.action.ts +522 -571
  170. package/src/providers/learnworlds/actions/get-users.action.ts +231 -282
  171. package/src/providers/learnworlds/actions/index.ts +8 -1
  172. package/src/providers/learnworlds/actions/onboard-learner.action.ts +286 -0
  173. package/src/providers/learnworlds/actions/sso-login.action.ts +116 -0
  174. package/src/providers/learnworlds/actions/update-user-progress.action.ts +301 -263
  175. package/src/providers/learnworlds/actions/update-user.action.ts +181 -0
  176. package/src/providers/learnworlds/index.ts +4 -1
  177. package/src/providers/learnworlds/interfaces/bundle.types.ts +17 -0
  178. package/src/providers/learnworlds/interfaces/certificate.types.ts +69 -0
  179. package/src/providers/learnworlds/interfaces/common.types.ts +233 -0
  180. package/src/providers/learnworlds/interfaces/course.types.ts +411 -0
  181. package/src/providers/learnworlds/interfaces/enrollment.types.ts +176 -0
  182. package/src/providers/learnworlds/interfaces/index.ts +12 -0
  183. package/src/providers/learnworlds/interfaces/onboarding.types.ts +41 -0
  184. package/src/providers/learnworlds/interfaces/quiz.types.ts +118 -0
  185. package/src/providers/learnworlds/interfaces/sso.types.ts +19 -0
  186. package/src/providers/learnworlds/interfaces/sync.types.ts +46 -0
  187. package/src/providers/learnworlds/interfaces/tag.types.ts +18 -0
  188. package/src/providers/learnworlds/interfaces/user.types.ts +204 -0
  189. package/src/providers/learnworlds/learnworlds-base.action.ts +468 -210
  190. package/tsconfig.json +1 -1
  191. package/dist/__tests__/lms.test.d.ts +0 -2
  192. package/dist/__tests__/lms.test.d.ts.map +0 -1
  193. package/dist/__tests__/lms.test.js +0 -232
  194. package/dist/__tests__/lms.test.js.map +0 -1
@@ -2,249 +2,305 @@ import { RegisterClass } from '@memberjunction/global';
2
2
  import { LearnWorldsBaseAction } from '../learnworlds-base.action';
3
3
  import { ActionParam, ActionResultSimple, RunActionParams } from '@memberjunction/actions-base';
4
4
  import { BaseAction } from '@memberjunction/actions';
5
+ import { UserInfo } from '@memberjunction/core';
6
+ import { CreateUserParams, CreateUserResult, CreateUserDetails, CreateUserEnrollmentResult, CreateUserSummary, LWApiUser } from '../interfaces';
7
+
8
+ /**
9
+ * Shape returned by the LearnWorlds POST /users endpoint
10
+ */
11
+ interface LWCreateUserResponse {
12
+ id: string;
13
+ email: string;
14
+ username?: string;
15
+ first_name?: string;
16
+ last_name?: string;
17
+ role?: string;
18
+ is_active?: boolean;
19
+ tags?: string[];
20
+ custom_fields?: Record<string, unknown>;
21
+ created_at?: string;
22
+ login_url?: string;
23
+ reset_password_url?: string;
24
+ }
25
+
26
+ /**
27
+ * Shape returned by the LearnWorlds POST /courses/{id}/enrollments endpoint
28
+ */
29
+ interface LWEnrollmentResponse {
30
+ id?: string;
31
+ }
5
32
 
6
33
  /**
7
34
  * Action to create a new user in LearnWorlds
8
35
  */
9
36
  @RegisterClass(BaseAction, 'CreateUserAction')
10
37
  export class CreateUserAction extends LearnWorldsBaseAction {
11
- /**
12
- * Create a new user
13
- */
14
- protected async InternalRunAction(params: RunActionParams): Promise<ActionResultSimple> {
15
- const { Params, ContextUser } = params;
16
- this.params = Params; // Set params for base class to use
17
-
18
- try {
19
- // Extract and validate parameters
20
- const email = this.getParamValue(Params, 'Email');
21
- const username = this.getParamValue(Params, 'Username');
22
- const password = this.getParamValue(Params, 'Password');
23
- const firstName = this.getParamValue(Params, 'FirstName');
24
- const lastName = this.getParamValue(Params, 'LastName');
25
- const role = this.getParamValue(Params, 'Role') || 'student';
26
- const isActive = this.getParamValue(Params, 'IsActive') !== false;
27
- const sendWelcomeEmail = this.getParamValue(Params, 'SendWelcomeEmail') !== false;
28
- const tags = this.getParamValue(Params, 'Tags');
29
- const customFields = this.getParamValue(Params, 'CustomFields');
30
- const enrollInCourses = this.getParamValue(Params, 'EnrollInCourses');
31
-
32
- if (!email) {
33
- return {
34
- Success: false,
35
- ResultCode: 'VALIDATION_ERROR',
36
- Message: 'Email is required',
37
- Params
38
- };
39
- }
40
-
41
- // Prepare user data
42
- const userData: any = {
43
- email: email,
44
- role: role,
45
- is_active: isActive
46
- };
47
-
48
- // Add optional fields
49
- if (username) userData.username = username;
50
- if (password) userData.password = password;
51
- if (firstName) userData.first_name = firstName;
52
- if (lastName) userData.last_name = lastName;
53
- if (sendWelcomeEmail !== undefined) userData.send_welcome_email = sendWelcomeEmail;
54
-
55
- // Add tags if provided (expecting comma-separated string or array)
56
- if (tags) {
57
- userData.tags = Array.isArray(tags) ? tags : tags.split(',').map((t: string) => t.trim());
58
- }
59
-
60
- // Add custom fields if provided
61
- if (customFields) {
62
- userData.custom_fields = customFields;
63
- }
64
-
65
- // Create user
66
- const newUser = await this.makeLearnWorldsRequest<any>(
67
- 'users',
68
- 'POST',
69
- userData,
70
- ContextUser
71
- );
72
-
73
- // Format user details
74
- const userDetails = {
75
- id: newUser.id,
76
- email: newUser.email,
77
- username: newUser.username,
78
- firstName: newUser.first_name,
79
- lastName: newUser.last_name,
80
- fullName: `${newUser.first_name || ''} ${newUser.last_name || ''}`.trim(),
81
- role: newUser.role,
82
- status: newUser.is_active ? 'active' : 'inactive',
83
- tags: newUser.tags || [],
84
- customFields: newUser.custom_fields || {},
85
- createdAt: newUser.created_at,
86
- loginUrl: newUser.login_url,
87
- resetPasswordUrl: newUser.reset_password_url
88
- };
89
-
90
- // Enroll in courses if requested
91
- const enrollmentResults: any[] = [];
92
- if (enrollInCourses && enrollInCourses.length > 0) {
93
- const courseIds = Array.isArray(enrollInCourses) ? enrollInCourses : [enrollInCourses];
94
-
95
- for (const courseId of courseIds) {
96
- try {
97
- const enrollData = await this.makeLearnWorldsRequest<any>(
98
- `courses/${courseId}/enrollments`,
99
- 'POST',
100
- {
101
- user_id: newUser.id,
102
- justification: 'Enrolled during user creation',
103
- notify_user: false
104
- },
105
- ContextUser
106
- );
107
-
108
- enrollmentResults.push({
109
- courseId: courseId,
110
- success: true,
111
- enrollmentId: enrollData.id
112
- });
113
- } catch (enrollError) {
114
- enrollmentResults.push({
115
- courseId: courseId,
116
- success: false,
117
- error: enrollError instanceof Error ? enrollError.message : 'Enrollment failed'
118
- });
119
- }
120
- }
121
- }
122
-
123
- // Create summary
124
- const summary = {
125
- userId: userDetails.id,
126
- email: userDetails.email,
127
- username: userDetails.username,
128
- fullName: userDetails.fullName,
129
- role: userDetails.role,
130
- status: userDetails.status,
131
- welcomeEmailSent: sendWelcomeEmail,
132
- coursesEnrolled: enrollmentResults.filter(r => r.success).length,
133
- totalCoursesRequested: enrollmentResults.length,
134
- loginUrl: userDetails.loginUrl
135
- };
136
-
137
- // Update output parameters
138
- const outputParams = [...Params];
139
- const userDetailsParam = outputParams.find(p => p.Name === 'UserDetails');
140
- if (userDetailsParam) userDetailsParam.Value = userDetails;
141
- const enrollmentResultsParam = outputParams.find(p => p.Name === 'EnrollmentResults');
142
- if (enrollmentResultsParam) enrollmentResultsParam.Value = enrollmentResults;
143
- const summaryParam = outputParams.find(p => p.Name === 'Summary');
144
- if (summaryParam) summaryParam.Value = summary;
145
-
146
- return {
147
- Success: true,
148
- ResultCode: 'SUCCESS',
149
- Message: `Successfully created user ${userDetails.email}`,
150
- Params: outputParams
151
- };
152
-
153
- } catch (error) {
154
- const errorMessage = error instanceof Error ? error.message : 'Unknown error occurred';
155
- return {
156
- Success: false,
157
- ResultCode: 'ERROR',
158
- Message: `Error creating user: ${errorMessage}`,
159
- Params
160
- };
161
- }
38
+ /**
39
+ * Typed public method for direct (non-framework) callers.
40
+ * Throws on failure instead of returning ActionResultSimple.
41
+ */
42
+ public async CreateUser(params: CreateUserParams, contextUser: UserInfo): Promise<CreateUserResult> {
43
+ this.SetCompanyContext(params.CompanyID);
44
+
45
+ const email = params.Email;
46
+ if (!email) {
47
+ throw new Error('Email is required');
162
48
  }
49
+ this.validateEmail(email);
50
+
51
+ const role = this.validateRole(params.Role || 'student');
52
+ const isActive = params.IsActive !== false;
53
+ const sendWelcomeEmail = params.SendWelcomeEmail !== false;
54
+
55
+ // Prepare user data
56
+ const userData: Record<string, unknown> = {
57
+ email: email,
58
+ role: role,
59
+ is_active: isActive,
60
+ };
163
61
 
164
- /**
165
- * Define the parameters this action expects
166
- */
167
- public get Params(): ActionParam[] {
168
- const baseParams = this.getCommonLMSParams();
169
- const specificParams: ActionParam[] = [
170
- {
171
- Name: 'Email',
172
- Type: 'Input',
173
- Value: null
174
- },
175
- {
176
- Name: 'Username',
177
- Type: 'Input',
178
- Value: null
179
- },
180
- {
181
- Name: 'Password',
182
- Type: 'Input',
183
- Value: null
184
- },
185
- {
186
- Name: 'FirstName',
187
- Type: 'Input',
188
- Value: null
189
- },
190
- {
191
- Name: 'LastName',
192
- Type: 'Input',
193
- Value: null
194
- },
195
- {
196
- Name: 'Role',
197
- Type: 'Input',
198
- Value: 'student'
199
- },
200
- {
201
- Name: 'IsActive',
202
- Type: 'Input',
203
- Value: true
204
- },
205
- {
206
- Name: 'SendWelcomeEmail',
207
- Type: 'Input',
208
- Value: true
209
- },
210
- {
211
- Name: 'Tags',
212
- Type: 'Input',
213
- Value: null
214
- },
215
- {
216
- Name: 'CustomFields',
217
- Type: 'Input',
218
- Value: null
219
- },
220
- {
221
- Name: 'EnrollInCourses',
222
- Type: 'Input',
223
- Value: null
224
- },
225
- {
226
- Name: 'UserDetails',
227
- Type: 'Output',
228
- Value: null
229
- },
230
- {
231
- Name: 'EnrollmentResults',
232
- Type: 'Output',
233
- Value: null
234
- },
235
- {
236
- Name: 'Summary',
237
- Type: 'Output',
238
- Value: null
239
- }
240
- ];
241
- return [...baseParams, ...specificParams];
62
+ // Add optional fields
63
+ if (params.Username) userData.username = params.Username;
64
+ if (params.Password) userData.password = params.Password;
65
+ if (params.FirstName) userData.first_name = params.FirstName;
66
+ if (params.LastName) userData.last_name = params.LastName;
67
+ userData.send_welcome_email = sendWelcomeEmail;
68
+
69
+ // Add tags if provided (expecting comma-separated string or array)
70
+ if (params.Tags) {
71
+ userData.tags = Array.isArray(params.Tags) ? params.Tags : params.Tags.split(',').map((t: string) => t.trim());
242
72
  }
243
73
 
244
- /**
245
- * Metadata about this action
246
- */
247
- public get Description(): string {
248
- return 'Creates a new user in LearnWorlds with optional course enrollments and welcome email';
74
+ // Add custom fields if provided
75
+ if (params.CustomFields) {
76
+ userData.custom_fields = params.CustomFields;
249
77
  }
250
- }
78
+
79
+ // Create user
80
+ const newUser = await this.makeLearnWorldsRequest<LWCreateUserResponse>('users', 'POST', userData, contextUser);
81
+
82
+ // Format user details
83
+ const userDetails = this.buildUserDetails(newUser);
84
+
85
+ // Enroll in courses if requested
86
+ const enrollmentResults = await this.enrollInCourses(newUser.id, params.EnrollInCourses, contextUser);
87
+
88
+ // Create summary
89
+ const summary = this.buildSummary(userDetails, sendWelcomeEmail, enrollmentResults);
90
+
91
+ return {
92
+ UserDetails: userDetails,
93
+ EnrollmentResults: enrollmentResults,
94
+ Summary: summary,
95
+ };
96
+ }
97
+
98
+ /**
99
+ * Framework entry point -- delegates to the typed public method.
100
+ */
101
+ protected async InternalRunAction(params: RunActionParams): Promise<ActionResultSimple> {
102
+ const { Params, ContextUser } = params;
103
+ this.params = Params;
104
+
105
+ try {
106
+ const typedParams = this.extractCreateUserParams(Params);
107
+ const result = await this.CreateUser(typedParams, ContextUser);
108
+
109
+ this.setOutputParam(Params, 'UserDetails', result.UserDetails);
110
+ this.setOutputParam(Params, 'EnrollmentResults', result.EnrollmentResults);
111
+ this.setOutputParam(Params, 'Summary', result.Summary);
112
+
113
+ return this.buildSuccessResult(`Successfully created user ${result.UserDetails.email}`, Params);
114
+ } catch (error) {
115
+ const msg = error instanceof Error ? error.message : 'Unknown error';
116
+ return this.buildErrorResult('ERROR', `Error creating user: ${msg}`, Params);
117
+ }
118
+ }
119
+
120
+ /**
121
+ * Extract typed params from the generic ActionParam array
122
+ */
123
+ private extractCreateUserParams(params: ActionParam[]): CreateUserParams {
124
+ return {
125
+ CompanyID: this.getRequiredStringParam(params, 'CompanyID'),
126
+ Email: this.getRequiredStringParam(params, 'Email'),
127
+ Username: this.getOptionalStringParam(params, 'Username'),
128
+ Password: this.getOptionalStringParam(params, 'Password'),
129
+ FirstName: this.getOptionalStringParam(params, 'FirstName'),
130
+ LastName: this.getOptionalStringParam(params, 'LastName'),
131
+ Role: this.getOptionalStringParam(params, 'Role') || 'student',
132
+ IsActive: this.getOptionalBooleanParam(params, 'IsActive', undefined),
133
+ SendWelcomeEmail: this.getOptionalBooleanParam(params, 'SendWelcomeEmail', undefined),
134
+ Tags: this.getParamValue(params, 'Tags') as string | string[] | undefined,
135
+ CustomFields: this.getParamValue(params, 'CustomFields') as Record<string, unknown> | undefined,
136
+ EnrollInCourses: this.getParamValue(params, 'EnrollInCourses') as string | string[] | undefined,
137
+ };
138
+ }
139
+
140
+ /**
141
+ * Map the raw LW create-user response to our typed details shape.
142
+ */
143
+ private buildUserDetails(newUser: LWCreateUserResponse): CreateUserDetails {
144
+ return {
145
+ id: newUser.id,
146
+ email: newUser.email,
147
+ username: newUser.username,
148
+ firstName: newUser.first_name,
149
+ lastName: newUser.last_name,
150
+ fullName: `${newUser.first_name || ''} ${newUser.last_name || ''}`.trim(),
151
+ role: newUser.role || 'student',
152
+ status: newUser.is_active ? 'active' : 'inactive',
153
+ tags: newUser.tags || [],
154
+ customFields: newUser.custom_fields || {},
155
+ createdAt: newUser.created_at,
156
+ loginUrl: newUser.login_url,
157
+ resetPasswordUrl: newUser.reset_password_url,
158
+ };
159
+ }
160
+
161
+ /**
162
+ * Enroll the newly created user in any requested courses.
163
+ */
164
+ private async enrollInCourses(userId: string, enrollInCourses: string | string[] | undefined, contextUser: UserInfo): Promise<CreateUserEnrollmentResult[]> {
165
+ const enrollmentResults: CreateUserEnrollmentResult[] = [];
166
+
167
+ if (!enrollInCourses || (Array.isArray(enrollInCourses) && enrollInCourses.length === 0)) {
168
+ return enrollmentResults;
169
+ }
170
+
171
+ const courseIds = Array.isArray(enrollInCourses) ? enrollInCourses : [enrollInCourses];
172
+
173
+ for (const courseId of courseIds) {
174
+ try {
175
+ this.validatePathSegment(courseId, 'CourseID');
176
+
177
+ const enrollBody: Record<string, unknown> = {
178
+ user_id: userId,
179
+ justification: 'Enrolled during user creation',
180
+ notify_user: false,
181
+ };
182
+
183
+ const enrollData = await this.makeLearnWorldsRequest<LWEnrollmentResponse>(`courses/${courseId}/enrollments`, 'POST', enrollBody, contextUser);
184
+
185
+ enrollmentResults.push({
186
+ courseId: courseId,
187
+ success: true,
188
+ enrollmentId: enrollData.id,
189
+ });
190
+ } catch (enrollError) {
191
+ enrollmentResults.push({
192
+ courseId: courseId,
193
+ success: false,
194
+ error: enrollError instanceof Error ? enrollError.message : 'Enrollment failed',
195
+ });
196
+ }
197
+ }
198
+
199
+ return enrollmentResults;
200
+ }
201
+
202
+ /**
203
+ * Build the summary object from user details and enrollment results.
204
+ */
205
+ private buildSummary(userDetails: CreateUserDetails, sendWelcomeEmail: boolean, enrollmentResults: CreateUserEnrollmentResult[]): CreateUserSummary {
206
+ return {
207
+ userId: userDetails.id,
208
+ email: userDetails.email,
209
+ username: userDetails.username,
210
+ fullName: userDetails.fullName,
211
+ role: userDetails.role,
212
+ status: userDetails.status,
213
+ welcomeEmailSent: sendWelcomeEmail,
214
+ coursesEnrolled: enrollmentResults.filter((r) => r.success).length,
215
+ totalCoursesRequested: enrollmentResults.length,
216
+ loginUrl: userDetails.loginUrl,
217
+ };
218
+ }
219
+
220
+ /**
221
+ * Define the parameters this action expects
222
+ */
223
+ public get Params(): ActionParam[] {
224
+ const baseParams = this.getCommonLMSParams();
225
+ const specificParams: ActionParam[] = [
226
+ {
227
+ Name: 'Email',
228
+ Type: 'Input',
229
+ Value: null,
230
+ },
231
+ {
232
+ Name: 'Username',
233
+ Type: 'Input',
234
+ Value: null,
235
+ },
236
+ {
237
+ Name: 'Password',
238
+ Type: 'Input',
239
+ Value: null,
240
+ },
241
+ {
242
+ Name: 'FirstName',
243
+ Type: 'Input',
244
+ Value: null,
245
+ },
246
+ {
247
+ Name: 'LastName',
248
+ Type: 'Input',
249
+ Value: null,
250
+ },
251
+ {
252
+ Name: 'Role',
253
+ Type: 'Input',
254
+ Value: 'student',
255
+ },
256
+ {
257
+ Name: 'IsActive',
258
+ Type: 'Input',
259
+ Value: true,
260
+ },
261
+ {
262
+ Name: 'SendWelcomeEmail',
263
+ Type: 'Input',
264
+ Value: true,
265
+ },
266
+ {
267
+ Name: 'Tags',
268
+ Type: 'Input',
269
+ Value: null,
270
+ },
271
+ {
272
+ Name: 'CustomFields',
273
+ Type: 'Input',
274
+ Value: null,
275
+ },
276
+ {
277
+ Name: 'EnrollInCourses',
278
+ Type: 'Input',
279
+ Value: null,
280
+ },
281
+ {
282
+ Name: 'UserDetails',
283
+ Type: 'Output',
284
+ Value: null,
285
+ },
286
+ {
287
+ Name: 'EnrollmentResults',
288
+ Type: 'Output',
289
+ Value: null,
290
+ },
291
+ {
292
+ Name: 'Summary',
293
+ Type: 'Output',
294
+ Value: null,
295
+ },
296
+ ];
297
+ return [...baseParams, ...specificParams];
298
+ }
299
+
300
+ /**
301
+ * Metadata about this action
302
+ */
303
+ public get Description(): string {
304
+ return 'Creates a new user in LearnWorlds with optional course enrollments and welcome email';
305
+ }
306
+ }
@@ -0,0 +1,90 @@
1
+ import { RegisterClass } from '@memberjunction/global';
2
+ import { LearnWorldsBaseAction } from '../learnworlds-base.action';
3
+ import { ActionParam, ActionResultSimple, RunActionParams } from '@memberjunction/actions-base';
4
+ import { BaseAction } from '@memberjunction/actions';
5
+ import { UserInfo } from '@memberjunction/core';
6
+ import { TagParams, TagResult } from '../interfaces';
7
+
8
+ /**
9
+ * Shape of the LearnWorlds tag operation API response
10
+ */
11
+ interface LWApiTagResponse {
12
+ tags?: string[];
13
+ }
14
+
15
+ /**
16
+ * Action to detach tags from a LearnWorlds user
17
+ */
18
+ @RegisterClass(BaseAction, 'DetachTagsAction')
19
+ export class DetachTagsAction extends LearnWorldsBaseAction {
20
+ /**
21
+ * Detaches one or more tags from a LearnWorlds user.
22
+ */
23
+ public async DetachTags(params: TagParams, contextUser: UserInfo): Promise<TagResult> {
24
+ this.SetCompanyContext(params.CompanyID);
25
+
26
+ if (!params.UserID) {
27
+ throw new Error('UserID is required');
28
+ }
29
+ if (!params.Tags || params.Tags.length === 0) {
30
+ throw new Error('At least one tag is required');
31
+ }
32
+
33
+ this.validatePathSegment(params.UserID, 'UserID');
34
+
35
+ const response = await this.makeLearnWorldsRequest<LWApiTagResponse>(`users/${params.UserID}/tags`, 'DELETE', { tags: params.Tags }, contextUser);
36
+
37
+ return {
38
+ Success: true,
39
+ UserID: params.UserID,
40
+ Tags: response.tags || params.Tags,
41
+ };
42
+ }
43
+
44
+ /**
45
+ * Framework entry point that delegates to the typed public method
46
+ */
47
+ protected async InternalRunAction(params: RunActionParams): Promise<ActionResultSimple> {
48
+ const { Params, ContextUser } = params;
49
+ this.params = Params;
50
+
51
+ try {
52
+ const tagParams: TagParams = {
53
+ CompanyID: this.getRequiredStringParam(Params, 'CompanyID'),
54
+ UserID: this.getRequiredStringParam(Params, 'UserID'),
55
+ Tags: this.getOptionalStringArrayParam(Params, 'Tags') || [],
56
+ };
57
+
58
+ const result = await this.DetachTags(tagParams, ContextUser);
59
+
60
+ this.setOutputParam(Params, 'Success', result.Success);
61
+ this.setOutputParam(Params, 'ResultTags', result.Tags);
62
+
63
+ return this.buildSuccessResult(`Successfully detached ${result.Tags.length} tag(s) from user ${result.UserID}`, Params);
64
+ } catch (error) {
65
+ const errorMessage = error instanceof Error ? error.message : 'Unknown error occurred';
66
+ return this.buildErrorResult('ERROR', `Error detaching tags: ${errorMessage}`, Params);
67
+ }
68
+ }
69
+
70
+ /**
71
+ * Define the parameters this action expects
72
+ */
73
+ public get Params(): ActionParam[] {
74
+ const baseParams = this.getCommonLMSParams();
75
+ const specificParams: ActionParam[] = [
76
+ { Name: 'UserID', Type: 'Input', Value: null },
77
+ { Name: 'Tags', Type: 'Input', Value: null },
78
+ { Name: 'Success', Type: 'Output', Value: null },
79
+ { Name: 'ResultTags', Type: 'Output', Value: null },
80
+ ];
81
+ return [...baseParams, ...specificParams];
82
+ }
83
+
84
+ /**
85
+ * Metadata about this action
86
+ */
87
+ public get Description(): string {
88
+ return 'Detaches one or more tags from a LearnWorlds user';
89
+ }
90
+ }