@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,242 +2,342 @@ 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 { GetUserEnrollmentsParams, GetUserEnrollmentsResult, FormattedEnrollment, EnrollmentsSummary } from '../interfaces';
7
+
8
+ /**
9
+ * Raw API shape for a single enrollment from LearnWorlds
10
+ */
11
+ interface LWApiEnrollmentData {
12
+ id?: string;
13
+ course_id?: string;
14
+ enrolled_at?: string;
15
+ created_at?: string;
16
+ starts_at?: string;
17
+ expires_at?: string;
18
+ status?: string;
19
+ progress_percentage?: number;
20
+ completed_units?: number;
21
+ total_units?: number;
22
+ completed_lessons?: number;
23
+ total_lessons?: number;
24
+ last_accessed_at?: string;
25
+ total_time_spent?: number;
26
+ grade?: number;
27
+ certificate_eligible?: boolean;
28
+ certificate_issued_at?: string;
29
+ completed_at?: string;
30
+ }
31
+
32
+ /**
33
+ * Raw API shape for course details looked up per enrollment
34
+ */
35
+ interface LWApiCourseBasic {
36
+ id?: string;
37
+ title?: string;
38
+ short_description?: string;
39
+ description?: string;
40
+ image_url?: string;
41
+ level?: string;
42
+ duration?: number;
43
+ instructor_name?: string;
44
+ certificate_enabled?: boolean;
45
+ }
5
46
 
6
47
  /**
7
48
  * Action to retrieve all course enrollments for a specific user
8
49
  */
9
50
  @RegisterClass(BaseAction, 'GetUserEnrollmentsAction')
