Skip to content
Merged
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
5 changes: 4 additions & 1 deletion src/components/KYCWall/BaseKYCWall.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {setKYCWallSource} from '@userActions/Wallet';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import {personalDetailsLoginSelector} from '@src/selectors/PersonalDetails';
import {doesPersonalDetailExistSelector, personalDetailsLoginSelector} from '@src/selectors/PersonalDetails';
import {lastWorkspaceNumberSelector} from '@src/selectors/Policy';
import type {BankAccountList, Policy} from '@src/types/onyx';
import {getEmptyObject} from '@src/types/utils/EmptyObject';
Expand Down Expand Up @@ -67,6 +67,7 @@ function KYCWall({
const [isSelfTourViewed] = useOnyx(ONYXKEYS.NVP_ONBOARDING, {selector: hasSeenTourSelector});
const [betas] = useOnyx(ONYXKEYS.BETAS);
const [employeeLogin] = useOnyx(ONYXKEYS.PERSONAL_DETAILS_LIST, {selector: personalDetailsLoginSelector(iouReport?.ownerAccountID)}, [iouReport?.ownerAccountID]);
const [doesSubmitterPersonalDetailExist] = useOnyx(ONYXKEYS.PERSONAL_DETAILS_LIST, {selector: doesPersonalDetailExistSelector(iouReport?.ownerAccountID)}, [iouReport?.ownerAccountID]);

const {formatPhoneNumber, translate} = useLocalize();
const currentUserDetails = useCurrentUserPersonalDetails();
Expand Down Expand Up @@ -150,6 +151,7 @@ function KYCWall({
filteredReportActions,
currentUserAccountID,
employeeLogin,
doesSubmitterPersonalDetailExist ?? false,
reportTransactions,
);
if (inviteResult?.policyExpenseChatReportID) {
Expand Down Expand Up @@ -236,6 +238,7 @@ function KYCWall({
currentUserAccountID,
currentUserEmail,
employeeLogin,
doesSubmitterPersonalDetailExist,
introSelected,
formatPhoneNumber,
translate,
Expand Down
2 changes: 2 additions & 0 deletions src/hooks/useSearchBulkActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import {columnsSelector} from '@src/selectors/AdvancedSearchFiltersForm';
import {doesPersonalDetailExistSelector} from '@src/selectors/PersonalDetails';
import type {BillingGraceEndPeriod, Policy, Report, ReportAction, ReportNameValuePairs, SearchResults, Transaction, TransactionViolations} from '@src/types/onyx';
import type {SearchResultDataType} from '@src/types/onyx/SearchResults';
import type DeepValueOf from '@src/types/utils/DeepValueOf';
Expand Down Expand Up @@ -1010,6 +1011,7 @@ function useSearchBulkActions({queryJSON}: UseSearchBulkActionsParams) {
policyExpenseChatReportActions,
accountID,
getLoginByAccountID(itemReport?.ownerAccountID, personalDetails),
doesPersonalDetailExistSelector(itemReport?.ownerAccountID)(personalDetails),
reportTransactions,
);
if (!invite?.policyExpenseChatReportID) {
Expand Down
3 changes: 2 additions & 1 deletion src/libs/actions/Policy/Policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1579,6 +1579,7 @@ function createPolicyExpenseChats(
reportActionsList?: OnyxCollection<ReportActions>,
hasOutstandingChildRequest = false,
notificationPreference: NotificationPreference = CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN,
doesPersonalDetailExistByAccountID?: Record<number, boolean>,
): WorkspaceMembersChats {
const {accountID: currentUserAccountID, displayName: currentUserDisplayName, email: currentUserEmail, avatar: currentUserAvatar} = currentUser;
const workspaceMembersChats: WorkspaceMembersChats = {
Expand Down Expand Up @@ -1711,7 +1712,7 @@ function createPolicyExpenseChats(
createChat: null,
},
participants: {
[accountID]: deprecatedAllPersonalDetails?.[accountID] ? {} : null,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't seem like we are checking whether personal details exist or not. Instead we assigning the personal details of account ID.

So we should just the simple personalDetailselector and remove doesPersonalDetailExistSelector. If we need to check if value exists, we can use !! on the value in place where needed.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@parasharrajat It was my intention to move the check into the selector. As mentioned in this rule, returning a boolean from a selector is better than returning an object.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see what you mean here. I didn't see properly that we are setting {} if the value exists. It does not make sense to me. Shouldn't we set the real value here?

Anyways, your logic looks correct to me if you just want to check presence.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@parasharrajat In this place, if the personal details are empty, we need to remove the participant, and then the response from the backend will update them. If the personal details are already available, merging an empty object (= does nothing)

[accountID]: (doesPersonalDetailExistByAccountID?.[accountID] ?? !!deprecatedAllPersonalDetails?.[accountID]) ? {} : null,
},
},
},
Expand Down
5 changes: 4 additions & 1 deletion src/libs/actions/Report/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6605,6 +6605,7 @@ function moveIOUReportToPolicyAndInviteSubmitter(
reportActions: OnyxCollection<ReportActions>,
currentUserAccountID: number,
submitterLogin: string | undefined,
doesSubmitterPersonalDetailExist: boolean,
reportTransactions: Transaction[] = [],
): {policyExpenseChatReportID?: string} | undefined {
if (!policy || !iouReport) {
Expand Down Expand Up @@ -6677,7 +6678,9 @@ function moveIOUReportToPolicyAndInviteSubmitter(
const announceRoomMembers = buildRoomMembersOnyxData(CONST.REPORT.CHAT_TYPE.POLICY_ANNOUNCE, policyID, [submitterAccountID]);

// Create policy expense chat for the submitter
const policyExpenseChats = createPolicyExpenseChats(policyID, invitedEmailsToAccountIDs, {accountID: currentUserAccountID}, reportActions);
const policyExpenseChats = createPolicyExpenseChats(policyID, invitedEmailsToAccountIDs, {accountID: currentUserAccountID}, reportActions, undefined, undefined, {
[submitterAccountID]: doesSubmitterPersonalDetailExist,
});
const optimisticPolicyExpenseChatReportID = policyExpenseChats.reportCreationData[submitterLogin].reportID;
const optimisticPolicyExpenseChatCreatedReportActionID = policyExpenseChats.reportCreationData[submitterLogin].reportActionID;

Expand Down
4 changes: 3 additions & 1 deletion src/pages/DynamicReportChangeWorkspacePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES, {DYNAMIC_ROUTES} from '@src/ROUTES';
import type SCREENS from '@src/SCREENS';
import {personalDetailsLoginSelector} from '@src/selectors/PersonalDetails';
import {doesPersonalDetailExistSelector, personalDetailsLoginSelector} from '@src/selectors/PersonalDetails';
import type {DismissedProductTraining} from '@src/types/onyx';
import NotFoundPage from './ErrorPage/NotFoundPage';
import type {WithReportOrNotFoundProps} from './inbox/report/withReportOrNotFound';
Expand Down Expand Up @@ -69,6 +69,7 @@ function DynamicReportChangeWorkspacePage({report}: DynamicReportChangeWorkspace
const [transactionViolations] = useOnyx(ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS);
const isReportLastVisibleArchived = useReportIsArchived(report?.parentReportID);
const [submitterLogin] = useOnyx(ONYXKEYS.PERSONAL_DETAILS_LIST, {selector: personalDetailsLoginSelector(report?.ownerAccountID)}, [report?.ownerAccountID]);
const [doesSubmitterPersonalDetailExist] = useOnyx(ONYXKEYS.PERSONAL_DETAILS_LIST, {selector: doesPersonalDetailExistSelector(report?.ownerAccountID)}, [report?.ownerAccountID]);
const [managerLogin] = useOnyx(ONYXKEYS.PERSONAL_DETAILS_LIST, {selector: personalDetailsLoginSelector(report?.managerID)}, [report?.managerID]);
const shouldShowLoadingIndicator = isLoadingApp && !isOffline;
const {isBetaEnabled} = usePermissions();
Expand Down Expand Up @@ -102,6 +103,7 @@ function DynamicReportChangeWorkspacePage({report}: DynamicReportChangeWorkspace
filteredReportActions,
session?.accountID ?? CONST.DEFAULT_NUMBER_ID,
submitterLogin,
doesSubmitterPersonalDetailExist ?? false,
reportTransactions,
);
if (!invite?.policyExpenseChatReportID) {
Expand Down
6 changes: 6 additions & 0 deletions src/selectors/PersonalDetails.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ const createDisplayDetailsByAccountIDsSelector =
return result;
};

const doesPersonalDetailExistSelector =
(accountID: number | undefined) =>
(personalDetailsList: OnyxEntry<PersonalDetailsList>): boolean =>
accountID !== undefined && !!personalDetailsList?.[accountID];

const accountIDToLoginSelector = (reportsToArchive: Report[]) => (personalDetailsList: OnyxEntry<PersonalDetailsList>) => {
const map: Record<number, string> = {};
for (const report of reportsToArchive) {
Expand Down Expand Up @@ -68,6 +73,7 @@ export {
personalDetailsDisplayNameSelector,
personalDetailsLoginSelector,
conciergePersonalDetailSelector,
doesPersonalDetailExistSelector,
accountIDToLoginSelector,
isOptimisticPersonalDetailSelector,
createDisplayDetailsByAccountIDsSelector,
Expand Down
30 changes: 30 additions & 0 deletions tests/actions/PolicyTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5931,6 +5931,36 @@ describe('actions/Policy', () => {
expect(reportValue?.participants?.[customAccountID]).toBeTruthy();
expect(reportValue?.participants?.[ESH_ACCOUNT_ID]).toBeUndefined();
});

it('uses the explicit doesPersonalDetailExistByAccountID map for success-data participants instead of the deprecated personal details list', async () => {
const policyID = Policy.generatePolicyID();
const newMemberEmail = 'mapmember@test.com';
const newMemberAccountID = 314;

// Given a signed-in user, and a personal details list that does NOT contain the new member
// (so the deprecated module-level fallback would treat the member as unknown)
await Onyx.set(ONYXKEYS.SESSION, {email: ESH_EMAIL, accountID: ESH_ACCOUNT_ID});
await Onyx.set(ONYXKEYS.PERSONAL_DETAILS_LIST, {[ESH_ACCOUNT_ID]: {accountID: ESH_ACCOUNT_ID, login: ESH_EMAIL}});
await waitForBatchedUpdates();

const getSuccessValue = (result: ReturnType<typeof Policy.createPolicyExpenseChats>) => {
const reportID = result.reportCreationData[newMemberEmail]?.reportID;
return result.onyxSuccessData.find((data) => data.key === `${ONYXKEYS.COLLECTION.REPORT}${reportID}`)?.value;
};
const participantPath = ['participants', String(newMemberAccountID)];

// When the explicit map says the member exists, the success-data participant is kept ({}), beating the empty fallback
const existsResult = Policy.createPolicyExpenseChats(policyID, {[newMemberEmail]: newMemberAccountID}, {accountID: ESH_ACCOUNT_ID}, undefined, undefined, undefined, {
[newMemberAccountID]: true,
});
expect(getSuccessValue(existsResult)).toHaveProperty(participantPath, {});

// When the explicit map says the member does NOT exist, the success-data participant is removed (null)
const missingResult = Policy.createPolicyExpenseChats(policyID, {[newMemberEmail]: newMemberAccountID}, {accountID: ESH_ACCOUNT_ID}, undefined, undefined, undefined, {
[newMemberAccountID]: false,
});
expect(getSuccessValue(missingResult)).toHaveProperty(participantPath, null);
});
});
describe('setPolicyCustomTaxName', () => {
it('should set custom tax name optimistically and succeed', async () => {
Expand Down
12 changes: 6 additions & 6 deletions tests/actions/ReportTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4031,7 +4031,7 @@ describe('actions/Report', () => {
await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${policy.id}`, policy);

// When moving iou to a workspace and invite the submitter
Report.moveIOUReportToPolicyAndInviteSubmitter(iouReport, policy, (phone: string) => phone, {}, TEST_USER_ACCOUNT_ID, ownerEmail);
Report.moveIOUReportToPolicyAndInviteSubmitter(iouReport, policy, (phone: string) => phone, {}, TEST_USER_ACCOUNT_ID, ownerEmail, true);
await waitForBatchedUpdates();

// Then MOVED report action should be added to the expense report
Expand Down Expand Up @@ -4098,7 +4098,7 @@ describe('actions/Report', () => {

// Call moveIOUReportToPolicyAndInviteSubmitter
const formatPhoneNumber = (phoneNumber: string) => phoneNumber;
Report.moveIOUReportToPolicyAndInviteSubmitter(iouReport, policy, formatPhoneNumber, {}, TEST_USER_ACCOUNT_ID, ownerEmail);
Report.moveIOUReportToPolicyAndInviteSubmitter(iouReport, policy, formatPhoneNumber, {}, TEST_USER_ACCOUNT_ID, ownerEmail, true);
await waitForBatchedUpdates();

// Simulate network failure
Expand Down Expand Up @@ -4156,7 +4156,7 @@ describe('actions/Report', () => {
await Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`, transaction);

// When moving IOU to a workspace with reportTransactions
Report.moveIOUReportToPolicyAndInviteSubmitter(iouReport, policy, (phone: string) => phone, {}, TEST_USER_ACCOUNT_ID, ownerEmail, [transaction]);
Report.moveIOUReportToPolicyAndInviteSubmitter(iouReport, policy, (phone: string) => phone, {}, TEST_USER_ACCOUNT_ID, ownerEmail, true, [transaction]);
await waitForBatchedUpdates();

// Then the transaction amounts should be negated optimistically
Expand Down Expand Up @@ -4202,7 +4202,7 @@ describe('actions/Report', () => {
await Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`, transaction);

// When moving IOU to a workspace with transactions
Report.moveIOUReportToPolicyAndInviteSubmitter(iouReport, policy, (phone: string) => phone, {}, TEST_USER_ACCOUNT_ID, ownerEmail, [transaction]);
Report.moveIOUReportToPolicyAndInviteSubmitter(iouReport, policy, (phone: string) => phone, {}, TEST_USER_ACCOUNT_ID, ownerEmail, true, [transaction]);
await waitForBatchedUpdates();

// Then the report should be converted to an expense report with the new policyID
Expand All @@ -4225,13 +4225,13 @@ describe('actions/Report', () => {
...createRandomReport(1, undefined),
type: CONST.REPORT.TYPE.IOU,
};
const result = Report.moveIOUReportToPolicyAndInviteSubmitter(iouReport, undefined as unknown as OnyxTypes.Policy, (phone: string) => phone, {}, TEST_USER_ACCOUNT_ID, '');
const result = Report.moveIOUReportToPolicyAndInviteSubmitter(iouReport, undefined as unknown as OnyxTypes.Policy, (phone: string) => phone, {}, TEST_USER_ACCOUNT_ID, '', false);
expect(result).toBeUndefined();
});

it('should return undefined when iouReport is missing', () => {
const policy: OnyxTypes.Policy = {...createRandomPolicy(1), role: CONST.POLICY.ROLE.ADMIN};
const result = Report.moveIOUReportToPolicyAndInviteSubmitter(undefined, policy, (phone: string) => phone, {}, TEST_USER_ACCOUNT_ID, '');
const result = Report.moveIOUReportToPolicyAndInviteSubmitter(undefined, policy, (phone: string) => phone, {}, TEST_USER_ACCOUNT_ID, '', false);
expect(result).toBeUndefined();
});
});
Expand Down
Loading