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
4 changes: 2 additions & 2 deletions src/libs/actions/Policy/Member.ts
Original file line number Diff line number Diff line change
Expand Up @@ -930,7 +930,7 @@ function addMembersToWorkspace(
policyMemberAccountIDs: number[],
role: string,
formatPhoneNumber: LocaleContextProps['formatPhoneNumber'],
currentUserAccountID: number,
currentUser: CurrentUser,
approverEmail?: string,
// TODO: Remove optional (?) once all callers are updated in follow-up PRs of https://github.com/Expensify/App/issues/66578
reportActionsList?: OnyxCollection<ReportActions>,
Expand All @@ -945,7 +945,7 @@ function addMembersToWorkspace(
policyMemberAccountIDs,
role,
formatPhoneNumber,
{accountID: currentUserAccountID},
currentUser,
approverEmail,
undefined,
reportActionsList,
Expand Down
12 changes: 6 additions & 6 deletions src/libs/actions/Report/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ import navigateFromNotification from '@userActions/navigateFromNotification';
import {getAll} from '@userActions/PersistedRequests';
import {buildAddMembersToWorkspaceOnyxData, buildRoomMembersOnyxData} from '@userActions/Policy/Member';
import {createPolicyExpenseChats} from '@userActions/Policy/Policy';
import type {CurrentUser} from '@userActions/Policy/Policy';
import {
createUpdateCommentMatcher,
resolveCommentDeletionConflicts,
Expand Down Expand Up @@ -7535,8 +7536,7 @@ function changeReportPolicyAndInviteSubmitter({
report,
parentReport,
policy,
currentUserAccountID,
email,
currentUser,
hasViolationsParam,
isChangePolicyTrainingModalDismissed,
isASAPSubmitBetaEnabled,
Expand All @@ -7549,8 +7549,7 @@ function changeReportPolicyAndInviteSubmitter({
report: Report;
parentReport: OnyxEntry<Report>;
policy: Policy;
currentUserAccountID: number;
email: string;
currentUser: CurrentUser;
hasViolationsParam: boolean;
isChangePolicyTrainingModalDismissed: boolean;
isASAPSubmitBetaEnabled: boolean;
Expand All @@ -7569,6 +7568,7 @@ function changeReportPolicyAndInviteSubmitter({
if (!submitterEmail) {
return;
}
const {accountID: currentUserAccountID, email: currentUserEmail = ''} = currentUser;
const policyMemberAccountIDs = Object.values(getMemberAccountIDsForWorkspace(employeeList, false, false));
const {
optimisticData: optimisticAddMembersData,
Expand All @@ -7581,7 +7581,7 @@ function changeReportPolicyAndInviteSubmitter({
policyMemberAccountIDs,
CONST.POLICY.ROLE.USER,
formatPhoneNumber,
{accountID: currentUserAccountID},
currentUser,
undefined,
CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS,
reportActionsList,
Expand All @@ -7604,7 +7604,7 @@ function changeReportPolicyAndInviteSubmitter({
parentReport,
policy,
currentUserAccountID,
email,
currentUserEmail,
hasViolationsParam,
isASAPSubmitBetaEnabled,
isReportLastVisibleArchived,
Expand Down
11 changes: 9 additions & 2 deletions src/pages/DynamicReportChangeWorkspacePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,12 @@ function DynamicReportChangeWorkspacePage({report}: DynamicReportChangeWorkspace
report,
parentReport,
policy,
currentUserAccountID: session?.accountID ?? CONST.DEFAULT_NUMBER_ID,
email: session?.email ?? '',
currentUser: {
accountID: currentUserPersonalDetails.accountID,
displayName: currentUserPersonalDetails.displayName,
email: currentUserPersonalDetails.email,
avatar: currentUserPersonalDetails.avatar,
},
hasViolationsParam: hasViolations,
isChangePolicyTrainingModalDismissed,
isASAPSubmitBetaEnabled,
Expand Down Expand Up @@ -160,6 +164,9 @@ function DynamicReportChangeWorkspacePage({report}: DynamicReportChangeWorkspace
reportNextStep,
isChangePolicyTrainingModalDismissed,
currentUserPersonalDetails.accountID,
currentUserPersonalDetails.displayName,
currentUserPersonalDetails.avatar,
currentUserPersonalDetails.email,
],
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,12 @@ function BaseOnboardingWorkspaceInvite({shouldUseNativeStyles}: BaseOnboardingWo
policyMemberAccountIDs,
CONST.POLICY.ROLE.USER,
formatPhoneNumber,
currentUserPersonalDetails.accountID,
{
accountID: currentUserPersonalDetails.accountID,
displayName: currentUserPersonalDetails.displayName,
email: currentUserPersonalDetails.email,
avatar: currentUserPersonalDetails.avatar,
},
undefined,
filteredReportActions,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import AccessOrNotFoundWrapper from '@src/pages/workspace/AccessOrNotFoundWrappe
import ROUTES, {DYNAMIC_ROUTES} from '@src/ROUTES';
import type {Route as Routes} from '@src/ROUTES';
import INPUT_IDS from '@src/types/form/WorkspaceInviteMessageForm';
import type {PersonalDetails} from '@src/types/onyx';
import type {CurrentUserPersonalDetails} from '@src/types/onyx/PersonalDetails';
import type Policy from '@src/types/onyx/Policy';
import {isEmptyObject} from '@src/types/utils/EmptyObject';
import isLoadingOnyxValue from '@src/types/utils/isLoadingOnyxValue';
Expand All @@ -45,7 +45,7 @@ type WorkspaceInviteMessageComponentProps = {
policy: OnyxEntry<Policy>;
policyID: string;
backTo: Routes | undefined;
currentUserPersonalDetails: OnyxEntry<PersonalDetails>;
currentUserPersonalDetails: CurrentUserPersonalDetails;
shouldShowTooltip?: boolean;
shouldShowBackButton?: boolean;
shouldShowMemberNames?: boolean;
Expand Down Expand Up @@ -156,7 +156,12 @@ function WorkspaceInviteMessageComponent({
policyMemberAccountIDs,
workspaceInviteRoleDraft,
formatPhoneNumber,
currentUserPersonalDetails?.accountID ?? CONST.DEFAULT_NUMBER_ID,
{
accountID: currentUserPersonalDetails?.accountID,
displayName: currentUserPersonalDetails?.displayName,
email: currentUserPersonalDetails.email,
avatar: currentUserPersonalDetails?.avatar,
},
shouldShowApproverRow ? validatedApprover : undefined,
filteredReportActions,
);
Expand Down
70 changes: 63 additions & 7 deletions tests/actions/PolicyMemberTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ describe('actions/PolicyMember', () => {
});

describe('addMembersToWorkspace', () => {
const currentUserAccountID = 1;
const currentUser = {accountID: 1, displayName: 'Current User', email: 'current@example.com'};

it('Add a new member to a workspace', async () => {
const policyID = '1';
Expand All @@ -376,7 +376,7 @@ describe('actions/PolicyMember', () => {
await Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`, policy);

mockFetch?.pause?.();
Member.addMembersToWorkspace({[newUserEmail]: 1234}, 'Welcome', policy, [], CONST.POLICY.ROLE.USER, TestHelper.formatPhoneNumber, currentUserAccountID);
Member.addMembersToWorkspace({[newUserEmail]: 1234}, 'Welcome', policy, [], CONST.POLICY.ROLE.USER, TestHelper.formatPhoneNumber, currentUser);

await waitForBatchedUpdates();

Expand Down Expand Up @@ -430,9 +430,9 @@ describe('actions/PolicyMember', () => {

// When adding a new admin, auditor, and user members
mockFetch?.pause?.();
Member.addMembersToWorkspace({[adminEmail]: adminAccountID}, 'Welcome', policy, [], CONST.POLICY.ROLE.ADMIN, TestHelper.formatPhoneNumber, currentUserAccountID);
Member.addMembersToWorkspace({[auditorEmail]: auditorAccountID}, 'Welcome', policy, [], CONST.POLICY.ROLE.AUDITOR, TestHelper.formatPhoneNumber, currentUserAccountID);
Member.addMembersToWorkspace({[userEmail]: userAccountID}, 'Welcome', policy, [], CONST.POLICY.ROLE.USER, TestHelper.formatPhoneNumber, currentUserAccountID);
Member.addMembersToWorkspace({[adminEmail]: adminAccountID}, 'Welcome', policy, [], CONST.POLICY.ROLE.ADMIN, TestHelper.formatPhoneNumber, currentUser);
Member.addMembersToWorkspace({[auditorEmail]: auditorAccountID}, 'Welcome', policy, [], CONST.POLICY.ROLE.AUDITOR, TestHelper.formatPhoneNumber, currentUser);
Member.addMembersToWorkspace({[userEmail]: userAccountID}, 'Welcome', policy, [], CONST.POLICY.ROLE.USER, TestHelper.formatPhoneNumber, currentUser);

await waitForBatchedUpdates();

Expand Down Expand Up @@ -495,7 +495,7 @@ describe('actions/PolicyMember', () => {
});

// When adding the user to the workspace
Member.addMembersToWorkspace({[userEmail]: userAccountID}, 'Welcome', policy, [], CONST.POLICY.ROLE.USER, TestHelper.formatPhoneNumber, currentUserAccountID);
Member.addMembersToWorkspace({[userEmail]: userAccountID}, 'Welcome', policy, [], CONST.POLICY.ROLE.USER, TestHelper.formatPhoneNumber, currentUser);

await waitForBatchedUpdates();

Expand Down Expand Up @@ -562,7 +562,7 @@ describe('actions/PolicyMember', () => {
[],
CONST.POLICY.ROLE.USER,
TestHelper.formatPhoneNumber,
currentUserAccountID,
currentUser,
undefined,
reportActionsList,
);
Expand All @@ -580,6 +580,62 @@ describe('actions/PolicyMember', () => {
});
expect(isExpenseReportArchived).toBe(false);
});

describe('buildAddMembersToWorkspaceOnyxData currentUser dependency', () => {
const buildForCurrentUser = (currentUserInput: {accountID: number; displayName?: string; email?: string; avatar?: string}) =>
Member.buildAddMembersToWorkspaceOnyxData(
// eslint-disable-next-line @typescript-eslint/naming-convention
{'new-member@example.com': 9001},
createRandomPolicy(101),
[],
CONST.POLICY.ROLE.USER,
TestHelper.formatPhoneNumber,
currentUserInput,
);

type BuildResult = ReturnType<typeof buildForCurrentUser>;

const findOptimisticCreatedAction = (optimisticData: BuildResult['optimisticData']) => {
for (const update of optimisticData) {
if (!update.key.startsWith(ONYXKEYS.COLLECTION.REPORT_ACTIONS)) {
continue;
}
const value = update.value as Record<string, ReportAction> | null | undefined;
if (!value) {
continue;
}
const createdAction = Object.values(value).find((action) => action?.actionName === CONST.REPORT.ACTIONS.TYPE.CREATED);
if (createdAction) {
return createdAction;
}
}
return undefined;
};

it('actorAccountID on the optimistic CREATED action equals currentUser.accountID', () => {
const {optimisticData: dataForA} = buildForCurrentUser({accountID: 42, displayName: 'A', email: 'a@example.com'});
const {optimisticData: dataForB} = buildForCurrentUser({accountID: 99, displayName: 'B', email: 'b@example.com'});

expect(findOptimisticCreatedAction(dataForA)?.actorAccountID).toBe(42);
expect(findOptimisticCreatedAction(dataForB)?.actorAccountID).toBe(99);
});

it('person text on the optimistic CREATED action equals currentUser.displayName when defined', () => {
const {optimisticData} = buildForCurrentUser({accountID: 1, displayName: 'Alice Smith', email: 'alice@example.com'});
expect(findOptimisticCreatedAction(optimisticData)?.person?.at(0)?.text).toBe('Alice Smith');
});

it('person text on the optimistic CREATED action falls back to currentUser.email when displayName is undefined', () => {
const {optimisticData} = buildForCurrentUser({accountID: 1, email: 'fallback@example.com'});
expect(findOptimisticCreatedAction(optimisticData)?.person?.at(0)?.text).toBe('fallback@example.com');
});

it('avatar on the optimistic CREATED action equals currentUser.avatar when defined', () => {
const avatar = 'https://avatar.example/me.png';
const {optimisticData} = buildForCurrentUser({accountID: 1, displayName: 'Alice', email: 'alice@example.com', avatar});
expect(findOptimisticCreatedAction(optimisticData)?.avatar).toBe(avatar);
});
});
});

describe('removeMembers', () => {
Expand Down
24 changes: 8 additions & 16 deletions tests/actions/ReportTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3424,8 +3424,7 @@ describe('actions/Report', () => {
report: expenseReport,
parentReport: undefined,
policy: createRandomPolicy(Number(2)),
currentUserAccountID: 1,
email: '',
currentUser: {accountID: 1},
hasViolationsParam: true,
isChangePolicyTrainingModalDismissed: false,
isASAPSubmitBetaEnabled: false,
Expand Down Expand Up @@ -3514,8 +3513,7 @@ describe('actions/Report', () => {
report: expenseReport,
parentReport: undefined,
policy: newPolicy,
currentUserAccountID: 1,
email: '',
currentUser: {accountID: 1},
hasViolationsParam: true,
isChangePolicyTrainingModalDismissed: false,
isASAPSubmitBetaEnabled: false,
Expand Down Expand Up @@ -3557,8 +3555,7 @@ describe('actions/Report', () => {
report: expenseReport,
parentReport: undefined,
policy: createRandomPolicy(Number(2)),
currentUserAccountID: 1,
email: '',
currentUser: {accountID: 1},
hasViolationsParam: false,
isChangePolicyTrainingModalDismissed: false,
isASAPSubmitBetaEnabled: false,
Expand Down Expand Up @@ -3586,8 +3583,7 @@ describe('actions/Report', () => {
report: expenseReport,
parentReport: undefined,
policy: targetPolicy,
currentUserAccountID: 1,
email: '',
currentUser: {accountID: 1},
hasViolationsParam: false,
isChangePolicyTrainingModalDismissed: false,
isASAPSubmitBetaEnabled: false,
Expand All @@ -3614,8 +3610,7 @@ describe('actions/Report', () => {
report: expenseReport,
parentReport: undefined,
policy: createRandomPolicy(Number(2)),
currentUserAccountID: 1,
email: '',
currentUser: {accountID: 1},
hasViolationsParam: false,
isChangePolicyTrainingModalDismissed: false,
isASAPSubmitBetaEnabled: false,
Expand All @@ -3642,8 +3637,7 @@ describe('actions/Report', () => {
report: expenseReport,
parentReport: undefined,
policy: createRandomPolicy(Number(2)),
currentUserAccountID: 1,
email: '',
currentUser: {accountID: 1},
hasViolationsParam: false,
isChangePolicyTrainingModalDismissed: false,
isASAPSubmitBetaEnabled: false,
Expand Down Expand Up @@ -3672,8 +3666,7 @@ describe('actions/Report', () => {
report: expenseReport,
parentReport: undefined,
policy: createRandomPolicy(Number(2)),
currentUserAccountID: 1,
email: '',
currentUser: {accountID: 1},
hasViolationsParam: false,
isChangePolicyTrainingModalDismissed: false,
isASAPSubmitBetaEnabled: false,
Expand Down Expand Up @@ -3723,8 +3716,7 @@ describe('actions/Report', () => {
report: expenseReport,
parentReport: undefined,
policy: targetPolicy,
currentUserAccountID: 1,
email: 'current-user@expensifail.com',
currentUser: {accountID: 1, email: 'current-user@expensifail.com'},
hasViolationsParam: false,
isChangePolicyTrainingModalDismissed: false,
isASAPSubmitBetaEnabled: false,
Expand Down
Loading