@memberjunction/actions-bizapps-lms 5.3.1 → 5.4.1

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,233 +2,339 @@ 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 { EnrollUserParams, EnrollUserResult, EnrollUserSummary, LearnWorldsEnrollment } from '../interfaces';
5
7
 
6
8
  /**
7
- * Action to enroll a user in a LearnWorlds course
9
+ * Shape returned by the LearnWorlds enrollment endpoint
10
+ */
11
+ interface LWEnrollmentResponse {
12
+ success?: boolean;
13
+ message?: string;
14
+ data?: {
15
+ id?: string;
16
+ user_id?: string;
17
+ course_id?: string;
18
+ enrolled_at?: string;
19
+ created_at?: string;
20
+ starts_at?: string;
21
+ expires_at?: string;
22
+ status?: string;
23
+ price?: number;
24
+ progress_percentage?: number;
25
+ completed_units?: number;
26
+ total_units?: number;
27
+ last_accessed_at?: string;
28
+ certificate_eligible?: boolean;
29
+ certificate_issued_at?: string;
30
+ };
31
+ }
32
+
33
+ /**
34
+ * Shape returned by the LearnWorlds GET /courses/{id} endpoint (partial)
35
+ */
36
+ interface LWCourseResponse {
37
+ success?: boolean;
38
+ data?: {
39
+ title?: string;
40
+ };
41
+ }
42
+
43
+ /**
44
+ * Shape returned by the LearnWorlds GET /users/{id} endpoint (partial)
45
+ */
46
+ interface LWUserLookupResponse {
47
+ success?: boolean;
48
+ data?: {
49
+ email?: string;
50
+ username?: string;
51
+ };
52
+ }
53
+
54
+ /**
55
+ * Action to enroll a user in a LearnWorlds course or bundle
8
56
  */
9
57
  @RegisterClass(BaseAction, 'EnrollUserAction')
