Skip to content

Commit 7c72dff

Browse files
authored
Merge pull request Expensify#86951 from DylanDylann/refactor-66579-p2
Part 2: Remove Onyx.connect() for the key: ONYXKEYS.SESSION in src/libs/actions/Policy/Policy.ts
2 parents 425edfe + c0e2b77 commit 7c72dff

4 files changed

Lines changed: 104 additions & 13 deletions

File tree

src/libs/actions/Policy/Policy.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1399,6 +1399,8 @@ function leaveWorkspace(currentUserAccountID: number, currentUserEmail: string,
13991399

14001400
function addBillingCardAndRequestPolicyOwnerChange(
14011401
policyID: string | undefined,
1402+
currentUserAccountID: number,
1403+
currentUserEmail: string,
14021404
cardData: {
14031405
cardNumber: string;
14041406
cardYear: string;
@@ -1436,8 +1438,8 @@ function addBillingCardAndRequestPolicyOwnerChange(
14361438
isLoading: false,
14371439
isChangeOwnerSuccessful: true,
14381440
isChangeOwnerFailed: false,
1439-
owner: deprecatedSessionEmail,
1440-
ownerAccountID: deprecatedSessionAccountID,
1441+
owner: currentUserEmail,
1442+
ownerAccountID: currentUserAccountID,
14411443
},
14421444
},
14431445
];
@@ -1486,7 +1488,7 @@ function addBillingCardAndRequestPolicyOwnerChange(
14861488
* Properly updates the nvp_privateStripeCustomerID onyx data for 3DS payment
14871489
*
14881490
*/
1489-
function verifySetupIntentAndRequestPolicyOwnerChange(policyID: string) {
1491+
function verifySetupIntentAndRequestPolicyOwnerChange(policyID: string, currentUserAccountID: number, currentUserEmail: string) {
14901492
const optimisticData: Array<OnyxUpdate<typeof ONYXKEYS.COLLECTION.POLICY>> = [
14911493
{
14921494
onyxMethod: Onyx.METHOD.MERGE,
@@ -1508,8 +1510,8 @@ function verifySetupIntentAndRequestPolicyOwnerChange(policyID: string) {
15081510
isLoading: false,
15091511
isChangeOwnerSuccessful: true,
15101512
isChangeOwnerFailed: false,
1511-
owner: deprecatedSessionEmail,
1512-
ownerAccountID: deprecatedSessionAccountID,
1513+
owner: currentUserEmail,
1514+
ownerAccountID: currentUserAccountID,
15131515
},
15141516
},
15151517
];
@@ -1525,7 +1527,7 @@ function verifySetupIntentAndRequestPolicyOwnerChange(policyID: string) {
15251527
},
15261528
},
15271529
];
1528-
API.write(WRITE_COMMANDS.VERIFY_SETUP_INTENT_AND_REQUEST_POLICY_OWNER_CHANGE, {accountID: deprecatedSessionAccountID, policyID}, {optimisticData, successData, failureData});
1530+
API.write(WRITE_COMMANDS.VERIFY_SETUP_INTENT_AND_REQUEST_POLICY_OWNER_CHANGE, {accountID: currentUserAccountID, policyID}, {optimisticData, successData, failureData});
15291531
}
15301532

15311533
/**

src/pages/settings/Subscription/CardAuthenticationModal/index.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import FullScreenLoadingIndicator from '@components/FullscreenLoadingIndicator';
44
import HeaderWithBackButton from '@components/HeaderWithBackButton';
55
import Modal from '@components/Modal';
66
import ScreenWrapper from '@components/ScreenWrapper';
7+
import useCurrentUserPersonalDetails from '@hooks/useCurrentUserPersonalDetails';
78
import useOnyx from '@hooks/useOnyx';
89
import useResponsiveLayout from '@hooks/useResponsiveLayout';
910
import useThemeStyles from '@hooks/useThemeStyles';
@@ -29,10 +30,10 @@ function CardAuthenticationModal({headerTitle, policyID}: CardAuthenticationModa
2930
// eslint-disable-next-line rulesdir/prefer-shouldUseNarrowLayout-instead-of-isSmallScreenWidth
3031
const {isSmallScreenWidth} = useResponsiveLayout();
3132
const [authenticationLink] = useOnyx(ONYXKEYS.VERIFY_3DS_SUBSCRIPTION);
32-
const [session] = useOnyx(ONYXKEYS.SESSION);
3333
const [isLoading, setIsLoading] = useState(true);
3434
const [isVisible, setIsVisible] = useState(false);
3535
const reasonAttributes: SkeletonSpanReasonAttributes = {context: 'CardAuthenticationModal', isLoading};
36+
const {accountID: currentUserAccountID, email: currentUserEmail = ''} = useCurrentUserPersonalDetails();
3637

3738
const onModalClose = useCallback(() => {
3839
setIsVisible(false);
@@ -54,14 +55,14 @@ function CardAuthenticationModal({headerTitle, policyID}: CardAuthenticationModa
5455
const message = event.data;
5556
if (message === CONST.SCA_AUTHENTICATION_COMPLETE) {
5657
if (policyID) {
57-
verifySetupIntentAndRequestPolicyOwnerChange(policyID);
58+
verifySetupIntentAndRequestPolicyOwnerChange(policyID, currentUserAccountID, currentUserEmail);
5859
} else {
59-
verifySetupIntent(session?.accountID ?? CONST.DEFAULT_NUMBER_ID, true);
60+
verifySetupIntent(currentUserAccountID, true);
6061
}
6162
onModalClose();
6263
}
6364
},
64-
[onModalClose, policyID, session?.accountID],
65+
[currentUserAccountID, currentUserEmail, onModalClose, policyID],
6566
);
6667

6768
useEffect(() => {

src/pages/workspace/members/WorkspaceOwnerPaymentCardForm.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import type {IllustrationName} from '@components/Icon/IllustrationLoader';
99
import RenderHTML from '@components/RenderHTML';
1010
import Section, {CARD_LAYOUT} from '@components/Section';
1111
import Text from '@components/Text';
12+
import useCurrentUserPersonalDetails from '@hooks/useCurrentUserPersonalDetails';
1213
import {useMemoizedLazyAsset, useMemoizedLazyExpensifyIcons} from '@hooks/useLazyAsset';
1314
import useLocalize from '@hooks/useLocalize';
1415
import useTheme from '@hooks/useTheme';
@@ -32,6 +33,7 @@ function WorkspaceOwnerPaymentCardForm({policy}: WorkspaceOwnerPaymentCardFormPr
3233
const [shouldShowPaymentCardForm, setShouldShowPaymentCardForm] = useState(false);
3334
const {asset: ShieldYellow} = useMemoizedLazyAsset(() => loadIllustration('ShieldYellow' as IllustrationName));
3435
const policyID = policy?.id;
36+
const {accountID: currentUserAccountID, email: currentUserEmail = ''} = useCurrentUserPersonalDetails();
3537

3638
const checkIfCanBeRendered = useCallback(() => {
3739
const changeOwnerErrors = Object.keys(policy?.errorFields?.changeOwner ?? {});
@@ -70,9 +72,9 @@ function WorkspaceOwnerPaymentCardForm({policy}: WorkspaceOwnerPaymentCardFormPr
7072
addressZip: values.addressZipCode,
7173
currency: values.currency,
7274
};
73-
addBillingCardAndRequestPolicyOwnerChange(policyID, cardData);
75+
addBillingCardAndRequestPolicyOwnerChange(policyID, currentUserAccountID, currentUserEmail, cardData);
7476
},
75-
[policyID],
77+
[currentUserAccountID, currentUserEmail, policyID],
7678
);
7779
const icons = useMemoizedLazyExpensifyIcons(['Checkmark']);
7880

tests/actions/PolicyMemberTest.ts

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ describe('actions/PolicyMember', () => {
241241
mockFetch?.pause?.();
242242
Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy);
243243
Onyx.merge(ONYXKEYS.SESSION, {email: fakeEmail, accountID: fakeAccountID});
244-
Policy.addBillingCardAndRequestPolicyOwnerChange(fakePolicy.id, fakeCard);
244+
Policy.addBillingCardAndRequestPolicyOwnerChange(fakePolicy.id, fakeAccountID, fakeEmail, fakeCard);
245245
await waitForBatchedUpdates();
246246
await new Promise<void>((resolve) => {
247247
const connection = Onyx.connect({
@@ -273,6 +273,92 @@ describe('actions/PolicyMember', () => {
273273
});
274274
});
275275
});
276+
277+
it('should set owner and ownerAccountID from explicit parameters on success', async () => {
278+
const fakePolicy: PolicyType = createRandomPolicy(0);
279+
const fakeEmail = 'newowner@gmail.com';
280+
const fakeAccountID = 42;
281+
const fakeCard = {
282+
cardNumber: '1234567890123456',
283+
cardYear: '2023',
284+
cardMonth: '05',
285+
cardCVV: '123',
286+
addressName: 'John Doe',
287+
addressZip: '12345',
288+
currency: 'USD',
289+
};
290+
291+
await Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy);
292+
Policy.addBillingCardAndRequestPolicyOwnerChange(fakePolicy.id, fakeAccountID, fakeEmail, fakeCard);
293+
await waitForBatchedUpdates();
294+
await new Promise<void>((resolve) => {
295+
const connection = Onyx.connect({
296+
key: `${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`,
297+
waitForCollectionCallback: false,
298+
callback: (policy) => {
299+
Onyx.disconnect(connection);
300+
expect(policy?.isLoading).toBeFalsy();
301+
expect(policy?.isChangeOwnerSuccessful).toBeTruthy();
302+
expect(policy?.owner).toBe(fakeEmail);
303+
expect(policy?.ownerAccountID).toBe(fakeAccountID);
304+
resolve();
305+
},
306+
});
307+
});
308+
});
309+
});
310+
311+
describe('verifySetupIntentAndRequestPolicyOwnerChange', () => {
312+
it('should set optimistic loading state', async () => {
313+
const fakePolicy: PolicyType = createRandomPolicy(0);
314+
const fakeEmail = 'newowner@gmail.com';
315+
const fakeAccountID = 42;
316+
317+
mockFetch?.pause?.();
318+
await Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy);
319+
Policy.verifySetupIntentAndRequestPolicyOwnerChange(fakePolicy.id, fakeAccountID, fakeEmail);
320+
await waitForBatchedUpdates();
321+
await new Promise<void>((resolve) => {
322+
const connection = Onyx.connect({
323+
key: `${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`,
324+
waitForCollectionCallback: false,
325+
callback: (policy) => {
326+
Onyx.disconnect(connection);
327+
expect(policy?.errorFields).toBeFalsy();
328+
expect(policy?.isLoading).toBeTruthy();
329+
expect(policy?.isChangeOwnerSuccessful).toBeFalsy();
330+
expect(policy?.isChangeOwnerFailed).toBeFalsy();
331+
resolve();
332+
},
333+
});
334+
});
335+
await mockFetch?.resume?.();
336+
});
337+
338+
it('should set owner and ownerAccountID from explicit parameters on success', async () => {
339+
const fakePolicy: PolicyType = createRandomPolicy(0);
340+
const fakeEmail = 'newowner@gmail.com';
341+
const fakeAccountID = 42;
342+
343+
await Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy);
344+
Policy.verifySetupIntentAndRequestPolicyOwnerChange(fakePolicy.id, fakeAccountID, fakeEmail);
345+
await waitForBatchedUpdates();
346+
await new Promise<void>((resolve) => {
347+
const connection = Onyx.connect({
348+
key: `${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`,
349+
waitForCollectionCallback: false,
350+
callback: (policy) => {
351+
Onyx.disconnect(connection);
352+
expect(policy?.isLoading).toBeFalsy();
353+
expect(policy?.isChangeOwnerSuccessful).toBeTruthy();
354+
expect(policy?.isChangeOwnerFailed).toBeFalsy();
355+
expect(policy?.owner).toBe(fakeEmail);
356+
expect(policy?.ownerAccountID).toBe(fakeAccountID);
357+
resolve();
358+
},
359+
});
360+
});
361+
});
276362
});
277363

278364
describe('addMembersToWorkspace', () => {

0 commit comments

Comments
 (0)