diff --git a/src/data/validation/email.js b/src/data/validation/email.js index 8248daa574..a7f757d8a7 100644 --- a/src/data/validation/email.js +++ b/src/data/validation/email.js @@ -86,25 +86,25 @@ const validateEmailAddresses = (emails) => { // Email and salesforce id will be separated by comma. This function will read // each row, split it by comma and then return an object with three properties: // textEmails: All emails extracted from textarea -// CSVEmails: All emails extracted from CSV -// allEmails: Concatenation of `textEmails` and `CSVEmails` +// csvEmails: All emails extracted from CSV +// allEmails: Concatenation of `textEmails` and `csvEmails` const extractEmails = (formData) => { let textEmails = []; - let CSVEmails = []; + let csvEmails = []; let allEmails = []; if (formData[EMAIL_ADDRESS_TEXT_FORM_DATA] && formData[EMAIL_ADDRESS_TEXT_FORM_DATA].length) { textEmails = formData[EMAIL_ADDRESS_TEXT_FORM_DATA].split(/\r\n|\n/).map(item => item.split(',')[0]); } if (formData[EMAIL_ADDRESS_CSV_FORM_DATA] && formData[EMAIL_ADDRESS_CSV_FORM_DATA].length) { - CSVEmails = formData[EMAIL_ADDRESS_CSV_FORM_DATA].map(item => item.split(',')[0]); + csvEmails = formData[EMAIL_ADDRESS_CSV_FORM_DATA].map(item => item.split(',')[0]); } - allEmails = [...textEmails, ...CSVEmails]; + allEmails = [...textEmails, ...csvEmails]; return { - textEmails: textEmails.length ? textEmails : undefined, - CSVEmails: CSVEmails.length ? CSVEmails : undefined, + textEmails, + csvEmails, allEmails, }; }; @@ -117,12 +117,11 @@ const validateEmailAddressesFields = (formData) => { _error: [], }; - const extractedEmails = extractEmails(formData); - const textAreaEmails = extractedEmails.textEmails; - const csvEmails = extractedEmails.CSVEmails; + const { csvEmails, textEmails } = extractEmails(formData); + const emails = textEmails.length ? textEmails : csvEmails; let { invalidEmailIndices, - } = validateEmailAddresses(textAreaEmails || csvEmails); + } = validateEmailAddresses(emails); // 1 is added to every index to fix off-by-one error in messages shown to the user. invalidEmailIndices = invalidEmailIndices.map(i => i + 1); @@ -133,7 +132,7 @@ const validateEmailAddressesFields = (formData) => { ${invalidEmailIndices.length !== 0 ? `and ${lastEmail}` : `${lastEmail}`} \ is invalid. Please try again.`; - errorsDict[textAreaEmails ? EMAIL_ADDRESS_TEXT_FORM_DATA : EMAIL_ADDRESS_CSV_FORM_DATA] = message; + errorsDict[textEmails.length ? EMAIL_ADDRESS_TEXT_FORM_DATA : EMAIL_ADDRESS_CSV_FORM_DATA] = message; errorsDict._error.push(message); } @@ -183,13 +182,13 @@ const returnValidatedEmails = (formData) => { if (errorsDict._error.length > 0) { throw new SubmissionError(errorsDict); } - let emails = extractEmails(formData).allEmails; - emails = _.union(emails); // Dedup emails + + const emails = _.union(extractEmails(formData).allEmails); // Dedup emails return validateEmailAddresses(emails).validEmails; }; // Combine all the rows from textarea and CSV and then make a map of email to salesforce id -const parseData = (formData) => { +const getSalesforceIdsByEmail = (formData) => { const rows = []; const allRecords = {}; @@ -202,8 +201,8 @@ const parseData = (formData) => { } rows.forEach((row) => { - const [email, id] = row.split(',').map(item => item.trim()); - allRecords[email] = id; + const [email, salesforceId] = row.split(',').map(item => item.trim()); + allRecords[email] = salesforceId; }); return allRecords; @@ -211,15 +210,16 @@ const parseData = (formData) => { // Extract salesforce ids for all validated emails const extractSalesforceIds = (formData, userEmails) => { - const parsedData = parseData(formData); + const salesforceIdsByEmail = getSalesforceIdsByEmail(formData); const ids = []; userEmails.forEach((email) => { - ids.push(parsedData[email]); + ids.push(salesforceIdsByEmail[email]); }); - const allFalse = ids.every(item => !item); - return allFalse ? undefined : ids; + // check if `ids` array contain non-empty, not-null values + const noIdsPresent = ids.every(item => !item); + return noIdsPresent ? undefined : ids; }; /* eslint-enable no-underscore-dangle */ diff --git a/src/data/validation/email.test.js b/src/data/validation/email.test.js index 9507eaeb88..4cc46ac214 100644 --- a/src/data/validation/email.test.js +++ b/src/data/validation/email.test.js @@ -236,7 +236,7 @@ describe('email validation', () => { }); describe('validate emails and ids extraction', () => { - it('extracted correct emails and ids', () => { + it('extracted correct emails and ids from textarea and csv', () => { const formData = new FormData(); formData[EMAIL_ADDRESS_TEXT_FORM_DATA] = [ 'abc@example.com,000000000000ABCABC', @@ -249,6 +249,7 @@ describe('email validation', () => { 'three@example.com,000000000000ABCDDD', 'wow@example.com,', 'abc@example.com,000000000000ABCABC', + 'ama@example.com', ]; const userEmails = [ 'abc@example.com', @@ -258,10 +259,11 @@ describe('email validation', () => { 'two@example.com', 'three@example.com', 'wow@example.com', + 'ama@example.com', ]; - const ids = extractSalesforceIds(formData, userEmails); - expect(ids).toEqual([ + const salesforceIds = extractSalesforceIds(formData, userEmails); + expect(salesforceIds).toEqual([ '000000000000ABCABC', '', '000000000000XYZXYZ', @@ -269,8 +271,68 @@ describe('email validation', () => { '000000000000ZZZZZZ', '000000000000ABCDDD', '', + undefined, ]); - expect(userEmails.length).toEqual(ids.length); + expect(userEmails.length).toEqual(salesforceIds.length); + }); + + it('extracted correct emails and ids from textarea only', () => { + const formData = new FormData(); + formData[EMAIL_ADDRESS_TEXT_FORM_DATA] = [ + 'aaa@example.com,000000000000ABCABC', + 'bbb@example.com,', + 'ccc@example.com,000000000000XYZXYZ', + 'ddd@example.com', + ].join('\n'); + + const userEmails = returnValidatedEmails(formData); + expect(userEmails).toEqual(['aaa@example.com', 'bbb@example.com', 'ccc@example.com', 'ddd@example.com']); + const salesforceIds = extractSalesforceIds(formData, userEmails); + expect(salesforceIds).toEqual([ + '000000000000ABCABC', + '', + '000000000000XYZXYZ', + undefined, + ]); + expect(userEmails.length).toEqual(salesforceIds.length); + }); + + it('extracted correct emails and ids from csv only', () => { + const formData = new FormData(); + formData[EMAIL_ADDRESS_CSV_FORM_DATA] = [ + 'eee@example.com,000000000000YYYYYY', + 'fff@example.com,', + 'ggg@example.com,000000000000ZZZZZZ', + 'hhh@example.com', + ]; + + const userEmails = returnValidatedEmails(formData); + expect(userEmails).toEqual(['eee@example.com', 'fff@example.com', 'ggg@example.com', 'hhh@example.com']); + const salesforceIds = extractSalesforceIds(formData, userEmails); + expect(salesforceIds).toEqual([ + '000000000000YYYYYY', + '', + '000000000000ZZZZZZ', + undefined, + ]); + expect(userEmails.length).toEqual(salesforceIds.length); + }); + + it('returns no salesforce ids for emails only', () => { + const formData = new FormData(); + formData[EMAIL_ADDRESS_TEXT_FORM_DATA] = [ + 'abc@example.com', + 'asdf@example.com,', + ].join('\n'); + formData[EMAIL_ADDRESS_CSV_FORM_DATA] = [ + 'one@example.com,', + 'two@example.com', + ]; + + const userEmails = returnValidatedEmails(formData); + expect(userEmails).toEqual(['abc@example.com', 'asdf@example.com', 'one@example.com', 'two@example.com']); + const salesforceIds = extractSalesforceIds(formData, userEmails); + expect(salesforceIds).toEqual(undefined); }); }); });