Skip to content

Commit

Permalink
feat: add support for optional salesforce ids in csv upload
Browse files Browse the repository at this point in the history
  • Loading branch information
muhammad-ammar committed Sep 20, 2023
1 parent 0eca22c commit bdd0ae0
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/components/InviteLearnersModal/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { camelCaseObject } from '@edx/frontend-platform/utils';
import emailTemplate from './emailTemplate';
import TextAreaAutoSize from '../TextAreaAutoSize';
import FileInput from '../FileInput';
import { returnValidatedEmails, validateEmailAddrTemplateForm } from '../../data/validation/email';
import { extractEmailAndIds, returnValidatedEmails, validateEmailAddrTemplateForm } from '../../data/validation/email';
import { normalizeFileUpload } from '../../utils';

class InviteLearnersModal extends React.Component {
Expand Down Expand Up @@ -64,6 +64,18 @@ class InviteLearnersModal extends React.Component {
} = this.props;

const emailTemplateKey = 'email-template-body';

// Data can contain
// 1. emails only, OR
// 2. emails + Salesforce IDs (every email will have a corresponding sfid)
// In the 2nd case, remove the Salesforce IDs from formData and
// handle the Salesforce IDs independently, this way emails
// validation will remain as it is.
const data = extractEmailAndIds(formData);

Check warning on line 74 in src/components/InviteLearnersModal/index.jsx

View check run for this annotation

Codecov / codecov/patch

src/components/InviteLearnersModal/index.jsx#L74

Added line #L74 was not covered by tests
if (data.haveSFIDs) {
formData['csv-email-addresses'] = data.emails; // eslint-disable-line no-param-reassign

Check warning on line 76 in src/components/InviteLearnersModal/index.jsx

View check run for this annotation

Codecov / codecov/patch

src/components/InviteLearnersModal/index.jsx#L76

Added line #L76 was not covered by tests
}

// Validate form data
validateEmailAddrTemplateForm(formData, emailTemplateKey);

Expand All @@ -74,6 +86,9 @@ class InviteLearnersModal extends React.Component {
};

options.user_emails = returnValidatedEmails(formData);
if (data.haveSFIDs) {
options.user_sfids = data.ids;

Check warning on line 90 in src/components/InviteLearnersModal/index.jsx

View check run for this annotation

Codecov / codecov/patch

src/components/InviteLearnersModal/index.jsx#L90

Added line #L90 was not covered by tests
}

/* eslint-disable no-underscore-dangle */
return addLicensesForUsers(options, subscriptionUUID)
Expand Down
54 changes: 54 additions & 0 deletions src/data/validation/email.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,63 @@ const returnValidatedEmails = (formData) => {
emails = _.union(emails); // Dedup emails
return validateEmailAddresses(emails).validEmails;
};

// Return an object of email and id if non-empty email and id are present
const sanitize = (row) => {
const data = row.split(',');

Check warning on line 171 in src/data/validation/email.js

View check run for this annotation

Codecov / codecov/patch

src/data/validation/email.js#L171

Added line #L171 was not covered by tests
if (data.length === 2) {
const email = data[0].trim();
const id = data[1].trim();

Check warning on line 174 in src/data/validation/email.js

View check run for this annotation

Codecov / codecov/patch

src/data/validation/email.js#L173-L174

Added lines #L173 - L174 were not covered by tests
if (email && id) {
return { email, id };

Check warning on line 176 in src/data/validation/email.js

View check run for this annotation

Codecov / codecov/patch

src/data/validation/email.js#L176

Added line #L176 was not covered by tests
}
}
return null;

Check warning on line 179 in src/data/validation/email.js

View check run for this annotation

Codecov / codecov/patch

src/data/validation/email.js#L179

Added line #L179 was not covered by tests
};

/**
* return an object containing
* emails (array): Learner emails
* ids (array): Salesforce ids corresponding to each learner
* dataHaveSFIDs (bool): Whether the formData contains Salesforce ids or not
*/
const extractEmailAndIds = (formData) => {
const emails = []; const
ids = [];

Check warning on line 190 in src/data/validation/email.js

View check run for this annotation

Codecov / codecov/patch

src/data/validation/email.js#L189-L190

Added lines #L189 - L190 were not covered by tests

// TODO: TBD: Most probably we will remove this and will not handle emails + salesforce ids in textarea
if (formData[EMAIL_ADDRESS_TEXT_FORM_DATA] && formData[EMAIL_ADDRESS_TEXT_FORM_DATA].length) {
const rows = formData[EMAIL_ADDRESS_TEXT_FORM_DATA].split(/\r\n|\n/);
rows.forEach((row) => {
const data = sanitize(row);

Check warning on line 196 in src/data/validation/email.js

View check run for this annotation

Codecov / codecov/patch

src/data/validation/email.js#L194-L196

Added lines #L194 - L196 were not covered by tests
if (data) {
emails.push(data.email);
ids.push(data.id);

Check warning on line 199 in src/data/validation/email.js

View check run for this annotation

Codecov / codecov/patch

src/data/validation/email.js#L198-L199

Added lines #L198 - L199 were not covered by tests
}
});
}

// TBD: We will only handle emails + salesforce ids in CSV
if (formData[EMAIL_ADDRESS_CSV_FORM_DATA] && formData[EMAIL_ADDRESS_CSV_FORM_DATA].length) {
formData[EMAIL_ADDRESS_CSV_FORM_DATA].forEach((row) => {
const data = sanitize(row);

Check warning on line 207 in src/data/validation/email.js

View check run for this annotation

Codecov / codecov/patch

src/data/validation/email.js#L206-L207

Added lines #L206 - L207 were not covered by tests
if (data) {
emails.push(data.email);
ids.push(data.id);

Check warning on line 210 in src/data/validation/email.js

View check run for this annotation

Codecov / codecov/patch

src/data/validation/email.js#L209-L210

Added lines #L209 - L210 were not covered by tests
}
});
}

return {

Check warning on line 215 in src/data/validation/email.js

View check run for this annotation

Codecov / codecov/patch

src/data/validation/email.js#L215

Added line #L215 was not covered by tests
emails,
ids,
haveSFIDs: !!(emails.length > 0 && ids.length > 0 && emails.length === ids.length),

Check warning on line 218 in src/data/validation/email.js

View check run for this annotation

Codecov / codecov/patch

src/data/validation/email.js#L218

Added line #L218 was not covered by tests
};
};
/* eslint-enable no-underscore-dangle */

export {
extractEmailAndIds,
validateEmailAddresses,
validateEmailAddressesFields,
validateEmailTemplateForm,
Expand Down

0 comments on commit bdd0ae0

Please sign in to comment.