10
58
  export class EnrollUserAction extends LearnWorldsBaseAction {
11
- /**
12
- * Enroll a user in a course
13
- */
14
- public async InternalRunAction(params: RunActionParams): Promise<ActionResultSimple> {
15
- const { Params, ContextUser } = params;
16
- this.params = Params;
17
-
18
- try {
19
- // Extract and validate parameters
20
- const userId = this.getParamValue(Params, 'UserID');
21
- const courseId = this.getParamValue(Params, 'CourseID');
22
- const price = this.getParamValue(Params, 'Price') || 0;
23
- const justification = this.getParamValue(Params, 'Justification') || 'API Enrollment';
24
- const notifyUser = this.getParamValue(Params, 'NotifyUser') !== false;
25
- const startDate = this.getParamValue(Params, 'StartDate');
26
- const expiryDate = this.getParamValue(Params, 'ExpiryDate');
27
-
28
- if (!userId) {
29
- return {
30
- Success: false,
31
- ResultCode: 'VALIDATION_ERROR',
32
- Message: 'UserID is required',
33
- Params
34
- };
35
- }
36
-
37
- if (!courseId) {
38
- return {
39
- Success: false,
40
- ResultCode: 'VALIDATION_ERROR',
41
- Message: 'CourseID is required',
42
- Params
43
- };
44
- }
45
-
46
-
47
- // Prepare enrollment data
48
- const enrollmentData: any = {
49
- user_id: userId,
50
- justification: justification,
51
- price: price,
52
- notify_user: notifyUser
53
- };
54
-
55
- if (startDate) {
56
- enrollmentData.starts_at = new Date(startDate).toISOString();
57
- }
58
-
59
- if (expiryDate) {
60
- enrollmentData.expires_at = new Date(expiryDate).toISOString();
61
- }
62
-
63
- // Create enrollment
64
- const enrollmentResponse = await this.makeLearnWorldsRequest(
65
- `/courses/${courseId}/enrollments`,
66
- 'POST',
67
- enrollmentData,
68
- ContextUser
69
- );
70
-
71
- if (!enrollmentResponse.success) {
72
- return {
73
- Success: false,
74
- ResultCode: 'API_ERROR',
75
- Message: enrollmentResponse.message || 'Failed to enroll user',
76
- Params
77
- };
78
- }
79
-
80
- const enrollment = enrollmentResponse.data;
81
-
82
- // Format enrollment details
83
- const enrollmentDetails = {
84
- id: enrollment.id,
85
- userId: enrollment.user_id || userId,
86
- courseId: enrollment.course_id || courseId,
87
- enrolledAt: enrollment.enrolled_at || enrollment.created_at,
88
- startsAt: enrollment.starts_at,
89
- expiresAt: enrollment.expires_at,
90
- status: enrollment.status || 'active',
91
- price: enrollment.price || price,
92
- progress: {
93
- percentage: enrollment.progress_percentage || 0,
94
- completedUnits: enrollment.completed_units || 0,
95
- totalUnits: enrollment.total_units || 0,
96
- lastAccessedAt: enrollment.last_accessed_at
97
- },
98
- certificateEligible: enrollment.certificate_eligible || false,
99
- certificateIssuedAt: enrollment.certificate_issued_at
100
- };
101
-
102
- // Get course and user details for summary
103
- let courseTitle = 'Unknown Course';
104
- let userName = 'Unknown User';
105
-
106
- // Try to get course details
107
- const courseResponse = await this.makeLearnWorldsRequest(
108
- `/courses/${courseId}`,
109
- 'GET',
110
- null,
111
- ContextUser
112
- );
113
-
114
- if (courseResponse.success && courseResponse.data) {
115
- courseTitle = courseResponse.data.title || courseTitle;
116
- }
117
-
118
- // Try to get user details
119
- const userResponse = await this.makeLearnWorldsRequest(
120
- `/users/${userId}`,
121
- 'GET',
122
- null,
123
- ContextUser
124
- );
125
-
126
- if (userResponse.success && userResponse.data) {
127
- const user = userResponse.data;
128
- userName = user.email || user.username || userName;
129
- }
130
-
131
- // Create summary
132
- const summary = {
133
- enrollmentId: enrollmentDetails.id,
134
- userId: userId,
135
- userName: userName,
136
- courseId: courseId,
137
- courseTitle: courseTitle,
138
- enrolledAt: enrollmentDetails.enrolledAt,
139
- status: enrollmentDetails.status,
140
- price: enrollmentDetails.price,
141
- notificationSent: notifyUser
142
- };
143
-
144
- // Update output parameters
145
- const outputParams = [...Params];
146
- const enrollmentDetailsParam = outputParams.find(p => p.Name === 'EnrollmentDetails');
147
- if (enrollmentDetailsParam) {
148
- enrollmentDetailsParam.Value = enrollmentDetails;
149
- }
150
- const summaryParam = outputParams.find(p => p.Name === 'Summary');
151
- if (summaryParam) {
152
- summaryParam.Value = summary;
153
- }
154
-
155
- return {
156
- Success: true,
157
- ResultCode: 'SUCCESS',
158
- Message: `Successfully enrolled user ${userName} in course ${courseTitle}`,
159
- Params: outputParams
160
- };
161
-
162
- } catch (error) {
163
- const errorMessage = error instanceof Error ? error.message : 'Unknown error occurred';
164
- return {
165
- Success: false,
166
- ResultCode: 'EXECUTION_ERROR',
167
- Message: `Error enrolling user: ${errorMessage}`,
168
- Params
169
- };
170
- }
59
+ /**
60
+ * Typed public method for direct (non-framework) callers.
61
+ * Supports both course and bundle enrollment via ProductType.
62
+ * Throws on failure.
63
+ */
64
+ public async EnrollUser(params: EnrollUserParams, contextUser: UserInfo): Promise<EnrollUserResult> {
65
+ this.SetCompanyContext(params.CompanyID);
66
+
67
+ if (!params.UserID) {
68
+ throw new Error('UserID is required');
69
+ }
70
+ if (!params.CourseID) {
71
+ throw new Error('CourseID is required');
72
+ }
73
+
74
+ this.validatePathSegment(params.UserID, 'UserID');
75
+ this.validatePathSegment(params.CourseID, 'CourseID');
76
+
77
+ const price = params.Price ?? 0;
78
+ const justification = params.Justification || 'API Enrollment';
79
+ const notifyUser = params.NotifyUser !== false;
80
+ const productType = params.ProductType || 'course';
81
+
82
+ // Build enrollment body and choose endpoint based on product type
83
+ const { endpoint, enrollmentData } = this.buildEnrollmentRequest(
84
+ params.UserID,
85
+ params.CourseID,
86
+ productType,
87
+ price,
88
+ justification,
89
+ notifyUser,
90
+ params.StartDate,
91
+ params.ExpiryDate,
92
+ );
93
+
94
+ // Create enrollment
95
+ const enrollmentResponse = await this.makeLearnWorldsRequest<LWEnrollmentResponse>(endpoint, 'POST', enrollmentData, contextUser);
96
+
97
+ if (enrollmentResponse.success === false) {
98
+ throw new Error(enrollmentResponse.message || 'Failed to enroll user');
99
+ }
100
+
101
+ const enrollment = enrollmentResponse.data;
102
+
103
+ // Format enrollment details
104
+ const enrollmentDetails = this.buildEnrollmentDetails(enrollment, params.UserID, params.CourseID, price);
105
+
106
+ // Fetch course title and user name in parallel for the summary
107
+ const [courseTitle, userName] = await Promise.all([
108
+ this.fetchCourseTitle(params.CourseID, contextUser),
109
+ this.fetchUserName(params.UserID, contextUser),
110
+ ]);
111
+
112
+ // Create summary
113
+ const summary: EnrollUserSummary = {
114
+ enrollmentId: enrollmentDetails.id,
115
+ userId: params.UserID,
116
+ userName: userName,
117
+ courseId: params.CourseID,
118
+ courseTitle: courseTitle,
119
+ enrolledAt: enrollmentDetails.enrolledAt,
120
+ status: enrollmentDetails.status,
121
+ price: enrollmentDetails.price,
122
+ notificationSent: notifyUser,
123
+ };
124
+
125
+ return {
126
+ EnrollmentDetails: enrollmentDetails,
127
+ Summary: summary,
128
+ };
129
+ }
130
+
131
+ /**
132
+ * Framework entry point -- delegates to the typed public method.
133
+ */
134
+ protected async InternalRunAction(params: RunActionParams): Promise<ActionResultSimple> {
135
+ const { Params, ContextUser } = params;
136
+ this.params = Params;
137
+
138
+ try {
139
+ const typedParams = this.extractEnrollUserParams(Params);
140
+ const result = await this.EnrollUser(typedParams, ContextUser);
141
+
142
+ this.setOutputParam(Params, 'EnrollmentDetails', result.EnrollmentDetails);
143
+ this.setOutputParam(Params, 'Summary', result.Summary);
144
+
145
+ return this.buildSuccessResult(`Successfully enrolled user ${result.Summary.userName} in course ${result.Summary.courseTitle}`, Params);
146
+ } catch (error) {
147
+ const msg = error instanceof Error ? error.message : 'Unknown error';
148
+ return this.buildErrorResult('ERROR', `Error enrolling user: ${msg}`, Params);
171
149
  }
150
+ }
172
151
 
173
- /**
174
- * Define the parameters this action expects
175
- */
176
- public get Params(): ActionParam[] {
177
- const baseParams = this.getCommonLMSParams();
178
- const specificParams: ActionParam[] = [
179
- {
180
- Name: 'UserID',
181
- Type: 'Input',
182
- Value: null
183
- },
184
- {
185
- Name: 'CourseID',
186
- Type: 'Input',
187
- Value: null
188
- },
189
- {
190
- Name: 'Price',
191
- Type: 'Input',
192
- Value: 0
193
- },
194
- {
195
- Name: 'Justification',
196
- Type: 'Input',
197
- Value: 'API Enrollment'
198
- },
199
- {
200
- Name: 'NotifyUser',
201
- Type: 'Input',
202
- Value: true
203
- },
204
- {
205
- Name: 'StartDate',
206
- Type: 'Input',
207
- Value: null
208
- },
209
- {
210
- Name: 'ExpiryDate',
211
- Type: 'Input',
212
- Value: null
213
- },
214
- {
215
- Name: 'EnrollmentDetails',
216
- Type: 'Output',
217
- Value: null
218
- },
219
- {
220
- Name: 'Summary',
221
- Type: 'Output',
222
- Value: null
223
- }
224
- ];
225
- return [...baseParams, ...specificParams];
152
+ /**
153
+ * Extract typed params from the generic ActionParam array
154
+ */
155
+ private extractEnrollUserParams(params: ActionParam[]): EnrollUserParams {
156
+ return {
157
+ CompanyID: this.getRequiredStringParam(params, 'CompanyID'),
158
+ UserID: this.getRequiredStringParam(params, 'UserID'),
159
+ CourseID: this.getRequiredStringParam(params, 'CourseID'),
160
+ ProductType: (this.getOptionalStringParam(params, 'ProductType') || 'course') as 'course' | 'bundle',
161
+ Price: this.getOptionalNumberParam(params, 'Price', 0),
162
+ Justification: this.getOptionalStringParam(params, 'Justification') || 'API Enrollment',
163
+ NotifyUser: this.getOptionalBooleanParam(params, 'NotifyUser', true),
164
+ StartDate: this.getOptionalStringParam(params, 'StartDate'),
165
+ ExpiryDate: this.getOptionalStringParam(params, 'ExpiryDate'),
166
+ };
167
+ }
168
+
169
+ /**
170
+ * Build the endpoint and body based on product type (course vs bundle).
171
+ */
172
+ private buildEnrollmentRequest(
173
+ userId: string,
174
+ courseId: string,
175
+ productType: 'course' | 'bundle',
176
+ price: number,
177
+ justification: string,
178
+ notifyUser: boolean,
179
+ startDate?: string,
180
+ expiryDate?: string,
181
+ ): { endpoint: string; enrollmentData: Record<string, unknown> } {
182
+ if (productType === 'bundle') {
183
+ // Bundles use the generic enrollments endpoint with product_type
184
+ return {
185
+ endpoint: 'enrollments',
186
+ enrollmentData: {
187
+ product_id: courseId,
188
+ product_type: 'bundle',
189
+ user_id: userId,
190
+ price: price,
191
+ justification: justification,
192
+ notify_user: notifyUser,
193
+ ...(startDate ? { starts_at: new Date(startDate).toISOString() } : {}),
194
+ ...(expiryDate ? { expires_at: new Date(expiryDate).toISOString() } : {}),
195
+ },
196
+ };
226
197
  }
227
198
 
228
- /**
229
- * Metadata about this action
230
- */
231
- public get Description(): string {
232
- return 'Enrolls a user in a LearnWorlds course with optional pricing and notification settings';
199
+ // Courses use the per-course enrollment endpoint
200
+ const enrollmentData: Record<string, unknown> = {
201
+ user_id: userId,
202
+ justification: justification,
203
+ price: price,
204
+ notify_user: notifyUser,
205
+ };
206
+
207
+ if (startDate) {
208
+ enrollmentData.starts_at = new Date(startDate).toISOString();
233
209
  }
234
- }
210
+ if (expiryDate) {
211
+ enrollmentData.expires_at = new Date(expiryDate).toISOString();
212
+ }
213
+
214
+ return {
215
+ endpoint: `courses/${courseId}/enrollments`,
216
+ enrollmentData,
217
+ };
218
+ }
219
+
220
+ /**
221
+ * Map the raw enrollment response data to our typed LearnWorldsEnrollment shape.
222
+ */
223
+ private buildEnrollmentDetails(enrollment: LWEnrollmentResponse['data'] | undefined, userId: string, courseId: string, price: number): LearnWorldsEnrollment {
224
+ return {
225
+ id: enrollment?.id || '',
226
+ userId: enrollment?.user_id || userId,
227
+ courseId: enrollment?.course_id || courseId,
228
+ enrolledAt: enrollment?.enrolled_at || enrollment?.created_at || new Date().toISOString(),
229
+ startsAt: enrollment?.starts_at,
230
+ expiresAt: enrollment?.expires_at,
231
+ status: enrollment?.status || 'active',
232
+ price: enrollment?.price ?? price,
233
+ progress: {
234
+ percentage: enrollment?.progress_percentage || 0,
235
+ completedUnits: enrollment?.completed_units || 0,
236
+ totalUnits: enrollment?.total_units || 0,
237
+ lastAccessedAt: enrollment?.last_accessed_at,
238
+ },
239
+ certificateEligible: enrollment?.certificate_eligible || false,
240
+ certificateIssuedAt: enrollment?.certificate_issued_at,
241
+ };
242
+ }
243
+
244
+ /**
245
+ * Try to fetch the course title for the summary. Falls back to a default.
246
+ */
247
+ private async fetchCourseTitle(courseId: string, contextUser: UserInfo): Promise<string> {
248
+ try {
249
+ const courseResponse = await this.makeLearnWorldsRequest<LWCourseResponse>(`courses/${courseId}`, 'GET', null, contextUser);
250
+ if (courseResponse.success !== false && courseResponse.data) {
251
+ return courseResponse.data.title || 'Unknown Course';
252
+ }
253
+ } catch (error) {
254
+ console.warn(`Failed to fetch course title for ${courseId}:`, error instanceof Error ? error.message : error);
255
+ }
256
+ return 'Unknown Course';
257
+ }
258
+
259
+ /**
260
+ * Try to fetch the user display name for the summary. Falls back to a default.
261
+ */
262
+ private async fetchUserName(userId: string, contextUser: UserInfo): Promise<string> {
263
+ try {
264
+ const userResponse = await this.makeLearnWorldsRequest<LWUserLookupResponse>(`users/${userId}`, 'GET', null, contextUser);
265
+ if (userResponse.success !== false && userResponse.data) {
266
+ return userResponse.data.email || userResponse.data.username || 'Unknown User';
267
+ }
268
+ } catch (error) {
269
+ console.warn(`Failed to fetch user name for ${userId}:`, error instanceof Error ? error.message : error);
270
+ }
271
+ return 'Unknown User';
272
+ }
273
+
274
+ /**
275
+ * Define the parameters this action expects
276
+ */
277
+ public get Params(): ActionParam[] {
278
+ const baseParams = this.getCommonLMSParams();
279
+ const specificParams: ActionParam[] = [
280
+ {
281
+ Name: 'UserID',
282
+ Type: 'Input',
283
+ Value: null,
284
+ },
285
+ {
286
+ Name: 'CourseID',
287
+ Type: 'Input',
288
+ Value: null,
289
+ },
290
+ {
291
+ Name: 'ProductType',
292
+ Type: 'Input',
293
+ Value: 'course',
294
+ },
295
+ {
296
+ Name: 'Price',
297
+ Type: 'Input',
298
+ Value: 0,
299
+ },
300
+ {
301
+ Name: 'Justification',
302
+ Type: 'Input',
303
+ Value: 'API Enrollment',
304
+ },
305
+ {
306
+ Name: 'NotifyUser',
307
+ Type: 'Input',
308
+ Value: true,
309
+ },
310
+ {
311
+ Name: 'StartDate',
312
+ Type: 'Input',
313
+ Value: null,
314
+ },
315
+ {
316
+ Name: 'ExpiryDate',
317
+ Type: 'Input',
318
+ Value: null,
319
+ },
320
+ {
321
+ Name: 'EnrollmentDetails',
322
+ Type: 'Output',
323
+ Value: null,
324
+ },
325
+ {
326
+ Name: 'Summary',
327
+ Type: 'Output',
328
+ Value: null,
329
+ },
330
+ ];
331
+ return [...baseParams, ...specificParams];
332
+ }
333
+
334
+ /**
335
+ * Metadata about this action
336
+ */
337
+ public get Description(): string {
338
+ return 'Enrolls a user in a LearnWorlds course or bundle with optional pricing and notification settings';
339
+ }
340
+ }