@memberjunction/actions-bizapps-lms 2.55.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.
- package/.turbo/turbo-build.log +4 -0
- package/CHANGELOG.md +12 -0
- package/dist/base/base-lms.action.d.ts +77 -0
- package/dist/base/base-lms.action.d.ts.map +1 -0
- package/dist/base/base-lms.action.js +176 -0
- package/dist/base/base-lms.action.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +21 -0
- package/dist/index.js.map +1 -0
- package/dist/providers/learnworlds/actions/get-courses.action.d.ts +125 -0
- package/dist/providers/learnworlds/actions/get-courses.action.d.ts.map +1 -0
- package/dist/providers/learnworlds/actions/get-courses.action.js +436 -0
- package/dist/providers/learnworlds/actions/get-courses.action.js.map +1 -0
- package/dist/providers/learnworlds/actions/get-user-details.action.d.ts +95 -0
- package/dist/providers/learnworlds/actions/get-user-details.action.d.ts.map +1 -0
- package/dist/providers/learnworlds/actions/get-user-details.action.js +280 -0
- package/dist/providers/learnworlds/actions/get-user-details.action.js.map +1 -0
- package/dist/providers/learnworlds/actions/get-user-progress.action.d.ts +151 -0
- package/dist/providers/learnworlds/actions/get-user-progress.action.d.ts.map +1 -0
- package/dist/providers/learnworlds/actions/get-user-progress.action.js +417 -0
- package/dist/providers/learnworlds/actions/get-user-progress.action.js.map +1 -0
- package/dist/providers/learnworlds/actions/get-users.action.d.ts +53 -0
- package/dist/providers/learnworlds/actions/get-users.action.d.ts.map +1 -0
- package/dist/providers/learnworlds/actions/get-users.action.js +272 -0
- package/dist/providers/learnworlds/actions/get-users.action.js.map +1 -0
- package/dist/providers/learnworlds/actions/index.d.ts +5 -0
- package/dist/providers/learnworlds/actions/index.d.ts.map +1 -0
- package/dist/providers/learnworlds/actions/index.js +22 -0
- package/dist/providers/learnworlds/actions/index.js.map +1 -0
- package/dist/providers/learnworlds/index.d.ts +3 -0
- package/dist/providers/learnworlds/index.d.ts.map +1 -0
- package/dist/providers/learnworlds/index.js +21 -0
- package/dist/providers/learnworlds/index.js.map +1 -0
- package/dist/providers/learnworlds/learnworlds-base.action.d.ts +52 -0
- package/dist/providers/learnworlds/learnworlds-base.action.d.ts.map +1 -0
- package/dist/providers/learnworlds/learnworlds-base.action.js +186 -0
- package/dist/providers/learnworlds/learnworlds-base.action.js.map +1 -0
- package/package.json +40 -0
- package/src/base/base-lms.action.ts +197 -0
- package/src/index.ts +5 -0
- package/src/providers/learnworlds/actions/get-courses.action.ts +592 -0
- package/src/providers/learnworlds/actions/get-user-details.action.ts +381 -0
- package/src/providers/learnworlds/actions/get-user-progress.action.ts +625 -0
- package/src/providers/learnworlds/actions/get-users.action.ts +319 -0
- package/src/providers/learnworlds/actions/index.ts +5 -0
- package/src/providers/learnworlds/index.ts +5 -0
- package/src/providers/learnworlds/learnworlds-base.action.ts +221 -0
- package/tsconfig.json +18 -0
|
@@ -0,0 +1,319 @@
|
|
|
1
|
+
import { RegisterClass } from '@memberjunction/global';
|
|
2
|
+
import { LearnWorldsBaseAction } from '../learnworlds-base.action';
|
|
3
|
+
import { ActionParam, ActionResultSimple, RunActionParams } from '@memberjunction/actions-base';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Interface for a LearnWorlds user
|
|
7
|
+
*/
|
|
8
|
+
export interface LearnWorldsUser {
|
|
9
|
+
id: string;
|
|
10
|
+
email: string;
|
|
11
|
+
username: string;
|
|
12
|
+
firstName?: string;
|
|
13
|
+
lastName?: string;
|
|
14
|
+
fullName?: string;
|
|
15
|
+
status: 'active' | 'inactive' | 'suspended';
|
|
16
|
+
role: string;
|
|
17
|
+
createdAt: Date;
|
|
18
|
+
lastLoginAt?: Date;
|
|
19
|
+
tags?: string[];
|
|
20
|
+
customFields?: Record<string, any>;
|
|
21
|
+
totalCourses?: number;
|
|
22
|
+
completedCourses?: number;
|
|
23
|
+
inProgressCourses?: number;
|
|
24
|
+
totalTimeSpent?: number;
|
|
25
|
+
avatarUrl?: string;
|
|
26
|
+
bio?: string;
|
|
27
|
+
location?: string;
|
|
28
|
+
timezone?: string;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Action to retrieve users from LearnWorlds LMS
|
|
33
|
+
*/
|
|
34
|
+
@RegisterClass(LearnWorldsBaseAction, 'GetLearnWorldsUsersAction')
|
|
35
|
+
export class GetLearnWorldsUsersAction extends LearnWorldsBaseAction {
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Description of the action
|
|
39
|
+
*/
|
|
40
|
+
public get Description(): string {
|
|
41
|
+
return 'Retrieves users (students, instructors, admins) from LearnWorlds LMS with filtering and search options';
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Main execution method
|
|
46
|
+
*/
|
|
47
|
+
protected async InternalRunAction(params: RunActionParams): Promise<ActionResultSimple> {
|
|
48
|
+
try {
|
|
49
|
+
const contextUser = params.ContextUser;
|
|
50
|
+
if (!contextUser) {
|
|
51
|
+
return {
|
|
52
|
+
Success: false,
|
|
53
|
+
ResultCode: 'ERROR',
|
|
54
|
+
Message: 'Context user is required for LearnWorlds API calls'
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// Store params for base class methods
|
|
59
|
+
this.params = params.Params;
|
|
60
|
+
|
|
61
|
+
// Build query parameters
|
|
62
|
+
const queryParams: Record<string, any> = {};
|
|
63
|
+
|
|
64
|
+
// Search filter
|
|
65
|
+
const searchText = this.getParamValue(params.Params, 'SearchText');
|
|
66
|
+
if (searchText) {
|
|
67
|
+
queryParams.search = searchText;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Role filter
|
|
71
|
+
const role = this.getParamValue(params.Params, 'Role');
|
|
72
|
+
if (role) {
|
|
73
|
+
queryParams.role = role;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// Status filter
|
|
77
|
+
const status = this.getParamValue(params.Params, 'Status');
|
|
78
|
+
if (status) {
|
|
79
|
+
queryParams.status = status;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// Tag filter
|
|
83
|
+
const tags = this.getParamValue(params.Params, 'Tags');
|
|
84
|
+
if (tags) {
|
|
85
|
+
queryParams.tags = tags;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// Date filters
|
|
89
|
+
const createdAfter = this.getParamValue(params.Params, 'CreatedAfter');
|
|
90
|
+
if (createdAfter) {
|
|
91
|
+
queryParams.created_after = this.formatLearnWorldsDate(new Date(createdAfter));
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const createdBefore = this.getParamValue(params.Params, 'CreatedBefore');
|
|
95
|
+
if (createdBefore) {
|
|
96
|
+
queryParams.created_before = this.formatLearnWorldsDate(new Date(createdBefore));
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// Sorting
|
|
100
|
+
const sortBy = this.getParamValue(params.Params, 'SortBy') || 'created';
|
|
101
|
+
const sortOrder = this.getParamValue(params.Params, 'SortOrder') || 'desc';
|
|
102
|
+
queryParams.sort = `${sortOrder === 'asc' ? '' : '-'}${sortBy}`;
|
|
103
|
+
|
|
104
|
+
// Include course stats
|
|
105
|
+
const includeCourseStats = this.getParamValue(params.Params, 'IncludeCourseStats');
|
|
106
|
+
if (includeCourseStats) {
|
|
107
|
+
queryParams.include = 'course_stats';
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// Limit for pagination
|
|
111
|
+
const maxResults = this.getParamValue(params.Params, 'MaxResults') || 100;
|
|
112
|
+
queryParams.limit = Math.min(maxResults, 100); // LearnWorlds max is usually 100
|
|
113
|
+
|
|
114
|
+
// Make the API request
|
|
115
|
+
const users = await this.makeLearnWorldsPaginatedRequest<any>(
|
|
116
|
+
'users',
|
|
117
|
+
queryParams,
|
|
118
|
+
contextUser
|
|
119
|
+
);
|
|
120
|
+
|
|
121
|
+
// Map to our interface
|
|
122
|
+
const mappedUsers: LearnWorldsUser[] = users.map(user => this.mapLearnWorldsUser(user));
|
|
123
|
+
|
|
124
|
+
// Calculate summary
|
|
125
|
+
const summary = this.calculateUserSummary(mappedUsers);
|
|
126
|
+
|
|
127
|
+
// Create output parameters
|
|
128
|
+
if (!params.Params.find(p => p.Name === 'Users')) {
|
|
129
|
+
params.Params.push({
|
|
130
|
+
Name: 'Users',
|
|
131
|
+
Type: 'Output',
|
|
132
|
+
Value: mappedUsers
|
|
133
|
+
});
|
|
134
|
+
} else {
|
|
135
|
+
params.Params.find(p => p.Name === 'Users')!.Value = mappedUsers;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
if (!params.Params.find(p => p.Name === 'TotalCount')) {
|
|
139
|
+
params.Params.push({
|
|
140
|
+
Name: 'TotalCount',
|
|
141
|
+
Type: 'Output',
|
|
142
|
+
Value: mappedUsers.length
|
|
143
|
+
});
|
|
144
|
+
} else {
|
|
145
|
+
params.Params.find(p => p.Name === 'TotalCount')!.Value = mappedUsers.length;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
if (!params.Params.find(p => p.Name === 'Summary')) {
|
|
149
|
+
params.Params.push({
|
|
150
|
+
Name: 'Summary',
|
|
151
|
+
Type: 'Output',
|
|
152
|
+
Value: summary
|
|
153
|
+
});
|
|
154
|
+
} else {
|
|
155
|
+
params.Params.find(p => p.Name === 'Summary')!.Value = summary;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
return {
|
|
159
|
+
Success: true,
|
|
160
|
+
ResultCode: 'SUCCESS',
|
|
161
|
+
Message: `Successfully retrieved ${mappedUsers.length} users from LearnWorlds`
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
} catch (error) {
|
|
165
|
+
const errorMessage = error instanceof Error ? error.message : 'Unknown error occurred';
|
|
166
|
+
return {
|
|
167
|
+
Success: false,
|
|
168
|
+
ResultCode: 'ERROR',
|
|
169
|
+
Message: errorMessage
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Map LearnWorlds user data to our interface
|
|
176
|
+
*/
|
|
177
|
+
private mapLearnWorldsUser(lwUser: any): LearnWorldsUser {
|
|
178
|
+
return {
|
|
179
|
+
id: lwUser.id || lwUser._id,
|
|
180
|
+
email: lwUser.email,
|
|
181
|
+
username: lwUser.username || lwUser.email,
|
|
182
|
+
firstName: lwUser.first_name,
|
|
183
|
+
lastName: lwUser.last_name,
|
|
184
|
+
fullName: lwUser.full_name || `${lwUser.first_name || ''} ${lwUser.last_name || ''}`.trim(),
|
|
185
|
+
status: this.mapUserStatus(lwUser.status || 'active'),
|
|
186
|
+
role: lwUser.role || 'student',
|
|
187
|
+
createdAt: this.parseLearnWorldsDate(lwUser.created || lwUser.created_at),
|
|
188
|
+
lastLoginAt: lwUser.last_login ? this.parseLearnWorldsDate(lwUser.last_login) : undefined,
|
|
189
|
+
tags: lwUser.tags || [],
|
|
190
|
+
customFields: lwUser.custom_fields || {},
|
|
191
|
+
totalCourses: lwUser.course_stats?.total || 0,
|
|
192
|
+
completedCourses: lwUser.course_stats?.completed || 0,
|
|
193
|
+
inProgressCourses: lwUser.course_stats?.in_progress || 0,
|
|
194
|
+
totalTimeSpent: lwUser.course_stats?.total_time_spent || 0,
|
|
195
|
+
avatarUrl: lwUser.avatar_url,
|
|
196
|
+
bio: lwUser.bio,
|
|
197
|
+
location: lwUser.location,
|
|
198
|
+
timezone: lwUser.timezone
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Calculate summary statistics
|
|
204
|
+
*/
|
|
205
|
+
private calculateUserSummary(users: LearnWorldsUser[]): any {
|
|
206
|
+
const summary = {
|
|
207
|
+
totalUsers: users.length,
|
|
208
|
+
activeUsers: users.filter(u => u.status === 'active').length,
|
|
209
|
+
inactiveUsers: users.filter(u => u.status === 'inactive').length,
|
|
210
|
+
suspendedUsers: users.filter(u => u.status === 'suspended').length,
|
|
211
|
+
usersByRole: {} as Record<string, number>,
|
|
212
|
+
averageCoursesPerUser: 0,
|
|
213
|
+
totalTimeSpent: 0,
|
|
214
|
+
mostActiveUsers: [] as any[],
|
|
215
|
+
recentSignups: [] as any[]
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
// Count by role
|
|
219
|
+
users.forEach(user => {
|
|
220
|
+
summary.usersByRole[user.role] = (summary.usersByRole[user.role] || 0) + 1;
|
|
221
|
+
summary.totalTimeSpent += user.totalTimeSpent || 0;
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
// Calculate averages
|
|
225
|
+
if (users.length > 0) {
|
|
226
|
+
const totalCourses = users.reduce((sum, u) => sum + (u.totalCourses || 0), 0);
|
|
227
|
+
summary.averageCoursesPerUser = totalCourses / users.length;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
// Find most active users (by completed courses)
|
|
231
|
+
summary.mostActiveUsers = users
|
|
232
|
+
.filter(u => u.completedCourses && u.completedCourses > 0)
|
|
233
|
+
.sort((a, b) => (b.completedCourses || 0) - (a.completedCourses || 0))
|
|
234
|
+
.slice(0, 5)
|
|
235
|
+
.map(u => ({
|
|
236
|
+
id: u.id,
|
|
237
|
+
name: u.fullName || u.email,
|
|
238
|
+
completedCourses: u.completedCourses
|
|
239
|
+
}));
|
|
240
|
+
|
|
241
|
+
// Find recent signups (last 30 days)
|
|
242
|
+
const thirtyDaysAgo = new Date();
|
|
243
|
+
thirtyDaysAgo.setDate(thirtyDaysAgo.getDate() - 30);
|
|
244
|
+
|
|
245
|
+
summary.recentSignups = users
|
|
246
|
+
.filter(u => u.createdAt > thirtyDaysAgo)
|
|
247
|
+
.sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime())
|
|
248
|
+
.slice(0, 10)
|
|
249
|
+
.map(u => ({
|
|
250
|
+
id: u.id,
|
|
251
|
+
name: u.fullName || u.email,
|
|
252
|
+
signupDate: u.createdAt
|
|
253
|
+
}));
|
|
254
|
+
|
|
255
|
+
return summary;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Define the parameters for this action
|
|
260
|
+
*/
|
|
261
|
+
public get Params(): ActionParam[] {
|
|
262
|
+
const baseParams = this.getCommonLMSParams();
|
|
263
|
+
|
|
264
|
+
const specificParams: ActionParam[] = [
|
|
265
|
+
{
|
|
266
|
+
Name: 'SearchText',
|
|
267
|
+
Type: 'Input',
|
|
268
|
+
Value: null
|
|
269
|
+
},
|
|
270
|
+
{
|
|
271
|
+
Name: 'Role',
|
|
272
|
+
Type: 'Input',
|
|
273
|
+
Value: null
|
|
274
|
+
},
|
|
275
|
+
{
|
|
276
|
+
Name: 'Status',
|
|
277
|
+
Type: 'Input',
|
|
278
|
+
Value: null
|
|
279
|
+
},
|
|
280
|
+
{
|
|
281
|
+
Name: 'Tags',
|
|
282
|
+
Type: 'Input',
|
|
283
|
+
Value: null
|
|
284
|
+
},
|
|
285
|
+
{
|
|
286
|
+
Name: 'CreatedAfter',
|
|
287
|
+
Type: 'Input',
|
|
288
|
+
Value: null
|
|
289
|
+
},
|
|
290
|
+
{
|
|
291
|
+
Name: 'CreatedBefore',
|
|
292
|
+
Type: 'Input',
|
|
293
|
+
Value: null
|
|
294
|
+
},
|
|
295
|
+
{
|
|
296
|
+
Name: 'SortBy',
|
|
297
|
+
Type: 'Input',
|
|
298
|
+
Value: 'created'
|
|
299
|
+
},
|
|
300
|
+
{
|
|
301
|
+
Name: 'SortOrder',
|
|
302
|
+
Type: 'Input',
|
|
303
|
+
Value: 'desc'
|
|
304
|
+
},
|
|
305
|
+
{
|
|
306
|
+
Name: 'IncludeCourseStats',
|
|
307
|
+
Type: 'Input',
|
|
308
|
+
Value: true
|
|
309
|
+
},
|
|
310
|
+
{
|
|
311
|
+
Name: 'MaxResults',
|
|
312
|
+
Type: 'Input',
|
|
313
|
+
Value: 100
|
|
314
|
+
}
|
|
315
|
+
];
|
|
316
|
+
|
|
317
|
+
return [...baseParams, ...specificParams];
|
|
318
|
+
}
|
|
319
|
+
}
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
import { RegisterClass } from '@memberjunction/global';
|
|
2
|
+
import { BaseLMSAction } from '../../base/base-lms.action';
|
|
3
|
+
import { UserInfo } from '@memberjunction/core';
|
|
4
|
+
import { CompanyIntegrationEntity } from '@memberjunction/core-entities';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Base class for all LearnWorlds LMS actions.
|
|
8
|
+
* Handles LearnWorlds-specific authentication and API interaction patterns.
|
|
9
|
+
*/
|
|
10
|
+
@RegisterClass(BaseLMSAction, 'LearnWorldsBaseAction')
|
|
11
|
+
export abstract class LearnWorldsBaseAction extends BaseLMSAction {
|
|
12
|
+
protected lmsProvider = 'LearnWorlds';
|
|
13
|
+
protected integrationName = 'LearnWorlds';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* LearnWorlds API version
|
|
17
|
+
*/
|
|
18
|
+
protected apiVersion = 'v2';
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Current action parameters (set by the framework)
|
|
22
|
+
*/
|
|
23
|
+
protected params: any;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Makes an authenticated request to LearnWorlds API
|
|
27
|
+
*/
|
|
28
|
+
protected async makeLearnWorldsRequest<T = any>(
|
|
29
|
+
endpoint: string,
|
|
30
|
+
method: 'GET' | 'POST' | 'PUT' | 'DELETE' = 'GET',
|
|
31
|
+
body?: any,
|
|
32
|
+
contextUser?: UserInfo
|
|
33
|
+
): Promise<T> {
|
|
34
|
+
if (!contextUser) {
|
|
35
|
+
throw new Error('Context user is required for LearnWorlds API calls');
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// Get company ID from action params
|
|
39
|
+
const companyId = this.getParamValue(this.params, 'CompanyID');
|
|
40
|
+
if (!companyId) {
|
|
41
|
+
throw new Error('CompanyID parameter is required');
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// Get the integration credentials
|
|
45
|
+
const integration = await this.getCompanyIntegration(companyId, contextUser);
|
|
46
|
+
|
|
47
|
+
// Get API credentials (from env vars or database)
|
|
48
|
+
const credentials = await this.getAPICredentials(integration);
|
|
49
|
+
|
|
50
|
+
if (!credentials.apiKey) {
|
|
51
|
+
throw new Error('API Key is required for LearnWorlds integration');
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// Get the school domain from ExternalSystemID or environment
|
|
55
|
+
const schoolDomain = integration.ExternalSystemID || this.getCredentialFromEnv(companyId, 'SCHOOL_DOMAIN');
|
|
56
|
+
if (!schoolDomain) {
|
|
57
|
+
throw new Error('School domain not found. Set in CompanyIntegration.ExternalSystemID or environment variable');
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// Build the full URL
|
|
61
|
+
const baseUrl = `https://${schoolDomain}/api/${this.apiVersion}`;
|
|
62
|
+
const fullUrl = `${baseUrl}/${endpoint}`;
|
|
63
|
+
|
|
64
|
+
// Prepare headers
|
|
65
|
+
const headers: Record<string, string> = {
|
|
66
|
+
'Authorization': `Bearer ${credentials.apiKey}`,
|
|
67
|
+
'Accept': 'application/json',
|
|
68
|
+
'Content-Type': 'application/json',
|
|
69
|
+
'Lw-Client': 'MemberJunction'
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
try {
|
|
73
|
+
const response = await fetch(fullUrl, {
|
|
74
|
+
method,
|
|
75
|
+
headers,
|
|
76
|
+
body: body ? JSON.stringify(body) : undefined
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
if (!response.ok) {
|
|
80
|
+
const errorText = await response.text();
|
|
81
|
+
let errorMessage = `LearnWorlds API error: ${response.status} ${response.statusText}`;
|
|
82
|
+
|
|
83
|
+
try {
|
|
84
|
+
const errorJson = JSON.parse(errorText);
|
|
85
|
+
if (errorJson.error) {
|
|
86
|
+
errorMessage = `LearnWorlds API error: ${errorJson.error.message || errorJson.error}`;
|
|
87
|
+
} else if (errorJson.message) {
|
|
88
|
+
errorMessage = `LearnWorlds API error: ${errorJson.message}`;
|
|
89
|
+
}
|
|
90
|
+
} catch {
|
|
91
|
+
errorMessage += ` - ${errorText}`;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
throw new Error(errorMessage);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const result = await response.json();
|
|
98
|
+
return result as T;
|
|
99
|
+
} catch (error) {
|
|
100
|
+
if (error instanceof Error) {
|
|
101
|
+
throw error;
|
|
102
|
+
}
|
|
103
|
+
throw new Error(`LearnWorlds API request failed: ${error}`);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Makes a paginated request to LearnWorlds API
|
|
109
|
+
*/
|
|
110
|
+
protected async makeLearnWorldsPaginatedRequest<T = any>(
|
|
111
|
+
endpoint: string,
|
|
112
|
+
params: Record<string, any> = {},
|
|
113
|
+
contextUser?: UserInfo
|
|
114
|
+
): Promise<T[]> {
|
|
115
|
+
const results: T[] = [];
|
|
116
|
+
let page = 1;
|
|
117
|
+
let hasMore = true;
|
|
118
|
+
const limit = params.limit || 50;
|
|
119
|
+
|
|
120
|
+
while (hasMore) {
|
|
121
|
+
const queryParams = new URLSearchParams({
|
|
122
|
+
...params,
|
|
123
|
+
page: page.toString(),
|
|
124
|
+
limit: limit.toString()
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
const response = await this.makeLearnWorldsRequest<{
|
|
128
|
+
data: T[];
|
|
129
|
+
meta?: {
|
|
130
|
+
page: number;
|
|
131
|
+
totalPages: number;
|
|
132
|
+
totalItems: number;
|
|
133
|
+
};
|
|
134
|
+
}>(`${endpoint}?${queryParams}`, 'GET', undefined, contextUser);
|
|
135
|
+
|
|
136
|
+
if (response.data && Array.isArray(response.data)) {
|
|
137
|
+
results.push(...response.data);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// Check if there are more pages
|
|
141
|
+
if (response.meta && response.meta.page < response.meta.totalPages) {
|
|
142
|
+
page++;
|
|
143
|
+
} else {
|
|
144
|
+
hasMore = false;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// Respect max results if specified
|
|
148
|
+
const maxResults = this.getParamValue(this.params, 'MaxResults');
|
|
149
|
+
if (maxResults && results.length >= maxResults) {
|
|
150
|
+
return results.slice(0, maxResults);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
return results;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Convert LearnWorlds date format to Date object
|
|
159
|
+
*/
|
|
160
|
+
protected parseLearnWorldsDate(dateString: string | number): Date {
|
|
161
|
+
// LearnWorlds sometimes returns timestamps as seconds since epoch
|
|
162
|
+
if (typeof dateString === 'number') {
|
|
163
|
+
return new Date(dateString * 1000);
|
|
164
|
+
}
|
|
165
|
+
return new Date(dateString);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Format date for LearnWorlds API (ISO 8601)
|
|
170
|
+
*/
|
|
171
|
+
protected formatLearnWorldsDate(date: Date): string {
|
|
172
|
+
return date.toISOString();
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Map LearnWorlds user status to standard status
|
|
177
|
+
*/
|
|
178
|
+
protected mapUserStatus(status: string): 'active' | 'inactive' | 'suspended' {
|
|
179
|
+
const statusMap: Record<string, 'active' | 'inactive' | 'suspended'> = {
|
|
180
|
+
'active': 'active',
|
|
181
|
+
'inactive': 'inactive',
|
|
182
|
+
'suspended': 'suspended',
|
|
183
|
+
'blocked': 'suspended'
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
return statusMap[status.toLowerCase()] || 'inactive';
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Map LearnWorlds enrollment status
|
|
191
|
+
*/
|
|
192
|
+
protected mapLearnWorldsEnrollmentStatus(enrollment: any): 'active' | 'completed' | 'expired' | 'suspended' {
|
|
193
|
+
if (enrollment.completed) {
|
|
194
|
+
return 'completed';
|
|
195
|
+
}
|
|
196
|
+
if (enrollment.expired) {
|
|
197
|
+
return 'expired';
|
|
198
|
+
}
|
|
199
|
+
if (enrollment.suspended || !enrollment.active) {
|
|
200
|
+
return 'suspended';
|
|
201
|
+
}
|
|
202
|
+
return 'active';
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Calculate progress from LearnWorlds data
|
|
207
|
+
*/
|
|
208
|
+
protected calculateProgress(progressData: any): {
|
|
209
|
+
percentage: number;
|
|
210
|
+
completedUnits: number;
|
|
211
|
+
totalUnits: number;
|
|
212
|
+
timeSpent: number;
|
|
213
|
+
} {
|
|
214
|
+
return {
|
|
215
|
+
percentage: progressData.percentage || 0,
|
|
216
|
+
completedUnits: progressData.completed_units || 0,
|
|
217
|
+
totalUnits: progressData.total_units || 0,
|
|
218
|
+
timeSpent: progressData.time_spent || 0
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"experimentalDecorators": true,
|
|
4
|
+
"emitDecoratorMetadata": true,
|
|
5
|
+
"module": "commonjs",
|
|
6
|
+
"esModuleInterop": true,
|
|
7
|
+
"target": "es2022",
|
|
8
|
+
"lib": ["es2022"],
|
|
9
|
+
"skipLibCheck": true,
|
|
10
|
+
"declaration": true,
|
|
11
|
+
"declarationMap": true,
|
|
12
|
+
"moduleResolution": "node",
|
|
13
|
+
"sourceMap": true,
|
|
14
|
+
"outDir": "dist"
|
|
15
|
+
},
|
|
16
|
+
"include": ["src/**/*"],
|
|
17
|
+
"exclude": ["node_modules", "**/*.spec.ts"]
|
|
18
|
+
}
|