Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions src/libs/PersonalDetailsUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,14 +287,16 @@ function getLoginByAccountID(accountID: number | undefined, personalDetails: Ony
* @param accountIDs Array of user accountIDs
* @returns Array of logins according to passed accountIDs
*/
function getLoginsByAccountIDs(accountIDs: number[]): string[] {
return accountIDs.reduce((foundLogins: string[], accountID) => {
const currentLogin = getLoginByAccountID(accountID);
if (currentLogin) {
foundLogins.push(currentLogin);
}
return foundLogins;
}, []);
function getLoginsByAccountIDs(accountIDs: number[] | undefined, personalDetailsList: OnyxEntry<PersonalDetailsList> = allPersonalDetails): string[] {
return (
accountIDs?.reduce((foundLogins: string[], accountID) => {
const currentLogin = getLoginByAccountID(accountID, personalDetailsList);
if (currentLogin) {
foundLogins.push(currentLogin);
}
return foundLogins;
}, []) ?? []
);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/pages/DynamicReportParticipantsInvitePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ function DynamicReportParticipantsInvitePage({report}: DynamicReportParticipants
...CONST.EXPENSIFY_EMAILS_OBJECT,
};
const participantsAccountIDs = getParticipantsAccountIDsForDisplay(report, false, true);
const loginsByAccountIDs = getLoginsByAccountIDs(participantsAccountIDs);
const loginsByAccountIDs = getLoginsByAccountIDs(participantsAccountIDs, personalDetailsList);
for (const login of loginsByAccountIDs) {
excludedUsers[login] = true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/pages/DynamicRoomInvitePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ function DynamicRoomInvitePage({report, policy, didScreenTransitionEnd}: Dynamic
...CONST.EXPENSIFY_EMAILS_OBJECT,
};
const participantsAccountIDs = getParticipantsAccountIDsForDisplay(report, false, true, undefined, reportMetadata);
const loginsByAccountIDs = getLoginsByAccountIDs(participantsAccountIDs);
const loginsByAccountIDs = getLoginsByAccountIDs(participantsAccountIDs, personalDetailsList);
for (const login of loginsByAccountIDs) {
const smsDomain = addSMSDomainIfPhoneNumber(login);
excludedUsers[smsDomain] = true;
Expand Down
5 changes: 3 additions & 2 deletions src/pages/domain/Admins/DomainAddAdminPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import Navigation from '@libs/Navigation/Navigation';
import type {PlatformStackScreenProps} from '@libs/Navigation/PlatformStackNavigation/types';
import {getHeaderMessage} from '@libs/PersonalDetailOptionsListUtils';
import type {OptionData} from '@libs/PersonalDetailOptionsListUtils';
import {getLoginsByAccountIDs} from '@libs/PersonalDetailsUtils';
import {addSMSDomainIfPhoneNumber, parsePhoneNumber} from '@libs/PhoneNumber';
import type {SettingsNavigatorParamList} from '@navigation/types';
import DomainNotFoundPageWrapper from '@pages/domain/DomainNotFoundPageWrapper';
Expand All @@ -27,6 +26,8 @@ import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import type SCREENS from '@src/SCREENS';
import {personalDetailsLoginsSelector} from '@src/selectors/PersonalDetails';
import getEmptyArray from '@src/types/utils/getEmptyArray';

type Sections = Section<OptionData>;

Expand All @@ -46,6 +47,7 @@ function DomainAddAdminPage({route}: DomainAddAdminProps) {
const [adminIDs] = useOnyx(`${ONYXKEYS.COLLECTION.DOMAIN}${domainAccountID}`, {
selector: adminAccountIDsSelector,
});
const [adminLoginsByAccountIDs = getEmptyArray<string>()] = useOnyx(ONYXKEYS.PERSONAL_DETAILS_LIST, {selector: personalDetailsLoginsSelector(adminIDs)});
Comment thread
bernhardoj marked this conversation as resolved.

const [didScreenTransitionEnd, setDidScreenTransitionEnd] = useState(false);
const didInvite = useRef<boolean>(false);
Expand All @@ -56,7 +58,6 @@ function DomainAddAdminPage({route}: DomainAddAdminProps) {
const excludedUsers: Record<string, boolean> = {
...CONST.EXPENSIFY_EMAILS_OBJECT,
};
const adminLoginsByAccountIDs = getLoginsByAccountIDs(adminIDs ?? []);
for (const login of adminLoginsByAccountIDs) {
const smsDomain = addSMSDomainIfPhoneNumber(login);
excludedUsers[smsDomain] = true;
Expand Down
12 changes: 11 additions & 1 deletion src/selectors/PersonalDetails.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import type {OnyxEntry} from 'react-native-onyx';
import {getDisplayNameOrDefault, getLoginByAccountID, getPersonalDetailsByID, getPersonalDetailsListByIDs, newGetPersonalDetailsByIDs} from '@libs/PersonalDetailsUtils';
import {
getDisplayNameOrDefault,
getLoginByAccountID,
getLoginsByAccountIDs,
getPersonalDetailsByID,
getPersonalDetailsListByIDs,
newGetPersonalDetailsByIDs,
} from '@libs/PersonalDetailsUtils';
import CONST from '@src/CONST';
import type {PersonalDetails, PersonalDetailsList, Report} from '@src/types/onyx';
import {isEmptyObject} from '@src/types/utils/EmptyObject';
Expand All @@ -13,6 +20,8 @@ const personalDetailsListSelector = (accountIDs: Array<number | undefined> | und

const personalDetailsLoginSelector = (accountID: number | undefined) => (personalDetailsList: OnyxEntry<PersonalDetailsList>) => getLoginByAccountID(accountID, personalDetailsList);

const personalDetailsLoginsSelector = (accountIDs: number[] | undefined) => (personalDetailsList: OnyxEntry<PersonalDetailsList>) => getLoginsByAccountIDs(accountIDs, personalDetailsList);

const personalDetailsDisplayNameSelector = (accountID: number) => (personalDetails: OnyxEntry<PersonalDetailsList>) => getDisplayNameOrDefault(personalDetails?.[accountID]);

const conciergePersonalDetailSelector = personalDetailsSelector(CONST.ACCOUNT_ID.CONCIERGE);
Expand Down Expand Up @@ -72,6 +81,7 @@ export {
personalDetailsListSelector,
personalDetailsDisplayNameSelector,
personalDetailsLoginSelector,
personalDetailsLoginsSelector,
conciergePersonalDetailSelector,
doesPersonalDetailExistSelector,
accountIDToLoginSelector,
Expand Down
24 changes: 12 additions & 12 deletions tests/actions/IOUTest/DeleteMoneyRequestTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -522,9 +522,9 @@ describe('actions/IOU/DeleteMoneyRequest', () => {
jest.advanceTimersByTime(10);

// Given User logins from the participant accounts
const participantAccountIDs = Object.keys(thread.participants ?? {}).map(Number);
const userLogins = getLoginsByAccountIDs(participantAccountIDs);
const allPersonalDetails = await getOnyxValue(ONYXKEYS.PERSONAL_DETAILS_LIST);
const participantAccountIDs = Object.keys(thread.participants ?? {}).map(Number);
const userLogins = getLoginsByAccountIDs(participantAccountIDs, allPersonalDetails);
const participants = userLogins.map((login, index) => ({
login,
accountID: participantAccountIDs.at(index),
Expand Down Expand Up @@ -629,9 +629,9 @@ describe('actions/IOU/DeleteMoneyRequest', () => {
jest.advanceTimersByTime(10);

// Given User logins from the participant accounts
const participantAccountIDs = Object.keys(thread.participants ?? {}).map(Number);
const userLogins = getLoginsByAccountIDs(participantAccountIDs);
const allPersonalDetails = await getOnyxValue(ONYXKEYS.PERSONAL_DETAILS_LIST);
const participantAccountIDs = Object.keys(thread.participants ?? {}).map(Number);
const userLogins = getLoginsByAccountIDs(participantAccountIDs, allPersonalDetails);
const participants = userLogins.map((login, index) => ({
login,
accountID: participantAccountIDs.at(index),
Expand Down Expand Up @@ -762,10 +762,10 @@ describe('actions/IOU/DeleteMoneyRequest', () => {

expect(thread.participants).toEqual(expectedTransactionThreadParticipants);

const allPersonalDetails = await getOnyxValue(ONYXKEYS.PERSONAL_DETAILS_LIST);
const participantAccountIDs = Object.keys(thread.participants ?? {}).map(Number);
const userLogins = getLoginsByAccountIDs(participantAccountIDs);
const userLogins = getLoginsByAccountIDs(participantAccountIDs, allPersonalDetails);
jest.advanceTimersByTime(10);
const allPersonalDetails = await getOnyxValue(ONYXKEYS.PERSONAL_DETAILS_LIST);
const participants = userLogins.map((login, index) => ({
login,
accountID: participantAccountIDs.at(index),
Expand Down Expand Up @@ -909,9 +909,9 @@ describe('actions/IOU/DeleteMoneyRequest', () => {
await waitForBatchedUpdates();

jest.advanceTimersByTime(10);
const participantAccountIDs = Object.keys(thread.participants ?? {}).map(Number);
const userLogins = getLoginsByAccountIDs(participantAccountIDs);
const allPersonalDetails = await getOnyxValue(ONYXKEYS.PERSONAL_DETAILS_LIST);
const participantAccountIDs = Object.keys(thread.participants ?? {}).map(Number);
const userLogins = getLoginsByAccountIDs(participantAccountIDs, allPersonalDetails);
const participants = userLogins.map((login, index) => ({
login,
accountID: participantAccountIDs.at(index),
Expand Down Expand Up @@ -1225,9 +1225,9 @@ describe('actions/IOU/DeleteMoneyRequest', () => {
expect(thread.participants).toStrictEqual(expectedTransactionThreadParticipants);

jest.advanceTimersByTime(10);
const participantAccountIDs = Object.keys(thread.participants ?? {}).map(Number);
const userLogins = getLoginsByAccountIDs(participantAccountIDs);
const allPersonalDetails = await getOnyxValue(ONYXKEYS.PERSONAL_DETAILS_LIST);
const participantAccountIDs = Object.keys(thread.participants ?? {}).map(Number);
const userLogins = getLoginsByAccountIDs(participantAccountIDs, allPersonalDetails);
const participants = userLogins.map((login, index) => ({
login,
accountID: participantAccountIDs.at(index),
Expand Down Expand Up @@ -1405,10 +1405,10 @@ describe('actions/IOU/DeleteMoneyRequest', () => {

expect(thread.participants).toEqual(expectedTransactionThreadParticipants);

const allPersonalDetails = await getOnyxValue(ONYXKEYS.PERSONAL_DETAILS_LIST);
const participantAccountIDs = Object.keys(thread.participants ?? {}).map(Number);
const userLogins = getLoginsByAccountIDs(participantAccountIDs);
const userLogins = getLoginsByAccountIDs(participantAccountIDs, allPersonalDetails);
jest.advanceTimersByTime(10);
const allPersonalDetails = await getOnyxValue(ONYXKEYS.PERSONAL_DETAILS_LIST);
const participants = userLogins.map((login, index) => ({
login,
accountID: participantAccountIDs.at(index),
Expand Down
4 changes: 2 additions & 2 deletions tests/actions/IOUTest/DuplicateTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -448,10 +448,10 @@ describe('actions/Duplicate', () => {
[RORY_ACCOUNT_ID]: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN, role: CONST.REPORT.ROLE.ADMIN},
});

const allPersonalDetails = await getOnyxValue(ONYXKEYS.PERSONAL_DETAILS_LIST);
const participantAccountIDs = Object.keys(transactionThreadReport1.participants ?? {}).map(Number);
const userLogins = getLoginsByAccountIDs(participantAccountIDs);
const userLogins = getLoginsByAccountIDs(participantAccountIDs, allPersonalDetails);
jest.advanceTimersByTime(10);
const allPersonalDetails = await getOnyxValue(ONYXKEYS.PERSONAL_DETAILS_LIST);
const participants = userLogins.map((login, index) => ({
login,
accountID: participantAccountIDs.at(index),
Expand Down
8 changes: 4 additions & 4 deletions tests/actions/IOUTest/TrackExpenseTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2326,10 +2326,10 @@ describe('actions/IOU/TrackExpense', () => {
[CARLOS_ACCOUNT_ID]: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN, role: CONST.REPORT.ROLE.MEMBER},
});

const allPersonalDetails = await getOnyxValue(ONYXKEYS.PERSONAL_DETAILS_LIST);
const participantAccountIDs = Object.keys(thread.participants ?? {}).map(Number);
const userLogins = getLoginsByAccountIDs(participantAccountIDs);
const userLogins = getLoginsByAccountIDs(participantAccountIDs, allPersonalDetails);
jest.advanceTimersByTime(10);
const allPersonalDetails = await getOnyxValue(ONYXKEYS.PERSONAL_DETAILS_LIST);
const participants = userLogins.map((login, index) => ({
login,
accountID: participantAccountIDs.at(index),
Expand Down Expand Up @@ -2437,10 +2437,10 @@ describe('actions/IOU/TrackExpense', () => {
[CARLOS_ACCOUNT_ID]: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN, role: CONST.REPORT.ROLE.MEMBER},
});

const allPersonalDetails = await getOnyxValue(ONYXKEYS.PERSONAL_DETAILS_LIST);
const participantAccountIDs = Object.keys(thread.participants ?? {}).map(Number);
const userLogins = getLoginsByAccountIDs(participantAccountIDs);
const userLogins = getLoginsByAccountIDs(participantAccountIDs, allPersonalDetails);
jest.advanceTimersByTime(10);
const allPersonalDetails = await getOnyxValue(ONYXKEYS.PERSONAL_DETAILS_LIST);
const participants = userLogins.map((login, index) => ({
login,
accountID: participantAccountIDs.at(index),
Expand Down
8 changes: 4 additions & 4 deletions tests/actions/MergeTransactionTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1131,10 +1131,10 @@ describe('mergeTransactionRequest', () => {
[TEST_ACCOUNT_ID]: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN, role: CONST.REPORT.ROLE.ADMIN},
});

const allPersonalDetails = await getOnyxValue(ONYXKEYS.PERSONAL_DETAILS_LIST);
const participantAccountIDs = Object.keys(thread.participants ?? {}).map(Number);
const userLogins = getLoginsByAccountIDs(participantAccountIDs);
const userLogins = getLoginsByAccountIDs(participantAccountIDs, allPersonalDetails);
jest.advanceTimersByTime(10);
const allPersonalDetails = await getOnyxValue(ONYXKEYS.PERSONAL_DETAILS_LIST);
const participants = userLogins.map((login, index) => ({
login,
accountID: participantAccountIDs.at(index),
Expand Down Expand Up @@ -1315,10 +1315,10 @@ describe('mergeTransactionRequest', () => {
[TEST_ACCOUNT_ID]: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN, role: CONST.REPORT.ROLE.ADMIN},
});

const allPersonalDetails = await getOnyxValue(ONYXKEYS.PERSONAL_DETAILS_LIST);
const participantAccountIDs = Object.keys(thread.participants ?? {}).map(Number);
const userLogins = getLoginsByAccountIDs(participantAccountIDs);
const userLogins = getLoginsByAccountIDs(participantAccountIDs, allPersonalDetails);
jest.advanceTimersByTime(10);
const allPersonalDetails = await getOnyxValue(ONYXKEYS.PERSONAL_DETAILS_LIST);
const participants = userLogins.map((login, index) => ({
login,
accountID: participantAccountIDs.at(index),
Expand Down
34 changes: 34 additions & 0 deletions tests/unit/PersonalDetailsSelectorTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
personalDetailsDisplayNameSelector,
personalDetailsListSelector,
personalDetailsLoginSelector,
personalDetailsLoginsSelector,
personalDetailsSelector,
} from '@selectors/PersonalDetails';
import {getDisplayNameOrDefault} from '@libs/PersonalDetailsUtils';
Expand Down Expand Up @@ -98,6 +99,39 @@ describe('PersonalDetailsSelector', () => {
});
});

describe('personalDetailsLoginsSelector', () => {
const secondAccountID = 456;
const secondPersonalDetails = {
accountID: secondAccountID,
displayName: 'Second User',
login: 'second@user.com',
};
const multiPersonalDetailsList = {
[accountID]: personalDetails,
[secondAccountID]: secondPersonalDetails,
} as unknown as PersonalDetailsList;

it('should return the logins for the given accountIDs', () => {
const result = personalDetailsLoginsSelector([accountID, secondAccountID])(multiPersonalDetailsList);
expect(result).toEqual([personalDetails.login, secondPersonalDetails.login]);
});

it('should filter out accountIDs that do not exist in the list', () => {
const result = personalDetailsLoginsSelector([accountID, 999])(multiPersonalDetailsList);
expect(result).toEqual([personalDetails.login]);
});

it('should return an empty array if accountIDs is empty', () => {
const result = personalDetailsLoginsSelector([])(multiPersonalDetailsList);
expect(result).toEqual([]);
});

it('should return an empty array if none of the accountIDs exist in the list', () => {
const result = personalDetailsLoginsSelector([888, 999])(multiPersonalDetailsList);
expect(result).toEqual([]);
});
});

describe('multiPersonalDetailsSelector', () => {
it('should return the personal details for the given accountIDs', () => {
const result = multiPersonalDetailsSelector([accountID])(personalDetailsList);
Expand Down
Loading