Skip to content

Commit

Permalink
fix: start migration for getSearchOptions, getOptions and createOption
Browse files Browse the repository at this point in the history
  • Loading branch information
BeeMargarida committed Jun 7, 2023
1 parent b9d30ef commit ef3d955
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 45 deletions.
4 changes: 4 additions & 0 deletions src/CONST.js
Original file line number Diff line number Diff line change
Expand Up @@ -872,6 +872,10 @@ const CONST = {
GUIDES_DOMAIN: 'team.expensify.com',
},

ACCOUNT_ID: {
CONCIERGE: '8392101',
},

ENVIRONMENT: {
DEV: 'development',
STAGING: 'staging',
Expand Down
79 changes: 45 additions & 34 deletions src/libs/OptionsListUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,36 +161,34 @@ function getAvatarsForLogins(logins, personalDetails) {
}

/**
* Returns the personal details for an array of logins
* Returns the personal details for an array of accountIDs
*
* @param {Array} logins
* @param {Array} accountIDs
* @param {Object} personalDetails
* @returns {Object} – keys of the object are emails, values are PersonalDetails objects.
*/
function getPersonalDetailsForLogins(logins, personalDetails) {
function getPersonalDetailsForAccountIDs(accountIDs, personalDetails) {
const personalDetailsForLogins = {};
if (!personalDetails) {
return personalDetailsForLogins;
}
_.chain(logins)

// Somehow it's possible for the logins coming from report.participants to contain undefined values so we use compact to remove them.
_.chain(accountIDs)
// Somehow it's possible for the accountIDs coming from report.participantAccountIDs to contain undefined values so we use compact to remove them.
.compact()
.each((login) => {
let personalDetail = personalDetails[login];
.each((accountID) => {
let personalDetail = personalDetails[accountID];
if (!personalDetail) {
personalDetail = {
login,
displayName: LocalePhoneNumber.formatPhoneNumber(login),
avatar: UserUtils.getDefaultAvatar(login),
accountID,
avatar: UserUtils.getDefaultAvatar(accountID),
};
}

if (login === CONST.EMAIL.CONCIERGE) {
if (accountID === CONST.ACCOUNT_ID.CONCIERGE) {
personalDetail.avatar = CONST.CONCIERGE_ICON_URL;
}

personalDetailsForLogins[login] = personalDetail;
personalDetailsForLogins[accountID] = personalDetail;
});
return personalDetailsForLogins;
}
Expand All @@ -201,7 +199,7 @@ function getPersonalDetailsForLogins(logins, personalDetails) {
* @returns {Boolean}
*/
function isPersonalDetailsReady(personalDetails) {
return !_.isEmpty(personalDetails) && _.some(_.keys(personalDetails), (key) => personalDetails[key].login);
return !_.isEmpty(personalDetails) && _.some(_.keys(personalDetails), (key) => personalDetails[key].accountID);
}

/**
Expand All @@ -212,7 +210,7 @@ function isPersonalDetailsReady(personalDetails) {
*/
function getParticipantsOptions(report, personalDetails) {
const participants = lodashGet(report, 'participants', []);
return _.map(getPersonalDetailsForLogins(participants, personalDetails), (details) => ({
return _.map(getPersonalDetailsForAccountIDs(participants, personalDetails), (details) => ({
keyForList: details.login,
login: details.login,
text: details.displayName,
Expand Down Expand Up @@ -302,12 +300,14 @@ function getSearchText(report, reportName, personalDetailList, isChatRoomOrPolic
for (let i = 0; i < personalDetailList.length; i++) {
const personalDetail = personalDetailList[i];

if (personalDetail.login) {
// The regex below is used to remove dots only from the local part of the user email (local-part@domain)
// so that we can match emails that have dots without explicitly writing the dots (e.g: fistlast@domain will match first.last@domain)
// More info https://github.com/Expensify/App/issues/8007
searchTerms = searchTerms.concat([personalDetail.displayName, personalDetail.login, personalDetail.login.replace(/\.(?=[^\s@]*@)/g, '')]);
}
}
}
if (report) {
Array.prototype.push.apply(searchTerms, reportName.split(/[,\s]/));

Expand Down Expand Up @@ -362,7 +362,7 @@ function getAllReportErrors(report, reportActions) {
/**
* Creates a report list option
*
* @param {Array<String>} logins
* @param {Array<String>} accountIDs
* @param {Object} personalDetails
* @param {Object} report
* @param {Object} reportActions
Expand All @@ -371,7 +371,7 @@ function getAllReportErrors(report, reportActions) {
* @param {Boolean} [options.forcePolicyNamePreview]
* @returns {Object}
*/
function createOption(logins, personalDetails, report, reportActions = {}, {showChatPreviewLine = false, forcePolicyNamePreview = false}) {
function createOption(accountIDs, personalDetails, report, reportActions = {}, {showChatPreviewLine = false, forcePolicyNamePreview = false}) {
const result = {
text: null,
alternateText: null,
Expand Down Expand Up @@ -402,7 +402,8 @@ function createOption(logins, personalDetails, report, reportActions = {}, {show
isPolicyExpenseChat: false,
};

const personalDetailMap = getPersonalDetailsForLogins(logins, personalDetails);

const personalDetailMap = getPersonalDetailsForAccountIDs(accountIDs, personalDetails);
const personalDetailList = _.values(personalDetailMap);
const personalDetail = personalDetailList[0] || {};
let hasMultipleParticipants = personalDetailList.length > 1;
Expand Down Expand Up @@ -443,7 +444,7 @@ function createOption(logins, personalDetails, report, reportActions = {}, {show
lastMessageTextFromReport = report ? report.lastMessageText || '' : '';
}

const lastActorDetails = personalDetailMap[report.lastActorEmail] || null;
const lastActorDetails = personalDetailMap[report.lastActorAccountID] || null;
let lastMessageText = hasMultipleParticipants && lastActorDetails && lastActorDetails.login !== currentUserLogin ? `${lastActorDetails.displayName}: ` : '';
lastMessageText += report ? lastMessageTextFromReport : '';

Expand All @@ -466,10 +467,10 @@ function createOption(logins, personalDetails, report, reportActions = {}, {show
}
reportName = ReportUtils.getReportName(report);
} else {
const login = logins[0];
reportName = ReportUtils.getDisplayNameForParticipant(login);
result.keyForList = login;
result.alternateText = LocalePhoneNumber.formatPhoneNumber(login);
const accountID = accountIDs[0];
reportName = ReportUtils.getDisplayNameForParticipant(accountID);
result.keyForList = accountID;
result.alternateText = '';
}

result.isIOUReportOwner = ReportUtils.isIOUOwnedByCurrentUser(result, iouReports);
Expand All @@ -483,7 +484,7 @@ function createOption(logins, personalDetails, report, reportActions = {}, {show

result.text = reportName;
result.searchText = getSearchText(report, reportName, personalDetailList, result.isChatRoom || result.isPolicyExpenseChat, result.isThread);
result.icons = ReportUtils.getIcons(report, personalDetails, UserUtils.getAvatar(personalDetail.avatar, personalDetail.login));
result.icons = ReportUtils.getIcons(report, personalDetails, UserUtils.getAvatar(personalDetail.avatar, personalDetail.accountID));
result.subtitle = subtitle;

return result;
Expand Down Expand Up @@ -572,7 +573,7 @@ function getOptions(

let recentReportOptions = [];
let personalDetailsOptions = [];
const reportMapForLogins = {};
const reportMapForAccountIDs = {};
const parsedPhoneNumber = parsePhoneNumber(LoginUtils.appendCountryCode(searchInputValue));
const searchValue = parsedPhoneNumber.possible ? parsedPhoneNumber.number.e164 : searchInputValue;

Expand Down Expand Up @@ -603,7 +604,7 @@ function getOptions(
const isChatRoom = ReportUtils.isChatRoom(report);
const isTaskReport = ReportUtils.isTaskReport(report);
const isPolicyExpenseChat = ReportUtils.isPolicyExpenseChat(report);
const logins = report.participants || [];
const accountIDs = report.participantAccountIDs || [];

if (isPolicyExpenseChat && report.isOwnPolicyExpenseChat && !includeOwnedWorkspaceChats) {
return;
Expand All @@ -620,8 +621,8 @@ function getOptions(
// Save the report in the map if this is a single participant so we can associate the reportID with the
// personal detail option later. Individuals should not be associated with single participant
// policyExpenseChats or chatRooms since those are not people.
if (logins.length <= 1 && !isPolicyExpenseChat && !isChatRoom) {
reportMapForLogins[logins[0]] = report;
if (accountIDs.length <= 1 && !isPolicyExpenseChat && !isChatRoom) {
reportMapForAccountIDs[accountIDs[0]] = report;
}
const isSearchingSomeonesPolicyExpenseChat = !report.isOwnPolicyExpenseChat && searchValue !== '';

Expand All @@ -630,15 +631,15 @@ function getOptions(
const isPolicyChatAdmin = ReportUtils.isPolicyExpenseChatAdmin(report, policies);

allReportOptions.push(
createOption(logins, personalDetails, report, reportActions, {
createOption(accountIDs, personalDetails, report, reportActions, {
showChatPreviewLine,
forcePolicyNamePreview: isPolicyExpenseChat ? isSearchingSomeonesPolicyExpenseChat || isPolicyChatAdmin : forcePolicyNamePreview,
}),
);
});

let allPersonalDetailsOptions = _.map(personalDetails, (personalDetail) =>
createOption([personalDetail.login], personalDetails, reportMapForLogins[personalDetail.login], reportActions, {
createOption([personalDetail.accountID], personalDetails, reportMapForAccountIDs[personalDetail.accountID], reportActions, {
showChatPreviewLine,
forcePolicyNamePreview,
}),
Expand Down Expand Up @@ -727,15 +728,25 @@ function getOptions(
!_.find(loginOptionsToExclude, (loginOptionToExclude) => loginOptionToExclude.login === addSMSDomainIfPhoneNumber(searchValue).toLowerCase()) &&
(searchValue !== CONST.EMAIL.CHRONOS || Permissions.canUseChronos(betas))
) {
userToInvite = createOption([searchValue], personalDetails, null, reportActions, {
const searchPersonalDetails = _.find(personalDetails, (personalDetail) => personalDetail.login === searchValue);
if (!searchPersonalDetails) {
return {
recentReports: [],
personalDetails: [],
userToInvite: null,
currentUserOption: null,
};
}

userToInvite = createOption([searchPersonalDetails.accountID], personalDetails, null, reportActions, {
showChatPreviewLine,
});

// If user doesn't exist, use a default avatar
userToInvite.icons = [
{
source: UserUtils.getAvatar('', searchValue),
name: searchValue,
source: UserUtils.getAvatar('', searchPersonalDetails.accountID),
name: searchPersonalDetails.login,
type: CONST.ICON_TYPE_AVATAR,
},
];
Expand Down Expand Up @@ -960,7 +971,7 @@ export {
getShareDestinationOptions,
getMemberInviteOptions,
getHeaderMessage,
getPersonalDetailsForLogins,
getPersonalDetailsForAccountIDs,
getIOUConfirmationOptionsFromPayeePersonalDetail,
getIOUConfirmationOptionsFromParticipants,
getSearchText,
Expand Down
20 changes: 9 additions & 11 deletions src/libs/ReportUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -829,34 +829,32 @@ function getIcons(report, personalDetails, defaultIcon = null, isPayer = false)
/**
* Gets the personal details for a login by looking in the ONYXKEYS.PERSONAL_DETAILS_LIST Onyx key (stored in the local variable, allPersonalDetails). If it doesn't exist in Onyx,
* then a default object is constructed.
* @param {String} login
* @param {String} accountID
* @returns {Object}
*/
function getPersonalDetailsForLogin(login) {
if (!login) {
function getPersonalDetailsForAccountID(accountID) {
if (!accountID) {
return {};
}
return (
(allPersonalDetails && allPersonalDetails[login]) || {
login,
displayName: LocalePhoneNumber.formatPhoneNumber(login),
avatar: UserUtils.getDefaultAvatar(login),
(allPersonalDetails && allPersonalDetails[accountID]) || {
avatar: UserUtils.getDefaultAvatar(accountID),
}
);
}

/**
* Get the displayName for a single report participant.
*
* @param {String} login
* @param {String} accountID
* @param {Boolean} [shouldUseShortForm]
* @returns {String}
*/
function getDisplayNameForParticipant(login, shouldUseShortForm = false) {
if (!login) {
function getDisplayNameForParticipant(accountID, shouldUseShortForm = false) {
if (!accountID) {
return '';
}
const personalDetails = getPersonalDetailsForLogin(login);
const personalDetails = getPersonalDetailsForAccountID(accountID);

const longName = personalDetails.displayName;

Expand Down
10 changes: 10 additions & 0 deletions tests/unit/OptionsListUtilsTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ describe('OptionsListUtils', () => {
isPinned: false,
reportID: 1,
participants: ['tonystark@expensify.com', 'reedrichards@expensify.com'],
participantAccountIDs: [2, 1],
reportName: 'Iron Man, Mister Fantastic',
hasDraft: true,
},
Expand All @@ -23,6 +24,7 @@ describe('OptionsListUtils', () => {
isPinned: false,
reportID: 2,
participants: ['peterparker@expensify.com'],
participantAccountIDs: [3],
reportName: 'Spider-Man',
},

Expand All @@ -33,6 +35,7 @@ describe('OptionsListUtils', () => {
isPinned: true,
reportID: 3,
participants: ['reedrichards@expensify.com'],
participantAccountIDs: [1],
reportName: 'Mister Fantastic',
},
4: {
Expand All @@ -41,6 +44,7 @@ describe('OptionsListUtils', () => {
isPinned: false,
reportID: 4,
participants: ['tchalla@expensify.com'],
participantAccountIDs: [4],
reportName: 'Black Panther',
},
5: {
Expand All @@ -49,6 +53,7 @@ describe('OptionsListUtils', () => {
isPinned: false,
reportID: 5,
participants: ['suestorm@expensify.com'],
participantAccountIDs: [5],
reportName: 'Invisible Woman',
},
6: {
Expand All @@ -57,6 +62,7 @@ describe('OptionsListUtils', () => {
isPinned: false,
reportID: 6,
participants: ['thor@expensify.com'],
participantAccountIDs: [6],
reportName: 'Thor',
},

Expand All @@ -67,6 +73,7 @@ describe('OptionsListUtils', () => {
isPinned: false,
reportID: 7,
participants: ['steverogers@expensify.com'],
participantAccountIDs: [7],
reportName: 'Captain America',
},

Expand All @@ -77,6 +84,7 @@ describe('OptionsListUtils', () => {
isPinned: false,
reportID: 8,
participants: ['galactus_herald@expensify.com'],
participantAccountIDs: [12],
reportName: 'Silver Surfer',
},

Expand All @@ -87,6 +95,7 @@ describe('OptionsListUtils', () => {
isPinned: false,
reportID: 9,
participants: ['mistersinister@marauders.com'],
participantAccountIDs: [8],
reportName: 'Mister Sinister',
iouReportID: 100,
hasOutstandingIOU: true,
Expand All @@ -99,6 +108,7 @@ describe('OptionsListUtils', () => {
reportID: 10,
isPinned: false,
participants: ['tonystark@expensify.com', 'steverogers@expensify.com'],
participantAccountIDs: [2, 7],
reportName: '',
oldPolicyName: "SHIELD's workspace",
chatType: CONST.REPORT.CHAT_TYPE.POLICY_EXPENSE_CHAT,
Expand Down

0 comments on commit ef3d955

Please sign in to comment.