@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
@@ -1,4 +1,4 @@
1
- import { ActionParam } from '@memberjunction/actions-base';
1
+ import { ActionParam, ActionResultSimple } from '@memberjunction/actions-base';
2
2
  import { BaseAction } from '@memberjunction/actions';
3
3
  import { RegisterClass } from '@memberjunction/global';
4
4
  import { UserInfo } from '@memberjunction/core';
@@ -11,187 +11,250 @@ import { Metadata, RunView } from '@memberjunction/core';
11
11
  */
12
12
  @RegisterClass(BaseAction, 'BaseLMSAction')
13
13
  export abstract class BaseLMSAction extends BaseAction {
14
- /**
15
- * The LMS provider this action is designed for (e.g., 'LearnWorlds', 'Moodle', etc.)
16
- */
17
- protected abstract lmsProvider: string;
18
-
19
- /**
20
- * The integration name to look up in the Integration entity
21
- */
22
- protected abstract integrationName: string;
23
-
24
- /**
25
- * Cached company integration for the current execution
26
- */
27
- private _companyIntegration: MJCompanyIntegrationEntity | null = null;
28
-
29
- /**
30
- * Common LMS parameters that many actions will need
31
- */
32
- protected getCommonLMSParams(): ActionParam[] {
33
- return [
34
- {
35
- Name: 'CompanyID',
36
- Type: 'Input',
37
- Value: null
38
- }
39
- ];
40
- }
14
+ /**
15
+ * The LMS provider this action is designed for (e.g., 'LearnWorlds', 'Moodle', etc.)
16
+ */
17
+ protected abstract lmsProvider: string;
18
+
19
+ /**
20
+ * The integration name to look up in the Integration entity
21
+ */
22
+ protected abstract integrationName: string;
23
+
24
+ /**
25
+ * Cached company integration for the current execution
26
+ */
27
+ private _companyIntegration: MJCompanyIntegrationEntity | null = null;
41
28
 
42
- /**
43
- * Gets the company integration record for the specified company and LMS
44
- */
45
- protected async getCompanyIntegration(companyId: string, contextUser: UserInfo): Promise<MJCompanyIntegrationEntity> {
46
- // Check cache first
47
- if (this._companyIntegration && this._companyIntegration.CompanyID === companyId) {
48
- return this._companyIntegration;
49
- }
50
-
51
- const rv = new RunView();
52
- const result = await rv.RunView<MJCompanyIntegrationEntity>({
53
- EntityName: 'MJ: Company Integrations',
54
- ExtraFilter: `CompanyID = '${companyId}' AND Integration.Name = '${this.integrationName}'`,
55
- ResultType: 'entity_object'
56
- }, contextUser);
57
-
58
- if (!result.Success) {
59
- throw new Error(`Failed to retrieve company integration: ${result.ErrorMessage}`);
60
- }
61
-
62
- if (!result.Results || result.Results.length === 0) {
63
- throw new Error(`No ${this.integrationName} integration found for company ${companyId}. Please configure the integration first.`);
64
- }
65
-
66
- this._companyIntegration = result.Results[0];
67
- return this._companyIntegration;
29
+ /**
30
+ * Common LMS parameters that many actions will need
31
+ */
32
+ protected getCommonLMSParams(): ActionParam[] {
33
+ return [
34
+ {
35
+ Name: 'CompanyID',
36
+ Type: 'Input',
37
+ Value: null,
38
+ },
39
+ ];
40
+ }
41
+
42
+ /**
43
+ * Validates that a string is a valid UUID format to prevent SQL injection.
44
+ */
45
+ private static readonly uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
46
+
47
+ /**
48
+ * Validates that an integration name contains only safe characters (alphanumeric, spaces, hyphens, underscores).
49
+ */
50
+ private validateIntegrationName(name: string): string {
51
+ if (!/^[a-zA-Z0-9 _-]+$/.test(name)) {
52
+ throw new Error('Invalid integration name: contains forbidden characters');
68
53
  }
54
+ return name;
55
+ }
69
56
 
70
- /**
71
- * Gets credentials from environment variables
72
- * Format: BIZAPPS_{PROVIDER}_{COMPANY_ID}_{CREDENTIAL_TYPE}
73
- * Example: BIZAPPS_LEARNWORLDS_12345_API_KEY
74
- */
75
- protected getCredentialFromEnv(companyId: string, credentialType: string): string | undefined {
76
- const envKey = `BIZAPPS_${this.lmsProvider.toUpperCase().replace(/\s+/g, '_')}_${companyId}_${credentialType.toUpperCase()}`;
77
- return process.env[envKey];
57
+ /**
58
+ * Gets the company integration record for the specified company and LMS
59
+ */
60
+ protected async getCompanyIntegration(companyId: string, contextUser: UserInfo): Promise<MJCompanyIntegrationEntity> {
61
+ // Validate companyId format to prevent SQL injection
62
+ if (!BaseLMSAction.uuidRegex.test(companyId)) {
63
+ throw new Error('CompanyID must be a valid UUID');
78
64
  }
79
65
 
80
- /**
81
- * Gets API credentials - first tries environment variables, then falls back to database
82
- */
83
- protected async getAPICredentials(integration: MJCompanyIntegrationEntity): Promise<{ apiKey?: string; apiSecret?: string; accessToken?: string }> {
84
- const companyId = integration.CompanyID;
85
-
86
- // Try environment variables first
87
- const envApiKey = this.getCredentialFromEnv(companyId, 'API_KEY');
88
- const envApiSecret = this.getCredentialFromEnv(companyId, 'API_SECRET');
89
- const envAccessToken = this.getCredentialFromEnv(companyId, 'ACCESS_TOKEN');
90
-
91
- if (envApiKey || envAccessToken) {
92
- return {
93
- apiKey: envApiKey,
94
- apiSecret: envApiSecret,
95
- accessToken: envAccessToken
96
- };
97
- }
98
-
99
- // Fall back to database (for backwards compatibility)
100
- if (!integration.APIKey && !integration.AccessToken) {
101
- throw new Error(`No API credentials found for ${this.integrationName} integration. Please set environment variables or configure in database.`);
102
- }
103
-
104
- return {
105
- apiKey: integration.APIKey || undefined,
106
- accessToken: integration.AccessToken || undefined
107
- };
66
+ // Validate integration name before SQL interpolation
67
+ const safeIntegrationName = this.validateIntegrationName(this.integrationName);
68
+
69
+ // Check cache first
70
+ if (this._companyIntegration && this._companyIntegration.CompanyID === companyId) {
71
+ return this._companyIntegration;
108
72
  }
109
73
 
110
- /**
111
- * Gets the base URL for API calls
112
- */
113
- protected async getAPIBaseURL(integration: MJCompanyIntegrationEntity): Promise<string> {
114
- // Check if custom URL is stored in the integration
115
- if (integration.CustomAttribute1) {
116
- return integration.CustomAttribute1;
117
- }
118
-
119
- // Return empty string - derived classes should override this
120
- return '';
74
+ const rv = new RunView();
75
+ const result = await rv.RunView<MJCompanyIntegrationEntity>(
76
+ {
77
+ EntityName: 'MJ: Company Integrations',
78
+ ExtraFilter: `CompanyID = '${companyId}' AND Integration.Name = '${safeIntegrationName}'`,
79
+ ResultType: 'entity_object',
80
+ },
81
+ contextUser,
82
+ );
83
+
84
+ if (!result.Success) {
85
+ throw new Error(`Failed to retrieve company integration: ${result.ErrorMessage}`);
121
86
  }
122
87
 
123
- /**
124
- * Helper to get parameter value with type safety
125
- */
126
- protected getParamValue(params: ActionParam[], paramName: string): any {
127
- const param = params.find(p => p.Name === paramName);
128
- return param?.Value;
88
+ if (!result.Results || result.Results.length === 0) {
89
+ throw new Error(`No ${this.integrationName} integration found for company ${companyId}. Please configure the integration first.`);
129
90
  }
130
91
 
131
- /**
132
- * Standard date format for LMS systems (ISO 8601)
133
- */
134
- protected formatLMSDate(date: Date): string {
135
- return date.toISOString();
92
+ this._companyIntegration = result.Results[0];
93
+ return this._companyIntegration;
94
+ }
95
+
96
+ /**
97
+ * Gets credentials from environment variables
98
+ * Format: BIZAPPS_{PROVIDER}_{COMPANY_ID}_{CREDENTIAL_TYPE}
99
+ * Example: BIZAPPS_LEARNWORLDS_12345_API_KEY
100
+ */
101
+ protected getCredentialFromEnv(companyId: string, credentialType: string): string | undefined {
102
+ const envKey = `BIZAPPS_${this.lmsProvider.toUpperCase().replace(/\s+/g, '_')}_${companyId}_${credentialType.toUpperCase()}`;
103
+ return process.env[envKey];
104
+ }
105
+
106
+ /**
107
+ * Gets API credentials - first tries environment variables, then falls back to database
108
+ */
109
+ protected async getAPICredentials(integration: MJCompanyIntegrationEntity): Promise<{ apiKey?: string; apiSecret?: string; accessToken?: string }> {
110
+ const companyId = integration.CompanyID;
111
+
112
+ // Try environment variables first
113
+ const envApiKey = this.getCredentialFromEnv(companyId, 'API_KEY');
114
+ const envApiSecret = this.getCredentialFromEnv(companyId, 'API_SECRET');
115
+ const envAccessToken = this.getCredentialFromEnv(companyId, 'ACCESS_TOKEN');
116
+
117
+ if (envApiKey || envAccessToken) {
118
+ return {
119
+ apiKey: envApiKey,
120
+ apiSecret: envApiSecret,
121
+ accessToken: envAccessToken,
122
+ };
136
123
  }
137
124
 
138
- /**
139
- * Parse date from LMS format
140
- */
141
- protected parseLMSDate(dateString: string): Date {
142
- return new Date(dateString);
125
+ // Fall back to database (for backwards compatibility)
126
+ if (!integration.APIKey && !integration.AccessToken) {
127
+ throw new Error(`No API credentials found for ${this.integrationName} integration. Please set environment variables or configure in database.`);
143
128
  }
144
129
 
145
- /**
146
- * Calculate progress percentage
147
- */
148
- protected calculateProgressPercentage(completed: number, total: number): number {
149
- if (total === 0) return 0;
150
- return Math.round((completed / total) * 100);
130
+ return {
131
+ apiKey: integration.APIKey || undefined,
132
+ accessToken: integration.AccessToken || undefined,
133
+ };
134
+ }
135
+
136
+ /**
137
+ * Gets the base URL for API calls
138
+ */
139
+ protected async getAPIBaseURL(integration: MJCompanyIntegrationEntity): Promise<string> {
140
+ // Check if custom URL is stored in the integration
141
+ if (integration.CustomAttribute1) {
142
+ return integration.CustomAttribute1;
151
143
  }
152
144
 
153
- /**
154
- * Format duration in seconds to human readable format
155
- */
156
- protected formatDuration(seconds: number): string {
157
- const hours = Math.floor(seconds / 3600);
158
- const minutes = Math.floor((seconds % 3600) / 60);
159
- const secs = seconds % 60;
160
-
161
- if (hours > 0) {
162
- return `${hours}h ${minutes}m ${secs}s`;
163
- } else if (minutes > 0) {
164
- return `${minutes}m ${secs}s`;
165
- } else {
166
- return `${secs}s`;
167
- }
145
+ // Return empty string - derived classes should override this
146
+ return '';
147
+ }
148
+
149
+ /**
150
+ * Helper to get parameter value from an ActionParam array
151
+ */
152
+ protected getParamValue(params: ActionParam[], paramName: string): unknown {
153
+ const param = params.find((p) => p.Name === paramName);
154
+ return param?.Value;
155
+ }
156
+
157
+ /**
158
+ * Standard date format for LMS systems (ISO 8601)
159
+ */
160
+ protected formatLMSDate(date: Date): string {
161
+ return date.toISOString();
162
+ }
163
+
164
+ /**
165
+ * Parse date from LMS format
166
+ */
167
+ protected parseLMSDate(dateString: string): Date {
168
+ return new Date(dateString);
169
+ }
170
+
171
+ /**
172
+ * Calculate progress percentage
173
+ */
174
+ protected calculateProgressPercentage(completed: number, total: number): number {
175
+ if (total === 0) return 0;
176
+ return Math.round((completed / total) * 100);
177
+ }
178
+
179
+ /**
180
+ * Format duration in seconds to human readable format
181
+ */
182
+ protected formatDuration(seconds: number): string {
183
+ const hours = Math.floor(seconds / 3600);
184
+ const minutes = Math.floor((seconds % 3600) / 60);
185
+ const secs = seconds % 60;
186
+
187
+ if (hours > 0) {
188
+ return `${hours}h ${minutes}m ${secs}s`;
189
+ } else if (minutes > 0) {
190
+ return `${minutes}m ${secs}s`;
191
+ } else {
192
+ return `${secs}s`;
168
193
  }
194
+ }
169
195
 
170
- /**
171
- * Helper to build consistent error messages for LMS operations
172
- */
173
- protected buildLMSErrorMessage(operation: string, details: string, systemError?: any): string {
174
- let message = `LMS operation failed: ${operation}. ${details}`;
175
- if (systemError) {
176
- message += ` System error: ${systemError.message || systemError}`;
177
- }
178
- return message;
196
+ /**
197
+ * Helper to build consistent error messages for LMS operations
198
+ */
199
+ protected buildLMSErrorMessage(operation: string, details: string, systemError?: Error | string | { message?: string }): string {
200
+ let message = `LMS operation failed: ${operation}. ${details}`;
201
+ if (systemError) {
202
+ const errorMsg = typeof systemError === 'string' ? systemError : (systemError as { message?: string }).message || String(systemError);
203
+ message += ` System error: ${errorMsg}`;
179
204
  }
205
+ return message;
206
+ }
207
+
208
+ /**
209
+ * Common enrollment status mapping
210
+ */
211
+ protected mapEnrollmentStatus(status: string): 'active' | 'completed' | 'expired' | 'suspended' | 'unknown' {
212
+ const statusMap: Record<string, 'active' | 'completed' | 'expired' | 'suspended' | 'unknown'> = {
213
+ active: 'active',
214
+ completed: 'completed',
215
+ finished: 'completed',
216
+ expired: 'expired',
217
+ suspended: 'suspended',
218
+ paused: 'suspended',
219
+ inactive: 'suspended',
220
+ };
221
+
222
+ return statusMap[status.toLowerCase()] || 'unknown';
223
+ }
180
224
 
181
- /**
182
- * Common enrollment status mapping
183
- */
184
- protected mapEnrollmentStatus(status: string): 'active' | 'completed' | 'expired' | 'suspended' | 'unknown' {
185
- const statusMap: Record<string, 'active' | 'completed' | 'expired' | 'suspended' | 'unknown'> = {
186
- 'active': 'active',
187
- 'completed': 'completed',
188
- 'finished': 'completed',
189
- 'expired': 'expired',
190
- 'suspended': 'suspended',
191
- 'paused': 'suspended',
192
- 'inactive': 'suspended'
193
- };
194
-
195
- return statusMap[status.toLowerCase()] || 'unknown';
225
+ /**
226
+ * Helper to set an output parameter, creating it if it doesn't exist
227
+ */
228
+ protected setOutputParam(params: ActionParam[], name: string, value: unknown): void {
229
+ const existing = params.find((p) => p.Name === name);
230
+ if (existing) {
231
+ existing.Value = value;
232
+ } else {
233
+ params.push({ Name: name, Type: 'Output', Value: value });
196
234
  }
197
- }
235
+ }
236
+
237
+ /**
238
+ * Build a standard success ActionResultSimple
239
+ */
240
+ protected buildSuccessResult(message: string, params?: ActionParam[]): ActionResultSimple {
241
+ return {
242
+ Success: true,
243
+ ResultCode: 'SUCCESS',
244
+ Message: message,
245
+ ...(params ? { Params: params } : {}),
246
+ };
247
+ }
248
+
249
+ /**
250
+ * Build a standard error ActionResultSimple
251
+ */
252
+ protected buildErrorResult(code: string, message: string, params?: ActionParam[]): ActionResultSimple {
253
+ return {
254
+ Success: false,
255
+ ResultCode: code,
256
+ Message: message,
257
+ ...(params ? { Params: params } : {}),
258
+ };
259
+ }
260
+ }
@@ -0,0 +1,90 @@
1
+ import { RegisterClass } from '@memberjunction/global';
2
+ import { LearnWorldsBaseAction } from '../learnworlds-base.action';
3
+ import { ActionParam, ActionResultSimple, RunActionParams } from '@memberjunction/actions-base';
4
+ import { BaseAction } from '@memberjunction/actions';
5
+ import { UserInfo } from '@memberjunction/core';
6
+ import { TagParams, TagResult } from '../interfaces';
7
+
8
+ /**
9
+ * Shape of the LearnWorlds tag operation API response
10
+ */
11
+ interface LWApiTagResponse {
12
+ tags?: string[];
13
+ }
14
+
15
+ /**
16
+ * Action to attach tags to a LearnWorlds user
17
+ */
18
+ @RegisterClass(BaseAction, 'AttachTagsAction')
19
+ export class AttachTagsAction extends LearnWorldsBaseAction {
20
+ /**
21
+ * Attaches one or more tags to a LearnWorlds user.
22
+ */
23
+ public async AttachTags(params: TagParams, contextUser: UserInfo): Promise<TagResult> {
24
+ this.SetCompanyContext(params.CompanyID);
25
+
26
+ if (!params.UserID) {
27
+ throw new Error('UserID is required');
28
+ }
29
+ if (!params.Tags || params.Tags.length === 0) {
30
+ throw new Error('At least one tag is required');
31
+ }
32
+
33
+ this.validatePathSegment(params.UserID, 'UserID');
34
+
35
+ const response = await this.makeLearnWorldsRequest<LWApiTagResponse>(`users/${params.UserID}/tags`, 'POST', { tags: params.Tags }, contextUser);
36
+
37
+ return {
38
+ Success: true,
39
+ UserID: params.UserID,
40
+ Tags: response.tags || params.Tags,
41
+ };
42
+ }
43
+
44
+ /**
45
+ * Framework entry point that delegates to the typed public method
46
+ */
47
+ protected async InternalRunAction(params: RunActionParams): Promise<ActionResultSimple> {
48
+ const { Params, ContextUser } = params;
49
+ this.params = Params;
50
+
51
+ try {
52
+ const tagParams: TagParams = {
53
+ CompanyID: this.getRequiredStringParam(Params, 'CompanyID'),
54
+ UserID: this.getRequiredStringParam(Params, 'UserID'),
55
+ Tags: this.getOptionalStringArrayParam(Params, 'Tags') || [],
56
+ };
57
+
58
+ const result = await this.AttachTags(tagParams, ContextUser);
59
+
60
+ this.setOutputParam(Params, 'Success', result.Success);
61
+ this.setOutputParam(Params, 'ResultTags', result.Tags);
62
+
63
+ return this.buildSuccessResult(`Successfully attached ${result.Tags.length} tag(s) to user ${result.UserID}`, Params);
64
+ } catch (error) {
65
+ const errorMessage = error instanceof Error ? error.message : 'Unknown error occurred';
66
+ return this.buildErrorResult('ERROR', `Error attaching tags: ${errorMessage}`, Params);
67
+ }
68
+ }
69
+
70
+ /**
71
+ * Define the parameters this action expects
72
+ */
73
+ public get Params(): ActionParam[] {
74
+ const baseParams = this.getCommonLMSParams();
75
+ const specificParams: ActionParam[] = [
76
+ { Name: 'UserID', Type: 'Input', Value: null },
77
+ { Name: 'Tags', Type: 'Input', Value: null },
78
+ { Name: 'Success', Type: 'Output', Value: null },
79
+ { Name: 'ResultTags', Type: 'Output', Value: null },
80
+ ];
81
+ return [...baseParams, ...specificParams];
82
+ }
83
+
84
+ /**
85
+ * Metadata about this action
86
+ */
87
+ public get Description(): string {
88
+ return 'Attaches one or more tags to a LearnWorlds user';
89
+ }
90
+ }