Skip to content

Commit fb2fa72

Browse files
DylanDylannclaude
andcommitted
refactor: pass explicit currentUserAccountID/Email to createDraftInitialWorkspace
Replace deprecated module-level session variables (deprecatedSessionAccountID, deprecatedSessionEmail) with explicit parameters in createDraftInitialWorkspace. Update callers in App.ts and add unit tests verifying the explicit params are used. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent b96c699 commit fb2fa72

3 files changed

Lines changed: 74 additions & 9 deletions

File tree

src/libs/actions/App.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,7 @@ function createWorkspaceWithPolicyDraftAndNavigateToIt(params: CreateWorkspaceWi
600600
} = params;
601601

602602
const policyIDWithDefault = policyID || generatePolicyID();
603-
createDraftInitialWorkspace(introSelected, policyName, policyIDWithDefault, makeMeAdmin, currency, file, type);
603+
createDraftInitialWorkspace(introSelected, policyName, currentUserAccountIDParam, currentUserEmailParam, policyIDWithDefault, makeMeAdmin, currency, file, type);
604604
Navigation.isNavigationReady().then(() => {
605605
if (transitionFromOldDot) {
606606
// We must call goBack() to remove the /transition route from history
@@ -649,7 +649,7 @@ function createWorkspaceWithPolicyDraft(params: CreateWorkspaceWithPolicyDraftPa
649649
hasActiveAdminPolicies,
650650
} = params;
651651

652-
createDraftInitialWorkspace(introSelected, policyName, policyID, makeMeAdmin, currency, file);
652+
createDraftInitialWorkspace(introSelected, policyName, currentUserAccountIDParam, currentUserEmailParam, policyID, makeMeAdmin, currency, file);
653653
savePolicyDraftByNewWorkspace({
654654
policyID,
655655
policyName,

src/libs/actions/Policy/Policy.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2314,6 +2314,8 @@ function buildOptimisticDistanceRateCustomUnits(reportCurrency?: string): Optimi
23142314
function createDraftInitialWorkspace(
23152315
introSelected: OnyxEntry<IntroSelected>,
23162316
workspaceName: string,
2317+
currentUserAccountID: number,
2318+
currentUserEmail: string,
23172319
policyID = generatePolicyID(),
23182320
makeMeAdmin = false,
23192321
currency = '',
@@ -2333,11 +2335,11 @@ function createDraftInitialWorkspace(
23332335
type: type || CONST.POLICY.TYPE.TEAM,
23342336
name: workspaceName,
23352337
role: CONST.POLICY.ROLE.ADMIN,
2336-
owner: deprecatedSessionEmail,
2337-
ownerAccountID: deprecatedSessionAccountID,
2338+
owner: currentUserEmail,
2339+
ownerAccountID: currentUserAccountID,
23382340
isPolicyExpenseChatEnabled: true,
23392341
areCategoriesEnabled: true,
2340-
approver: deprecatedSessionEmail,
2342+
approver: currentUserEmail,
23412343
areCompanyCardsEnabled: true,
23422344
areExpensifyCardsEnabled: false,
23432345
outputCurrency,
@@ -2352,9 +2354,9 @@ function createDraftInitialWorkspace(
23522354
},
23532355
originalFileName: file?.name,
23542356
employeeList: {
2355-
[deprecatedSessionEmail]: {
2356-
submitsTo: deprecatedSessionEmail,
2357-
email: deprecatedSessionEmail,
2357+
[currentUserEmail]: {
2358+
submitsTo: currentUserEmail,
2359+
email: currentUserEmail,
23582360
role: CONST.POLICY.ROLE.ADMIN,
23592361
errors: {},
23602362
},

tests/actions/PolicyTest.ts

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1843,7 +1843,7 @@ describe('actions/Policy', () => {
18431843

18441844
const policyID = Policy.generatePolicyID();
18451845

1846-
Policy.createDraftInitialWorkspace({choice: CONST.ONBOARDING_CHOICES.TRACK_WORKSPACE}, WORKSPACE_NAME, policyID, false, CONST.CURRENCY.EUR);
1846+
Policy.createDraftInitialWorkspace({choice: CONST.ONBOARDING_CHOICES.TRACK_WORKSPACE}, WORKSPACE_NAME, ESH_ACCOUNT_ID, ESH_EMAIL, policyID, false, CONST.CURRENCY.EUR);
18471847
await waitForBatchedUpdates();
18481848

18491849
const draft = await getOnyxValue(`${ONYXKEYS.COLLECTION.POLICY_DRAFTS}${policyID}`);
@@ -1853,6 +1853,69 @@ describe('actions/Policy', () => {
18531853
expect(draft?.harvesting?.enabled).toBe(true);
18541854
expect(draft?.outputCurrency).toBe(CONST.CURRENCY.EUR);
18551855
});
1856+
1857+
it('should set owner and ownerAccountID from explicit parameters', async () => {
1858+
// Set Onyx session to a DIFFERENT accountID/email to verify the explicit parameters are used
1859+
await Onyx.set(ONYXKEYS.SESSION, {email: ESH_EMAIL, accountID: ESH_ACCOUNT_ID});
1860+
await waitForBatchedUpdates();
1861+
1862+
const customAccountID = 999;
1863+
const customEmail = 'custom@example.com';
1864+
const policyID = Policy.generatePolicyID();
1865+
1866+
Policy.createDraftInitialWorkspace({choice: CONST.ONBOARDING_CHOICES.MANAGE_TEAM}, WORKSPACE_NAME, customAccountID, customEmail, policyID);
1867+
await waitForBatchedUpdates();
1868+
1869+
const draft = await getOnyxValue(`${ONYXKEYS.COLLECTION.POLICY_DRAFTS}${policyID}`);
1870+
1871+
// Verify owner and ownerAccountID use the explicit parameters, not the Onyx session
1872+
expect(draft?.owner).toBe(customEmail);
1873+
expect(draft?.ownerAccountID).toBe(customAccountID);
1874+
1875+
// Verify that the Onyx session values are NOT used
1876+
expect(draft?.owner).not.toBe(ESH_EMAIL);
1877+
expect(draft?.ownerAccountID).not.toBe(ESH_ACCOUNT_ID);
1878+
});
1879+
1880+
it('should set approver from explicit currentUserEmail parameter', async () => {
1881+
await Onyx.set(ONYXKEYS.SESSION, {email: ESH_EMAIL, accountID: ESH_ACCOUNT_ID});
1882+
await waitForBatchedUpdates();
1883+
1884+
const customAccountID = 888;
1885+
const customEmail = 'approver@example.com';
1886+
const policyID = Policy.generatePolicyID();
1887+
1888+
Policy.createDraftInitialWorkspace({choice: CONST.ONBOARDING_CHOICES.MANAGE_TEAM}, WORKSPACE_NAME, customAccountID, customEmail, policyID);
1889+
await waitForBatchedUpdates();
1890+
1891+
const draft = await getOnyxValue(`${ONYXKEYS.COLLECTION.POLICY_DRAFTS}${policyID}`);
1892+
1893+
expect(draft?.approver).toBe(customEmail);
1894+
expect(draft?.approver).not.toBe(ESH_EMAIL);
1895+
});
1896+
1897+
it('should set employeeList using explicit currentUserEmail parameter', async () => {
1898+
await Onyx.set(ONYXKEYS.SESSION, {email: ESH_EMAIL, accountID: ESH_ACCOUNT_ID});
1899+
await waitForBatchedUpdates();
1900+
1901+
const customAccountID = 777;
1902+
const customEmail = 'employee@example.com';
1903+
const policyID = Policy.generatePolicyID();
1904+
1905+
Policy.createDraftInitialWorkspace({choice: CONST.ONBOARDING_CHOICES.MANAGE_TEAM}, WORKSPACE_NAME, customAccountID, customEmail, policyID);
1906+
await waitForBatchedUpdates();
1907+
1908+
const draft = await getOnyxValue(`${ONYXKEYS.COLLECTION.POLICY_DRAFTS}${policyID}`);
1909+
1910+
// Verify the employeeList uses the explicit email parameter
1911+
expect(draft?.employeeList?.[customEmail]).toBeDefined();
1912+
expect(draft?.employeeList?.[customEmail]?.email).toBe(customEmail);
1913+
expect(draft?.employeeList?.[customEmail]?.submitsTo).toBe(customEmail);
1914+
expect(draft?.employeeList?.[customEmail]?.role).toBe(CONST.POLICY.ROLE.ADMIN);
1915+
1916+
// Verify the Onyx session email is NOT used in employeeList
1917+
expect(draft?.employeeList?.[ESH_EMAIL]).toBeUndefined();
1918+
});
18561919
});
18571920

18581921
describe('createDraftWorkspace', () => {

0 commit comments

Comments
 (0)