Skip to content

Commit 1744268

Browse files
DylanDylannclaude
andcommitted
refactor: pass explicit currentUserAccountID/Email to createDraftWorkspace
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent fb2fa72 commit 1744268

5 files changed

Lines changed: 73 additions & 11 deletions

File tree

src/libs/ReportUtils.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11328,7 +11328,12 @@ function canLeaveChat(report: OnyxEntry<Report>, policy: OnyxEntry<Policy>, isRe
1132811328

1132911329
function createDraftWorkspaceAndNavigateToConfirmationScreen(introSelected: OnyxEntry<IntroSelected>, transactionID: string, actionName: IOUAction, workspaceName: string): void {
1133011330
const isCategorizing = actionName === CONST.IOU.ACTION.CATEGORIZE;
11331-
const {expenseChatReportID, policyID, policyName} = createDraftWorkspace(introSelected, workspaceName, deprecatedCurrentUserEmail);
11331+
const {expenseChatReportID, policyID, policyName} = createDraftWorkspace(
11332+
introSelected,
11333+
workspaceName,
11334+
deprecatedCurrentUserAccountID ?? CONST.DEFAULT_NUMBER_ID,
11335+
deprecatedCurrentUserEmail ?? '',
11336+
);
1133211337
setMoneyRequestParticipants(transactionID, [
1133311338
{
1133411339
selected: true,

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/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/iou/request/step/IOURequestStepParticipants.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,12 @@ function IOURequestStepParticipants({
336336
if ((isCategorizing || isShareAction) && numberOfParticipants.current === 0) {
337337
const email = currentUserPersonalDetails.email ?? '';
338338
const lastWorkspaceNumber = lastWorkspaceNumberSelector(allPolicies, email);
339-
const {expenseChatReportID, policyID, policyName} = createDraftWorkspace(introSelected, newGenerateDefaultWorkspaceName(email, lastWorkspaceNumber, translate));
339+
const {expenseChatReportID, policyID, policyName} = createDraftWorkspace(
340+
introSelected,
341+
newGenerateDefaultWorkspaceName(email, lastWorkspaceNumber, translate),
342+
currentUserPersonalDetails.accountID,
343+
email,
344+
);
340345
for (const transaction of draftTransactions) {
341346
setMoneyRequestParticipants(transaction.transactionID, [
342347
{

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)