From bdd0ae00e7778eb0a4613e8b3e574d11318c23f6 Mon Sep 17 00:00:00 2001 From: muhammad-ammar Date: Tue, 19 Sep 2023 18:06:58 +0500 Subject: [PATCH] feat: add support for optional salesforce ids in csv upload --- src/components/InviteLearnersModal/index.jsx | 17 +++++- src/data/validation/email.js | 54 ++++++++++++++++++++ 2 files changed, 70 insertions(+), 1 deletion(-) diff --git a/src/components/InviteLearnersModal/index.jsx b/src/components/InviteLearnersModal/index.jsx index 89ee1e62d6..6446bf7302 100644 --- a/src/components/InviteLearnersModal/index.jsx +++ b/src/components/InviteLearnersModal/index.jsx @@ -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 { @@ -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); + if (data.haveSFIDs) { + formData['csv-email-addresses'] = data.emails; // eslint-disable-line no-param-reassign + } + // Validate form data validateEmailAddrTemplateForm(formData, emailTemplateKey); @@ -74,6 +86,9 @@ class InviteLearnersModal extends React.Component { }; options.user_emails = returnValidatedEmails(formData); + if (data.haveSFIDs) { + options.user_sfids = data.ids; + } /* eslint-disable no-underscore-dangle */ return addLicensesForUsers(options, subscriptionUUID) diff --git a/src/data/validation/email.js b/src/data/validation/email.js index c39edc6585..0db85634c7 100644 --- a/src/data/validation/email.js +++ b/src/data/validation/email.js @@ -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(','); + if (data.length === 2) { + const email = data[0].trim(); + const id = data[1].trim(); + if (email && id) { + return { email, id }; + } + } + return null; +}; + +/** +* 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 = []; + + // 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); + if (data) { + emails.push(data.email); + ids.push(data.id); + } + }); + } + + // 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); + if (data) { + emails.push(data.email); + ids.push(data.id); + } + }); + } + + return { + emails, + ids, + haveSFIDs: !!(emails.length > 0 && ids.length > 0 && emails.length === ids.length), + }; +}; /* eslint-enable no-underscore-dangle */ export { + extractEmailAndIds, validateEmailAddresses, validateEmailAddressesFields, validateEmailTemplateForm,