diff --git a/src/CONST.ts b/src/CONST.ts index 175aa8cd3c16..043b1e269c2e 100755 --- a/src/CONST.ts +++ b/src/CONST.ts @@ -641,6 +641,9 @@ const CONST = { INBOX: 'inbox', }, + EXPENSIFY_POLICY_DOMAIN: 'expensify-policy', + EXPENSIFY_POLICY_DOMAIN_EXTENSION: '.exfy', + SIGN_IN_FORM_WIDTH: 300, REQUEST_CODE_DELAY: 30, diff --git a/src/libs/API/parameters/UpdateCardSettlementAccountParams.ts b/src/libs/API/parameters/UpdateCardSettlementAccountParams.ts new file mode 100644 index 000000000000..8af37bb9d4ce --- /dev/null +++ b/src/libs/API/parameters/UpdateCardSettlementAccountParams.ts @@ -0,0 +1,6 @@ +type UpdateCardSettlementAccountParams = { + domainName: string; + settlementBankAccountID: number; +}; + +export default UpdateCardSettlementAccountParams; diff --git a/src/libs/API/parameters/UpdateCardSettlementFrequencyParams.ts b/src/libs/API/parameters/UpdateCardSettlementFrequencyParams.ts new file mode 100644 index 000000000000..6085713bac97 --- /dev/null +++ b/src/libs/API/parameters/UpdateCardSettlementFrequencyParams.ts @@ -0,0 +1,9 @@ +import type {ValueOf} from 'type-fest'; +import type CONST from '@src/CONST'; + +type UpdateCardSettlementFrequencyParams = { + settlementFrequency: ValueOf; + workspaceAccountID: number; +}; + +export default UpdateCardSettlementFrequencyParams; diff --git a/src/libs/API/parameters/index.ts b/src/libs/API/parameters/index.ts index 7083823d871e..e06b819c84a5 100644 --- a/src/libs/API/parameters/index.ts +++ b/src/libs/API/parameters/index.ts @@ -286,3 +286,5 @@ export type {default as EnablePolicyCompanyCardsParams} from './EnablePolicyComp export type {default as ToggleCardContinuousReconciliationParams} from './ToggleCardContinuousReconciliationParams'; export type {default as UpdateExpensifyCardLimitTypeParams} from './UpdateExpensifyCardLimitTypeParams'; export type {default as UpdateXeroGenericTypeParams} from './UpdateXeroGenericTypeParams'; +export type {default as UpdateCardSettlementFrequencyParams} from './UpdateCardSettlementFrequencyParams'; +export type {default as UpdateCardSettlementAccountParams} from './UpdateCardSettlementAccountParams'; diff --git a/src/libs/API/types.ts b/src/libs/API/types.ts index 4e55de0640b3..e0255dfb38fc 100644 --- a/src/libs/API/types.ts +++ b/src/libs/API/types.ts @@ -341,6 +341,8 @@ const WRITE_COMMANDS = { CREATE_EXPENSIFY_CARD: 'CreateExpensifyCard', CREATE_ADMIN_ISSUED_VIRTUAL_CARD: 'CreateAdminIssuedVirtualCard', TOGGLE_CARD_CONTINUOUS_RECONCILIATION: 'ToggleCardContinuousReconciliation', + UPDATE_CARD_SETTLEMENT_FREQUENCY: 'UpdateCardSettlementFrequency', + UPDATE_CARD_SETTLEMENT_ACCOUNT: 'UpdateCardSettlementAccount', UPDATE_XERO_IMPORT_TRACKING_CATEGORIES: 'UpdateXeroImportTrackingCategories', UPDATE_XERO_IMPORT_TAX_RATES: 'UpdateXeroImportTaxRates', UPDATE_XERO_TENANT_ID: 'UpdateXeroTenantID', @@ -692,6 +694,8 @@ type WriteCommandParameters = { [WRITE_COMMANDS.CREATE_EXPENSIFY_CARD]: Parameters.CreateExpensifyCardParams; [WRITE_COMMANDS.CREATE_ADMIN_ISSUED_VIRTUAL_CARD]: Omit; [WRITE_COMMANDS.TOGGLE_CARD_CONTINUOUS_RECONCILIATION]: Parameters.ToggleCardContinuousReconciliationParams; + [WRITE_COMMANDS.UPDATE_CARD_SETTLEMENT_FREQUENCY]: Parameters.UpdateCardSettlementFrequencyParams; + [WRITE_COMMANDS.UPDATE_CARD_SETTLEMENT_ACCOUNT]: Parameters.UpdateCardSettlementAccountParams; // Xero API [WRITE_COMMANDS.UPDATE_XERO_TENANT_ID]: Parameters.UpdateXeroGenericTypeParams; diff --git a/src/libs/PolicyUtils.ts b/src/libs/PolicyUtils.ts index 3f3a2a96a1e1..07dfe7a89b85 100644 --- a/src/libs/PolicyUtils.ts +++ b/src/libs/PolicyUtils.ts @@ -969,6 +969,14 @@ function getWorkspaceAccountID(policyID: string) { return policy.workspaceAccountID ?? 0; } +function getDomainNameForPolicy(policyID?: string): string { + if (!policyID) { + return ''; + } + + return `${CONST.EXPENSIFY_POLICY_DOMAIN}${policyID}${CONST.EXPENSIFY_POLICY_DOMAIN_EXTENSION}`; +} + export { canEditTaxRate, extractPolicyIDFromPath, @@ -1074,6 +1082,7 @@ export { getWorkspaceAccountID, getAllTaxRatesNamesAndKeys as getAllTaxRates, getTagNamesFromTagsLists, + getDomainNameForPolicy, }; export type {MemberEmailsToAccountIDs}; diff --git a/src/libs/actions/Card.ts b/src/libs/actions/Card.ts index 747df5d3998e..69bd78156381 100644 --- a/src/libs/actions/Card.ts +++ b/src/libs/actions/Card.ts @@ -210,100 +210,88 @@ function revealVirtualCardDetails(cardID: number): Promise }); } -function updateSettlementFrequency(workspaceAccountID: number, frequency: ValueOf) { - // TODO: remove this code when the API is ready - if (frequency === CONST.EXPENSIFY_CARD.FREQUENCY_SETTING.DAILY) { - Onyx.merge(`${ONYXKEYS.COLLECTION.PRIVATE_EXPENSIFY_CARD_SETTINGS}${workspaceAccountID}`, { - monthlySettlementDate: null, - }); - } else { - Onyx.merge(`${ONYXKEYS.COLLECTION.PRIVATE_EXPENSIFY_CARD_SETTINGS}${workspaceAccountID}`, { - monthlySettlementDate: new Date(), - }); - } +function updateSettlementFrequency(workspaceAccountID: number, settlementFrequency: ValueOf, currentFrequency?: Date) { + const monthlySettlementDate = settlementFrequency === CONST.EXPENSIFY_CARD.FREQUENCY_SETTING.DAILY ? null : new Date(); - // TODO: uncomment this code when the API is ready - // const optimisticData: OnyxUpdate[] = [ - // { - // onyxMethod: Onyx.METHOD.MERGE, - // key: `${ONYXKEYS.COLLECTION.PRIVATE_EXPENSIFY_CARD_SETTINGS}${workspaceAccountID}`, - // value: { - // monthlySettlementDate: '', - // }, - // }, - // ]; - // - // const successData: OnyxUpdate[] = [ - // { - // onyxMethod: Onyx.METHOD.MERGE, - // key: `${ONYXKEYS.COLLECTION.PRIVATE_EXPENSIFY_CARD_SETTINGS}${workspaceAccountID}`, - // value: { - // monthlySettlementDate: '', - // }, - // }, - // ]; - // - // const failureData: OnyxUpdate[] = [ - // { - // onyxMethod: Onyx.METHOD.MERGE, - // key: `${ONYXKEYS.COLLECTION.PRIVATE_EXPENSIFY_CARD_SETTINGS}${workspaceAccountID}`, - // value: { - // monthlySettlementDate: null, - // }, - // }, - // ]; - // - // const parameters = { - // workspaceAccountID, - // settlementFrequency: frequency, - // }; - // - // API.write(WRITE_COMMANDS.UPDATE_CARD_SETTLEMENT_FREQUENCY, parameters, {optimisticData, successData, failureData}); + const optimisticData: OnyxUpdate[] = [ + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.PRIVATE_EXPENSIFY_CARD_SETTINGS}${workspaceAccountID}`, + value: { + monthlySettlementDate, + }, + }, + ]; + + const successData: OnyxUpdate[] = [ + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.PRIVATE_EXPENSIFY_CARD_SETTINGS}${workspaceAccountID}`, + value: { + monthlySettlementDate, + }, + }, + ]; + + const failureData: OnyxUpdate[] = [ + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.PRIVATE_EXPENSIFY_CARD_SETTINGS}${workspaceAccountID}`, + value: { + monthlySettlementDate: currentFrequency, + }, + }, + ]; + + const parameters = { + workspaceAccountID, + settlementFrequency, + }; + + API.write(WRITE_COMMANDS.UPDATE_CARD_SETTLEMENT_FREQUENCY, parameters, {optimisticData, successData, failureData}); } -function updateSettlementAccount(workspaceAccountID: number, accountID: number) { - // TODO: remove this code when the API is ready - Onyx.merge(`${ONYXKEYS.COLLECTION.PRIVATE_EXPENSIFY_CARD_SETTINGS}${workspaceAccountID}`, { - paymentBankAccountID: accountID, - }); +function updateSettlementAccount(workspaceAccountID: number, domainName: string, settlementBankAccountID?: number, currentSettlementBankAccountID?: number) { + if (!settlementBankAccountID) { + return; + } + + const optimisticData: OnyxUpdate[] = [ + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.PRIVATE_EXPENSIFY_CARD_SETTINGS}${workspaceAccountID}`, + value: { + paymentBankAccountID: settlementBankAccountID, + }, + }, + ]; + + const successData: OnyxUpdate[] = [ + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.PRIVATE_EXPENSIFY_CARD_SETTINGS}${workspaceAccountID}`, + value: { + paymentBankAccountID: settlementBankAccountID, + }, + }, + ]; + + const failureData: OnyxUpdate[] = [ + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.PRIVATE_EXPENSIFY_CARD_SETTINGS}${workspaceAccountID}`, + value: { + paymentBankAccountID: currentSettlementBankAccountID, + }, + }, + ]; + + const parameters = { + domainName, + settlementBankAccountID, + }; - // TODO: uncomment this code when the API is ready - // const optimisticData: OnyxUpdate[] = [ - // { - // onyxMethod: Onyx.METHOD.MERGE, - // key: `${ONYXKEYS.COLLECTION.PRIVATE_EXPENSIFY_CARD_SETTINGS}${workspaceAccountID}`, - // value: { - // paymentBankAccountID: accountID, - // }, - // }, - // ]; - // - // const successData: OnyxUpdate[] = [ - // { - // onyxMethod: Onyx.METHOD.MERGE, - // key: `${ONYXKEYS.COLLECTION.PRIVATE_EXPENSIFY_CARD_SETTINGS}${workspaceAccountID}`, - // value: { - // paymentBankAccountID: accountID, - // }, - // }, - // ]; - // - // const failureData: OnyxUpdate[] = [ - // { - // onyxMethod: Onyx.METHOD.MERGE, - // key: `${ONYXKEYS.COLLECTION.PRIVATE_EXPENSIFY_CARD_SETTINGS}${workspaceAccountID}`, - // value: { - // paymentBankAccountID: null, - // }, - // }, - // ]; - // - // const parameters = { - // workspaceAccountID, - // settlementBankAccountID: accountID, - // }; - // - // API.write(WRITE_COMMANDS.UPDATE_CARD_SETTLEMENT_ACCOUNT, parameters, {optimisticData, successData, failureData}); + API.write(WRITE_COMMANDS.UPDATE_CARD_SETTLEMENT_ACCOUNT, parameters, {optimisticData, successData, failureData}); } function setIssueNewCardStepAndData({data, isEditing, step}: IssueNewCardFlowData) { diff --git a/src/pages/workspace/expensifyCard/WorkspaceExpensifyCardBankAccounts.tsx b/src/pages/workspace/expensifyCard/WorkspaceExpensifyCardBankAccounts.tsx index b96d86d06582..78060487d35f 100644 --- a/src/pages/workspace/expensifyCard/WorkspaceExpensifyCardBankAccounts.tsx +++ b/src/pages/workspace/expensifyCard/WorkspaceExpensifyCardBankAccounts.tsx @@ -12,6 +12,7 @@ import useLocalize from '@hooks/useLocalize'; import useThemeStyles from '@hooks/useThemeStyles'; import {getLastFourDigits} from '@libs/BankAccountUtils'; import * as CardUtils from '@libs/CardUtils'; +import * as PolicyUtils from '@libs/PolicyUtils'; import Navigation from '@navigation/Navigation'; import type {SettingsNavigatorParamList} from '@navigation/types'; import * as Card from '@userActions/Card'; @@ -30,12 +31,16 @@ function WorkspaceExpensifyCardBankAccounts({route}: WorkspaceExpensifyCardBankA const policyID = route?.params?.policyID ?? '-1'; + const workspaceAccountID = PolicyUtils.getWorkspaceAccountID(policyID); + const handleAddBankAccount = () => { Navigation.navigate(ROUTES.BANK_ACCOUNT_WITH_STEP_TO_OPEN.getRoute('new', policyID, ROUTES.WORKSPACE_EXPENSIFY_CARD.getRoute(policyID))); }; const handleSelectBankAccount = (value?: number) => { Card.configureExpensifyCardsForPolicy(policyID, value); + const domainName = PolicyUtils.getDomainNameForPolicy(policyID); + Card.updateSettlementAccount(workspaceAccountID, domainName, value); Navigation.navigate(ROUTES.WORKSPACE_EXPENSIFY_CARD_ISSUE_NEW.getRoute(policyID)); }; diff --git a/src/pages/workspace/expensifyCard/WorkspaceSettlementAccountPage.tsx b/src/pages/workspace/expensifyCard/WorkspaceSettlementAccountPage.tsx index b75226ed4636..298d0c1c54f9 100644 --- a/src/pages/workspace/expensifyCard/WorkspaceSettlementAccountPage.tsx +++ b/src/pages/workspace/expensifyCard/WorkspaceSettlementAccountPage.tsx @@ -78,7 +78,8 @@ function WorkspaceSettlementAccountPage({route}: WorkspaceSettlementAccountPageP }, [eligibleBankAccounts, paymentBankAccountID, styles, translate]); const updateSettlementAccount = (value: number) => { - Card.updateSettlementAccount(workspaceAccountID, value); + const domainName = PolicyUtils.getDomainNameForPolicy(policyID); + Card.updateSettlementAccount(workspaceAccountID, domainName, value, paymentBankAccountID); Navigation.goBack(); }; diff --git a/src/pages/workspace/expensifyCard/WorkspaceSettlementFrequencyPage.tsx b/src/pages/workspace/expensifyCard/WorkspaceSettlementFrequencyPage.tsx index 8e837c5a8456..84f1bb810af2 100644 --- a/src/pages/workspace/expensifyCard/WorkspaceSettlementFrequencyPage.tsx +++ b/src/pages/workspace/expensifyCard/WorkspaceSettlementFrequencyPage.tsx @@ -29,8 +29,7 @@ function WorkspaceSettlementFrequencyPage({route}: WorkspaceSettlementFrequencyP const [cardSettings] = useOnyx(`${ONYXKEYS.COLLECTION.PRIVATE_EXPENSIFY_CARD_SETTINGS}${workspaceAccountID}`); - // TODO: change true for false after API is ready - true is for testing purposes - const shouldShowMonthlyOption = cardSettings?.isMonthlySettlementAllowed ?? true; + const shouldShowMonthlyOption = cardSettings?.isMonthlySettlementAllowed ?? false; const selectedFrequency = cardSettings?.monthlySettlementDate ? CONST.EXPENSIFY_CARD.FREQUENCY_SETTING.MONTHLY : CONST.EXPENSIFY_CARD.FREQUENCY_SETTING.DAILY; const data = useMemo(() => { @@ -56,7 +55,7 @@ function WorkspaceSettlementFrequencyPage({route}: WorkspaceSettlementFrequencyP }, [translate, shouldShowMonthlyOption, selectedFrequency]); const updateSettlementFrequency = (value: ValueOf) => { - Card.updateSettlementFrequency(workspaceAccountID, value); + Card.updateSettlementFrequency(workspaceAccountID, value, cardSettings?.monthlySettlementDate); }; return (