@memberjunction/actions-bizapps-lms 5.3.1 → 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 +16 -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,277 +2,315 @@ 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 { UpdateUserProgressParams, UpdateUserProgressResult, ProgressDetails, ProgressUpdateSummary, ProgressUpdateResult } from '../interfaces';
7
+
8
+ // ----------------------------------------------------------------
9
+ // File-local interfaces for raw LearnWorlds API shapes
10
+ // ----------------------------------------------------------------
11
+
12
+ /** Shape returned by the enrollment / progress GET endpoints */
13
+ interface LWEnrollmentProgress {
14
+ progress_percentage?: number;
15
+ completed_units?: number;
16
+ total_units?: number;
17
+ total_time_spent?: number;
18
+ last_accessed_at?: string;
19
+ completed?: boolean;
20
+ completed_at?: string;
21
+ }
22
+
23
+ /** Request body sent to the lesson-level progress PUT endpoint */
24
+ interface LWLessonProgressRequest {
25
+ completed: boolean;
26
+ progress_percentage?: number;
27
+ time_spent?: number;
28
+ score?: number;
29
+ notes?: string;
30
+ }
31
+
32
+ /** Request body sent to the course-level progress PUT endpoint */
33
+ interface LWCourseProgressRequest {
34
+ progress_percentage?: number;
35
+ completed?: boolean;
36
+ total_time_spent?: number;
37
+ }
38
+
5
39
 
6
40
  /**
7
41
  * Action to update a user's course progress in LearnWorlds
8
42
  */
9
43
  @RegisterClass(BaseAction, 'UpdateUserProgressAction')
