Skip to content

Commit 9e543f2

Browse files
authored
Merge pull request Expensify#87566 from DylanDylann/refactor-66579-p4
Part 4: Remove Onyx.connect() for the key: ONYXKEYS.SESSION in src/libs/actions/Policy/Policy.ts
2 parents 691982f + 02f3542 commit 9e543f2

11 files changed

Lines changed: 138 additions & 17 deletions

File tree

src/libs/ReportUtils.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11356,9 +11356,16 @@ function canLeaveChat(report: OnyxEntry<Report>, policy: OnyxEntry<Policy>, isRe
1135611356
);
1135711357
}
1135811358

11359-
function createDraftWorkspaceAndNavigateToConfirmationScreen(introSelected: OnyxEntry<IntroSelected>, transactionID: string, actionName: IOUAction, workspaceName: string): void {
11359+
function createDraftWorkspaceAndNavigateToConfirmationScreen(
11360+
introSelected: OnyxEntry<IntroSelected>,
11361+
transactionID: string,
11362+
actionName: IOUAction,
11363+
workspaceName: string,
11364+
currentUserAccountID: number,
11365+
currentUserEmail: string,
11366+
): void {
1136011367
const isCategorizing = actionName === CONST.IOU.ACTION.CATEGORIZE;
11361-
const {expenseChatReportID, policyID, policyName} = createDraftWorkspace(introSelected, workspaceName, deprecatedCurrentUserEmail);
11368+
const {expenseChatReportID, policyID, policyName} = createDraftWorkspace(introSelected, workspaceName, currentUserAccountID, currentUserEmail);
1136211369
setMoneyRequestParticipants(transactionID, [
1136311370
{
1136411371
selected: true,
@@ -11390,6 +11397,8 @@ type CreateDraftTransactionParams = {
1139011397
isRestrictedToPreferredPolicy?: boolean;
1139111398
preferredPolicyID?: string;
1139211399
transaction: OnyxEntry<Transaction>;
11400+
currentUserAccountID: number;
11401+
currentUserEmail: string;
1139311402
};
1139411403

1139511404
function createDraftTransactionAndNavigateToParticipantSelector({
@@ -11405,6 +11414,8 @@ function createDraftTransactionAndNavigateToParticipantSelector({
1140511414
isRestrictedToPreferredPolicy = false,
1140611415
preferredPolicyID,
1140711416
transaction,
11417+
currentUserAccountID,
11418+
currentUserEmail,
1140811419
}: CreateDraftTransactionParams): void {
1140911420
const transactionID = transaction?.transactionID;
1141011421
if (!transactionID || !reportID) {
@@ -11549,7 +11560,7 @@ function createDraftTransactionAndNavigateToParticipantSelector({
1154911560
return;
1155011561
}
1155111562

11552-
return createDraftWorkspaceAndNavigateToConfirmationScreen(introSelected, transactionID, actionName, '');
11563+
return createDraftWorkspaceAndNavigateToConfirmationScreen(introSelected, transactionID, actionName, '', currentUserAccountID, currentUserEmail);
1155311564
}
1155411565

1155511566
/**

src/libs/actions/Policy/Policy.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2977,6 +2977,8 @@ function createWorkspace(options: CreateWorkspaceDataOptions): CreateWorkspacePa
29772977
function createDraftWorkspace(
29782978
introSelected: OnyxEntry<IntroSelected>,
29792979
workspaceName: string,
2980+
currentUserAccountID: number,
2981+
currentUserEmail: string,
29802982
policyOwnerEmail = '',
29812983
makeMeAdmin = false,
29822984
policyID = generatePolicyID(),
@@ -3002,13 +3004,13 @@ function createDraftWorkspace(
30023004
type: CONST.POLICY.TYPE.TEAM,
30033005
name: workspaceName,
30043006
role: CONST.POLICY.ROLE.ADMIN,
3005-
owner: deprecatedSessionEmail,
3006-
ownerAccountID: deprecatedSessionAccountID,
3007+
owner: currentUserEmail,
3008+
ownerAccountID: currentUserAccountID,
30073009
isPolicyExpenseChatEnabled: true,
30083010
outputCurrency,
30093011
pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD,
30103012
autoReporting: true,
3011-
approver: deprecatedSessionEmail,
3013+
approver: currentUserEmail,
30123014
autoReportingFrequency: shouldEnableWorkflowsByDefault ? CONST.POLICY.AUTO_REPORTING_FREQUENCIES.IMMEDIATE : CONST.POLICY.AUTO_REPORTING_FREQUENCIES.INSTANT,
30133015
harvesting: {
30143016
enabled: !shouldEnableWorkflowsByDefault,
@@ -3024,9 +3026,9 @@ function createDraftWorkspace(
30243026
areConnectionsEnabled: false,
30253027
areExpensifyCardsEnabled: false,
30263028
employeeList: {
3027-
[deprecatedSessionEmail]: {
3028-
submitsTo: deprecatedSessionEmail,
3029-
email: deprecatedSessionEmail,
3029+
[currentUserEmail]: {
3030+
submitsTo: currentUserEmail,
3031+
email: currentUserEmail,
30303032
role: CONST.POLICY.ROLE.ADMIN,
30313033
errors: {},
30323034
},

src/pages/ReportDetailsPage.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,8 @@ function ReportDetailsPage({policy, report, route, reportMetadata}: ReportDetail
469469
isRestrictedToPreferredPolicy,
470470
preferredPolicyID,
471471
transaction: iouTransaction,
472+
currentUserAccountID: currentUserPersonalDetails.accountID,
473+
currentUserEmail: currentUserPersonalDetails.email ?? '',
472474
});
473475
},
474476
});
@@ -491,6 +493,8 @@ function ReportDetailsPage({policy, report, route, reportMetadata}: ReportDetail
491493
amountOwed,
492494
ownerBillingGracePeriodEnd,
493495
transaction: iouTransaction,
496+
currentUserAccountID: currentUserPersonalDetails.accountID,
497+
currentUserEmail: currentUserPersonalDetails.email ?? '',
494498
});
495499
},
496500
});
@@ -512,6 +516,8 @@ function ReportDetailsPage({policy, report, route, reportMetadata}: ReportDetail
512516
amountOwed,
513517
ownerBillingGracePeriodEnd,
514518
transaction: iouTransaction,
519+
currentUserAccountID: currentUserPersonalDetails.accountID,
520+
currentUserEmail: currentUserPersonalDetails.email ?? '',
515521
});
516522
},
517523
});

src/pages/Travel/WorkspaceConfirmationForTravelPage.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,17 @@ function WorkspaceConfirmationForTravelPage({route}: WorkspaceConfirmationForTra
3030
};
3131

3232
const onSubmit = (params: WorkspaceConfirmationSubmitFunctionParams) => {
33-
createDraftWorkspace(introSelected, params.name, '', false, params.policyID, params.currency, params.avatarFile as File);
33+
createDraftWorkspace(
34+
introSelected,
35+
params.name,
36+
currentUserPersonalDetails.accountID,
37+
currentUserPersonalDetails.email ?? '',
38+
'',
39+
false,
40+
params.policyID,
41+
params.currency,
42+
params.avatarFile as File,
43+
);
3444
createWorkspace({
3545
policyName: params.name,
3646
policyID: params.policyID,

src/pages/inbox/report/PureReportActionItem.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -977,6 +977,8 @@ function PureReportActionItem({
977977
isRestrictedToPreferredPolicy,
978978
preferredPolicyID,
979979
transaction: trackExpenseTransaction,
980+
currentUserAccountID: personalDetail.accountID,
981+
currentUserEmail: personalDetail.email ?? '',
980982
});
981983
},
982984
},
@@ -999,6 +1001,8 @@ function PureReportActionItem({
9991001
amountOwed,
10001002
ownerBillingGracePeriodEnd,
10011003
transaction: trackExpenseTransaction,
1004+
currentUserAccountID: personalDetail.accountID,
1005+
currentUserEmail: personalDetail.email ?? '',
10021006
});
10031007
},
10041008
},
@@ -1017,6 +1021,8 @@ function PureReportActionItem({
10171021
amountOwed,
10181022
ownerBillingGracePeriodEnd,
10191023
transaction: trackExpenseTransaction,
1024+
currentUserAccountID: personalDetail.accountID,
1025+
currentUserEmail: personalDetail.email ?? '',
10201026
});
10211027
},
10221028
},

src/pages/iou/request/step/IOURequestStepAccountant.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ function IOURequestStepAccountant({
2626
},
2727
}: IOURequestStepAccountantProps) {
2828
const {translate} = useLocalize();
29-
const {login, email = ''} = useCurrentUserPersonalDetails();
29+
const {accountID, login, email = ''} = useCurrentUserPersonalDetails();
3030
const selector = useCallback(
3131
(policies: OnyxCollection<Policy>) => {
3232
return activeAdminPoliciesSelector(policies, login ?? '');
@@ -49,7 +49,14 @@ function IOURequestStepAccountant({
4949
// Sharing with an accountant involves inviting them to the workspace and that requires admin access.
5050
const hasActiveAdminWorkspaces = (adminPolicies?.length ?? 0) > 0;
5151
if (!hasActiveAdminWorkspaces) {
52-
createDraftWorkspaceAndNavigateToConfirmationScreen(introSelected, transactionID, action, newGenerateDefaultWorkspaceName(email, lastWorkspaceNumber, translate));
52+
createDraftWorkspaceAndNavigateToConfirmationScreen(
53+
introSelected,
54+
transactionID,
55+
action,
56+
newGenerateDefaultWorkspaceName(email, lastWorkspaceNumber, translate),
57+
accountID,
58+
email,
59+
);
5360
return;
5461
}
5562

src/pages/iou/request/step/IOURequestStepParticipants.tsx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,12 @@ function IOURequestStepParticipants({
337337
if ((isCategorizing || isShareAction) && numberOfParticipants.current === 0) {
338338
const email = currentUserPersonalDetails.email ?? '';
339339
const lastWorkspaceNumber = lastWorkspaceNumberSelector(allPolicies, email);
340-
const {expenseChatReportID, policyID, policyName} = createDraftWorkspace(introSelected, newGenerateDefaultWorkspaceName(email, lastWorkspaceNumber, translate));
340+
const {expenseChatReportID, policyID, policyName} = createDraftWorkspace(
341+
introSelected,
342+
newGenerateDefaultWorkspaceName(email, lastWorkspaceNumber, translate),
343+
currentUserPersonalDetails.accountID,
344+
email,
345+
);
341346
for (const transaction of draftTransactions) {
342347
setMoneyRequestParticipants(transaction.transactionID, [
343348
{
@@ -393,17 +398,19 @@ function IOURequestStepParticipants({
393398
participants,
394399
iouType,
395400
initialTransaction,
401+
selfDMReportID,
396402
initialTransactionID,
397403
reportID,
398404
waitForKeyboardDismiss,
399405
draftTransactions,
400406
isMovingTransactionFromTrackExpense,
401407
allPolicies,
402408
policyForMovingExpenses,
409+
currentUserPersonalDetails.email,
410+
currentUserPersonalDetails.accountID,
403411
introSelected,
404-
backTo,
405-
selfDMReportID,
406412
translate,
413+
backTo,
407414
],
408415
);
409416

tests/actions/IOUTest.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,8 @@ describe('actions/IOU', () => {
508508
userBillingGracePeriodEnds: undefined,
509509
amountOwed: 0,
510510
transaction: transactionToCategorize,
511+
currentUserAccountID: RORY_ACCOUNT_ID,
512+
currentUserEmail: RORY_EMAIL,
511513
});
512514
await waitForBatchedUpdates();
513515

@@ -556,6 +558,8 @@ describe('actions/IOU', () => {
556558
userBillingGracePeriodEnds: undefined,
557559
amountOwed: 0,
558560
transaction: originalTransaction,
561+
currentUserAccountID: RORY_ACCOUNT_ID,
562+
currentUserEmail: RORY_EMAIL,
559563
});
560564
await waitForBatchedUpdates();
561565

@@ -593,6 +597,8 @@ describe('actions/IOU', () => {
593597
userBillingGracePeriodEnds: undefined,
594598
amountOwed: 0,
595599
transaction: undefined,
600+
currentUserAccountID: RORY_ACCOUNT_ID,
601+
currentUserEmail: RORY_EMAIL,
596602
});
597603
await waitForBatchedUpdates();
598604

@@ -625,6 +631,8 @@ describe('actions/IOU', () => {
625631
transaction,
626632
userBillingGracePeriodEnds: undefined,
627633
amountOwed: 0,
634+
currentUserAccountID: RORY_ACCOUNT_ID,
635+
currentUserEmail: RORY_EMAIL,
628636
});
629637
await waitForBatchedUpdates();
630638

tests/actions/IOUTest/TrackExpenseTest.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,8 @@ describe('actions/IOU/TrackExpense', () => {
268268
userBillingGracePeriodEnds: undefined,
269269
amountOwed: 0,
270270
transaction,
271+
currentUserAccountID: RORY_ACCOUNT_ID,
272+
currentUserEmail: RORY_EMAIL,
271273
});
272274
await waitForBatchedUpdates();
273275

@@ -841,6 +843,8 @@ describe('actions/IOU/TrackExpense', () => {
841843
userBillingGracePeriodEnds: undefined,
842844
amountOwed: 0,
843845
transaction: createdTransaction,
846+
currentUserAccountID: RORY_ACCOUNT_ID,
847+
currentUserEmail: RORY_EMAIL,
844848
});
845849
await waitForBatchedUpdates();
846850

tests/actions/PolicyTest.ts

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1924,7 +1924,16 @@ describe('actions/Policy', () => {
19241924
await waitForBatchedUpdates();
19251925

19261926
const policyID = Policy.generatePolicyID();
1927-
const params = Policy.createDraftWorkspace({choice: CONST.ONBOARDING_CHOICES.MANAGE_TEAM}, WORKSPACE_NAME, ESH_EMAIL, true, policyID, CONST.CURRENCY.USD);
1927+
const params = Policy.createDraftWorkspace(
1928+
{choice: CONST.ONBOARDING_CHOICES.MANAGE_TEAM},
1929+
WORKSPACE_NAME,
1930+
ESH_ACCOUNT_ID,
1931+
ESH_EMAIL,
1932+
ESH_EMAIL,
1933+
true,
1934+
policyID,
1935+
CONST.CURRENCY.USD,
1936+
);
19281937
await waitForBatchedUpdates();
19291938

19301939
const draft = await getOnyxValue(`${ONYXKEYS.COLLECTION.POLICY_DRAFTS}${policyID}`);
@@ -1952,7 +1961,7 @@ describe('actions/Policy', () => {
19521961
await waitForBatchedUpdates();
19531962

19541963
const policyID = Policy.generatePolicyID();
1955-
Policy.createDraftWorkspace({choice: CONST.ONBOARDING_CHOICES.TRACK_WORKSPACE}, WORKSPACE_NAME, ESH_EMAIL, false, policyID, CONST.CURRENCY.EUR);
1964+
Policy.createDraftWorkspace({choice: CONST.ONBOARDING_CHOICES.TRACK_WORKSPACE}, WORKSPACE_NAME, ESH_ACCOUNT_ID, ESH_EMAIL, ESH_EMAIL, false, policyID, CONST.CURRENCY.EUR);
19561965
await waitForBatchedUpdates();
19571966

19581967
const draft = await getOnyxValue(`${ONYXKEYS.COLLECTION.POLICY_DRAFTS}${policyID}`);
@@ -1963,6 +1972,37 @@ describe('actions/Policy', () => {
19631972
expect(draft?.harvesting?.enabled).toBe(true);
19641973
expect(draft?.outputCurrency).toBe(CONST.CURRENCY.EUR);
19651974
});
1975+
1976+
it('should set owner, ownerAccountID, approver, and employeeList from explicit parameters instead of Onyx session', async () => {
1977+
// Set Onyx session to a DIFFERENT accountID/email to verify the explicit parameters are used
1978+
await Onyx.set(ONYXKEYS.SESSION, {email: ESH_EMAIL, accountID: ESH_ACCOUNT_ID});
1979+
await waitForBatchedUpdates();
1980+
1981+
const customAccountID = 999;
1982+
const customEmail = 'custom@example.com';
1983+
const policyID = Policy.generatePolicyID();
1984+
1985+
Policy.createDraftWorkspace({choice: CONST.ONBOARDING_CHOICES.MANAGE_TEAM}, WORKSPACE_NAME, customAccountID, customEmail, customEmail, false, policyID);
1986+
await waitForBatchedUpdates();
1987+
1988+
const draft = await getOnyxValue(`${ONYXKEYS.COLLECTION.POLICY_DRAFTS}${policyID}`);
1989+
1990+
// Verify explicit params are used, not the Onyx session values
1991+
expect(draft?.owner).toBe(customEmail);
1992+
expect(draft?.ownerAccountID).toBe(customAccountID);
1993+
expect(draft?.approver).toBe(customEmail);
1994+
expect(draft?.employeeList?.[customEmail]).toEqual({
1995+
submitsTo: customEmail,
1996+
email: customEmail,
1997+
role: CONST.POLICY.ROLE.ADMIN,
1998+
errors: {},
1999+
});
2000+
2001+
// Verify the Onyx session values are NOT used
2002+
expect(draft?.owner).not.toBe(ESH_EMAIL);
2003+
expect(draft?.ownerAccountID).not.toBe(ESH_ACCOUNT_ID);
2004+
expect(draft?.employeeList?.[ESH_EMAIL]).toBeUndefined();
2005+
});
19662006
});
19672007

19682008
describe('upgradeToCorporate', () => {

0 commit comments

Comments
 (0)