Skip to content

Commit

Permalink
Create linked learners when assigning codes (#506)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lael Birch authored Mar 19, 2021
1 parent 0c73558 commit 37e853f
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 4 deletions.
27 changes: 23 additions & 4 deletions src/components/CodeAssignmentModal/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,10 @@ class BaseCodeAssignmentModal extends React.Component {
hasAllCodesSelected,
},
sendCodeAssignment,
createPendingEnterpriseUsers,
enableLearnerPortal,
enterpriseSlug,
enterpriseUuid,
} = this.props;

this.setMode('assign');
Expand All @@ -336,10 +338,11 @@ class BaseCodeAssignmentModal extends React.Component {
options.template_id = formData['template-id'];
}

const hasTextAreaEmails = !!formData[EMAIL_ADDRESS_TEXT_FORM_DATA];
const emails = hasTextAreaEmails ? formData[EMAIL_ADDRESS_TEXT_FORM_DATA].split(/\r\n|\n/) : formData[EMAIL_ADDRESS_CSV_FORM_DATA];
const { validEmails } = this.validateEmailAddresses(emails, !hasTextAreaEmails);

if (isBulkAssign) {
const hasTextAreaEmails = !!formData[EMAIL_ADDRESS_TEXT_FORM_DATA];
const emails = hasTextAreaEmails ? formData[EMAIL_ADDRESS_TEXT_FORM_DATA].split(/\r\n|\n/) : formData[EMAIL_ADDRESS_CSV_FORM_DATA];
const { validEmails } = this.validateEmailAddresses(emails, !hasTextAreaEmails);
options.emails = validEmails;

// Only includes `codes` in `options` if not all codes are selected.
Expand All @@ -351,7 +354,21 @@ class BaseCodeAssignmentModal extends React.Component {
options.codes = [code.code];
}

return sendCodeAssignment(couponId, options)
let pendingEnterpriseUserData;
if (hasTextAreaEmails) {
pendingEnterpriseUserData = validEmails.map((email) => ({
user_email: email,
enterprise_customer: enterpriseUuid,
}));
} else {
pendingEnterpriseUserData = {
user_email: formData['email-address'],
enterprise_customer: enterpriseUuid,
};
}

return createPendingEnterpriseUsers(pendingEnterpriseUserData)
.then(() => sendCodeAssignment(couponId, options))
.then((response) => {
this.props.onSuccess(response);
})
Expand Down Expand Up @@ -547,6 +564,7 @@ BaseCodeAssignmentModal.defaultProps = {
BaseCodeAssignmentModal.propTypes = {
// props from redux
enterpriseSlug: PropTypes.string.isRequired,
enterpriseUuid: PropTypes.string.isRequired,
currentEmail: PropTypes.string,
enableLearnerPortal: PropTypes.bool.isRequired,
// props From redux-form
Expand All @@ -562,6 +580,7 @@ BaseCodeAssignmentModal.propTypes = {
onClose: PropTypes.func.isRequired,
onSuccess: PropTypes.func.isRequired,
sendCodeAssignment: PropTypes.func.isRequired,
createPendingEnterpriseUsers: PropTypes.func.isRequired,
setEmailAddress: PropTypes.func.isRequired,
couponDetailsTable: PropTypes.shape({
data: PropTypes.shape({
Expand Down
11 changes: 11 additions & 0 deletions src/containers/CodeAssignmentModal/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { connect } from 'react-redux';
import CodeAssignmentModal from '../../components/CodeAssignmentModal';

import sendCodeAssignment from '../../data/actions/codeAssignment';
import createPendingEnterpriseUsers from '../../data/actions/createPendingEnterpriseUsers';
import { EMAIL_TEMPLATE_SOURCE_NEW_EMAIL } from '../../data/constants/emailTemplate';
import { setEmailAddress } from '../../data/actions/emailTemplate';

Expand All @@ -18,6 +19,9 @@ const mapStateToProps = (state) => {
couponDetailsTable: state.table['coupon-details'],
initialValues,
enableReinitialize: true,
enterpriseSlug: state.portalConfiguration.enterpriseSlug,
enterpriseUuid: state.portalConfiguration.enterpriseId,
enableLearnerPortal: state.portalConfiguration.enableLearnerPortal,
};
};

Expand All @@ -30,6 +34,13 @@ const mapDispatchToProps = dispatch => ({
onError: (error) => { reject(error); },
}));
}),
createPendingEnterpriseUsers: (users) => new Promise((resolve, reject) => {
dispatch(createPendingEnterpriseUsers({
users,
onSuccess: (response) => { resolve(response); },
onError: (error) => { reject(error); },
}));
}),
setEmailAddress: (emailAddress, emailType) => dispatch(setEmailAddress(emailAddress, emailType)),
});

Expand Down
46 changes: 46 additions & 0 deletions src/data/actions/createPendingEnterpriseUsers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import {
PENDING_ENT_USER_REQUEST,
PENDING_ENT_USER_SUCCESS,
PENDING_ENT_USER_FAILURE,
} from '../constants/createPendingEntUser';

import LmsApiService from '../services/LmsApiService';

const createPendingUsersRequest = () => ({
type: PENDING_ENT_USER_REQUEST,
});

const createPendingUsersSuccess = data => ({
type: PENDING_ENT_USER_SUCCESS,
payload: {
data,
},
});

const createPendingUsersFailure = error => ({
type: PENDING_ENT_USER_FAILURE,
payload: {
error,
},
});

const createPendingEnterpriseUsers = ({
users,
onSuccess = () => {},
onError = () => {},
}) => (
(dispatch) => {
dispatch(createPendingUsersRequest());
return LmsApiService.createPendingEnterpriseUsers(users)
.then((response) => {
dispatch(createPendingUsersSuccess(response.data));
onSuccess(response.data);
})
.catch((error) => {
dispatch(createPendingUsersFailure(error));
onError(error);
});
}
);

export default createPendingEnterpriseUsers;
9 changes: 9 additions & 0 deletions src/data/constants/createPendingEntUser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const PENDING_ENT_USER_REQUEST = 'PENDING_ENT_USER_REQUEST';
const PENDING_ENT_USER_SUCCESS = 'PENDING_ENT_USER_SUCCESS';
const PENDING_ENT_USER_FAILURE = 'PENDING_ENT_USER_FAILURE';

export {
PENDING_ENT_USER_REQUEST,
PENDING_ENT_USER_SUCCESS,
PENDING_ENT_USER_FAILURE,
};
6 changes: 6 additions & 0 deletions src/data/services/LmsApiService.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class LmsApiService {

static lmsIntegrationUrl = `${LmsApiService.baseUrl}/integrated_channels/api/v1`;

static createPendingUsersUrl = `${LmsApiService.baseUrl}/enterprise/api/v1/pending-enterprise-learner/`

static fetchCourseOutline(courseId) {
const options = {
course_id: courseId,
Expand Down Expand Up @@ -196,6 +198,10 @@ class LmsApiService {
const url = `${LmsApiService.enterpriseCustomerUrl}${enterpriseId}/enterprise_learners/`;
return LmsApiService.apiClient().post(url, options);
}

static createPendingEnterpriseUsers(formData) {
return LmsApiService.apiClient().post(LmsApiService.createPendingUsersUrl, formData);
}
}

export default LmsApiService;

0 comments on commit 37e853f

Please sign in to comment.