@memberjunction/actions-bizapps-lms
Version:
LMS system integration actions for MemberJunction
269 lines • 10.4 kB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import { RegisterClass } from '@memberjunction/global';
import { LearnWorldsBaseAction } from '../learnworlds-base.action.js';
import { BaseAction } from '@memberjunction/actions';
/**
* Action to create a new user in LearnWorlds
*/
let CreateUserAction = class CreateUserAction extends LearnWorldsBaseAction {
/**
* Typed public method for direct (non-framework) callers.
* Throws on failure instead of returning ActionResultSimple.
*/
async CreateUser(params, contextUser) {
this.SetCompanyContext(params.CompanyID);
const email = params.Email;
if (!email) {
throw new Error('Email is required');
}
this.validateEmail(email);
const role = this.validateRole(params.Role || 'student');
const isActive = params.IsActive !== false;
const sendWelcomeEmail = params.SendWelcomeEmail !== false;
// Prepare user data
const userData = {
email: email,
role: role,
is_active: isActive,
};
// Add optional fields
userData.username = params.Username || email;
if (params.Password)
userData.password = params.Password;
if (params.FirstName)
userData.first_name = params.FirstName;
if (params.LastName)
userData.last_name = params.LastName;
userData.send_welcome_email = sendWelcomeEmail;
// Add tags if provided (expecting comma-separated string or array)
if (params.Tags) {
userData.tags = Array.isArray(params.Tags) ? params.Tags : params.Tags.split(',').map((t) => t.trim());
}
// Add custom fields if provided
if (params.CustomFields) {
userData.custom_fields = params.CustomFields;
}
// Create user
const newUser = await this.makeLearnWorldsRequest('users', 'POST', userData, contextUser);
if (!newUser.id) {
throw new Error('User creation response missing expected user ID');
}
// Format user details
const userDetails = this.buildUserDetails(newUser);
// Enroll in courses if requested
const enrollmentResults = await this.enrollInCourses(newUser.id, params.EnrollInCourses, contextUser);
// Create summary
const summary = this.buildSummary(userDetails, sendWelcomeEmail, enrollmentResults);
return {
UserDetails: userDetails,
EnrollmentResults: enrollmentResults,
Summary: summary,
};
}
/**
* Framework entry point -- delegates to the typed public method.
*/
async InternalRunAction(params) {
const { Params, ContextUser } = params;
this.params = Params;
try {
const typedParams = this.extractCreateUserParams(Params);
const result = await this.CreateUser(typedParams, ContextUser);
this.setOutputParam(Params, 'UserDetails', result.UserDetails);
this.setOutputParam(Params, 'EnrollmentResults', result.EnrollmentResults);
this.setOutputParam(Params, 'Summary', result.Summary);
return this.buildSuccessResult(`Successfully created user ${result.UserDetails.email}`, Params);
}
catch (error) {
const msg = error instanceof Error ? error.message : 'Unknown error';
return this.buildErrorResult('ERROR', `Error creating user: ${msg}`, Params);
}
}
/**
* Extract typed params from the generic ActionParam array
*/
extractCreateUserParams(params) {
return {
CompanyID: this.getRequiredStringParam(params, 'CompanyID'),
Email: this.getRequiredStringParam(params, 'Email'),
Username: this.getOptionalStringParam(params, 'Username'),
Password: this.getOptionalStringParam(params, 'Password'),
FirstName: this.getOptionalStringParam(params, 'FirstName'),
LastName: this.getOptionalStringParam(params, 'LastName'),
Role: this.getOptionalStringParam(params, 'Role') || 'student',
IsActive: this.getOptionalBooleanParam(params, 'IsActive', undefined),
SendWelcomeEmail: this.getOptionalBooleanParam(params, 'SendWelcomeEmail', undefined),
Tags: this.getParamValue(params, 'Tags'),
CustomFields: this.getParamValue(params, 'CustomFields'),
EnrollInCourses: this.getParamValue(params, 'EnrollInCourses'),
};
}
/**
* Map the raw LW create-user response to our typed details shape.
*/
buildUserDetails(newUser) {
return {
id: newUser.id,
email: newUser.email,
username: newUser.username,
firstName: newUser.first_name,
lastName: newUser.last_name,
fullName: `${newUser.first_name || ''} ${newUser.last_name || ''}`.trim(),
role: newUser.role || 'student',
status: newUser.is_active ? 'active' : 'inactive',
tags: newUser.tags || [],
customFields: newUser.custom_fields || {},
createdAt: newUser.created_at,
loginUrl: newUser.login_url,
resetPasswordUrl: newUser.reset_password_url,
};
}
/**
* Enroll the newly created user in any requested courses.
*/
async enrollInCourses(userId, enrollInCourses, contextUser) {
const enrollmentResults = [];
if (!enrollInCourses || (Array.isArray(enrollInCourses) && enrollInCourses.length === 0)) {
return enrollmentResults;
}
const courseIds = Array.isArray(enrollInCourses) ? enrollInCourses : [enrollInCourses];
for (const courseId of courseIds) {
try {
this.validatePathSegment(courseId, 'CourseID');
const enrollBody = {
productId: courseId,
productType: 'course',
justification: 'Enrolled during user creation',
price: 0,
send_enrollment_email: false,
};
const enrollData = await this.makeLearnWorldsRequest(`users/${userId}/enrollment`, 'POST', enrollBody, contextUser);
enrollmentResults.push({
courseId: courseId,
success: true,
enrollmentId: enrollData.id,
});
}
catch (enrollError) {
enrollmentResults.push({
courseId: courseId,
success: false,
error: enrollError instanceof Error ? enrollError.message : 'Enrollment failed',
});
}
}
return enrollmentResults;
}
/**
* Build the summary object from user details and enrollment results.
*/
buildSummary(userDetails, sendWelcomeEmail, enrollmentResults) {
return {
userId: userDetails.id,
email: userDetails.email,
username: userDetails.username,
fullName: userDetails.fullName,
role: userDetails.role,
status: userDetails.status,
welcomeEmailSent: sendWelcomeEmail,
coursesEnrolled: enrollmentResults.filter((r) => r.success).length,
totalCoursesRequested: enrollmentResults.length,
loginUrl: userDetails.loginUrl,
};
}
/**
* Define the parameters this action expects
*/
get Params() {
const baseParams = this.getCommonLMSParams();
const specificParams = [
{
Name: 'Email',
Type: 'Input',
Value: null,
},
{
Name: 'Username',
Type: 'Input',
Value: null,
},
{
Name: 'Password',
Type: 'Input',
Value: null,
},
{
Name: 'FirstName',
Type: 'Input',
Value: null,
},
{
Name: 'LastName',
Type: 'Input',
Value: null,
},
{
Name: 'Role',
Type: 'Input',
Value: 'student',
},
{
Name: 'IsActive',
Type: 'Input',
Value: true,
},
{
Name: 'SendWelcomeEmail',
Type: 'Input',
Value: true,
},
{
Name: 'Tags',
Type: 'Input',
Value: null,
},
{
Name: 'CustomFields',
Type: 'Input',
Value: null,
},
{
Name: 'EnrollInCourses',
Type: 'Input',
Value: null,
},
{
Name: 'UserDetails',
Type: 'Output',
Value: null,
},
{
Name: 'EnrollmentResults',
Type: 'Output',
Value: null,
},
{
Name: 'Summary',
Type: 'Output',
Value: null,
},
];
return [...baseParams, ...specificParams];
}
/**
* Metadata about this action
*/
get Description() {
return 'Creates a new user in LearnWorlds with optional course enrollments and welcome email';
}
};
CreateUserAction = __decorate([
RegisterClass(BaseAction, 'CreateUserAction')
], CreateUserAction);
export { CreateUserAction };
//# sourceMappingURL=create-user.action.js.map