Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/ONYXKEYS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -796,6 +796,9 @@ const ONYXKEYS = {
/** The value that indicates whether Continuous Reconciliation should be used on the domain */
EXPENSIFY_CARD_USE_CONTINUOUS_RECONCILIATION: 'expensifyCard_useContinuousReconciliation_',

/** The selected accounting integration bank account ID for card reconciliation */
EXPENSIFY_CARD_RECONCILIATION_BANK_ACCOUNT_ID: 'expensifyCard_bankAccount_',

/** Currently displaying feed */
LAST_SELECTED_FEED: 'lastSelectedFeed_',

Expand Down Expand Up @@ -1291,6 +1294,7 @@ type OnyxCollectionValuesMapping = {
[ONYXKEYS.COLLECTION.WORKSPACE_CARDS_LIST]: OnyxTypes.WorkspaceCardsList;
[ONYXKEYS.COLLECTION.EXPENSIFY_CARD_CONTINUOUS_RECONCILIATION_CONNECTION]: OnyxTypes.PolicyConnectionName;
[ONYXKEYS.COLLECTION.EXPENSIFY_CARD_USE_CONTINUOUS_RECONCILIATION]: OnyxTypes.CardContinuousReconciliation;
[ONYXKEYS.COLLECTION.EXPENSIFY_CARD_RECONCILIATION_BANK_ACCOUNT_ID]: string;
[ONYXKEYS.COLLECTION.LAST_SELECTED_FEED]: OnyxTypes.CompanyCardFeedWithDomainID;
[ONYXKEYS.COLLECTION.LAST_SELECTED_EXPENSIFY_CARD_FEED]: OnyxTypes.FundID;
[ONYXKEYS.COLLECTION.NVP_EXPENSIFY_ON_CARD_WAITLIST]: OnyxTypes.CardOnWaitlist;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type SetCardReconciliationBankAccountParams = {
domainName: string;
expensifyCardIntegrationWithdrawalID: string;
};

export default SetCardReconciliationBankAccountParams;
1 change: 1 addition & 0 deletions src/libs/API/parameters/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,7 @@ export type {default as SetPolicyCategoryTaxParams} from './SetPolicyCategoryTax
export type {default as SetPolicyCategoryMaxAmountParams} from './SetPolicyCategoryMaxAmountParams';
export type {default as EnablePolicyCompanyCardsParams} from './EnablePolicyCompanyCardsParams';
export type {default as ToggleCardContinuousReconciliationParams} from './ToggleCardContinuousReconciliationParams';
export type {default as SetCardReconciliationBankAccountParams} from './SetCardReconciliationBankAccountParams';
export type {default as CardDeactivateParams} from './CardDeactivateParams';
export type {default as UpdateExpensifyCardLimitTypeParams} from './UpdateExpensifyCardLimitTypeParams';
export type {default as RemoveDelegateParams} from './RemoveDelegateParams';
Expand Down
2 changes: 2 additions & 0 deletions src/libs/API/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,7 @@ const WRITE_COMMANDS = {
REMOVE_DELEGATE: 'RemoveDelegate',
UPDATE_DELEGATE_ROLE: 'UpdateDelegateRole',
TOGGLE_CARD_CONTINUOUS_RECONCILIATION: 'ToggleCardContinuousReconciliation',
SET_CARD_RECONCILIATION_BANK_ACCOUNT: 'SetCardReconciliationBankAccount',
SET_POLICY_TAG_APPROVER: 'SetPolicyTagApprover',
SAVE_SEARCH: 'SaveSearch',
DELETE_SAVED_SEARCH: 'DeleteSavedSearch',
Expand Down Expand Up @@ -1097,6 +1098,7 @@ type WriteCommandParameters = {
[WRITE_COMMANDS.UPDATE_DELEGATE_ROLE]: Parameters.UpdateDelegateRoleParams;
[WRITE_COMMANDS.REMOVE_DELEGATE]: Parameters.RemoveDelegateParams;
[WRITE_COMMANDS.TOGGLE_CARD_CONTINUOUS_RECONCILIATION]: Parameters.ToggleCardContinuousReconciliationParams;
[WRITE_COMMANDS.SET_CARD_RECONCILIATION_BANK_ACCOUNT]: Parameters.SetCardReconciliationBankAccountParams;
[WRITE_COMMANDS.SAVE_SEARCH]: Parameters.SaveSearchParams;
[WRITE_COMMANDS.DELETE_SAVED_SEARCH]: Parameters.DeleteSavedSearchParams;
[WRITE_COMMANDS.UPDATE_CARD_SETTLEMENT_FREQUENCY]: Parameters.UpdateCardSettlementFrequencyParams;
Expand Down
24 changes: 24 additions & 0 deletions src/libs/CardUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import type {
ExpensifyCardSettingsBase,
PersonalDetailsList,
Policy,
PolicyConnectionName,
PrivatePersonalDetails,
WorkspaceCardsList,
} from '@src/types/onyx';
Expand All @@ -39,6 +40,7 @@ import type {
NonConnectableBankName,
} from '@src/types/onyx/CardFeeds';
import type {SelectedTimezone} from '@src/types/onyx/PersonalDetails';
import type {Connections} from '@src/types/onyx/Policy';
import {isEmptyObject} from '@src/types/utils/EmptyObject';
import type IconAsset from '@src/types/utils/IconAsset';
import {isBankAccountPartiallySetup} from './BankAccountUtils';
Expand Down Expand Up @@ -440,6 +442,27 @@ function getEligibleBankAccountsForCard(bankAccountsList: OnyxEntry<BankAccountL
);
}

function getConnectionBankAccountsForReconciliation(connections: OnyxEntry<Connections>, connectionName: PolicyConnectionName | undefined): Array<{id: string; name: string}> {
if (!connections || !connectionName) {
return [];
}

switch (connectionName) {
case CONST.POLICY.CONNECTIONS.NAME.QBO:
return (connections.quickbooksOnline?.data?.bankAccounts ?? []).map(({id, name}) => ({id, name}));
case CONST.POLICY.CONNECTIONS.NAME.XERO:
return (connections.xero?.data?.bankAccounts ?? []).map(({id, name}) => ({id, name}));
case CONST.POLICY.CONNECTIONS.NAME.NETSUITE:
return (connections.netsuite?.options?.data?.payableList ?? []).filter((account) => account.type === '_bank').map(({id, name}) => ({id, name}));
case CONST.POLICY.CONNECTIONS.NAME.SAGE_INTACCT:
return connections.intacct?.data?.bankAccounts ?? [];
case CONST.POLICY.CONNECTIONS.NAME.QBD:
return (connections.quickbooksDesktop?.data?.bankAccounts ?? []).map(({id, name}) => ({id, name}));
default:
return [];
}
}

function getEligibleBankAccountsForUkEuCard(bankAccountsList: OnyxEntry<BankAccountList>, outputCurrency?: string) {
if (!bankAccountsList || isEmptyObject(bankAccountsList)) {
return [];
Expand Down Expand Up @@ -1840,6 +1863,7 @@ export {
getCardFeedWithDomainID,
splitCardFeedWithDomainID,
getEligibleBankAccountsForUkEuCard,
getConnectionBankAccountsForReconciliation,
isPersonalCard,
COMPANY_CARD_FEED_ICON_NAMES,
COMPANY_CARD_BANK_ICON_NAMES,
Expand Down
26 changes: 26 additions & 0 deletions src/libs/actions/Card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1521,6 +1521,31 @@ function toggleContinuousReconciliation(workspaceAccountID: number, shouldUseCon
});
}

function setCardReconciliationAccount(workspaceAccountID: number, domainName: string, expensifyCardIntegrationWithdrawalID: string, currentReconciliationBankAccountID?: string) {
const parameters = {
domainName,
expensifyCardIntegrationWithdrawalID,
};

const optimisticData: Array<OnyxUpdate<typeof ONYXKEYS.COLLECTION.EXPENSIFY_CARD_RECONCILIATION_BANK_ACCOUNT_ID>> = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.EXPENSIFY_CARD_RECONCILIATION_BANK_ACCOUNT_ID}${workspaceAccountID}`,
value: expensifyCardIntegrationWithdrawalID,
},
];

const failureData: Array<OnyxUpdate<typeof ONYXKEYS.COLLECTION.EXPENSIFY_CARD_RECONCILIATION_BANK_ACCOUNT_ID>> = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.EXPENSIFY_CARD_RECONCILIATION_BANK_ACCOUNT_ID}${workspaceAccountID}`,
value: currentReconciliationBankAccountID ?? null,
},
];

API.write(WRITE_COMMANDS.SET_CARD_RECONCILIATION_BANK_ACCOUNT, parameters, {optimisticData, failureData});
}

function updateSelectedFeed(feed: CompanyCardFeedWithDomainID, policyID: string | undefined) {
if (!policyID) {
return;
Expand Down Expand Up @@ -1906,6 +1931,7 @@ export {
updateAssignedCardName,
updateAssignedCardTransactionStartDate,
toggleContinuousReconciliation,
setCardReconciliationAccount,
updateExpensifyCardLimitType,
updateSelectedFeed,
updateSelectedExpensifyCardFeed,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import useOnyx from '@hooks/useOnyx';
import useThemeStyles from '@hooks/useThemeStyles';
import {getConnectionNameFromRouteParam} from '@libs/AccountingUtils';
import {openPolicyAccountingPage} from '@libs/actions/PolicyConnections';
import {getCardSettings, isExpensifyCardFullySetUp} from '@libs/CardUtils';
import {getCardSettings, getConnectionBankAccountsForReconciliation, isExpensifyCardFullySetUp} from '@libs/CardUtils';
import createDynamicRoute from '@libs/Navigation/helpers/dynamicRoutesUtils/createDynamicRoute';
import type {PlatformStackScreenProps} from '@libs/Navigation/PlatformStackNavigation/types';
import Navigation from '@navigation/Navigation';
Expand Down Expand Up @@ -75,17 +75,19 @@ function CardReconciliationPage({policy, route}: CardReconciliationPageProps) {

const [continuousReconciliation] = useOnyx(`${ONYXKEYS.COLLECTION.EXPENSIFY_CARD_USE_CONTINUOUS_RECONCILIATION}${effectiveDomainID}`);
const [currentConnectionName] = useOnyx(`${ONYXKEYS.COLLECTION.EXPENSIFY_CARD_CONTINUOUS_RECONCILIATION_CONNECTION}${effectiveDomainID}`);
const [bankAccountList] = useOnyx(ONYXKEYS.BANK_ACCOUNT_LIST);
const [reconciliationBankAccountID] = useOnyx(`${ONYXKEYS.COLLECTION.EXPENSIFY_CARD_RECONCILIATION_BANK_ACCOUNT_ID}${effectiveDomainID}`);

const resolvedCardSettings = getCardSettings(fullySetUpCardSetting.cardSetting);
const paymentBankAccountID = resolvedCardSettings?.paymentBankAccountID ?? CONST.DEFAULT_NUMBER_ID;
const bankAccountTitle = bankAccountList?.[paymentBankAccountID]?.title ?? '';

const {connection} = route.params;
const connectionName = getConnectionNameFromRouteParam(connection) as AccountingConnectionName;
const autoSync = !!policy?.connections?.[connectionName]?.config?.autoSync?.enabled;
const shouldShow = !!resolvedCardSettings?.paymentBankAccountID;

const connectionBankAccounts = getConnectionBankAccountsForReconciliation(policy?.connections, connectionName);
const bankAccountTitle = connectionBankAccounts.find((account) => account.id === reconciliationBankAccountID)?.name ?? '';

const handleToggleContinuousReconciliation = (value: boolean) => {
toggleContinuousReconciliation(effectiveDomainID, value, connectionName, currentConnectionName);
if (value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,16 @@ import useOnyx from '@hooks/useOnyx';
import useThemeStyles from '@hooks/useThemeStyles';
import {getConnectionNameFromRouteParam} from '@libs/AccountingUtils';
import {getLastFourDigits} from '@libs/BankAccountUtils';
import {getCardProgramKey, getCardSettings, getEligibleBankAccountsForCard} from '@libs/CardUtils';
import Log from '@libs/Log';
import {getCardProgramKey, getCardSettings, getConnectionBankAccountsForReconciliation} from '@libs/CardUtils';
import type {PlatformStackScreenProps} from '@libs/Navigation/PlatformStackNavigation/types';
import {getDomainNameForPolicy} from '@libs/PolicyUtils';
import Navigation from '@navigation/Navigation';
import type {SettingsNavigatorParamList} from '@navigation/types';
import {updateSettlementAccount} from '@userActions/Card';
import {setCardReconciliationAccount} from '@userActions/Card';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES, {DYNAMIC_ROUTES} from '@src/ROUTES';
import type SCREENS from '@src/SCREENS';
import {isEmptyObject} from '@src/types/utils/EmptyObject';

type DynamicReconciliationAccountSettingsPageProps = PlatformStackScreenProps<SettingsNavigatorParamList, typeof SCREENS.WORKSPACE.ACCOUNTING.DYNAMIC_RECONCILIATION_ACCOUNT_SETTINGS>;

Expand All @@ -38,45 +36,41 @@ function DynamicReconciliationAccountSettingsPage({route}: DynamicReconciliation
const connectionName = getConnectionNameFromRouteParam(connection);
const defaultFundID = useDefaultFundID(policyID);

const [connections] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`, {selector: (policy) => policy?.connections});
const [workspaceAccountID = CONST.DEFAULT_NUMBER_ID] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`, {selector: (policy) => policy?.workspaceAccountID});
const [bankAccountList] = useOnyx(ONYXKEYS.BANK_ACCOUNT_LIST);
const [cardSettings] = useOnyx(`${ONYXKEYS.COLLECTION.PRIVATE_EXPENSIFY_CARD_SETTINGS}${defaultFundID}`);
const programKey = getCardProgramKey(cardSettings);
const settings = getCardSettings(cardSettings, programKey);
const paymentBankAccountID = settings?.paymentBankAccountID;
const [reconciliationBankAccountID] = useOnyx(`${ONYXKEYS.COLLECTION.EXPENSIFY_CARD_RECONCILIATION_BANK_ACCOUNT_ID}${workspaceAccountID}`);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Key reconciliation account by fund ID, not workspace ID

This screen reads/writes EXPENSIFY_CARD_RECONCILIATION_BANK_ACCOUNT_ID using workspaceAccountID, but CardReconciliationPage reads the same setting using effectiveDomainID (derived from the active card feed/domain). When those IDs differ (for example, domain-linked feeds where workspaceAccountID can be 0), the selected reconciliation account is stored under a different Onyx key than the one the parent page consumes, so the chosen account won’t appear selected/titled correctly after navigation and optimistic updates can be applied to the wrong entry.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like this could be a legit concern?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking into this, will probably update tomorrow

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The whole backend is wired looking at workspaceAccountID

see https://github.com/Expensify/Auth/blob/3ff45143231e21f4c387bc5a07b3ea9ea401ede3/auth/command/OpenPolicyAccountingPage.cpp#L45 and https://github.com/Expensify/Auth/blob/3ff45143231e21f4c387bc5a07b3ea9ea401ede3/auth/command/ToggleCardContinuousReconciliation.cpp#L53

I think there was an effort to try to make this sync with OldDot, but didn't finish or wasn't mantained? Not sure #61605

But regarding this PR I think this is fine to use workspaceAccountID here and if we want to make this cross compatible we should get an issue for it to update everything that is necessary.


const selectedBankAccount = useMemo(() => bankAccountList?.[paymentBankAccountID?.toString() ?? ''], [paymentBankAccountID, bankAccountList]);
const bankAccountNumber = useMemo(() => selectedBankAccount?.accountData?.accountNumber ?? '', [selectedBankAccount?.accountData?.accountNumber]);
const settlementAccountEnding = getLastFourDigits(bankAccountNumber);
const domainName = settings?.domainName ?? getDomainNameForPolicy(policyID);
const {environmentURL} = useEnvironment();

const connectionBankAccounts = getConnectionBankAccountsForReconciliation(connections, connectionName);

const goBack = useCallback(() => {
Navigation.goBack(backPath);
}, [backPath]);

const options = useMemo(() => {
if (!bankAccountList || isEmptyObject(bankAccountList)) {
return [];
}
const eligibleBankAccounts = getEligibleBankAccountsForCard(bankAccountList);

return eligibleBankAccounts.map((bankAccount, index) => ({
text: bankAccount.title,
value: bankAccount.accountData?.bankAccountID,
keyForList: bankAccount.accountData?.bankAccountID?.toString() ?? `${bankAccount.title}-${index}`,
isSelected: bankAccount.accountData?.bankAccountID === paymentBankAccountID,
alternateText:
bankAccount.description ??
(bankAccount.accountData?.accountNumber ? `${translate('bankAccount.accountEnding')} ${getLastFourDigits(bankAccount.accountData.accountNumber)}` : ''),
return connectionBankAccounts.map((bankAccount) => ({
text: bankAccount.name,
value: bankAccount.id,
keyForList: bankAccount.id,
isSelected: bankAccount.id === reconciliationBankAccountID,
}));
}, [bankAccountList, paymentBankAccountID, translate]);
}, [connectionBankAccounts, reconciliationBankAccountID]);

const selectBankAccount = (newBankAccountID?: number) => {
if (!programKey) {
Log.alert('[ReconciliationAccountSettingsPage] selectBankAccount called without a detected card program key');
const selectBankAccount = (newBankAccountID?: string) => {
if (!newBankAccountID) {
return;
}
updateSettlementAccount(domainName, defaultFundID, policyID, programKey, newBankAccountID, paymentBankAccountID);
setCardReconciliationAccount(workspaceAccountID, domainName, newBankAccountID, reconciliationBankAccountID);
goBack();
};

Expand Down Expand Up @@ -107,7 +101,7 @@ function DynamicReconciliationAccountSettingsPage({route}: DynamicReconciliation
data={options}
onSelectRow={({value}) => selectBankAccount(value)}
ListItem={RadioListItem}
initiallyFocusedItemKey={paymentBankAccountID?.toString()}
initiallyFocusedItemKey={reconciliationBankAccountID}
/>
</ConnectionLayout>
);
Expand Down
85 changes: 85 additions & 0 deletions tests/unit/CardUtilsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
getCompanyCardDescription,
getCompanyCardFeed,
getCompanyFeeds,
getConnectionBankAccountsForReconciliation,
getCSVFeedType,
getCustomFeedNameFromFeeds,
getCustomOrFormattedFeedName,
Expand Down Expand Up @@ -83,6 +84,7 @@ import type {
WorkspaceCardsList,
} from '@src/types/onyx';
import type {CardFeedWithDomainID, CardFeedWithNumber, CompanyCardFeedWithNumber, CompanyFeeds} from '@src/types/onyx/CardFeeds';
import type {Connections} from '@src/types/onyx/Policy';
import type IconAsset from '@src/types/utils/IconAsset';
import {localeCompare, translateLocal} from '../utils/TestHelper';
import waitForBatchedUpdates from '../utils/waitForBatchedUpdates';
Expand Down Expand Up @@ -4081,3 +4083,86 @@ describe('getEligibleBankAccountsForUkEuCard', () => {
expect(result.at(0)?.accountData?.state).toBe(CONST.BANK_ACCOUNT.STATE.OPEN);
});
});

describe('getConnectionBankAccountsForReconciliation', () => {
it('returns empty array when connections is undefined', () => {
expect(getConnectionBankAccountsForReconciliation(undefined, CONST.POLICY.CONNECTIONS.NAME.QBO)).toEqual([]);
});

it('returns QBO bank accounts mapped to id and name', () => {
const connections = {
quickbooksOnline: {
data: {
bankAccounts: [
{id: 'qbo-1', name: 'QBO Checking'},
{id: 'qbo-2', name: 'QBO Savings'},
],
},
},
} as unknown as Connections;
expect(getConnectionBankAccountsForReconciliation(connections, CONST.POLICY.CONNECTIONS.NAME.QBO)).toEqual([
{id: 'qbo-1', name: 'QBO Checking'},
{id: 'qbo-2', name: 'QBO Savings'},
]);
});

it('returns Xero bank accounts mapped to id and name', () => {
const connections = {
xero: {
data: {
bankAccounts: [{id: 'xero-1', name: 'Xero Account'}],
},
},
} as unknown as Connections;
expect(getConnectionBankAccountsForReconciliation(connections, CONST.POLICY.CONNECTIONS.NAME.XERO)).toEqual([{id: 'xero-1', name: 'Xero Account'}]);
});

it('returns only _bank type accounts for NetSuite', () => {
const connections = {
netsuite: {
options: {
data: {
payableList: [
{id: 'ns-1', name: 'NS Bank', type: '_bank'},
{id: 'ns-2', name: 'NS Credit Card', type: '_creditCard'},
{id: 'ns-3', name: 'NS Bank 2', type: '_bank'},
],
},
},
},
} as unknown as Connections;
expect(getConnectionBankAccountsForReconciliation(connections, CONST.POLICY.CONNECTIONS.NAME.NETSUITE)).toEqual([
{id: 'ns-1', name: 'NS Bank'},
{id: 'ns-3', name: 'NS Bank 2'},
]);
});

it('returns Sage Intacct bank accounts', () => {
const connections = {
intacct: {
data: {
bankAccounts: [{id: 'si-1', name: 'Intacct Account'}],
},
},
} as unknown as Connections;
expect(getConnectionBankAccountsForReconciliation(connections, CONST.POLICY.CONNECTIONS.NAME.SAGE_INTACCT)).toEqual([{id: 'si-1', name: 'Intacct Account'}]);
});

it('returns QBD bank accounts mapped to id and name', () => {
const connections = {
quickbooksDesktop: {
data: {
bankAccounts: [{id: 'qbd-1', name: 'QBD Checking'}],
},
},
} as unknown as Connections;
expect(getConnectionBankAccountsForReconciliation(connections, CONST.POLICY.CONNECTIONS.NAME.QBD)).toEqual([{id: 'qbd-1', name: 'QBD Checking'}]);
});

it('returns empty array when the connection has no bank accounts data', () => {
const connections = {
quickbooksOnline: {},
} as unknown as Connections;
expect(getConnectionBankAccountsForReconciliation(connections, CONST.POLICY.CONNECTIONS.NAME.QBO)).toEqual([]);
});
});
Loading