Skip to content

Commit

Permalink
chore: address feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
muhammad-ammar committed Sep 25, 2023
1 parent 81051cf commit f8b33ad
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 25 deletions.
42 changes: 21 additions & 21 deletions src/data/validation/email.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
};
Expand All @@ -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);
Expand All @@ -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);
}

Expand Down Expand Up @@ -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 = {};

Expand All @@ -202,24 +201,25 @@ 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;
};

// 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 */

Expand Down
70 changes: 66 additions & 4 deletions src/data/validation/email.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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] = [
'[email protected],000000000000ABCABC',
Expand All @@ -249,6 +249,7 @@ describe('email validation', () => {
'[email protected],000000000000ABCDDD',
'[email protected],',
'[email protected],000000000000ABCABC',
'[email protected]',
];
const userEmails = [
'[email protected]',
Expand All @@ -258,19 +259,80 @@ describe('email validation', () => {
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
];

const ids = extractSalesforceIds(formData, userEmails);
expect(ids).toEqual([
const salesforceIds = extractSalesforceIds(formData, userEmails);
expect(salesforceIds).toEqual([
'000000000000ABCABC',
'',
'000000000000XYZXYZ',
'000000000000YYYYYY',
'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] = [
'[email protected],000000000000ABCABC',
'[email protected],',
'[email protected],000000000000XYZXYZ',
'[email protected]',
].join('\n');

const userEmails = returnValidatedEmails(formData);
expect(userEmails).toEqual(['[email protected]', '[email protected]', '[email protected]', '[email protected]']);
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] = [
'[email protected],000000000000YYYYYY',
'[email protected],',
'[email protected],000000000000ZZZZZZ',
'[email protected]',
];

const userEmails = returnValidatedEmails(formData);
expect(userEmails).toEqual(['[email protected]', '[email protected]', '[email protected]', '[email protected]']);
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] = [
'[email protected]',
'[email protected],',
].join('\n');
formData[EMAIL_ADDRESS_CSV_FORM_DATA] = [
'[email protected],',
'[email protected]',
];

const userEmails = returnValidatedEmails(formData);
expect(userEmails).toEqual(['[email protected]', '[email protected]', '[email protected]', '[email protected]']);
const salesforceIds = extractSalesforceIds(formData, userEmails);
expect(salesforceIds).toEqual(undefined);
});
});
});

0 comments on commit f8b33ad

Please sign in to comment.