UNPKG

@memberjunction/actions-bizapps-lms

Version:

LMS system integration actions for MemberJunction

168 lines 7.57 kB
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 update an existing user in LearnWorlds */ let UpdateUserAction = class UpdateUserAction extends LearnWorldsBaseAction { /** * Updates a user's profile in LearnWorlds. * Only sends non-undefined fields in the update body. */ async UpdateUser(params, contextUser) { this.SetCompanyContext(params.CompanyID); this.validatePathSegment(params.UserID, 'UserID'); const { body, fieldsUpdated } = this.buildUpdateBody(params); if (fieldsUpdated.length === 0) { throw new Error('No fields provided to update'); } const response = await this.makeLearnWorldsRequest(`users/${params.UserID}`, 'PUT', body, contextUser); const userDetails = this.mapResponseToUser(response); const summary = this.buildUpdateSummary(params.UserID, userDetails.email, fieldsUpdated); return { UserDetails: userDetails, Summary: summary }; } /** * Builds the API request body from non-undefined params and tracks which fields were updated */ buildUpdateBody(params) { const body = {}; const fieldsUpdated = []; if (params.Email !== undefined) { body.email = params.Email; fieldsUpdated.push('Email'); } if (params.Username !== undefined) { body.username = params.Username; fieldsUpdated.push('Username'); } if (params.Password !== undefined) { body.password = params.Password; fieldsUpdated.push('Password'); } if (params.FirstName !== undefined) { body.first_name = params.FirstName; fieldsUpdated.push('FirstName'); } if (params.LastName !== undefined) { body.last_name = params.LastName; fieldsUpdated.push('LastName'); } if (params.Role !== undefined) { body.role = this.validateRole(params.Role); fieldsUpdated.push('Role'); } if (params.IsActive !== undefined) { body.is_active = params.IsActive; fieldsUpdated.push('IsActive'); } if (params.Tags !== undefined) { body.tags = params.Tags; fieldsUpdated.push('Tags'); } if (params.CustomFields !== undefined) { body.custom_fields = params.CustomFields; fieldsUpdated.push('CustomFields'); } return { body, fieldsUpdated }; } /** * Maps the raw LearnWorlds API response to a LearnWorldsUser */ mapResponseToUser(response) { return { id: response.id || response._id || '', email: response.email, username: response.username || response.email, firstName: response.first_name, lastName: response.last_name, fullName: response.full_name || `${response.first_name || ''} ${response.last_name || ''}`.trim(), status: this.mapUserStatus(response.status || 'active'), role: response.role || 'student', createdAt: this.parseLearnWorldsDate(response.created || response.created_at || ''), lastLoginAt: response.last_login ? this.parseLearnWorldsDate(response.last_login) : undefined, tags: response.tags || [], customFields: response.custom_fields || {}, }; } /** * Builds a summary of what was updated */ buildUpdateSummary(userId, email, fieldsUpdated) { return { userId, email, fieldsUpdated, }; } /** * Framework entry point that delegates to the typed public method */ async InternalRunAction(params) { const { Params, ContextUser } = params; this.params = Params; try { const updateParams = { CompanyID: this.getRequiredStringParam(Params, 'CompanyID'), UserID: this.getRequiredStringParam(Params, 'UserID'), Email: this.getOptionalStringParam(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'), IsActive: this.getOptionalBooleanParam(Params, 'IsActive', undefined), Tags: this.getOptionalStringArrayParam(Params, 'Tags'), CustomFields: this.getParamValue(Params, 'CustomFields'), }; if (!updateParams.UserID) { return this.buildErrorResult('VALIDATION_ERROR', 'UserID is required', Params); } const result = await this.UpdateUser(updateParams, ContextUser); this.setOutputParam(Params, 'UserDetails', result.UserDetails); this.setOutputParam(Params, 'Summary', result.Summary); return this.buildSuccessResult(`Successfully updated user ${result.UserDetails.email} (${result.Summary.fieldsUpdated.length} field(s) updated)`, Params); } catch (error) { const errorMessage = error instanceof Error ? error.message : 'Unknown error occurred'; return this.buildErrorResult('ERROR', `Error updating user: ${errorMessage}`, Params); } } /** * Define the parameters this action expects */ get Params() { const baseParams = this.getCommonLMSParams(); const specificParams = [ { Name: 'UserID', Type: 'Input', Value: null }, { 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: null }, { Name: 'IsActive', Type: 'Input', Value: null }, { Name: 'Tags', Type: 'Input', Value: null }, { Name: 'CustomFields', Type: 'Input', Value: null }, { Name: 'UserDetails', Type: 'Output', Value: null }, { Name: 'Summary', Type: 'Output', Value: null }, ]; return [...baseParams, ...specificParams]; } /** * Metadata about this action */ get Description() { return 'Updates an existing user profile in LearnWorlds with optional field-level changes'; } }; UpdateUserAction = __decorate([ RegisterClass(BaseAction, 'UpdateUserAction') ], UpdateUserAction); export { UpdateUserAction }; //# sourceMappingURL=update-user.action.js.map