Skip to content

Commit 41c5e4f

Browse files
authored
Merge pull request #91535 from dukenv0307/fix/66412-part-13
refactor addMembersToWorkspace and changeReportPolicyAndInviteSubmitter to use user data from useOnyx
2 parents 7508092 + 7873618 commit 41c5e4f

7 files changed

Lines changed: 102 additions & 37 deletions

File tree

src/libs/actions/Policy/Member.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -930,7 +930,7 @@ function addMembersToWorkspace(
930930
policyMemberAccountIDs: number[],
931931
role: string,
932932
formatPhoneNumber: LocaleContextProps['formatPhoneNumber'],
933-
currentUserAccountID: number,
933+
currentUser: CurrentUser,
934934
approverEmail?: string,
935935
// TODO: Remove optional (?) once all callers are updated in follow-up PRs of https://github.com/Expensify/App/issues/66578
936936
reportActionsList?: OnyxCollection<ReportActions>,
@@ -945,7 +945,7 @@ function addMembersToWorkspace(
945945
policyMemberAccountIDs,
946946
role,
947947
formatPhoneNumber,
948-
{accountID: currentUserAccountID},
948+
currentUser,
949949
approverEmail,
950950
undefined,
951951
reportActionsList,

src/libs/actions/Report/index.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ import navigateFromNotification from '@userActions/navigateFromNotification';
181181
import {getAll} from '@userActions/PersistedRequests';
182182
import {buildAddMembersToWorkspaceOnyxData, buildRoomMembersOnyxData} from '@userActions/Policy/Member';
183183
import {createPolicyExpenseChats} from '@userActions/Policy/Policy';
184+
import type {CurrentUser} from '@userActions/Policy/Policy';
184185
import {
185186
createUpdateCommentMatcher,
186187
resolveCommentDeletionConflicts,
@@ -7568,8 +7569,7 @@ function changeReportPolicyAndInviteSubmitter({
75687569
report,
75697570
parentReport,
75707571
policy,
7571-
currentUserAccountID,
7572-
email,
7572+
currentUser,
75737573
hasViolationsParam,
75747574
isChangePolicyTrainingModalDismissed,
75757575
isASAPSubmitBetaEnabled,
@@ -7582,8 +7582,7 @@ function changeReportPolicyAndInviteSubmitter({
75827582
report: Report;
75837583
parentReport: OnyxEntry<Report>;
75847584
policy: Policy;
7585-
currentUserAccountID: number;
7586-
email: string;
7585+
currentUser: CurrentUser;
75877586
hasViolationsParam: boolean;
75887587
isChangePolicyTrainingModalDismissed: boolean;
75897588
isASAPSubmitBetaEnabled: boolean;
@@ -7602,6 +7601,7 @@ function changeReportPolicyAndInviteSubmitter({
76027601
if (!submitterEmail) {
76037602
return;
76047603
}
7604+
const {accountID: currentUserAccountID, email: currentUserEmail = ''} = currentUser;
76057605
const policyMemberAccountIDs = Object.values(getMemberAccountIDsForWorkspace(employeeList, false, false));
76067606
const {
76077607
optimisticData: optimisticAddMembersData,
@@ -7614,7 +7614,7 @@ function changeReportPolicyAndInviteSubmitter({
76147614
policyMemberAccountIDs,
76157615
CONST.POLICY.ROLE.USER,
76167616
formatPhoneNumber,
7617-
{accountID: currentUserAccountID},
7617+
currentUser,
76187618
undefined,
76197619
CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS,
76207620
reportActionsList,
@@ -7637,7 +7637,7 @@ function changeReportPolicyAndInviteSubmitter({
76377637
parentReport,
76387638
policy,
76397639
currentUserAccountID,
7640-
email,
7640+
currentUserEmail,
76417641
hasViolationsParam,
76427642
isASAPSubmitBetaEnabled,
76437643
isReportLastVisibleArchived,

src/pages/DynamicReportChangeWorkspacePage.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,12 @@ function DynamicReportChangeWorkspacePage({report}: DynamicReportChangeWorkspace
114114
report,
115115
parentReport,
116116
policy,
117-
currentUserAccountID: session?.accountID ?? CONST.DEFAULT_NUMBER_ID,
118-
email: session?.email ?? '',
117+
currentUser: {
118+
accountID: currentUserPersonalDetails.accountID,
119+
displayName: currentUserPersonalDetails.displayName,
120+
email: currentUserPersonalDetails.email,
121+
avatar: currentUserPersonalDetails.avatar,
122+
},
119123
hasViolationsParam: hasViolations,
120124
isChangePolicyTrainingModalDismissed,
121125
isASAPSubmitBetaEnabled,
@@ -160,6 +164,9 @@ function DynamicReportChangeWorkspacePage({report}: DynamicReportChangeWorkspace
160164
reportNextStep,
161165
isChangePolicyTrainingModalDismissed,
162166
currentUserPersonalDetails.accountID,
167+
currentUserPersonalDetails.displayName,
168+
currentUserPersonalDetails.avatar,
169+
currentUserPersonalDetails.email,
163170
],
164171
);
165172

src/pages/OnboardingWorkspaceInvite/BaseOnboardingWorkspaceInvite.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,12 @@ function BaseOnboardingWorkspaceInvite({shouldUseNativeStyles}: BaseOnboardingWo
181181
policyMemberAccountIDs,
182182
CONST.POLICY.ROLE.USER,
183183
formatPhoneNumber,
184-
currentUserPersonalDetails.accountID,
184+
{
185+
accountID: currentUserPersonalDetails.accountID,
186+
displayName: currentUserPersonalDetails.displayName,
187+
email: currentUserPersonalDetails.email,
188+
avatar: currentUserPersonalDetails.avatar,
189+
},
185190
undefined,
186191
filteredReportActions,
187192
);

src/pages/workspace/members/WorkspaceInviteMessageComponent.tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import AccessOrNotFoundWrapper from '@src/pages/workspace/AccessOrNotFoundWrappe
3636
import ROUTES, {DYNAMIC_ROUTES} from '@src/ROUTES';
3737
import type {Route as Routes} from '@src/ROUTES';
3838
import INPUT_IDS from '@src/types/form/WorkspaceInviteMessageForm';
39-
import type {PersonalDetails} from '@src/types/onyx';
39+
import type {CurrentUserPersonalDetails} from '@src/types/onyx/PersonalDetails';
4040
import type Policy from '@src/types/onyx/Policy';
4141
import {isEmptyObject} from '@src/types/utils/EmptyObject';
4242
import isLoadingOnyxValue from '@src/types/utils/isLoadingOnyxValue';
@@ -45,7 +45,7 @@ type WorkspaceInviteMessageComponentProps = {
4545
policy: OnyxEntry<Policy>;
4646
policyID: string;
4747
backTo: Routes | undefined;
48-
currentUserPersonalDetails: OnyxEntry<PersonalDetails>;
48+
currentUserPersonalDetails: CurrentUserPersonalDetails;
4949
shouldShowTooltip?: boolean;
5050
shouldShowBackButton?: boolean;
5151
shouldShowMemberNames?: boolean;
@@ -156,7 +156,12 @@ function WorkspaceInviteMessageComponent({
156156
policyMemberAccountIDs,
157157
workspaceInviteRoleDraft,
158158
formatPhoneNumber,
159-
currentUserPersonalDetails?.accountID ?? CONST.DEFAULT_NUMBER_ID,
159+
{
160+
accountID: currentUserPersonalDetails?.accountID,
161+
displayName: currentUserPersonalDetails?.displayName,
162+
email: currentUserPersonalDetails.email,
163+
avatar: currentUserPersonalDetails?.avatar,
164+
},
160165
shouldShowApproverRow ? validatedApprover : undefined,
161166
filteredReportActions,
162167
);

tests/actions/PolicyMemberTest.ts

Lines changed: 63 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ describe('actions/PolicyMember', () => {
362362
});
363363

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

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

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

381381
await waitForBatchedUpdates();
382382

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

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

437437
await waitForBatchedUpdates();
438438

@@ -495,7 +495,7 @@ describe('actions/PolicyMember', () => {
495495
});
496496

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

500500
await waitForBatchedUpdates();
501501

@@ -562,7 +562,7 @@ describe('actions/PolicyMember', () => {
562562
[],
563563
CONST.POLICY.ROLE.USER,
564564
TestHelper.formatPhoneNumber,
565-
currentUserAccountID,
565+
currentUser,
566566
undefined,
567567
reportActionsList,
568568
);
@@ -580,6 +580,62 @@ describe('actions/PolicyMember', () => {
580580
});
581581
expect(isExpenseReportArchived).toBe(false);
582582
});
583+
584+
describe('buildAddMembersToWorkspaceOnyxData currentUser dependency', () => {
585+
const buildForCurrentUser = (currentUserInput: {accountID: number; displayName?: string; email?: string; avatar?: string}) =>
586+
Member.buildAddMembersToWorkspaceOnyxData(
587+
// eslint-disable-next-line @typescript-eslint/naming-convention
588+
{'new-member@example.com': 9001},
589+
createRandomPolicy(101),
590+
[],
591+
CONST.POLICY.ROLE.USER,
592+
TestHelper.formatPhoneNumber,
593+
currentUserInput,
594+
);
595+
596+
type BuildResult = ReturnType<typeof buildForCurrentUser>;
597+
598+
const findOptimisticCreatedAction = (optimisticData: BuildResult['optimisticData']) => {
599+
for (const update of optimisticData) {
600+
if (!update.key.startsWith(ONYXKEYS.COLLECTION.REPORT_ACTIONS)) {
601+
continue;
602+
}
603+
const value = update.value as Record<string, ReportAction> | null | undefined;
604+
if (!value) {
605+
continue;
606+
}
607+
const createdAction = Object.values(value).find((action) => action?.actionName === CONST.REPORT.ACTIONS.TYPE.CREATED);
608+
if (createdAction) {
609+
return createdAction;
610+
}
611+
}
612+
return undefined;
613+
};
614+
615+
it('actorAccountID on the optimistic CREATED action equals currentUser.accountID', () => {
616+
const {optimisticData: dataForA} = buildForCurrentUser({accountID: 42, displayName: 'A', email: 'a@example.com'});
617+
const {optimisticData: dataForB} = buildForCurrentUser({accountID: 99, displayName: 'B', email: 'b@example.com'});
618+
619+
expect(findOptimisticCreatedAction(dataForA)?.actorAccountID).toBe(42);
620+
expect(findOptimisticCreatedAction(dataForB)?.actorAccountID).toBe(99);
621+
});
622+
623+
it('person text on the optimistic CREATED action equals currentUser.displayName when defined', () => {
624+
const {optimisticData} = buildForCurrentUser({accountID: 1, displayName: 'Alice Smith', email: 'alice@example.com'});
625+
expect(findOptimisticCreatedAction(optimisticData)?.person?.at(0)?.text).toBe('Alice Smith');
626+
});
627+
628+
it('person text on the optimistic CREATED action falls back to currentUser.email when displayName is undefined', () => {
629+
const {optimisticData} = buildForCurrentUser({accountID: 1, email: 'fallback@example.com'});
630+
expect(findOptimisticCreatedAction(optimisticData)?.person?.at(0)?.text).toBe('fallback@example.com');
631+
});
632+
633+
it('avatar on the optimistic CREATED action equals currentUser.avatar when defined', () => {
634+
const avatar = 'https://avatar.example/me.png';
635+
const {optimisticData} = buildForCurrentUser({accountID: 1, displayName: 'Alice', email: 'alice@example.com', avatar});
636+
expect(findOptimisticCreatedAction(optimisticData)?.avatar).toBe(avatar);
637+
});
638+
});
583639
});
584640

585641
describe('removeMembers', () => {

tests/actions/ReportTest.ts

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3426,8 +3426,7 @@ describe('actions/Report', () => {
34263426
report: expenseReport,
34273427
parentReport: undefined,
34283428
policy: createRandomPolicy(Number(2)),
3429-
currentUserAccountID: 1,
3430-
email: '',
3429+
currentUser: {accountID: 1},
34313430
hasViolationsParam: true,
34323431
isChangePolicyTrainingModalDismissed: false,
34333432
isASAPSubmitBetaEnabled: false,
@@ -3516,8 +3515,7 @@ describe('actions/Report', () => {
35163515
report: expenseReport,
35173516
parentReport: undefined,
35183517
policy: newPolicy,
3519-
currentUserAccountID: 1,
3520-
email: '',
3518+
currentUser: {accountID: 1},
35213519
hasViolationsParam: true,
35223520
isChangePolicyTrainingModalDismissed: false,
35233521
isASAPSubmitBetaEnabled: false,
@@ -3559,8 +3557,7 @@ describe('actions/Report', () => {
35593557
report: expenseReport,
35603558
parentReport: undefined,
35613559
policy: createRandomPolicy(Number(2)),
3562-
currentUserAccountID: 1,
3563-
email: '',
3560+
currentUser: {accountID: 1},
35643561
hasViolationsParam: false,
35653562
isChangePolicyTrainingModalDismissed: false,
35663563
isASAPSubmitBetaEnabled: false,
@@ -3588,8 +3585,7 @@ describe('actions/Report', () => {
35883585
report: expenseReport,
35893586
parentReport: undefined,
35903587
policy: targetPolicy,
3591-
currentUserAccountID: 1,
3592-
email: '',
3588+
currentUser: {accountID: 1},
35933589
hasViolationsParam: false,
35943590
isChangePolicyTrainingModalDismissed: false,
35953591
isASAPSubmitBetaEnabled: false,
@@ -3616,8 +3612,7 @@ describe('actions/Report', () => {
36163612
report: expenseReport,
36173613
parentReport: undefined,
36183614
policy: createRandomPolicy(Number(2)),
3619-
currentUserAccountID: 1,
3620-
email: '',
3615+
currentUser: {accountID: 1},
36213616
hasViolationsParam: false,
36223617
isChangePolicyTrainingModalDismissed: false,
36233618
isASAPSubmitBetaEnabled: false,
@@ -3644,8 +3639,7 @@ describe('actions/Report', () => {
36443639
report: expenseReport,
36453640
parentReport: undefined,
36463641
policy: createRandomPolicy(Number(2)),
3647-
currentUserAccountID: 1,
3648-
email: '',
3642+
currentUser: {accountID: 1},
36493643
hasViolationsParam: false,
36503644
isChangePolicyTrainingModalDismissed: false,
36513645
isASAPSubmitBetaEnabled: false,
@@ -3674,8 +3668,7 @@ describe('actions/Report', () => {
36743668
report: expenseReport,
36753669
parentReport: undefined,
36763670
policy: createRandomPolicy(Number(2)),
3677-
currentUserAccountID: 1,
3678-
email: '',
3671+
currentUser: {accountID: 1},
36793672
hasViolationsParam: false,
36803673
isChangePolicyTrainingModalDismissed: false,
36813674
isASAPSubmitBetaEnabled: false,
@@ -3725,8 +3718,7 @@ describe('actions/Report', () => {
37253718
report: expenseReport,
37263719
parentReport: undefined,
37273720
policy: targetPolicy,
3728-
currentUserAccountID: 1,
3729-
email: 'current-user@expensifail.com',
3721+
currentUser: {accountID: 1, email: 'current-user@expensifail.com'},
37303722
hasViolationsParam: false,
37313723
isChangePolicyTrainingModalDismissed: false,
37323724
isASAPSubmitBetaEnabled: false,

0 commit comments

Comments
 (0)