@memberjunction/actions-bizapps-lms 5.3.0 → 5.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (194) hide show
  1. package/.turbo/turbo-build.log +1 -1
  2. package/CHANGELOG.md +26 -0
  3. package/LEARNWORLDS_SIDECAR_PLAN.md +845 -0
  4. package/dist/base/base-lms.action.d.ts +26 -4
  5. package/dist/base/base-lms.action.d.ts.map +1 -1
  6. package/dist/base/base-lms.action.js +73 -17
  7. package/dist/base/base-lms.action.js.map +1 -1
  8. package/dist/providers/learnworlds/actions/attach-tags.action.d.ts +26 -0
  9. package/dist/providers/learnworlds/actions/attach-tags.action.d.ts.map +1 -0
  10. package/dist/providers/learnworlds/actions/attach-tags.action.js +79 -0
  11. package/dist/providers/learnworlds/actions/attach-tags.action.js.map +1 -0
  12. package/dist/providers/learnworlds/actions/create-user.action.d.ts +24 -1
  13. package/dist/providers/learnworlds/actions/create-user.action.d.ts.map +1 -1
  14. package/dist/providers/learnworlds/actions/create-user.action.js +166 -142
  15. package/dist/providers/learnworlds/actions/create-user.action.js.map +1 -1
  16. package/dist/providers/learnworlds/actions/detach-tags.action.d.ts +26 -0
  17. package/dist/providers/learnworlds/actions/detach-tags.action.d.ts.map +1 -0
  18. package/dist/providers/learnworlds/actions/detach-tags.action.js +79 -0
  19. package/dist/providers/learnworlds/actions/detach-tags.action.js.map +1 -0
  20. package/dist/providers/learnworlds/actions/enroll-user.action.d.ts +31 -3
  21. package/dist/providers/learnworlds/actions/enroll-user.action.d.ts.map +1 -1
  22. package/dist/providers/learnworlds/actions/enroll-user.action.js +179 -125
  23. package/dist/providers/learnworlds/actions/enroll-user.action.js.map +1 -1
  24. package/dist/providers/learnworlds/actions/get-bulk-data.action.d.ts +79 -0
  25. package/dist/providers/learnworlds/actions/get-bulk-data.action.d.ts.map +1 -0
  26. package/dist/providers/learnworlds/actions/get-bulk-data.action.js +359 -0
  27. package/dist/providers/learnworlds/actions/get-bulk-data.action.js.map +1 -0
  28. package/dist/providers/learnworlds/actions/get-bundles.action.d.ts +34 -0
  29. package/dist/providers/learnworlds/actions/get-bundles.action.d.ts.map +1 -0
  30. package/dist/providers/learnworlds/actions/get-bundles.action.js +107 -0
  31. package/dist/providers/learnworlds/actions/get-bundles.action.js.map +1 -0
  32. package/dist/providers/learnworlds/actions/get-certificates.action.d.ts +33 -2
  33. package/dist/providers/learnworlds/actions/get-certificates.action.d.ts.map +1 -1
  34. package/dist/providers/learnworlds/actions/get-certificates.action.js +311 -244
  35. package/dist/providers/learnworlds/actions/get-certificates.action.js.map +1 -1
  36. package/dist/providers/learnworlds/actions/get-course-analytics.action.d.ts +14 -14
  37. package/dist/providers/learnworlds/actions/get-course-analytics.action.d.ts.map +1 -1
  38. package/dist/providers/learnworlds/actions/get-course-analytics.action.js +234 -226
  39. package/dist/providers/learnworlds/actions/get-course-analytics.action.js.map +1 -1
  40. package/dist/providers/learnworlds/actions/get-course-details.action.d.ts +42 -6
  41. package/dist/providers/learnworlds/actions/get-course-details.action.d.ts.map +1 -1
  42. package/dist/providers/learnworlds/actions/get-course-details.action.js +182 -148
  43. package/dist/providers/learnworlds/actions/get-course-details.action.js.map +1 -1
  44. package/dist/providers/learnworlds/actions/get-courses.action.d.ts +29 -82
  45. package/dist/providers/learnworlds/actions/get-courses.action.d.ts.map +1 -1
  46. package/dist/providers/learnworlds/actions/get-courses.action.js +188 -207
  47. package/dist/providers/learnworlds/actions/get-courses.action.js.map +1 -1
  48. package/dist/providers/learnworlds/actions/get-quiz-results.action.d.ts +22 -2
  49. package/dist/providers/learnworlds/actions/get-quiz-results.action.d.ts.map +1 -1
  50. package/dist/providers/learnworlds/actions/get-quiz-results.action.js +316 -294
  51. package/dist/providers/learnworlds/actions/get-quiz-results.action.js.map +1 -1
  52. package/dist/providers/learnworlds/actions/get-user-details.action.d.ts +23 -59
  53. package/dist/providers/learnworlds/actions/get-user-details.action.d.ts.map +1 -1
  54. package/dist/providers/learnworlds/actions/get-user-details.action.js +113 -114
  55. package/dist/providers/learnworlds/actions/get-user-details.action.js.map +1 -1
  56. package/dist/providers/learnworlds/actions/get-user-enrollments.action.d.ts +43 -5
  57. package/dist/providers/learnworlds/actions/get-user-enrollments.action.d.ts.map +1 -1
  58. package/dist/providers/learnworlds/actions/get-user-enrollments.action.js +191 -147
  59. package/dist/providers/learnworlds/actions/get-user-enrollments.action.js.map +1 -1
  60. package/dist/providers/learnworlds/actions/get-user-progress.action.d.ts +21 -92
  61. package/dist/providers/learnworlds/actions/get-user-progress.action.d.ts.map +1 -1
  62. package/dist/providers/learnworlds/actions/get-user-progress.action.js +145 -166
  63. package/dist/providers/learnworlds/actions/get-user-progress.action.js.map +1 -1
  64. package/dist/providers/learnworlds/actions/get-users.action.d.ts +14 -28
  65. package/dist/providers/learnworlds/actions/get-users.action.d.ts.map +1 -1
  66. package/dist/providers/learnworlds/actions/get-users.action.js +122 -143
  67. package/dist/providers/learnworlds/actions/get-users.action.js.map +1 -1
  68. package/dist/providers/learnworlds/actions/index.d.ts +7 -0
  69. package/dist/providers/learnworlds/actions/index.d.ts.map +1 -1
  70. package/dist/providers/learnworlds/actions/index.js +7 -0
  71. package/dist/providers/learnworlds/actions/index.js.map +1 -1
  72. package/dist/providers/learnworlds/actions/onboard-learner.action.d.ts +53 -0
  73. package/dist/providers/learnworlds/actions/onboard-learner.action.d.ts.map +1 -0
  74. package/dist/providers/learnworlds/actions/onboard-learner.action.js +236 -0
  75. package/dist/providers/learnworlds/actions/onboard-learner.action.js.map +1 -0
  76. package/dist/providers/learnworlds/actions/sso-login.action.d.ts +35 -0
  77. package/dist/providers/learnworlds/actions/sso-login.action.d.ts.map +1 -0
  78. package/dist/providers/learnworlds/actions/sso-login.action.js +101 -0
  79. package/dist/providers/learnworlds/actions/sso-login.action.js.map +1 -0
  80. package/dist/providers/learnworlds/actions/update-user-progress.action.d.ts +12 -1
  81. package/dist/providers/learnworlds/actions/update-user-progress.action.d.ts.map +1 -1
  82. package/dist/providers/learnworlds/actions/update-user-progress.action.js +167 -210
  83. package/dist/providers/learnworlds/actions/update-user-progress.action.js.map +1 -1
  84. package/dist/providers/learnworlds/actions/update-user.action.d.ts +39 -0
  85. package/dist/providers/learnworlds/actions/update-user.action.d.ts.map +1 -0
  86. package/dist/providers/learnworlds/actions/update-user.action.js +168 -0
  87. package/dist/providers/learnworlds/actions/update-user.action.js.map +1 -0
  88. package/dist/providers/learnworlds/index.d.ts +1 -0
  89. package/dist/providers/learnworlds/index.d.ts.map +1 -1
  90. package/dist/providers/learnworlds/index.js +2 -0
  91. package/dist/providers/learnworlds/index.js.map +1 -1
  92. package/dist/providers/learnworlds/interfaces/bundle.types.d.ts +16 -0
  93. package/dist/providers/learnworlds/interfaces/bundle.types.d.ts.map +1 -0
  94. package/dist/providers/learnworlds/interfaces/bundle.types.js +2 -0
  95. package/dist/providers/learnworlds/interfaces/bundle.types.js.map +1 -0
  96. package/dist/providers/learnworlds/interfaces/certificate.types.d.ts +66 -0
  97. package/dist/providers/learnworlds/interfaces/certificate.types.d.ts.map +1 -0
  98. package/dist/providers/learnworlds/interfaces/certificate.types.js +2 -0
  99. package/dist/providers/learnworlds/interfaces/certificate.types.js.map +1 -0
  100. package/dist/providers/learnworlds/interfaces/common.types.d.ts +221 -0
  101. package/dist/providers/learnworlds/interfaces/common.types.d.ts.map +1 -0
  102. package/dist/providers/learnworlds/interfaces/common.types.js +2 -0
  103. package/dist/providers/learnworlds/interfaces/common.types.js.map +1 -0
  104. package/dist/providers/learnworlds/interfaces/course.types.d.ts +393 -0
  105. package/dist/providers/learnworlds/interfaces/course.types.d.ts.map +1 -0
  106. package/dist/providers/learnworlds/interfaces/course.types.js +2 -0
  107. package/dist/providers/learnworlds/interfaces/course.types.js.map +1 -0
  108. package/dist/providers/learnworlds/interfaces/enrollment.types.d.ts +165 -0
  109. package/dist/providers/learnworlds/interfaces/enrollment.types.d.ts.map +1 -0
  110. package/dist/providers/learnworlds/interfaces/enrollment.types.js +2 -0
  111. package/dist/providers/learnworlds/interfaces/enrollment.types.js.map +1 -0
  112. package/dist/providers/learnworlds/interfaces/index.d.ts +12 -0
  113. package/dist/providers/learnworlds/interfaces/index.d.ts.map +1 -0
  114. package/dist/providers/learnworlds/interfaces/index.js +13 -0
  115. package/dist/providers/learnworlds/interfaces/index.js.map +1 -0
  116. package/dist/providers/learnworlds/interfaces/onboarding.types.d.ts +39 -0
  117. package/dist/providers/learnworlds/interfaces/onboarding.types.d.ts.map +1 -0
  118. package/dist/providers/learnworlds/interfaces/onboarding.types.js +2 -0
  119. package/dist/providers/learnworlds/interfaces/onboarding.types.js.map +1 -0
  120. package/dist/providers/learnworlds/interfaces/quiz.types.d.ts +108 -0
  121. package/dist/providers/learnworlds/interfaces/quiz.types.d.ts.map +1 -0
  122. package/dist/providers/learnworlds/interfaces/quiz.types.js +2 -0
  123. package/dist/providers/learnworlds/interfaces/quiz.types.js.map +1 -0
  124. package/dist/providers/learnworlds/interfaces/sso.types.d.ts +18 -0
  125. package/dist/providers/learnworlds/interfaces/sso.types.d.ts.map +1 -0
  126. package/dist/providers/learnworlds/interfaces/sso.types.js +2 -0
  127. package/dist/providers/learnworlds/interfaces/sso.types.js.map +1 -0
  128. package/dist/providers/learnworlds/interfaces/sync.types.d.ts +44 -0
  129. package/dist/providers/learnworlds/interfaces/sync.types.d.ts.map +1 -0
  130. package/dist/providers/learnworlds/interfaces/sync.types.js +2 -0
  131. package/dist/providers/learnworlds/interfaces/sync.types.js.map +1 -0
  132. package/dist/providers/learnworlds/interfaces/tag.types.d.ts +17 -0
  133. package/dist/providers/learnworlds/interfaces/tag.types.d.ts.map +1 -0
  134. package/dist/providers/learnworlds/interfaces/tag.types.js +2 -0
  135. package/dist/providers/learnworlds/interfaces/tag.types.js.map +1 -0
  136. package/dist/providers/learnworlds/interfaces/user.types.d.ts +198 -0
  137. package/dist/providers/learnworlds/interfaces/user.types.d.ts.map +1 -0
  138. package/dist/providers/learnworlds/interfaces/user.types.js +2 -0
  139. package/dist/providers/learnworlds/interfaces/user.types.js.map +1 -0
  140. package/dist/providers/learnworlds/learnworlds-base.action.d.ts +111 -7
  141. package/dist/providers/learnworlds/learnworlds-base.action.d.ts.map +1 -1
  142. package/dist/providers/learnworlds/learnworlds-base.action.js +293 -56
  143. package/dist/providers/learnworlds/learnworlds-base.action.js.map +1 -1
  144. package/package.json +8 -7
  145. package/src/__tests__/attach-tags.action.test.ts +137 -0
  146. package/src/__tests__/create-user.action.test.ts +311 -0
  147. package/src/__tests__/detach-tags.action.test.ts +136 -0
  148. package/src/__tests__/enroll-user.action.test.ts +283 -0
  149. package/src/__tests__/get-bulk-data.action.test.ts +649 -0
  150. package/src/__tests__/get-bundles.action.test.ts +215 -0
  151. package/src/__tests__/lms.test.ts +275 -184
  152. package/src/__tests__/onboard-learner.action.test.ts +455 -0
  153. package/src/__tests__/sso-login.action.test.ts +256 -0
  154. package/src/__tests__/update-user.action.test.ts +350 -0
  155. package/src/base/base-lms.action.ts +226 -163
  156. package/src/providers/learnworlds/actions/attach-tags.action.ts +90 -0
  157. package/src/providers/learnworlds/actions/create-user.action.ts +291 -235
  158. package/src/providers/learnworlds/actions/detach-tags.action.ts +90 -0
  159. package/src/providers/learnworlds/actions/enroll-user.action.ts +326 -220
  160. package/src/providers/learnworlds/actions/get-bulk-data.action.ts +438 -0
  161. package/src/providers/learnworlds/actions/get-bundles.action.ts +139 -0
  162. package/src/providers/learnworlds/actions/get-certificates.action.ts +499 -309
  163. package/src/providers/learnworlds/actions/get-course-analytics.action.ts +470 -337
  164. package/src/providers/learnworlds/actions/get-course-details.action.ts +395 -249
  165. package/src/providers/learnworlds/actions/get-courses.action.ts +508 -565
  166. package/src/providers/learnworlds/actions/get-quiz-results.action.ts +606 -381
  167. package/src/providers/learnworlds/actions/get-user-details.action.ts +335 -349
  168. package/src/providers/learnworlds/actions/get-user-enrollments.action.ts +328 -228
  169. package/src/providers/learnworlds/actions/get-user-progress.action.ts +522 -571
  170. package/src/providers/learnworlds/actions/get-users.action.ts +231 -282
  171. package/src/providers/learnworlds/actions/index.ts +8 -1
  172. package/src/providers/learnworlds/actions/onboard-learner.action.ts +286 -0
  173. package/src/providers/learnworlds/actions/sso-login.action.ts +116 -0
  174. package/src/providers/learnworlds/actions/update-user-progress.action.ts +301 -263
  175. package/src/providers/learnworlds/actions/update-user.action.ts +181 -0
  176. package/src/providers/learnworlds/index.ts +4 -1
  177. package/src/providers/learnworlds/interfaces/bundle.types.ts +17 -0
  178. package/src/providers/learnworlds/interfaces/certificate.types.ts +69 -0
  179. package/src/providers/learnworlds/interfaces/common.types.ts +233 -0
  180. package/src/providers/learnworlds/interfaces/course.types.ts +411 -0
  181. package/src/providers/learnworlds/interfaces/enrollment.types.ts +176 -0
  182. package/src/providers/learnworlds/interfaces/index.ts +12 -0
  183. package/src/providers/learnworlds/interfaces/onboarding.types.ts +41 -0
  184. package/src/providers/learnworlds/interfaces/quiz.types.ts +118 -0
  185. package/src/providers/learnworlds/interfaces/sso.types.ts +19 -0
  186. package/src/providers/learnworlds/interfaces/sync.types.ts +46 -0
  187. package/src/providers/learnworlds/interfaces/tag.types.ts +18 -0
  188. package/src/providers/learnworlds/interfaces/user.types.ts +204 -0
  189. package/src/providers/learnworlds/learnworlds-base.action.ts +468 -210
  190. package/tsconfig.json +1 -1
  191. package/dist/__tests__/lms.test.d.ts +0 -2
  192. package/dist/__tests__/lms.test.d.ts.map +0 -1
  193. package/dist/__tests__/lms.test.js +0 -232
  194. package/dist/__tests__/lms.test.js.map +0 -1
