Skip to content

Commit 36c1f57

Browse files
authored
Merge pull request Expensify#88307 from DylanDylann/refactor-66579-p5
Part 5: Remove Onyx.connect() for the key: ONYXKEYS.SESSION in src/libs/actions/Policy/Policy.ts
2 parents cebabfe + 245b705 commit 36c1f57

9 files changed

Lines changed: 63 additions & 19 deletions

File tree

src/components/KYCWall/BaseKYCWall.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,14 @@ function KYCWall({
144144
if (iouReport && isIOUReport(iouReport)) {
145145
const adminPolicy = policies?.[`${ONYXKEYS.COLLECTION.POLICY}${policy?.id}`];
146146
if (adminPolicy) {
147-
const inviteResult = moveIOUReportToPolicyAndInviteSubmitter(iouReport, adminPolicy, formatPhoneNumber, filteredReportActions, reportTransactions);
147+
const inviteResult = moveIOUReportToPolicyAndInviteSubmitter(
148+
iouReport,
149+
adminPolicy,
150+
formatPhoneNumber,
151+
filteredReportActions,
152+
currentUserAccountID,
153+
reportTransactions,
154+
);
148155
if (inviteResult?.policyExpenseChatReportID) {
149156
setNavigationActionToMicrotaskQueue(() => {
150157
Navigation.navigate(ROUTES.REPORT_WITH_ID.getRoute(inviteResult.policyExpenseChatReportID));

src/hooks/useSearchBulkActions.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -722,7 +722,7 @@ function useSearchBulkActions({queryJSON}: UseSearchBulkActionsParams) {
722722
);
723723
return;
724724
}
725-
const invite = moveIOUReportToPolicyAndInviteSubmitter(itemReport, adminPolicy, formatPhoneNumber, policyExpenseChatReportActions, reportTransactions);
725+
const invite = moveIOUReportToPolicyAndInviteSubmitter(itemReport, adminPolicy, formatPhoneNumber, policyExpenseChatReportActions, accountID, reportTransactions);
726726
if (!invite?.policyExpenseChatReportID) {
727727
moveIOUReportToPolicy(itemReport, adminPolicy, false, reportTransactions);
728728
}
@@ -789,6 +789,7 @@ function useSearchBulkActions({queryJSON}: UseSearchBulkActionsParams) {
789789
policyExpenseChatReportActions,
790790
formatPhoneNumber,
791791
clearSelectedTransactions,
792+
accountID,
792793
],
793794
);
794795

src/libs/actions/Policy/Member.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -818,7 +818,7 @@ function buildAddMembersToWorkspaceOnyxData(
818818
policyMemberAccountIDs: number[],
819819
role: string,
820820
formatPhoneNumber: LocaleContextProps['formatPhoneNumber'],
821-
currentUserAccountID: number | undefined,
821+
currentUserAccountID: number,
822822
approverEmail?: string,
823823
policyExpenseChatNotificationPreference?: NotificationPreference,
824824
// TODO: Remove optional (?) once all callers are updated in follow-up PRs of https://github.com/Expensify/App/issues/66578
@@ -843,7 +843,7 @@ function buildAddMembersToWorkspaceOnyxData(
843843
const announceRoomChat = optimisticAnnounceChat.announceChatData;
844844

845845
// create onyx data for policy expense chats for each new member
846-
const membersChats = createPolicyExpenseChats(policyID, invitedEmailsToAccountIDs, reportActionsList, undefined, policyExpenseChatNotificationPreference);
846+
const membersChats = createPolicyExpenseChats(policyID, invitedEmailsToAccountIDs, currentUserAccountID, reportActionsList, undefined, policyExpenseChatNotificationPreference);
847847

848848
const optimisticMembersState: OnyxCollectionInputValue<PolicyEmployee> = {};
849849
const successMembersState: OnyxCollectionInputValue<PolicyEmployee> = {};
@@ -954,7 +954,7 @@ function addMembersToWorkspace(
954954
policyMemberAccountIDs: number[],
955955
role: string,
956956
formatPhoneNumber: LocaleContextProps['formatPhoneNumber'],
957-
currentUserAccountID: number | undefined,
957+
currentUserAccountID: number,
958958
approverEmail?: string,
959959
// TODO: Remove optional (?) once all callers are updated in follow-up PRs of https://github.com/Expensify/App/issues/66578
960960
reportActionsList?: OnyxCollection<ReportActions>,

src/libs/actions/Policy/Policy.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1585,6 +1585,7 @@ function verifySetupIntentAndRequestPolicyOwnerChange(policyID: string, currentU
15851585
function createPolicyExpenseChats(
15861586
policyID: string,
15871587
invitedEmailsToAccountIDs: InvitedEmailsToAccountIDs,
1588+
currentUserAccountID: number,
15881589
// TODO: Remove optional (?) once all is updated (https://github.com/Expensify/App/issues/66578)
15891590
reportActionsList?: OnyxCollection<ReportActions>,
15901591
hasOutstandingChildRequest = false,
@@ -1648,12 +1649,12 @@ function createPolicyExpenseChats(
16481649
continue;
16491650
}
16501651
const optimisticReport = ReportUtils.buildOptimisticChatReport({
1651-
participantList: [deprecatedSessionAccountID, cleanAccountID],
1652+
participantList: [currentUserAccountID, cleanAccountID],
16521653
chatType: CONST.REPORT.CHAT_TYPE.POLICY_EXPENSE_CHAT,
16531654
policyID,
16541655
ownerAccountID: cleanAccountID,
16551656
notificationPreference,
1656-
currentUserAccountID: deprecatedSessionAccountID,
1657+
currentUserAccountID,
16571658
});
16581659

16591660
// Set correct notification preferences: visible for the submitter, hidden for others until there's activity
@@ -3031,6 +3032,7 @@ function buildPolicyData(options: BuildPolicyDataOptions): OnyxData<BuildPolicyD
30313032
const employeeWorkspaceChat = createPolicyExpenseChats(
30323033
policyID,
30333034
{[adminParticipant.login]: adminParticipant.accountID ?? CONST.DEFAULT_NUMBER_ID},
3035+
currentUserAccountIDParam,
30343036
undefined,
30353037
hasOutstandingChildRequest,
30363038
);
@@ -4086,7 +4088,7 @@ function createWorkspaceFromIOUPayment(
40864088
}
40874089

40884090
// Create the expense chat for the employee whose IOU is being paid
4089-
const employeeWorkspaceChat = createPolicyExpenseChats(policyID, {[iouReportOwnerEmail]: employeeAccountID}, reportActionsList, true);
4091+
const employeeWorkspaceChat = createPolicyExpenseChats(policyID, {[iouReportOwnerEmail]: employeeAccountID}, currentUserAccountID, reportActionsList, true);
40904092
const newWorkspace = {
40914093
id: policyID,
40924094

src/libs/actions/Report/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6279,6 +6279,7 @@ function moveIOUReportToPolicyAndInviteSubmitter(
62796279
policy: Policy,
62806280
formatPhoneNumber: LocaleContextProps['formatPhoneNumber'],
62816281
reportActions: OnyxCollection<ReportActions>,
6282+
currentUserAccountID: number,
62826283
reportTransactions: Transaction[] = [],
62836284
): {policyExpenseChatReportID?: string} | undefined {
62846285
if (!policy || !iouReport) {
@@ -6353,7 +6354,7 @@ function moveIOUReportToPolicyAndInviteSubmitter(
63536354
const announceRoomMembers = buildRoomMembersOnyxData(CONST.REPORT.CHAT_TYPE.POLICY_ANNOUNCE, policyID, [submitterAccountID]);
63546355

63556356
// Create policy expense chat for the submitter
6356-
const policyExpenseChats = createPolicyExpenseChats(policyID, invitedEmailsToAccountIDs, reportActions);
6357+
const policyExpenseChats = createPolicyExpenseChats(policyID, invitedEmailsToAccountIDs, currentUserAccountID, reportActions);
63576358
const optimisticPolicyExpenseChatReportID = policyExpenseChats.reportCreationData[submitterEmail].reportID;
63586359
const optimisticPolicyExpenseChatCreatedReportActionID = policyExpenseChats.reportCreationData[submitterEmail].reportActionID;
63596360

src/pages/ReportChangeWorkspacePage.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,14 @@ function ReportChangeWorkspacePage({report, route}: ReportChangeWorkspacePagePro
9292
const {backTo} = route.params;
9393
Navigation.goBack(backTo);
9494
if (isIOUReport(reportID)) {
95-
const invite = moveIOUReportToPolicyAndInviteSubmitter(report, policy, formatPhoneNumber, filteredReportActions, reportTransactions);
95+
const invite = moveIOUReportToPolicyAndInviteSubmitter(
96+
report,
97+
policy,
98+
formatPhoneNumber,
99+
filteredReportActions,
100+
session?.accountID ?? CONST.DEFAULT_NUMBER_ID,
101+
reportTransactions,
102+
);
96103
if (!invite?.policyExpenseChatReportID) {
97104
moveIOUReportToPolicy(report, policy, false, reportTransactions);
98105
}

src/pages/workspace/members/WorkspaceInviteMessageComponent.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ function WorkspaceInviteMessageComponent({
157157
policyMemberAccountIDs,
158158
workspaceInviteRoleDraft,
159159
formatPhoneNumber,
160-
currentUserPersonalDetails?.accountID,
160+
currentUserPersonalDetails?.accountID ?? CONST.DEFAULT_NUMBER_ID,
161161
shouldShowApproverRow ? validatedApprover : undefined,
162162
filteredReportActions,
163163
);

tests/actions/PolicyTest.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5199,7 +5199,7 @@ describe('actions/Policy', () => {
51995199
await waitForBatchedUpdates();
52005200

52015201
// When creating policy expense chats for a new member
5202-
const result = Policy.createPolicyExpenseChats(policyID, {[newMemberEmail]: newMemberAccountID});
5202+
const result = Policy.createPolicyExpenseChats(policyID, {[newMemberEmail]: newMemberAccountID}, ESH_ACCOUNT_ID);
52035203

52045204
// Then optimistic data should be generated
52055205
expect(result.onyxOptimisticData.length).toBeGreaterThan(0);
@@ -5257,14 +5257,38 @@ describe('actions/Policy', () => {
52575257
await waitForBatchedUpdates();
52585258

52595259
// When creating policy expense chats for the existing member
5260-
const result = Policy.createPolicyExpenseChats(policyID, {[existingMemberEmail]: existingMemberAccountID});
5260+
const result = Policy.createPolicyExpenseChats(policyID, {[existingMemberEmail]: existingMemberAccountID}, ESH_ACCOUNT_ID);
52615261

52625262
// Then the existing report should be reused (no new reportActionID)
52635263
const reportCreationEntry = result.reportCreationData[existingMemberEmail];
52645264
expect(reportCreationEntry).toBeTruthy();
52655265
expect(reportCreationEntry.reportID).toBe(existingReportID);
52665266
expect(reportCreationEntry.reportActionID).toBeUndefined();
52675267
});
5268+
5269+
it('should use explicit currentUserAccountID for optimistic report participants instead of Onyx session', async () => {
5270+
// Set Onyx session to a DIFFERENT accountID to verify the explicit parameter is used
5271+
await Onyx.set(ONYXKEYS.SESSION, {email: ESH_EMAIL, accountID: ESH_ACCOUNT_ID});
5272+
await waitForBatchedUpdates();
5273+
5274+
const policyID = Policy.generatePolicyID();
5275+
const newMemberEmail = 'newmember@test.com';
5276+
const newMemberAccountID = 200;
5277+
const customAccountID = 999;
5278+
5279+
const result = Policy.createPolicyExpenseChats(policyID, {[newMemberEmail]: newMemberAccountID}, customAccountID);
5280+
5281+
// Find the optimistic report data
5282+
const reportCreationEntry = result.reportCreationData[newMemberEmail];
5283+
expect(reportCreationEntry).toBeTruthy();
5284+
5285+
const reportOnyxData = result.onyxOptimisticData.find((data) => data.key === `${ONYXKEYS.COLLECTION.REPORT}${reportCreationEntry.reportID}`);
5286+
const reportValue = reportOnyxData?.value as {participants?: Record<string, unknown>};
5287+
5288+
// Verify the explicit customAccountID is used in participants, not the Onyx session accountID
5289+
expect(reportValue?.participants?.[customAccountID]).toBeTruthy();
5290+
expect(reportValue?.participants?.[ESH_ACCOUNT_ID]).toBeUndefined();
5291+
});
52685292
});
52695293
describe('setPolicyCustomTaxName', () => {
52705294
it('should set custom tax name optimistically and succeed', async () => {

tests/actions/ReportTest.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3838,6 +3838,8 @@ describe('actions/Report', () => {
38383838
});
38393839

38403840
describe('moveIOUReportToPolicyAndInviteSubmitter', () => {
3841+
const TEST_USER_ACCOUNT_ID = 1;
3842+
38413843
it('should create moved action on the expense report', async () => {
38423844
const ownerAccountID = 1;
38433845
const ownerEmail = 'owner@gmail.com';
@@ -3857,7 +3859,7 @@ describe('actions/Report', () => {
38573859
await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${policy.id}`, policy);
38583860

38593861
// When moving iou to a workspace and invite the submitter
3860-
Report.moveIOUReportToPolicyAndInviteSubmitter(iouReport, policy, (phone: string) => phone, {});
3862+
Report.moveIOUReportToPolicyAndInviteSubmitter(iouReport, policy, (phone: string) => phone, {}, TEST_USER_ACCOUNT_ID);
38613863
await waitForBatchedUpdates();
38623864

38633865
// Then MOVED report action should be added to the expense report
@@ -3924,7 +3926,7 @@ describe('actions/Report', () => {
39243926

39253927
// Call moveIOUReportToPolicyAndInviteSubmitter
39263928
const formatPhoneNumber = (phoneNumber: string) => phoneNumber;
3927-
Report.moveIOUReportToPolicyAndInviteSubmitter(iouReport, policy, formatPhoneNumber, {});
3929+
Report.moveIOUReportToPolicyAndInviteSubmitter(iouReport, policy, formatPhoneNumber, {}, TEST_USER_ACCOUNT_ID);
39283930
await waitForBatchedUpdates();
39293931

39303932
// Simulate network failure
@@ -3981,7 +3983,7 @@ describe('actions/Report', () => {
39813983
await Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`, transaction);
39823984

39833985
// When moving IOU to a workspace with reportTransactions
3984-
Report.moveIOUReportToPolicyAndInviteSubmitter(iouReport, policy, (phone: string) => phone, {}, [transaction]);
3986+
Report.moveIOUReportToPolicyAndInviteSubmitter(iouReport, policy, (phone: string) => phone, {}, TEST_USER_ACCOUNT_ID, [transaction]);
39853987
await waitForBatchedUpdates();
39863988

39873989
// Then the transaction amounts should be negated optimistically
@@ -4026,7 +4028,7 @@ describe('actions/Report', () => {
40264028
await Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`, transaction);
40274029

40284030
// When moving IOU to a workspace with transactions
4029-
Report.moveIOUReportToPolicyAndInviteSubmitter(iouReport, policy, (phone: string) => phone, {}, [transaction]);
4031+
Report.moveIOUReportToPolicyAndInviteSubmitter(iouReport, policy, (phone: string) => phone, {}, TEST_USER_ACCOUNT_ID, [transaction]);
40304032
await waitForBatchedUpdates();
40314033

40324034
// Then the report should be converted to an expense report with the new policyID
@@ -4049,13 +4051,13 @@ describe('actions/Report', () => {
40494051
...createRandomReport(1, undefined),
40504052
type: CONST.REPORT.TYPE.IOU,
40514053
};
4052-
const result = Report.moveIOUReportToPolicyAndInviteSubmitter(iouReport, undefined as unknown as OnyxTypes.Policy, (phone: string) => phone, {});
4054+
const result = Report.moveIOUReportToPolicyAndInviteSubmitter(iouReport, undefined as unknown as OnyxTypes.Policy, (phone: string) => phone, {}, TEST_USER_ACCOUNT_ID);
40534055
expect(result).toBeUndefined();
40544056
});
40554057

40564058
it('should return undefined when iouReport is missing', () => {
40574059
const policy: OnyxTypes.Policy = {...createRandomPolicy(1), role: CONST.POLICY.ROLE.ADMIN};
4058-
const result = Report.moveIOUReportToPolicyAndInviteSubmitter(undefined, policy, (phone: string) => phone, {});
4060+
const result = Report.moveIOUReportToPolicyAndInviteSubmitter(undefined, policy, (phone: string) => phone, {}, TEST_USER_ACCOUNT_ID);
40594061
expect(result).toBeUndefined();
40604062
});
40614063
});

0 commit comments

Comments
 (0)