From e53f0bfee86c3a3bc1eff0efa4d6b765916d7c83 Mon Sep 17 00:00:00 2001
From: VickyStash <vikstash@gmail.com>
Date: Fri, 30 Aug 2024 12:07:04 +0200
Subject: [PATCH 1/4] Uncomment isMonthlySettlementAllowed related code

---
 .../workspace/expensifyCard/WorkspaceCardSettingsPage.tsx | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)

diff --git a/src/pages/workspace/expensifyCard/WorkspaceCardSettingsPage.tsx b/src/pages/workspace/expensifyCard/WorkspaceCardSettingsPage.tsx
index 4e295c66060a..4b6e267cac60 100644
--- a/src/pages/workspace/expensifyCard/WorkspaceCardSettingsPage.tsx
+++ b/src/pages/workspace/expensifyCard/WorkspaceCardSettingsPage.tsx
@@ -32,11 +32,9 @@ function WorkspaceCardSettingsPage({route}: WorkspaceCardSettingsPageProps) {
     const [cardSettings] = useOnyx(`${ONYXKEYS.COLLECTION.PRIVATE_EXPENSIFY_CARD_SETTINGS}${workspaceAccountID}`);
 
     const paymentBankAccountID = cardSettings?.paymentBankAccountID ?? 0;
-    // const isMonthlySettlementAllowed = cardSettings?.isMonthlySettlementAllowed ?? true;
+    const isMonthlySettlementAllowed = cardSettings?.isMonthlySettlementAllowed;
     const settlementFrequency = cardSettings?.monthlySettlementDate ? CONST.EXPENSIFY_CARD.FREQUENCY_SETTING.MONTHLY : CONST.EXPENSIFY_CARD.FREQUENCY_SETTING.DAILY;
-    // TODO: replace this line with the following line and uncomment the previous comment
-    const isSettlementFrequencyBlocked = settlementFrequency === CONST.EXPENSIFY_CARD.FREQUENCY_SETTING.DAILY;
-    // const isSettlementFrequencyBlocked = !isMonthlySettlementAllowed && settlementFrequency === CONST.EXPENSIFY_CARD.FREQUENCY_SETTING.DAILY;
+    const isSettlementFrequencyBlocked = !isMonthlySettlementAllowed && settlementFrequency === CONST.EXPENSIFY_CARD.FREQUENCY_SETTING.DAILY;
     const bankAccountNumber = bankAccountList?.[paymentBankAccountID.toString()]?.accountData?.accountNumber ?? '';
 
     return (
@@ -66,7 +64,7 @@ function WorkspaceCardSettingsPage({route}: WorkspaceCardSettingsPageProps) {
                                 description={translate('workspace.expensifyCard.settlementFrequency')}
                                 title={translate(`workspace.expensifyCard.frequency.${settlementFrequency}`)}
                                 shouldShowRightIcon={settlementFrequency !== CONST.EXPENSIFY_CARD.FREQUENCY_SETTING.DAILY}
-                                // interactive={!isSettlementFrequencyBlocked}
+                                interactive={!isSettlementFrequencyBlocked}
                                 onPress={() => Navigation.navigate(ROUTES.WORKSPACE_EXPENSIFY_CARD_SETTINGS_FREQUENCY.getRoute(policyID))}
                                 hintText={
                                     isSettlementFrequencyBlocked ? (

From 881bc10d24e29960858e4412344b52bc18d09f77 Mon Sep 17 00:00:00 2001
From: VickyStash <vikstash@gmail.com>
Date: Fri, 30 Aug 2024 12:42:29 +0200
Subject: [PATCH 2/4] Go back to the workspace member details page if the card
 creation from was initiated from there

---
 src/ROUTES.ts                                     |  2 +-
 src/libs/Navigation/types.ts                      |  1 +
 .../WorkspaceExpensifyCardListPage.tsx            |  7 ++++++-
 .../expensifyCard/issueNew/ConfirmationStep.tsx   | 10 +++++++---
 .../expensifyCard/issueNew/IssueNewCardPage.tsx   | 15 +++++++++++++--
 .../members/WorkspaceMemberDetailsPage.tsx        |  4 +++-
 src/pages/workspace/withPolicy.tsx                |  1 +
 7 files changed, 32 insertions(+), 8 deletions(-)

diff --git a/src/ROUTES.ts b/src/ROUTES.ts
index c532970824b0..e9f797e23c26 100644
--- a/src/ROUTES.ts
+++ b/src/ROUTES.ts
@@ -917,7 +917,7 @@ const ROUTES = {
     },
     WORKSPACE_EXPENSIFY_CARD_ISSUE_NEW: {
         route: 'settings/workspaces/:policyID/expensify-card/issue-new',
-        getRoute: (policyID: string) => `settings/workspaces/${policyID}/expensify-card/issue-new` as const,
+        getRoute: (policyID: string, backTo?: string) => getUrlWithBackToParam(`settings/workspaces/${policyID}/expensify-card/issue-new`, backTo),
     },
     WORKSPACE_EXPENSIFY_CARD_BANK_ACCOUNT: {
         route: 'settings/workspaces/:policyID/expensify-card/choose-bank-account',
diff --git a/src/libs/Navigation/types.ts b/src/libs/Navigation/types.ts
index 28544da90423..2c41c7809c78 100644
--- a/src/libs/Navigation/types.ts
+++ b/src/libs/Navigation/types.ts
@@ -676,6 +676,7 @@ type SettingsNavigatorParamList = {
     };
     [SCREENS.WORKSPACE.EXPENSIFY_CARD_ISSUE_NEW]: {
         policyID: string;
+        backTo?: Routes;
     };
     [SCREENS.WORKSPACE.EXPENSIFY_CARD_BANK_ACCOUNT]: {
         policyID: string;
diff --git a/src/pages/workspace/expensifyCard/WorkspaceExpensifyCardListPage.tsx b/src/pages/workspace/expensifyCard/WorkspaceExpensifyCardListPage.tsx
index c27d5a7168ba..bb25ca4e90a6 100644
--- a/src/pages/workspace/expensifyCard/WorkspaceExpensifyCardListPage.tsx
+++ b/src/pages/workspace/expensifyCard/WorkspaceExpensifyCardListPage.tsx
@@ -49,12 +49,17 @@ function WorkspaceExpensifyCardListPage({route, cardsList}: WorkspaceExpensifyCa
 
     const sortedCards = useMemo(() => CardUtils.sortCardsByCardholderName(cardsList, personalDetails), [cardsList, personalDetails]);
 
+    const issueCard = () => {
+        const activeRoute = Navigation.getActiveRoute();
+        Navigation.navigate(ROUTES.WORKSPACE_EXPENSIFY_CARD_ISSUE_NEW.getRoute(policyID, activeRoute));
+    };
+
     const getHeaderButtons = () => (
         <View style={[styles.w100, styles.flexRow, styles.gap2, shouldUseNarrowLayout && styles.mb3]}>
             <Button
                 medium
                 success
-                onPress={() => Navigation.navigate(ROUTES.WORKSPACE_EXPENSIFY_CARD_ISSUE_NEW.getRoute(policyID))}
+                onPress={issueCard}
                 icon={Expensicons.Plus}
                 text={translate('workspace.expensifyCard.issueCard')}
                 style={shouldUseNarrowLayout && styles.flex1}
diff --git a/src/pages/workspace/expensifyCard/issueNew/ConfirmationStep.tsx b/src/pages/workspace/expensifyCard/issueNew/ConfirmationStep.tsx
index aed246227d7d..d6f1575dcb58 100644
--- a/src/pages/workspace/expensifyCard/issueNew/ConfirmationStep.tsx
+++ b/src/pages/workspace/expensifyCard/issueNew/ConfirmationStep.tsx
@@ -19,14 +19,18 @@ import * as Card from '@userActions/Card';
 import CONST from '@src/CONST';
 import ONYXKEYS from '@src/ONYXKEYS';
 import ROUTES from '@src/ROUTES';
+import type {Route} from '@src/ROUTES';
 import type {IssueNewCardStep} from '@src/types/onyx/Card';
 
 type ConfirmationStepProps = {
-    // ID of the policy that the card will be issued under
+    /** ID of the policy that the card will be issued under */
     policyID: string;
+
+    /** Route to navigate to */
+    backTo?: Route;
 };
 
-function ConfirmationStep({policyID}: ConfirmationStepProps) {
+function ConfirmationStep({policyID, backTo}: ConfirmationStepProps) {
     const {translate} = useLocalize();
     const styles = useThemeStyles();
     const {isOffline} = useNetwork();
@@ -37,7 +41,7 @@ function ConfirmationStep({policyID}: ConfirmationStepProps) {
 
     const submit = () => {
         Card.issueExpensifyCard(policyID, CONST.COUNTRY.US, data);
-        Navigation.navigate(ROUTES.WORKSPACE_EXPENSIFY_CARD.getRoute(policyID ?? '-1'));
+        Navigation.navigate(backTo ?? ROUTES.WORKSPACE_EXPENSIFY_CARD.getRoute(policyID ?? '-1'));
         Card.clearIssueNewCardFlow();
     };
 
diff --git a/src/pages/workspace/expensifyCard/issueNew/IssueNewCardPage.tsx b/src/pages/workspace/expensifyCard/issueNew/IssueNewCardPage.tsx
index f1cd737eed46..23daf00534f1 100644
--- a/src/pages/workspace/expensifyCard/issueNew/IssueNewCardPage.tsx
+++ b/src/pages/workspace/expensifyCard/issueNew/IssueNewCardPage.tsx
@@ -1,10 +1,13 @@
+import type {StackScreenProps} from '@react-navigation/stack';
 import React, {useEffect} from 'react';
 import {useOnyx} from 'react-native-onyx';
+import type {SettingsNavigatorParamList} from '@navigation/types';
 import type {WithPolicyAndFullscreenLoadingProps} from '@pages/workspace/withPolicyAndFullscreenLoading';
 import withPolicyAndFullscreenLoading from '@pages/workspace/withPolicyAndFullscreenLoading';
 import * as Card from '@userActions/Card';
 import CONST from '@src/CONST';
 import ONYXKEYS from '@src/ONYXKEYS';
+import type SCREENS from '@src/SCREENS';
 import AssigneeStep from './AssigneeStep';
 import CardNameStep from './CardNameStep';
 import CardTypeStep from './CardTypeStep';
@@ -12,12 +15,15 @@ import ConfirmationStep from './ConfirmationStep';
 import LimitStep from './LimitStep';
 import LimitTypeStep from './LimitTypeStep';
 
-function IssueNewCardPage({policy}: WithPolicyAndFullscreenLoadingProps) {
+type IssueNewCardPageProps = WithPolicyAndFullscreenLoadingProps & StackScreenProps<SettingsNavigatorParamList, typeof SCREENS.WORKSPACE.EXPENSIFY_CARD_ISSUE_NEW>;
+
+function IssueNewCardPage({policy, route}: IssueNewCardPageProps) {
     const [issueNewCard] = useOnyx(ONYXKEYS.ISSUE_NEW_EXPENSIFY_CARD);
 
     const {currentStep} = issueNewCard ?? {};
 
     const policyID = policy?.id ?? '-1';
+    const backTo = route.params.backTo;
 
     // TODO: add logic to skip Assignee step when the flow is started from the member's profile page
     useEffect(() => {
@@ -36,7 +42,12 @@ function IssueNewCardPage({policy}: WithPolicyAndFullscreenLoadingProps) {
         case CONST.EXPENSIFY_CARD.STEP.CARD_NAME:
             return <CardNameStep />;
         case CONST.EXPENSIFY_CARD.STEP.CONFIRMATION:
-            return <ConfirmationStep policyID={policyID} />;
+            return (
+                <ConfirmationStep
+                    policyID={policyID}
+                    backTo={backTo}
+                />
+            );
         default:
             return <AssigneeStep policy={policy} />;
     }
diff --git a/src/pages/workspace/members/WorkspaceMemberDetailsPage.tsx b/src/pages/workspace/members/WorkspaceMemberDetailsPage.tsx
index 60216745c11e..e6e17b207fc4 100644
--- a/src/pages/workspace/members/WorkspaceMemberDetailsPage.tsx
+++ b/src/pages/workspace/members/WorkspaceMemberDetailsPage.tsx
@@ -139,6 +139,8 @@ function WorkspaceMemberDetailsPage({personalDetails, policy, route}: WorkspaceM
     );
 
     const navigateToIssueNewCard = useCallback(() => {
+        const activeRoute = Navigation.getActiveRoute();
+
         Card.setIssueNewCardStepAndData({
             step: CONST.EXPENSIFY_CARD.STEP.CARD_TYPE,
             data: {
@@ -146,7 +148,7 @@ function WorkspaceMemberDetailsPage({personalDetails, policy, route}: WorkspaceM
             },
             isEditing: false,
         });
-        Navigation.navigate(ROUTES.WORKSPACE_EXPENSIFY_CARD_ISSUE_NEW.getRoute(policyID));
+        Navigation.navigate(ROUTES.WORKSPACE_EXPENSIFY_CARD_ISSUE_NEW.getRoute(policyID, activeRoute));
     }, [memberLogin, policyID]);
 
     const openRoleSelectionModal = useCallback(() => {
diff --git a/src/pages/workspace/withPolicy.tsx b/src/pages/workspace/withPolicy.tsx
index b6da4dd689e6..db55db2514ef 100644
--- a/src/pages/workspace/withPolicy.tsx
+++ b/src/pages/workspace/withPolicy.tsx
@@ -47,6 +47,7 @@ type PolicyRoute = RouteProp<
     | typeof SCREENS.WORKSPACE.REPORT_FIELDS_VALUE_SETTINGS
     | typeof SCREENS.WORKSPACE.ACCOUNTING.CARD_RECONCILIATION
     | typeof SCREENS.WORKSPACE.RULES
+    | typeof SCREENS.WORKSPACE.EXPENSIFY_CARD_ISSUE_NEW
 >;
 
 function getPolicyIDFromRoute(route: PolicyRoute): string {

From d2c093bddd87c4114b0af5fd377b20b3a91c972a Mon Sep 17 00:00:00 2001
From: VickyStash <vikstash@gmail.com>
Date: Fri, 30 Aug 2024 18:02:40 +0200
Subject: [PATCH 3/4] Prevent user from using deep link to access
 WorkspaceSettlementFrequencyPage if monthly option isn't allowed

---
 .../expensifyCard/WorkspaceSettlementFrequencyPage.tsx          | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/src/pages/workspace/expensifyCard/WorkspaceSettlementFrequencyPage.tsx b/src/pages/workspace/expensifyCard/WorkspaceSettlementFrequencyPage.tsx
index 84f1bb810af2..62db3af81da6 100644
--- a/src/pages/workspace/expensifyCard/WorkspaceSettlementFrequencyPage.tsx
+++ b/src/pages/workspace/expensifyCard/WorkspaceSettlementFrequencyPage.tsx
@@ -31,6 +31,7 @@ function WorkspaceSettlementFrequencyPage({route}: WorkspaceSettlementFrequencyP
 
     const shouldShowMonthlyOption = cardSettings?.isMonthlySettlementAllowed ?? false;
     const selectedFrequency = cardSettings?.monthlySettlementDate ? CONST.EXPENSIFY_CARD.FREQUENCY_SETTING.MONTHLY : CONST.EXPENSIFY_CARD.FREQUENCY_SETTING.DAILY;
+    const isSettlementFrequencyBlocked = !shouldShowMonthlyOption && selectedFrequency === CONST.EXPENSIFY_CARD.FREQUENCY_SETTING.DAILY;
 
     const data = useMemo(() => {
         const options = [];
@@ -62,6 +63,7 @@ function WorkspaceSettlementFrequencyPage({route}: WorkspaceSettlementFrequencyP
         <AccessOrNotFoundWrapper
             accessVariants={[CONST.POLICY.ACCESS_VARIANTS.ADMIN, CONST.POLICY.ACCESS_VARIANTS.PAID]}
             policyID={policyID}
+            shouldBeBlocked={isSettlementFrequencyBlocked}
             featureName={CONST.POLICY.MORE_FEATURES.ARE_EXPENSIFY_CARDS_ENABLED}
         >
             <ScreenWrapper

From 2e293ac83b201022c25a31d929eec6d3a742810f Mon Sep 17 00:00:00 2001
From: VickyStash <vikstash@gmail.com>
Date: Mon, 2 Sep 2024 09:52:54 +0200
Subject: [PATCH 4/4] Code improvements

---
 .../workspace/expensifyCard/WorkspaceCardSettingsPage.tsx      | 2 +-
 .../workspace/expensifyCard/issueNew/IssueNewCardPage.tsx      | 3 +--
 2 files changed, 2 insertions(+), 3 deletions(-)

diff --git a/src/pages/workspace/expensifyCard/WorkspaceCardSettingsPage.tsx b/src/pages/workspace/expensifyCard/WorkspaceCardSettingsPage.tsx
index 4b6e267cac60..b34397cc43e3 100644
--- a/src/pages/workspace/expensifyCard/WorkspaceCardSettingsPage.tsx
+++ b/src/pages/workspace/expensifyCard/WorkspaceCardSettingsPage.tsx
@@ -32,7 +32,7 @@ function WorkspaceCardSettingsPage({route}: WorkspaceCardSettingsPageProps) {
     const [cardSettings] = useOnyx(`${ONYXKEYS.COLLECTION.PRIVATE_EXPENSIFY_CARD_SETTINGS}${workspaceAccountID}`);
 
     const paymentBankAccountID = cardSettings?.paymentBankAccountID ?? 0;
-    const isMonthlySettlementAllowed = cardSettings?.isMonthlySettlementAllowed;
+    const isMonthlySettlementAllowed = cardSettings?.isMonthlySettlementAllowed ?? false;
     const settlementFrequency = cardSettings?.monthlySettlementDate ? CONST.EXPENSIFY_CARD.FREQUENCY_SETTING.MONTHLY : CONST.EXPENSIFY_CARD.FREQUENCY_SETTING.DAILY;
     const isSettlementFrequencyBlocked = !isMonthlySettlementAllowed && settlementFrequency === CONST.EXPENSIFY_CARD.FREQUENCY_SETTING.DAILY;
     const bankAccountNumber = bankAccountList?.[paymentBankAccountID.toString()]?.accountData?.accountNumber ?? '';
diff --git a/src/pages/workspace/expensifyCard/issueNew/IssueNewCardPage.tsx b/src/pages/workspace/expensifyCard/issueNew/IssueNewCardPage.tsx
index 23daf00534f1..b10e1cb18e33 100644
--- a/src/pages/workspace/expensifyCard/issueNew/IssueNewCardPage.tsx
+++ b/src/pages/workspace/expensifyCard/issueNew/IssueNewCardPage.tsx
@@ -23,9 +23,8 @@ function IssueNewCardPage({policy, route}: IssueNewCardPageProps) {
     const {currentStep} = issueNewCard ?? {};
 
     const policyID = policy?.id ?? '-1';
-    const backTo = route.params.backTo;
+    const backTo = route?.params?.backTo;
 
-    // TODO: add logic to skip Assignee step when the flow is started from the member's profile page
     useEffect(() => {
         Card.startIssueNewCardFlow(policyID);
     }, [policyID]);