@@ -0,0 +1,438 @@
1
+ import { RegisterClass } from '@memberjunction/global';
2
+ import { LearnWorldsBaseAction } from '../learnworlds-base.action';
3
+ import { ActionParam, ActionResultSimple, RunActionParams } from '@memberjunction/actions-base';
4
+ import { BaseAction } from '@memberjunction/actions';
5
+ import { UserInfo } from '@memberjunction/core';
6
+ import {
7
+ BulkDataParams,
8
+ LearnWorldsSyncPayload,
9
+ SyncError,
10
+ LearnWorldsUser,
11
+ LearnWorldsCourse,
12
+ LearnWorldsBundle,
13
+ LearnWorldsEnrollment,
14
+ LearnWorldsCertificate,
15
+ UserLearningProgress,
16
+ } from '../interfaces';
17
+ import { FormattedQuizResult } from '../interfaces/quiz.types';
18
+ import { GetLearnWorldsUsersAction } from './get-users.action';
19
+ import { GetLearnWorldsCoursesAction } from './get-courses.action';
20
+ import { GetBundlesAction } from './get-bundles.action';
21
+ import { GetUserEnrollmentsAction } from './get-user-enrollments.action';
22
+ import { GetLearnWorldsUserProgressAction } from './get-user-progress.action';
23
+ import { GetCertificatesAction } from './get-certificates.action';
24
+ import { GetQuizResultsAction } from './get-quiz-results.action';
25
+
26
+ /**
27
+ * Orchestrates bulk data retrieval from all LearnWorlds retrieval actions
28
+ * into a single LearnWorldsSyncPayload for downstream consumers.
29
+ */
30
+ @RegisterClass(BaseAction, 'GetLearnWorldsBulkDataAction')
31
+ export class GetLearnWorldsBulkDataAction extends LearnWorldsBaseAction {
32
+ /**
33
+ * Retrieves bulk data from LearnWorlds, orchestrating multiple retrieval actions.
34
+ * Each data type is optional and controlled via Include* boolean params.
35
+ * Partial failures are collected into errors without aborting the whole operation.
36
+ */
37
+ public async GetBulkData(params: BulkDataParams, contextUser: UserInfo): Promise<LearnWorldsSyncPayload> {
38
+ this.SetCompanyContext(params.CompanyID);
39
+
40
+ const errors: SyncError[] = [];
41
+ let totalApiCalls = 0;
42
+ const includeAll = this.shouldIncludeAll(params);
43
+ const maxResults = params.MaxResultsPerEntity ?? 100;
44
+
45
+ // Fetch users first (enrollments and progress depend on user IDs)
46
+ let users: LearnWorldsUser[] | undefined;
47
+ if (includeAll || params.IncludeUsers || params.IncludeEnrollments || params.IncludeProgress) {
48
+ const result = await this.fetchUsers(params.CompanyID, maxResults, contextUser, errors);
49
+ users = result.data;
50
+ totalApiCalls += result.apiCalls;
51
+ }
52
+
53
+ // Fetch remaining data types in parallel
54
+ const [coursesResult, bundlesResult, enrollmentsResult, progressResult, certificatesResult, quizResultsResult] = await Promise.all([
55
+ this.shouldFetch(includeAll, params.IncludeCourses) ? this.fetchCourses(params.CompanyID, maxResults, contextUser, errors) : Promise.resolve(undefined),
56
+ this.shouldFetch(includeAll, params.IncludeBundles) ? this.fetchBundles(params.CompanyID, maxResults, contextUser, errors) : Promise.resolve(undefined),
57
+ this.shouldFetch(includeAll, params.IncludeEnrollments) ? this.fetchEnrollmentsForUsers(params.CompanyID, users || [], maxResults, contextUser, errors) : Promise.resolve(undefined),
58
+ this.shouldFetch(includeAll, params.IncludeProgress) ? this.fetchProgressForUsers(params.CompanyID, users || [], contextUser, errors) : Promise.resolve(undefined),
59
+ this.shouldFetch(includeAll, params.IncludeCertificates) ? this.fetchCertificates(params.CompanyID, maxResults, contextUser, errors) : Promise.resolve(undefined),
60
+ this.shouldFetch(includeAll, params.IncludeQuizResults) ? this.fetchQuizResults(params.CompanyID, maxResults, contextUser, errors) : Promise.resolve(undefined),
61
+ ]);
62
+
63
+ if (coursesResult) totalApiCalls += coursesResult.apiCalls;
64
+ if (bundlesResult) totalApiCalls += bundlesResult.apiCalls;
65
+ if (enrollmentsResult) totalApiCalls += enrollmentsResult.apiCalls;
66
+ if (progressResult) totalApiCalls += progressResult.apiCalls;
67
+ if (certificatesResult) totalApiCalls += certificatesResult.apiCalls;
68
+ if (quizResultsResult) totalApiCalls += quizResultsResult.apiCalls;
69
+
70
+ // Only include user data in output if explicitly requested (not just fetched for enrollments)
71
+ const includeUsersInOutput = includeAll || params.IncludeUsers === true;
72
+
73
+ return {
74
+ users: includeUsersInOutput ? users : undefined,
75
+ courses: coursesResult?.data,
76
+ bundles: bundlesResult?.data,
77
+ enrollments: enrollmentsResult?.data,
78
+ progress: progressResult?.data,
79
+ certificates: certificatesResult?.data,
80
+ quizResults: quizResultsResult?.data,
81
+ syncedAt: new Date().toISOString(),
82
+ companyId: params.CompanyID,
83
+ totalApiCalls,
84
+ errors,
85
+ };
86
+ }
87
+
88
+ /**
89
+ * If no Include* flags are explicitly set, include everything by default.
90
+ */
91
+ private shouldIncludeAll(params: BulkDataParams): boolean {
92
+ return (
93
+ params.IncludeUsers === undefined &&
94
+ params.IncludeCourses === undefined &&
95
+ params.IncludeBundles === undefined &&
96
+ params.IncludeEnrollments === undefined &&
97
+ params.IncludeProgress === undefined &&
98
+ params.IncludeCertificates === undefined &&
99
+ params.IncludeQuizResults === undefined
100
+ );
101
+ }
102
+
103
+ /**
104
+ * Helper to determine if a data type should be fetched.
105
+ */
106
+ private shouldFetch(includeAll: boolean, flag: boolean | undefined): boolean {
107
+ return includeAll || flag === true;
108
+ }
109
+
110
+ /**
111
+ * Fetch users, collecting errors on failure.
112
+ */
113
+ private async fetchUsers(
114
+ companyId: string,
115
+ maxResults: number,
116
+ contextUser: UserInfo,
117
+ errors: SyncError[],
118
+ ): Promise<{ data: LearnWorldsUser[]; apiCalls: number }> {
119
+ const action = new GetLearnWorldsUsersAction();
120
+ try {
121
+ const result = await action.GetUsers({ CompanyID: companyId, MaxResults: maxResults }, contextUser);
122
+ return { data: result.Users, apiCalls: action.ApiCallCount };
123
+ } catch (error) {
124
+ this.collectError(errors, 'user', error);
125
+ return { data: [], apiCalls: action.ApiCallCount || 1 };
126
+ }
127
+ }
128
+
129
+ /**
130
+ * Fetch courses, collecting errors on failure.
131
+ */
132
+ private async fetchCourses(
133
+ companyId: string,
134
+ maxResults: number,
135
+ contextUser: UserInfo,
136
+ errors: SyncError[],
137
+ ): Promise<{ data: LearnWorldsCourse[]; apiCalls: number }> {
138
+ const action = new GetLearnWorldsCoursesAction();
139
+ try {
140
+ const result = await action.GetCourses({ CompanyID: companyId, MaxResults: maxResults }, contextUser);
141
+ return { data: result.Courses, apiCalls: action.ApiCallCount };
142
+ } catch (error) {
143
+ this.collectError(errors, 'course', error);
144
+ return { data: [], apiCalls: action.ApiCallCount || 1 };
145
+ }
146
+ }
147
+
148
+ /**
149
+ * Fetch bundles, collecting errors on failure.
150
+ */
151
+ private async fetchBundles(
152
+ companyId: string,
153
+ maxResults: number,
154
+ contextUser: UserInfo,
155
+ errors: SyncError[],
156
+ ): Promise<{ data: LearnWorldsBundle[]; apiCalls: number }> {
157
+ const action = new GetBundlesAction();
158
+ try {
159
+ const result = await action.GetBundles({ CompanyID: companyId, MaxResults: maxResults }, contextUser);
160
+ return { data: result.Bundles, apiCalls: action.ApiCallCount };
161
+ } catch (error) {
162
+ this.collectError(errors, 'bundle', error);
163
+ return { data: [], apiCalls: action.ApiCallCount || 1 };
164
+ }
165
+ }
166
+
167
+ /**
168
+ * Fetch enrollments for all provided users with controlled concurrency.
169
+ * Processes users in batches of CONCURRENCY_LIMIT to avoid overwhelming the API.
170
+ */
171
+ private async fetchEnrollmentsForUsers(
172
+ companyId: string,
173
+ users: LearnWorldsUser[],
174
+ maxResults: number,
175
+ contextUser: UserInfo,
176
+ errors: SyncError[],
177
+ ): Promise<{ data: LearnWorldsEnrollment[]; apiCalls: number }> {
178
+ if (users.length === 0) {
179
+ return { data: [], apiCalls: 0 };
180
+ }
181
+
182
+ const allEnrollments: LearnWorldsEnrollment[] = [];
183
+ let apiCalls = 0;
184
+
185
+ const results = await this.processInBatches(users, async (user) => {
186
+ const action = new GetUserEnrollmentsAction();
187
+ try {
188
+ const result = await action.GetUserEnrollments(
189
+ { CompanyID: companyId, UserID: user.id, MaxResults: maxResults },
190
+ contextUser,
191
+ );
192
+ apiCalls += action.ApiCallCount;
193
+ return { enrollments: result.Enrollments, userId: user.id, error: null };
194
+ } catch (error) {
195
+ apiCalls += action.ApiCallCount || 1;
196
+ this.collectError(errors, 'enrollment', error, user.id);
197
+ return { enrollments: [], userId: user.id, error };
198
+ }
199
+ });
200
+
201
+ for (const result of results) {
202
+ for (const enrollment of result.enrollments) {
203
+ allEnrollments.push({
204
+ id: enrollment.id,
205
+ userId: result.userId,
206
+ courseId: enrollment.courseId,
207
+ enrolledAt: enrollment.enrolledAt || '',
208
+ startsAt: enrollment.startsAt,
209
+ expiresAt: enrollment.expiresAt,
210
+ status: enrollment.status,
211
+ price: 0,
212
+ progress: {
213
+ percentage: enrollment.progress.percentage,
214
+ completedUnits: enrollment.progress.completedUnits,
215
+ totalUnits: enrollment.progress.totalUnits,
216
+ lastAccessedAt: enrollment.progress.lastAccessedAt,
217
+ },
218
+ certificateEligible: enrollment.certificateEligible,
219
+ certificateIssuedAt: enrollment.certificateIssuedAt,
220
+ });
221
+ }
222
+ }
223
+
224
+ return { data: allEnrollments, apiCalls };
225
+ }
226
+
227
+ /**
228
+ * Fetch learning progress for all provided users with controlled concurrency.
229
+ * Processes users in batches of CONCURRENCY_LIMIT to avoid overwhelming the API.
230
+ */
231
+ private async fetchProgressForUsers(
232
+ companyId: string,
233
+ users: LearnWorldsUser[],
234
+ contextUser: UserInfo,
235
+ errors: SyncError[],
236
+ ): Promise<{ data: UserLearningProgress[]; apiCalls: number }> {
237
+ if (users.length === 0) {
238
+ return { data: [], apiCalls: 0 };
239
+ }
240
+
241
+ const allProgress: UserLearningProgress[] = [];
242
+ let apiCalls = 0;
243
+
244
+ const results = await this.processInBatches(users, async (user) => {
245
+ const action = new GetLearnWorldsUserProgressAction();
246
+ try {
247
+ const result = await action.GetUserProgress(
248
+ { CompanyID: companyId, UserID: user.id },
249
+ contextUser,
250
+ );
251
+ apiCalls += action.ApiCallCount;
252
+ return { progress: result.UserProgress, error: null };
253
+ } catch (error) {
254
+ apiCalls += action.ApiCallCount || 1;
255
+ this.collectError(errors, 'progress', error, user.id);
256
+ return { progress: null, error };
257
+ }
258
+ });
259
+
260
+ for (const result of results) {
261
+ if (result.progress) {
262
+ allProgress.push(result.progress);
263
+ }
264
+ }
265
+
266
+ return { data: allProgress, apiCalls };
267
+ }
268
+
269
+ /**
270
+ * Fetch certificates, collecting errors on failure.
271
+ */
272
+ private async fetchCertificates(
273
+ companyId: string,
274
+ maxResults: number,
275
+ contextUser: UserInfo,
276
+ errors: SyncError[],
277
+ ): Promise<{ data: LearnWorldsCertificate[]; apiCalls: number }> {
278
+ const action = new GetCertificatesAction();
279
+ try {
280
+ const result = await action.GetCertificates(
281
+ { CompanyID: companyId, MaxResults: maxResults, IncludeDownloadLinks: false },
282
+ contextUser,
283
+ );
284
+
285
+ // Map FormattedCertificate to LearnWorldsCertificate
286
+ const certificates: LearnWorldsCertificate[] = result.Certificates.map((cert) => ({
287
+ id: cert.id,
288
+ userId: cert.userId,
289
+ courseId: cert.courseId,
290
+ certificateNumber: cert.certificateNumber,
291
+ issuedAt: cert.issuedAt || '',
292
+ expiresAt: cert.expiresAt,
293
+ status: cert.status,
294
+ grade: cert.grade,
295
+ score: cert.score,
296
+ completionPercentage: cert.completionPercentage,
297
+ }));
298
+
299
+ return { data: certificates, apiCalls: action.ApiCallCount };
300
+ } catch (error) {
301
+ this.collectError(errors, 'certificate', error);
302
+ return { data: [], apiCalls: action.ApiCallCount || 1 };
303
+ }
304
+ }
305
+
306
+ /**
307
+ * Fetch quiz results, collecting errors on failure.
308
+ */
309
+ private async fetchQuizResults(
310
+ companyId: string,
311
+ maxResults: number,
312
+ contextUser: UserInfo,
313
+ errors: SyncError[],
314
+ ): Promise<{ data: FormattedQuizResult[]; apiCalls: number }> {
315
+ const action = new GetQuizResultsAction();
316
+ try {
317
+ const result = await action.GetQuizResults(
318
+ { CompanyID: companyId, MaxResults: maxResults, IncludeQuestions: false, IncludeAnswers: false },
319
+ contextUser,
320
+ );
321
+ return { data: result.QuizResults, apiCalls: action.ApiCallCount };
322
+ } catch (error) {
323
+ this.collectError(errors, 'quizResult', error);
324
+ return { data: [], apiCalls: action.ApiCallCount || 1 };
325
+ }
326
+ }
327
+
328
+ /**
329
+ * Adds a SyncError to the errors collection from a caught exception.
330
+ */
331
+ private collectError(
332
+ errors: SyncError[],
333
+ entity: SyncError['entity'],
334
+ error: unknown,
335
+ entityId: string = '',
336
+ ): void {
337
+ const message = error instanceof Error ? error.message : String(error);
338
+ errors.push({
339
+ entity,
340
+ entityId,
341
+ operation: 'read',
342
+ message,
343
+ timestamp: new Date().toISOString(),
344
+ });
345
+ }
346
+
347
+ /**
348
+ * Framework entry point that delegates to the typed public method
349
+ */
350
+ protected async InternalRunAction(params: RunActionParams): Promise<ActionResultSimple> {
351
+ const { Params, ContextUser } = params;
352
+ this.params = Params;
353
+
354
+ try {
355
+ const bulkParams = this.extractBulkDataParams(Params);
356
+ const result = await this.GetBulkData(bulkParams, ContextUser);
357
+
358
+ this.setOutputParam(Params, 'SyncPayload', result);
359
+ this.setOutputParam(Params, 'TotalApiCalls', result.totalApiCalls);
360
+ this.setOutputParam(Params, 'Errors', result.errors);
361
+
362
+ const dataCounts = this.buildDataCountsSummary(result);
363
+
364
+ if (result.errors.length > 0) {
365
+ return this.buildErrorResult(
366
+ 'PARTIAL_FAILURE',
367
+ `Bulk data retrieval partially succeeded. ${dataCounts}. ${result.errors.length} error(s) occurred.`,
368
+ Params,
369
+ );
370
+ }
371
+
372
+ return this.buildSuccessResult(`Bulk data retrieval complete. ${dataCounts}.`, Params);
373
+ } catch (error) {
374
+ const errorMessage = error instanceof Error ? error.message : 'Unknown error occurred';
375
+ return this.buildErrorResult('ERROR', `Error during bulk data retrieval: ${errorMessage}`, Params);
376
+ }
377
+ }
378
+
379
+ /**
380
+ * Extract typed params from the generic ActionParam array
381
+ */
382
+ private extractBulkDataParams(params: ActionParam[]): BulkDataParams {
383
+ return {
384
+ CompanyID: this.getRequiredStringParam(params, 'CompanyID'),
385
+ IncludeUsers: this.getOptionalBooleanParam(params, 'IncludeUsers', undefined),
386
+ IncludeCourses: this.getOptionalBooleanParam(params, 'IncludeCourses', undefined),
387
+ IncludeBundles: this.getOptionalBooleanParam(params, 'IncludeBundles', undefined),
388
+ IncludeEnrollments: this.getOptionalBooleanParam(params, 'IncludeEnrollments', undefined),
389
+ IncludeProgress: this.getOptionalBooleanParam(params, 'IncludeProgress', undefined),
390
+ IncludeCertificates: this.getOptionalBooleanParam(params, 'IncludeCertificates', undefined),
391
+ IncludeQuizResults: this.getOptionalBooleanParam(params, 'IncludeQuizResults', undefined),
392
+ MaxResultsPerEntity: this.getOptionalNumberParam(params, 'MaxResultsPerEntity', undefined),
393
+ };
394
+ }
395
+
396
+ /**
397
+ * Builds a human-readable summary of data counts in the payload.
398
+ */
399
+ private buildDataCountsSummary(payload: LearnWorldsSyncPayload): string {
400
+ const parts: string[] = [];
401
+ if (payload.users) parts.push(`${payload.users.length} user(s)`);
402
+ if (payload.courses) parts.push(`${payload.courses.length} course(s)`);
403
+ if (payload.bundles) parts.push(`${payload.bundles.length} bundle(s)`);
404
+ if (payload.enrollments) parts.push(`${payload.enrollments.length} enrollment(s)`);
405
+ if (payload.progress) parts.push(`${payload.progress.length} progress record(s)`);
406
+ if (payload.certificates) parts.push(`${payload.certificates.length} certificate(s)`);
407
+ if (payload.quizResults) parts.push(`${payload.quizResults.length} quiz result(s)`);
408
+ return parts.length > 0 ? `Retrieved: ${parts.join(', ')}` : 'No data retrieved';
409
+ }
410
+
411
+ /**
412
+ * Define the parameters this action expects
413
+ */
414
+ public get Params(): ActionParam[] {
415
+ const baseParams = this.getCommonLMSParams();
416
+ const specificParams: ActionParam[] = [
417
+ { Name: 'IncludeUsers', Type: 'Input', Value: null },
418
+ { Name: 'IncludeCourses', Type: 'Input', Value: null },
419
+ { Name: 'IncludeBundles', Type: 'Input', Value: null },
420
+ { Name: 'IncludeEnrollments', Type: 'Input', Value: null },
421
+ { Name: 'IncludeProgress', Type: 'Input', Value: null },
422
+ { Name: 'IncludeCertificates', Type: 'Input', Value: null },
423
+ { Name: 'IncludeQuizResults', Type: 'Input', Value: null },
424
+ { Name: 'MaxResultsPerEntity', Type: 'Input', Value: 100 },
425
+ { Name: 'SyncPayload', Type: 'Output', Value: null },
426
+ { Name: 'TotalApiCalls', Type: 'Output', Value: null },
427
+ { Name: 'Errors', Type: 'Output', Value: null },
428
+ ];
429
+ return [...baseParams, ...specificParams];
430
+ }
431
+
432
+ /**
433
+ * Metadata about this action
434
+ */
435
+ public get Description(): string {
436
+ return 'Orchestrates bulk data retrieval from LearnWorlds, fetching users, courses, bundles, enrollments, progress, certificates, and quiz results into a single sync payload';
437
+ }
438
+ }
@@ -0,0 +1,139 @@
1
+ import { RegisterClass } from '@memberjunction/global';
2
+ import { LearnWorldsBaseAction } from '../learnworlds-base.action';
3
+ import { ActionParam, ActionResultSimple, RunActionParams } from '@memberjunction/actions-base';
4
+ import { BaseAction } from '@memberjunction/actions';
5
+ import { UserInfo } from '@memberjunction/core';
6
+ import { GetBundlesParams, GetBundlesResult, LearnWorldsBundle } from '../interfaces';
7
+
8
+ /**
9
+ * Raw bundle shape returned by the LearnWorlds API
10
+ */
11
+ interface LWApiBundle {
12
+ id?: string;
13
+ _id?: string;
14
+ title?: string;
15
+ description?: string;
16
+ price?: number;
17
+ currency?: string;
18
+ courses?: string[];
19
+ is_active?: boolean;
20
+ created?: string | number;
21
+ created_at?: string;
22
+ updated?: string | number;
23
+ updated_at?: string;
24
+ thumbnail_url?: string;
25
+ image?: string;
26
+ total_courses?: number;
27
+ total_enrollments?: number;
28
+ students_count?: number;
29
+ }
30
+
31
+ /**
32
+ * Action to retrieve bundles from LearnWorlds
33
+ */
34
+ @RegisterClass(BaseAction, 'GetBundlesAction')
35
+ export class GetBundlesAction extends LearnWorldsBaseAction {
36
+ /**
37
+ * Retrieves bundles from LearnWorlds with optional search filtering.
38
+ */
39
+ public async GetBundles(params: GetBundlesParams, contextUser: UserInfo): Promise<GetBundlesResult> {
40
+ this.SetCompanyContext(params.CompanyID);
41
+
42
+ const queryParams = this.buildBundleQueryParams(params);
43
+ const rawBundles = await this.makeLearnWorldsPaginatedRequest<LWApiBundle>('bundles', queryParams, contextUser);
44
+
45
+ const bundles = rawBundles.map((b) => this.mapApiBundle(b));
46
+
47
+ return {
48
+ Bundles: bundles,
49
+ TotalCount: bundles.length,
50
+ };
51
+ }
52
+
53
+ /**
54
+ * Builds query parameters for the bundles API request
55
+ */
56
+ private buildBundleQueryParams(params: GetBundlesParams): Record<string, string | number | boolean> {
57
+ const queryParams: Record<string, string | number | boolean> = {};
58
+
59
+ if (params.SearchText) {
60
+ queryParams.search = params.SearchText;
61
+ }
62
+
63
+ if (params.MaxResults) {
64
+ queryParams.limit = Math.min(params.MaxResults, 100);
65
+ }
66
+
67
+ return queryParams;
68
+ }
69
+
70
+ /**
71
+ * Maps a raw LearnWorlds API bundle to the standard LearnWorldsBundle interface
72
+ */
73
+ private mapApiBundle(raw: LWApiBundle): LearnWorldsBundle {
74
+ const createdRaw = raw.created || raw.created_at || '';
75
+ const updatedRaw = raw.updated || raw.updated_at || '';
76
+
77
+ return {
78
+ id: raw.id || raw._id || '',
79
+ title: raw.title || '',
80
+ description: raw.description,
81
+ price: raw.price,
82
+ currency: raw.currency,
83
+ courses: raw.courses || [],
84
+ isActive: raw.is_active !== false,
85
+ createdAt: createdRaw ? this.parseLearnWorldsDate(createdRaw).toISOString() : '',
86
+ updatedAt: updatedRaw ? this.parseLearnWorldsDate(updatedRaw).toISOString() : '',
87
+ thumbnailUrl: raw.thumbnail_url || raw.image,
88
+ totalCourses: raw.total_courses || (raw.courses ? raw.courses.length : 0),
89
+ totalEnrollments: raw.total_enrollments || raw.students_count || 0,
90
+ };
91
+ }
92
+
93
+ /**
94
+ * Framework entry point that delegates to the typed public method
95
+ */
96
+ protected async InternalRunAction(params: RunActionParams): Promise<ActionResultSimple> {
97
+ const { Params, ContextUser } = params;
98
+ this.params = Params;
99
+
100
+ try {
101
+ const bundleParams: GetBundlesParams = {
102
+ CompanyID: this.getRequiredStringParam(Params, 'CompanyID'),
103
+ SearchText: this.getOptionalStringParam(Params, 'SearchText'),
104
+ MaxResults: this.getOptionalNumberParam(Params, 'MaxResults', LearnWorldsBaseAction.LW_MAX_PAGE_SIZE),
105
+ };
106
+
107
+ const result = await this.GetBundles(bundleParams, ContextUser);
108
+
109
+ this.setOutputParam(Params, 'Bundles', result.Bundles);
110
+ this.setOutputParam(Params, 'TotalCount', result.TotalCount);
111
+
112
+ return this.buildSuccessResult(`Successfully retrieved ${result.TotalCount} bundle(s) from LearnWorlds`, Params);
113
+ } catch (error) {
114
+ const errorMessage = error instanceof Error ? error.message : 'Unknown error occurred';
115
+ return this.buildErrorResult('ERROR', `Error retrieving bundles: ${errorMessage}`, Params);
116
+ }
117
+ }
118
+
119
+ /**
120
+ * Define the parameters this action expects
121
+ */
122
+ public get Params(): ActionParam[] {
123
+ const baseParams = this.getCommonLMSParams();
124
+ const specificParams: ActionParam[] = [
125
+ { Name: 'SearchText', Type: 'Input', Value: null },
126
+ { Name: 'MaxResults', Type: 'Input', Value: null },
127
+ { Name: 'Bundles', Type: 'Output', Value: null },
128
+ { Name: 'TotalCount', Type: 'Output', Value: null },
129
+ ];
130
+ return [...baseParams, ...specificParams];
131
+ }
132
+
133
+ /**
134
+ * Metadata about this action
135
+ */
136
+ public get Description(): string {
137
+ return 'Retrieves course bundles from LearnWorlds with optional search filtering';
138
+ }
139
+ }