10
44
  export class UpdateUserProgressAction extends LearnWorldsBaseAction {
11
- /**
12
- * Update user progress for a course or lesson
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 courseId = this.getParamValue(Params, 'CourseID');
22
- const lessonId = this.getParamValue(Params, 'LessonID');
23
- const progressPercentage = this.getParamValue(Params, 'ProgressPercentage');
24
- const completed = this.getParamValue(Params, 'Completed');
25
- const timeSpent = this.getParamValue(Params, 'TimeSpent');
26
- const score = this.getParamValue(Params, 'Score');
27
- const notes = this.getParamValue(Params, 'Notes');
28
-
29
- if (!userId) {
30
- return {
31
- Success: false,
32
- ResultCode: 'VALIDATION_ERROR',
33
- Message: 'UserID is required',
34
- Params
35
- };
36
- }
37
-
38
- if (!courseId) {
39
- return {
40
- Success: false,
41
- ResultCode: 'VALIDATION_ERROR',
42
- Message: 'CourseID is required',
43
- Params
44
- };
45
- }
46
-
47
- // Get current enrollment first
48
- let currentEnrollment: any;
49
- try {
50
- currentEnrollment = await this.makeLearnWorldsRequest<any>(
51
- `users/${userId}/enrollments/${courseId}`,
52
- 'GET',
53
- undefined,
54
- ContextUser
55
- );
56
- } catch (error) {
57
- return {
58
- Success: false,
59
- ResultCode: 'NOT_ENROLLED',
60
- Message: 'User is not enrolled in this course',
61
- Params
62
- };
63
- }
64
- let updateResult: any = {};
65
-
66
- // Update lesson progress if lessonId is provided
67
- if (lessonId) {
68
- const lessonProgressData: any = {
69
- completed: completed !== undefined ? completed : false,
70
- progress_percentage: progressPercentage
71
- };
72
-
73
- if (timeSpent !== undefined) {
74
- lessonProgressData.time_spent = timeSpent;
75
- }
76
-
77
- if (score !== undefined) {
78
- lessonProgressData.score = score;
79
- }
80
-
81
- if (notes) {
82
- lessonProgressData.notes = notes;
83
- }
84
-
85
- try {
86
- const lessonUpdateData = await this.makeLearnWorldsRequest<any>(
87
- `users/${userId}/courses/${courseId}/lessons/${lessonId}/progress`,
88
- 'PUT',
89
- lessonProgressData,
90
- ContextUser
91
- );
92
- updateResult.lessonProgress = lessonUpdateData;
93
- } catch (error) {
94
- return {
95
- Success: false,
96
- ResultCode: 'UPDATE_FAILED',
97
- Message: error instanceof Error ? error.message : 'Failed to update lesson progress',
98
- Params
99
- };
100
- }
101
- }
102
-
103
- // Update overall course progress if progressPercentage is provided at course level
104
- if (progressPercentage !== undefined && !lessonId) {
105
- const courseProgressData: any = {
106
- progress_percentage: progressPercentage
107
- };
108
-
109
- if (completed !== undefined) {
110
- courseProgressData.completed = completed;
111
- }
112
-
113
- if (timeSpent !== undefined) {
114
- courseProgressData.total_time_spent = (currentEnrollment.total_time_spent || 0) + timeSpent;
115
- }
116
-
117
- try {
118
- const courseUpdateData = await this.makeLearnWorldsRequest<any>(
119
- `users/${userId}/enrollments/${courseId}/progress`,
120
- 'PUT',
121
- courseProgressData,
122
- ContextUser
123
- );
124
- updateResult.courseProgress = courseUpdateData;
125
- } catch (error) {
126
- return {
127
- Success: false,
128
- ResultCode: 'UPDATE_FAILED',
129
- Message: error instanceof Error ? error.message : 'Failed to update course progress',
130
- Params
131
- };
132
- }
133
- }
134
-
135
- // Get updated enrollment details
136
- let updatedProgress = currentEnrollment;
137
- try {
138
- updatedProgress = await this.makeLearnWorldsRequest<any>(
139
- `users/${userId}/enrollments/${courseId}`,
140
- 'GET',
141
- undefined,
142
- ContextUser
143
- );
144
- } catch (error) {
145
- // If we can't get updated details, use current enrollment
146
- console.warn('Failed to get updated enrollment details:', error);
147
- }
148
-
149
- // Format progress details
150
- const progressDetails = {
151
- userId: userId,
152
- courseId: courseId,
153
- lessonId: lessonId,
154
- previousProgress: {
155
- percentage: currentEnrollment.progress_percentage || 0,
156
- completedUnits: currentEnrollment.completed_units || 0,
157
- totalTimeSpent: currentEnrollment.total_time_spent || 0
158
- },
159
- updatedProgress: {
160
- percentage: updatedProgress.progress_percentage || 0,
161
- completedUnits: updatedProgress.completed_units || 0,
162
- totalUnits: updatedProgress.total_units || 0,
163
- totalTimeSpent: updatedProgress.total_time_spent || 0,
164
- totalTimeSpentText: this.formatDuration(updatedProgress.total_time_spent || 0),
165
- lastAccessedAt: updatedProgress.last_accessed_at || new Date().toISOString(),
166
- completed: updatedProgress.completed || false,
167
- completedAt: updatedProgress.completed_at
168
- },
169
- updateType: lessonId ? 'lesson' : 'course',
170
- updateResult: updateResult
171
- };
172
-
173
- // Create summary
174
- const summary = {
175
- userId: userId,
176
- courseId: courseId,
177
- lessonId: lessonId,
178
- progressIncreased: (updatedProgress.progress_percentage || 0) > (currentEnrollment.progress_percentage || 0),
179
- previousPercentage: currentEnrollment.progress_percentage || 0,
180
- newPercentage: updatedProgress.progress_percentage || 0,
181
- timeAdded: timeSpent || 0,
182
- totalTimeSpent: updatedProgress.total_time_spent || 0,
183
- isCompleted: updatedProgress.completed || false,
184
- updateType: progressDetails.updateType
185
- };
186
-
187
- // Update output parameters
188
- const outputParams = [...Params];
189
- const progressDetailsParam = outputParams.find(p => p.Name === 'ProgressDetails');
190
- if (progressDetailsParam) progressDetailsParam.Value = progressDetails;
191
- const summaryParam = outputParams.find(p => p.Name === 'Summary');
192
- if (summaryParam) summaryParam.Value = summary;
193
-
194
- return {
195
- Success: true,
196
- ResultCode: 'SUCCESS',
197
- Message: `Successfully updated ${progressDetails.updateType} progress`,
198
- Params: outputParams
199
- };
200
-
201
- } catch (error) {
202
- const errorMessage = error instanceof Error ? error.message : 'Unknown error occurred';
203
- return {
204
- Success: false,
205
- ResultCode: 'ERROR',
206
- Message: `Error updating progress: ${errorMessage}`,
207
- Params
208
- };
209
- }
45
+ // ----------------------------------------------------------------
46
+ // Typed public method – can be called directly from code
47
+ // ----------------------------------------------------------------
48
+
49
+ /**
50
+ * Update user progress for a course or lesson.
51
+ * Throws on any error.
52
+ */
53
+ public async UpdateProgress(params: UpdateUserProgressParams, contextUser: UserInfo): Promise<UpdateUserProgressResult> {
54
+ this.SetCompanyContext(params.CompanyID);
55
+
56
+ const { UserID, CourseID, LessonID, ProgressPercentage, Completed, TimeSpent, Score, Notes } = params;
57
+
58
+ // Validate required fields
59
+ if (!UserID) {
60
+ throw new Error('UserID is required');
61
+ }
62
+ if (!CourseID) {
63
+ throw new Error('CourseID is required');
64
+ }
65
+
66
+ this.validatePathSegment(UserID, 'UserID');
67
+ this.validatePathSegment(CourseID, 'CourseID');
68
+ if (LessonID) {
69
+ this.validatePathSegment(LessonID, 'LessonID');
70
+ }
71
+
72
+ if (ProgressPercentage !== undefined && (ProgressPercentage < 0 || ProgressPercentage > 100)) {
73
+ throw new Error('ProgressPercentage must be between 0 and 100');
74
+ }
75
+
76
+ // Get current enrollment first
77
+ const currentEnrollment = await this.fetchCurrentEnrollment(UserID, CourseID, contextUser);
78
+
79
+ const updateResult: ProgressUpdateResult = {};
80
+
81
+ // Update lesson progress if lessonId is provided
82
+ if (LessonID) {
83
+ updateResult.lessonProgress = await this.updateLessonProgress(
84
+ UserID,
85
+ CourseID,
86
+ LessonID,
87
+ { Completed, ProgressPercentage, TimeSpent, Score, Notes },
88
+ contextUser,
89
+ );
90
+ }
91
+
92
+ // Update overall course progress if progressPercentage is provided at course level
93
+ if (ProgressPercentage !== undefined && !LessonID) {
94
+ updateResult.courseProgress = await this.updateCourseProgress(
95
+ UserID,
96
+ CourseID,
97
+ currentEnrollment,
98
+ { Completed, ProgressPercentage, TimeSpent },
99
+ contextUser,
100
+ );
101
+ }
102
+
103
+ // Get updated enrollment details
104
+ const updatedProgress = await this.fetchUpdatedEnrollment(UserID, CourseID, currentEnrollment, contextUser);
105
+
106
+ // Build typed result
107
+ const updateType: 'lesson' | 'course' = LessonID ? 'lesson' : 'course';
108
+
109
+ const progressDetails = this.buildProgressDetails(UserID, CourseID, LessonID, currentEnrollment, updatedProgress, updateType, updateResult);
110
+
111
+ const summary = this.buildProgressSummary(UserID, CourseID, LessonID, currentEnrollment, updatedProgress, TimeSpent, updateType);
112
+
113
+ return { ProgressDetails: progressDetails, Summary: summary };
114
+ }
115
+
116
+ // ----------------------------------------------------------------
117
+ // Framework wrapper – thin delegation to the public method
118
+ // ----------------------------------------------------------------
119
+
120
+ protected async InternalRunAction(params: RunActionParams): Promise<ActionResultSimple> {
121
+ const { Params, ContextUser } = params;
122
+ this.params = Params;
123
+
124
+ try {
125
+ const typedParams = this.extractUpdateProgressParams(Params);
126
+ const result = await this.UpdateProgress(typedParams, ContextUser);
127
+
128
+ this.setOutputParam(Params, 'ProgressDetails', result.ProgressDetails);
129
+ this.setOutputParam(Params, 'Summary', result.Summary);
130
+
131
+ return this.buildSuccessResult(`Successfully updated ${result.ProgressDetails.updateType} progress`, Params);
132
+ } catch (error) {
133
+ const msg = error instanceof Error ? error.message : 'Unknown error';
134
+ return this.buildErrorResult('ERROR', `Error updating progress: ${msg}`, Params);
135
+ }
136
+ }
137
+
138
+ // ----------------------------------------------------------------
139
+ // Private helpers
140
+ // ----------------------------------------------------------------
141
+
142
+ private extractUpdateProgressParams(params: ActionParam[]): UpdateUserProgressParams {
143
+ return {
144
+ CompanyID: this.getRequiredStringParam(params, 'CompanyID'),
145
+ UserID: this.getRequiredStringParam(params, 'UserID'),
146
+ CourseID: this.getRequiredStringParam(params, 'CourseID'),
147
+ LessonID: this.getOptionalStringParam(params, 'LessonID'),
148
+ ProgressPercentage: this.getOptionalNumberParam(params, 'ProgressPercentage', undefined),
149
+ Completed: this.getOptionalBooleanParam(params, 'Completed', undefined),
150
+ TimeSpent: this.getOptionalNumberParam(params, 'TimeSpent', undefined),
151
+ Score: this.getOptionalNumberParam(params, 'Score', undefined),
152
+ Notes: this.getOptionalStringParam(params, 'Notes'),
153
+ };
154
+ }
155
+
156
+ private async fetchCurrentEnrollment(userId: string, courseId: string, contextUser: UserInfo): Promise<LWEnrollmentProgress> {
157
+ try {
158
+ return await this.makeLearnWorldsRequest<LWEnrollmentProgress>(`users/${userId}/enrollments/${courseId}`, 'GET', undefined, contextUser);
159
+ } catch {
160
+ throw new Error('User is not enrolled in this course');
161
+ }
162
+ }
163
+
164
+ private async updateLessonProgress(
165
+ userId: string,
166
+ courseId: string,
167
+ lessonId: string,
168
+ data: { Completed?: boolean; ProgressPercentage?: number; TimeSpent?: number; Score?: number; Notes?: string },
169
+ contextUser: UserInfo,
170
+ ): Promise<Record<string, unknown>> {
171
+ const body: LWLessonProgressRequest = {
172
+ completed: data.Completed !== undefined ? data.Completed : false,
173
+ progress_percentage: data.ProgressPercentage,
174
+ };
175
+
176
+ if (data.TimeSpent !== undefined) {
177
+ body.time_spent = data.TimeSpent;
178
+ }
179
+ if (data.Score !== undefined) {
180
+ body.score = data.Score;
181
+ }
182
+ if (data.Notes) {
183
+ body.notes = data.Notes;
210
184
  }
211
185
 
212
- /**
213
- * Define the parameters this action expects
214
- */
215
- public get Params(): ActionParam[] {
216
- const baseParams = this.getCommonLMSParams();
217
- const specificParams: ActionParam[] = [
218
- {
219
- Name: 'UserID',
220
- Type: 'Input',
221
- Value: null
222
- },
223
- {
224
- Name: 'CourseID',
225
- Type: 'Input',
226
- Value: null
227
- },
228
- {
229
- Name: 'LessonID',
230
- Type: 'Input',
231
- Value: null
232
- },
233
- {
234
- Name: 'ProgressPercentage',
235
- Type: 'Input',
236
- Value: null
237
- },
238
- {
239
- Name: 'Completed',
240
- Type: 'Input',
241
- Value: null
242
- },
243
- {
244
- Name: 'TimeSpent',
245
- Type: 'Input',
246
- Value: null
247
- },
248
- {
249
- Name: 'Score',
250
- Type: 'Input',
251
- Value: null
252
- },
253
- {
254
- Name: 'Notes',
255
- Type: 'Input',
256
- Value: null
257
- },
258
- {
259
- Name: 'ProgressDetails',
260
- Type: 'Output',
261
- Value: null
262
- },
263
- {
264
- Name: 'Summary',
265
- Type: 'Output',
266
- Value: null
267
- }
268
- ];
269
- return [...baseParams, ...specificParams];
186
+ return await this.makeLearnWorldsRequest<Record<string, unknown>>(
187
+ `users/${userId}/courses/${courseId}/lessons/${lessonId}/progress`,
188
+ 'PUT',
189
+ body,
190
+ contextUser,
191
+ );
192
+ }
193
+
194
+ private async updateCourseProgress(
195
+ userId: string,
196
+ courseId: string,
197
+ currentEnrollment: LWEnrollmentProgress,
198
+ data: { Completed?: boolean; ProgressPercentage?: number; TimeSpent?: number },
199
+ contextUser: UserInfo,
200
+ ): Promise<Record<string, unknown>> {
201
+ const body: LWCourseProgressRequest = {
202
+ progress_percentage: data.ProgressPercentage,
203
+ };
204
+
205
+ if (data.Completed !== undefined) {
206
+ body.completed = data.Completed;
207
+ }
208
+ if (data.TimeSpent !== undefined) {
209
+ body.total_time_spent = (currentEnrollment.total_time_spent || 0) + data.TimeSpent;
270
210
  }
271
211
 
272
- /**
273
- * Metadata about this action
274
- */
275
- public get Description(): string {
276
- return 'Updates a user\'s progress for a course or specific lesson in LearnWorlds';
212
+ return await this.makeLearnWorldsRequest<Record<string, unknown>>(
213
+ `users/${userId}/enrollments/${courseId}/progress`,
214
+ 'PUT',
215
+ body,
216
+ contextUser,
217
+ );
218
+ }
219
+
220
+ private async fetchUpdatedEnrollment(userId: string, courseId: string, fallback: LWEnrollmentProgress, contextUser: UserInfo): Promise<LWEnrollmentProgress> {
221
+ try {
222
+ return await this.makeLearnWorldsRequest<LWEnrollmentProgress>(`users/${userId}/enrollments/${courseId}`, 'GET', undefined, contextUser);
223
+ } catch (error) {
224
+ // If we can't get updated details, use previous enrollment
225
+ console.warn('Failed to get updated enrollment details:', error);
226
+ return fallback;
277
227
  }
278
- }
228
+ }
229
+
230
+ private buildProgressDetails(
231
+ userId: string,
232
+ courseId: string,
233
+ lessonId: string | undefined,
234
+ currentEnrollment: LWEnrollmentProgress,
235
+ updatedProgress: LWEnrollmentProgress,
236
+ updateType: 'lesson' | 'course',
237
+ updateResult: ProgressUpdateResult,
238
+ ): ProgressDetails {
239
+ return {
240
+ userId,
241
+ courseId,
242
+ lessonId,
243
+ previousProgress: {
244
+ percentage: currentEnrollment.progress_percentage || 0,
245
+ completedUnits: currentEnrollment.completed_units || 0,
246
+ totalTimeSpent: currentEnrollment.total_time_spent || 0,
247
+ },
248
+ updatedProgress: {
249
+ percentage: updatedProgress.progress_percentage || 0,
250
+ completedUnits: updatedProgress.completed_units || 0,
251
+ totalUnits: updatedProgress.total_units || 0,
252
+ totalTimeSpent: updatedProgress.total_time_spent || 0,
253
+ totalTimeSpentText: this.formatDuration(updatedProgress.total_time_spent || 0),
254
+ lastAccessedAt: updatedProgress.last_accessed_at || new Date().toISOString(),
255
+ completed: updatedProgress.completed || false,
256
+ completedAt: updatedProgress.completed_at,
257
+ },
258
+ updateType,
259
+ updateResult,
260
+ };
261
+ }
262
+
263
+ private buildProgressSummary(
264
+ userId: string,
265
+ courseId: string,
266
+ lessonId: string | undefined,
267
+ currentEnrollment: LWEnrollmentProgress,
268
+ updatedProgress: LWEnrollmentProgress,
269
+ timeSpent: number | undefined,
270
+ updateType: 'lesson' | 'course',
271
+ ): ProgressUpdateSummary {
272
+ return {
273
+ userId,
274
+ courseId,
275
+ lessonId,
276
+ progressIncreased: (updatedProgress.progress_percentage || 0) > (currentEnrollment.progress_percentage || 0),
277
+ previousPercentage: currentEnrollment.progress_percentage || 0,
278
+ newPercentage: updatedProgress.progress_percentage || 0,
279
+ timeAdded: timeSpent || 0,
280
+ totalTimeSpent: updatedProgress.total_time_spent || 0,
281
+ isCompleted: updatedProgress.completed || false,
282
+ updateType,
283
+ };
284
+ }
285
+
286
+ // ----------------------------------------------------------------
287
+ // Params & Description metadata
288
+ // ----------------------------------------------------------------
289
+
290
+ /**
291
+ * Define the parameters this action expects
292
+ */
293
+ public get Params(): ActionParam[] {
294
+ const baseParams = this.getCommonLMSParams();
295
+ const specificParams: ActionParam[] = [
296
+ { Name: 'UserID', Type: 'Input', Value: null },
297
+ { Name: 'CourseID', Type: 'Input', Value: null },
298
+ { Name: 'LessonID', Type: 'Input', Value: null },
299
+ { Name: 'ProgressPercentage', Type: 'Input', Value: null },
300
+ { Name: 'Completed', Type: 'Input', Value: null },
301
+ { Name: 'TimeSpent', Type: 'Input', Value: null },
302
+ { Name: 'Score', Type: 'Input', Value: null },
303
+ { Name: 'Notes', Type: 'Input', Value: null },
304
+ { Name: 'ProgressDetails', Type: 'Output', Value: null },
305
+ { Name: 'Summary', Type: 'Output', Value: null },
306
+ ];
307
+ return [...baseParams, ...specificParams];
308
+ }
309
+
310
+ /**
311
+ * Metadata about this action
312
+ */
313
+ public get Description(): string {
314
+ return "Updates a user's progress for a course or specific lesson in LearnWorlds";
315
+ }
316
+ }