10
51
  export class GetUserEnrollmentsAction extends LearnWorldsBaseAction {
11
- /**
12
- * Get all enrollments for a 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 userId = this.getParamValue(Params, 'UserID');
21
- const status = this.getParamValue(Params, 'Status');
22
- const includeExpired = this.getParamValue(Params, 'IncludeExpired') || false;
23
- const includeCourseDetails = this.getParamValue(Params, 'IncludeCourseDetails') !== false;
24
- const sortBy = this.getParamValue(Params, 'SortBy') || 'enrolled_at';
25
- const sortOrder = this.getParamValue(Params, 'SortOrder') || 'desc';
26
- const maxResults = this.getParamValue(Params, 'MaxResults') || 100;
27
-
28
- if (!userId) {
29
- return {
30
- Success: false,
31
- ResultCode: 'VALIDATION_ERROR',
32
- Message: 'UserID is required',
33
- Params
34
- };
35
- }
36
-
37
- // Build query parameters
38
- const queryParams: any = {
39
- limit: Math.min(maxResults, 100),
40
- sort: sortBy,
41
- order: sortOrder
42
- };
43
-
44
- if (status) {
45
- queryParams.status = status;
46
- }
47
-
48
- if (!includeExpired) {
49
- queryParams.hide_expired = true;
50
- }
51
-
52
- // Get user enrollments
53
- const enrollmentsData = await this.makeLearnWorldsPaginatedRequest<any>(
54
- `users/${userId}/enrollments`,
55
- queryParams,
56
- ContextUser
57
- );
58
-
59
- const enrollments = enrollmentsData || [];
60
- const formattedEnrollments: any[] = [];
61
-
62
- // Process each enrollment
63
- for (const enrollment of enrollments) {
64
- const formattedEnrollment: any = {
65
- id: enrollment.id,
66
- courseId: enrollment.course_id,
67
- enrolledAt: enrollment.enrolled_at || enrollment.created_at,
68
- startsAt: enrollment.starts_at,
69
- expiresAt: enrollment.expires_at,
70
- status: enrollment.status || 'active',
71
- progress: {
72
- percentage: enrollment.progress_percentage || 0,
73
- completedUnits: enrollment.completed_units || 0,
74
- totalUnits: enrollment.total_units || 0,
75
- completedLessons: enrollment.completed_lessons || 0,
76
- totalLessons: enrollment.total_lessons || 0,
77
- lastAccessedAt: enrollment.last_accessed_at,
78
- totalTimeSpent: enrollment.total_time_spent || 0,
79
- totalTimeSpentText: this.formatDuration(enrollment.total_time_spent || 0)
80
- },
81
- grade: enrollment.grade,
82
- certificateEligible: enrollment.certificate_eligible || false,
83
- certificateIssuedAt: enrollment.certificate_issued_at,
84
- completedAt: enrollment.completed_at
85
- };
86
-
87
- // Get course details if requested
88
- if (includeCourseDetails && enrollment.course_id) {
89
- try {
90
- const course = await this.makeLearnWorldsRequest<any>(
91
- `courses/${enrollment.course_id}`,
92
- 'GET',
93
- undefined,
94
- ContextUser
95
- );
96
- formattedEnrollment.course = {
97
- id: course.id,
98
- title: course.title,
99
- description: course.short_description || course.description,
100
- imageUrl: course.image_url,
101
- level: course.level,
102
- duration: course.duration,
103
- durationText: this.formatDuration(course.duration),
104
- instructorName: course.instructor_name,
105
- certificateEnabled: course.certificate_enabled || false
106
- };
107
- } catch (error) {
108
- // Log error but continue processing
109
- console.warn(`Failed to get course details for ${enrollment.course_id}:`, error);
110
- }
111
- }
112
-
113
- formattedEnrollments.push(formattedEnrollment);
114
- }
115
-
116
- // Calculate summary statistics
117
- const totalEnrollments = formattedEnrollments.length;
118
- const activeEnrollments = formattedEnrollments.filter(e => e.status === 'active').length;
119
- const completedEnrollments = formattedEnrollments.filter(e =>
120
- e.progress.percentage >= 100 || e.completedAt
121
- ).length;
122
- const expiredEnrollments = formattedEnrollments.filter(e => e.status === 'expired').length;
123
- const averageProgress = totalEnrollments > 0
124
- ? formattedEnrollments.reduce((sum, e) => sum + e.progress.percentage, 0) / totalEnrollments
125
- : 0;
126
- const totalTimeSpent = formattedEnrollments.reduce((sum, e) =>
127
- sum + (e.progress.totalTimeSpent || 0), 0
128
- );
129
- const certificatesEarned = formattedEnrollments.filter(e => e.certificateIssuedAt).length;
130
-
131
- // Create summary
132
- const summary = {
133
- userId: userId,
134
- totalEnrollments: totalEnrollments,
135
- activeEnrollments: activeEnrollments,
136
- completedEnrollments: completedEnrollments,
137
- expiredEnrollments: expiredEnrollments,
138
- inProgressEnrollments: activeEnrollments - completedEnrollments,
139
- averageProgressPercentage: Math.round(averageProgress * 100) / 100,
140
- totalTimeSpent: totalTimeSpent,
141
- totalTimeSpentText: this.formatDuration(totalTimeSpent),
142
- certificatesEarned: certificatesEarned,
143
- enrollmentsByStatus: {
144
- active: activeEnrollments,
145
- completed: completedEnrollments,
146
- expired: expiredEnrollments
147
- }
148
- };
149
-
150
- // Update output parameters
151
- const outputParams = [...Params];
152
- const enrollmentsParam = outputParams.find(p => p.Name === 'Enrollments');
153
- if (enrollmentsParam) enrollmentsParam.Value = formattedEnrollments;
154
- const totalCountParam = outputParams.find(p => p.Name === 'TotalCount');
155
- if (totalCountParam) totalCountParam.Value = totalEnrollments;
156
- const summaryParam = outputParams.find(p => p.Name === 'Summary');
157
- if (summaryParam) summaryParam.Value = summary;
158
-
159
- return {
160
- Success: true,
161
- ResultCode: 'SUCCESS',
162
- Message: `Retrieved ${totalEnrollments} enrollments for user`,
163
- Params: outputParams
164
- };
165
-
166
- } catch (error) {
167
- const errorMessage = error instanceof Error ? error.message : 'Unknown error occurred';
168
- return {
169
- Success: false,
170
- ResultCode: 'ERROR',
171
- Message: `Error retrieving enrollments: ${errorMessage}`,
172
- Params
173
- };
174
- }
52
+ /**
53
+ * Metadata about this action
54
+ */
55
+ public get Description(): string {
56
+ return 'Retrieves all course enrollments for a specific LearnWorlds user with detailed progress information';
57
+ }
58
+
59
+ /**
60
+ * Typed public method for direct (non-framework) callers.
61
+ * Sets company context, fetches enrollments, and returns a strongly-typed result.
62
+ * Throws on error.
63
+ */
64
+ public async GetUserEnrollments(params: GetUserEnrollmentsParams, contextUser: UserInfo): Promise<GetUserEnrollmentsResult> {
65
+ this.SetCompanyContext(params.CompanyID);
66
+
67
+ const {
68
+ UserID: userId,
69
+ Status: status,
70
+ IncludeExpired: includeExpired = false,
71
+ IncludeCourseDetails: includeCourseDetails = true,
72
+ SortBy: sortBy = 'enrolled_at',
73
+ SortOrder: sortOrder = 'desc',
74
+ MaxResults: maxResults = 100,
75
+ } = params;
76
+
77
+ if (!userId) {
78
+ throw new Error('UserID is required');
79
+ }
80
+
81
+ this.validatePathSegment(userId, 'UserID');
82
+
83
+ const queryParams = this.buildEnrollmentQueryParams(status, includeExpired, sortBy, sortOrder, maxResults);
84
+
85
+ const enrollmentsData = await this.makeLearnWorldsPaginatedRequest<LWApiEnrollmentData>(`users/${userId}/enrollments`, queryParams, contextUser);
86
+
87
+ const enrollments = enrollmentsData || [];
88
+ const formattedEnrollments = await this.formatEnrollments(enrollments, includeCourseDetails, contextUser);
89
+
90
+ const summary = this.calculateEnrollmentSummary(userId, formattedEnrollments);
91
+
92
+ return {
93
+ Enrollments: formattedEnrollments,
94
+ TotalCount: formattedEnrollments.length,
95
+ Summary: summary,
96
+ };
97
+ }
98
+
99
+ /**
100
+ * Framework entry-point – thin wrapper around the typed public method.
101
+ */
102
+ protected async InternalRunAction(params: RunActionParams): Promise<ActionResultSimple> {
103
+ const { Params, ContextUser } = params;
104
+ this.params = Params;
105
+
106
+ try {
107
+ const typedParams = this.extractGetUserEnrollmentsParams(Params);
108
+ const result = await this.GetUserEnrollments(typedParams, ContextUser);
109
+
110
+ this.setOutputParam(Params, 'Enrollments', result.Enrollments);
111
+ this.setOutputParam(Params, 'TotalCount', result.TotalCount);
112
+ this.setOutputParam(Params, 'Summary', result.Summary);
113
+
114
+ return this.buildSuccessResult(`Retrieved ${result.TotalCount} enrollments for user`, Params);
115
+ } catch (error) {
116
+ const msg = error instanceof Error ? error.message : 'Unknown error occurred';
117
+ return this.buildErrorResult('ERROR', `Error retrieving enrollments: ${msg}`, Params);
175
118
  }
119
+ }
120
+
121
+ /**
122
+ * Extract typed params from framework ActionParam[]
123
+ */
124
+ private extractGetUserEnrollmentsParams(params: ActionParam[]): GetUserEnrollmentsParams {
125
+ return {
126
+ CompanyID: this.getRequiredStringParam(params, 'CompanyID'),
127
+ UserID: this.getRequiredStringParam(params, 'UserID'),
128
+ Status: this.getOptionalStringParam(params, 'Status'),
129
+ IncludeExpired: this.getOptionalBooleanParam(params, 'IncludeExpired', false),
130
+ IncludeCourseDetails: this.getOptionalBooleanParam(params, 'IncludeCourseDetails', true),
131
+ SortBy: this.getOptionalStringParam(params, 'SortBy') || 'enrolled_at',
132
+ SortOrder: (this.getOptionalStringParam(params, 'SortOrder') || 'desc') as 'asc' | 'desc',
133
+ MaxResults: this.getOptionalNumberParam(params, 'MaxResults', LearnWorldsBaseAction.LW_MAX_PAGE_SIZE),
134
+ };
135
+ }
136
+
137
+ /**
138
+ * Build query parameters for the enrollments API request
139
+ */
140
+ private buildEnrollmentQueryParams(
141
+ status: string | undefined,
142
+ includeExpired: boolean,
143
+ sortBy: string,
144
+ sortOrder: string,
145
+ maxResults: number,
146
+ ): Record<string, string | number | boolean> {
147
+ const queryParams: Record<string, string | number | boolean> = {
148
+ limit: Math.min(maxResults, LearnWorldsBaseAction.LW_MAX_PAGE_SIZE),
149
+ sort: sortBy,
150
+ order: sortOrder,
151
+ };
176
152
 
177
- /**
178
- * Define the parameters this action expects
179
- */
180
- public get Params(): ActionParam[] {
181
- const baseParams = this.getCommonLMSParams();
182
- const specificParams: ActionParam[] = [
183
- {
184
- Name: 'UserID',
185
- Type: 'Input',
186
- Value: null
187
- },
188
- {
189
- Name: 'Status',
190
- Type: 'Input',
191
- Value: null
192
- },
193
- {
194
- Name: 'IncludeExpired',
195
- Type: 'Input',
196
- Value: false
197
- },
198
- {
199
- Name: 'IncludeCourseDetails',
200
- Type: 'Input',
201
- Value: true
202
- },
203
- {
204
- Name: 'SortBy',
205
- Type: 'Input',
206
- Value: 'enrolled_at'
207
- },
208
- {
209
- Name: 'SortOrder',
210
- Type: 'Input',
211
- Value: 'desc'
212
- },
213
- {
214
- Name: 'MaxResults',
215
- Type: 'Input',
216
- Value: 100
217
- },
218
- {
219
- Name: 'Enrollments',
220
- Type: 'Output',
221
- Value: null
222
- },
223
- {
224
- Name: 'TotalCount',
225
- Type: 'Output',
226
- Value: null
227
- },
228
- {
229
- Name: 'Summary',
230
- Type: 'Output',
231
- Value: null
232
- }
233
- ];
234
- return [...baseParams, ...specificParams];
153
+ if (status) {
154
+ queryParams.status = status;
235
155
  }
236
156
 
237
- /**
238
- * Metadata about this action
239
- */
240
- public get Description(): string {
241
- return 'Retrieves all course enrollments for a specific LearnWorlds user with detailed progress information';
157
+ if (!includeExpired) {
158
+ queryParams.hide_expired = true;
242
159
  }
243
- }
160
+
161
+ return queryParams;
162
+ }
163
+
164
+ /**
165
+ * Format all raw enrollments into typed FormattedEnrollment[].
166
+ * When course details are requested, fetches all unique courses in parallel
167
+ * and attaches results from a lookup map.
168
+ */
169
+ private async formatEnrollments(enrollments: LWApiEnrollmentData[], includeCourseDetails: boolean, contextUser: UserInfo): Promise<FormattedEnrollment[]> {
170
+ // Build a course details map in one batch to avoid per-item API calls
171
+ const courseDetailsMap = includeCourseDetails
172
+ ? await this.fetchCourseDetailsBatch(enrollments, contextUser)
173
+ : new Map<string, FormattedEnrollment['course']>();
174
+
175
+ return enrollments.map((enrollment) => {
176
+ const formatted = this.formatSingleEnrollment(enrollment);
177
+ if (includeCourseDetails && enrollment.course_id) {
178
+ formatted.course = courseDetailsMap.get(enrollment.course_id);
179
+ }
180
+ return formatted;
181
+ });
182
+ }
183
+
184
+ /**
185
+ * Fetch course details for all unique course IDs with controlled concurrency and return a lookup map.
186
+ */
187
+ private async fetchCourseDetailsBatch(enrollments: LWApiEnrollmentData[], contextUser: UserInfo): Promise<Map<string, FormattedEnrollment['course']>> {
188
+ const uniqueCourseIds = [...new Set(enrollments.map((e) => e.course_id).filter((id): id is string => !!id))];
189
+
190
+ const results = await this.processInBatches(uniqueCourseIds, (courseId) => {
191
+ this.validatePathSegment(courseId, 'CourseID');
192
+ return this.fetchCourseDetailsForEnrollment(courseId, contextUser);
193
+ });
194
+
195
+ const map = new Map<string, FormattedEnrollment['course']>();
196
+ uniqueCourseIds.forEach((courseId, index) => {
197
+ map.set(courseId, results[index]);
198
+ });
199
+ return map;
200
+ }
201
+
202
+ /**
203
+ * Format a single raw enrollment to a FormattedEnrollment
204
+ */
205
+ private formatSingleEnrollment(enrollment: LWApiEnrollmentData): FormattedEnrollment {
206
+ return {
207
+ id: enrollment.id || '',
208
+ courseId: enrollment.course_id || '',
209
+ enrolledAt: enrollment.enrolled_at || enrollment.created_at,
210
+ startsAt: enrollment.starts_at,
211
+ expiresAt: enrollment.expires_at,
212
+ status: enrollment.status || 'active',
213
+ progress: {
214
+ percentage: enrollment.progress_percentage || 0,
215
+ completedUnits: enrollment.completed_units || 0,
216
+ totalUnits: enrollment.total_units || 0,
217
+ completedLessons: enrollment.completed_lessons || 0,
218
+ totalLessons: enrollment.total_lessons || 0,
219
+ lastAccessedAt: enrollment.last_accessed_at,
220
+ totalTimeSpent: enrollment.total_time_spent || 0,
221
+ totalTimeSpentText: this.formatDuration(enrollment.total_time_spent || 0),
222
+ },
223
+ grade: enrollment.grade,
224
+ certificateEligible: enrollment.certificate_eligible || false,
225
+ certificateIssuedAt: enrollment.certificate_issued_at,
226
+ completedAt: enrollment.completed_at,
227
+ };
228
+ }
229
+
230
+ /**
231
+ * Fetch course details for a given enrollment's course ID
232
+ */
233
+ private async fetchCourseDetailsForEnrollment(courseId: string, contextUser: UserInfo): Promise<FormattedEnrollment['course']> {
234
+ try {
235
+ const course = await this.makeLearnWorldsRequest<LWApiCourseBasic>(`courses/${courseId}`, 'GET', undefined, contextUser);
236
+ return {
237
+ id: course.id || courseId,
238
+ title: course.title || '',
239
+ description: course.short_description || course.description,
240
+ imageUrl: course.image_url,
241
+ level: course.level,
242
+ duration: course.duration,
243
+ durationText: this.formatDuration(course.duration || 0),
244
+ instructorName: course.instructor_name,
245
+ certificateEnabled: course.certificate_enabled || false,
246
+ };
247
+ } catch (error) {
248
+ console.warn(`Failed to get course details for ${courseId}:`, error);
249
+ return undefined;
250
+ }
251
+ }
252
+
253
+ /**
254
+ * Calculate summary statistics from formatted enrollments
255
+ */
256
+ private calculateEnrollmentSummary(userId: string, formattedEnrollments: FormattedEnrollment[]): EnrollmentsSummary {
257
+ const totalEnrollments = formattedEnrollments.length;
258
+ const activeEnrollments = formattedEnrollments.filter((e) => e.status === 'active').length;
259
+ const completedEnrollments = formattedEnrollments.filter((e) => e.progress.percentage >= 100 || e.completedAt).length;
260
+ const expiredEnrollments = formattedEnrollments.filter((e) => e.status === 'expired').length;
261
+ const averageProgress = totalEnrollments > 0 ? formattedEnrollments.reduce((sum, e) => sum + e.progress.percentage, 0) / totalEnrollments : 0;
262
+ const totalTimeSpent = formattedEnrollments.reduce((sum, e) => sum + (e.progress.totalTimeSpent || 0), 0);
263
+ const certificatesEarned = formattedEnrollments.filter((e) => e.certificateIssuedAt).length;
264
+
265
+ return {
266
+ userId,
267
+ totalEnrollments,
268
+ activeEnrollments,
269
+ completedEnrollments,
270
+ expiredEnrollments,
271
+ inProgressEnrollments: Math.max(0, totalEnrollments - completedEnrollments - expiredEnrollments),
272
+ averageProgressPercentage: Math.round(averageProgress * 100) / 100,
273
+ totalTimeSpent,
274
+ totalTimeSpentText: this.formatDuration(totalTimeSpent),
275
+ certificatesEarned,
276
+ enrollmentsByStatus: {
277
+ active: activeEnrollments,
278
+ completed: completedEnrollments,
279
+ expired: expiredEnrollments,
280
+ },
281
+ };
282
+ }
283
+
284
+ /**
285
+ * Define the parameters this action expects
286
+ */
287
+ public get Params(): ActionParam[] {
288
+ const baseParams = this.getCommonLMSParams();
289
+ const specificParams: ActionParam[] = [
290
+ {
291
+ Name: 'UserID',
292
+ Type: 'Input',
293
+ Value: null,
294
+ },
295
+ {
296
+ Name: 'Status',
297
+ Type: 'Input',
298
+ Value: null,
299
+ },
300
+ {
301
+ Name: 'IncludeExpired',
302
+ Type: 'Input',
303
+ Value: false,
304
+ },
305
+ {
306
+ Name: 'IncludeCourseDetails',
307
+ Type: 'Input',
308
+ Value: true,
309
+ },
310
+ {
311
+ Name: 'SortBy',
312
+ Type: 'Input',
313
+ Value: 'enrolled_at',
314
+ },
315
+ {
316
+ Name: 'SortOrder',
317
+ Type: 'Input',
318
+ Value: 'desc',
319
+ },
320
+ {
321
+ Name: 'MaxResults',
322
+ Type: 'Input',
323
+ Value: 100,
324
+ },
325
+ {
326
+ Name: 'Enrollments',
327
+ Type: 'Output',
328
+ Value: null,
329
+ },
330
+ {
331
+ Name: 'TotalCount',
332
+ Type: 'Output',
333
+ Value: null,
334
+ },
335
+ {
336
+ Name: 'Summary',
337
+ Type: 'Output',
338
+ Value: null,
339
+ },
340
+ ];
341
+ return [...baseParams, ...specificParams];
342
+ }
343
+ }