From a8d4ac003be2813ad9453df49986188141be6ff1 Mon Sep 17 00:00:00 2001 From: Daniel Bisgrove Date: Wed, 16 Oct 2024 09:12:00 -0400 Subject: [PATCH] Edits to the export to ensure the actual amounts are shown without rounding or the currency sign --- .../[[...financialAccount]].page.test.tsx | 41 ++++------------ .../AccountTransactions.test.tsx | 26 +++++----- .../AccountTransactions.tsx | 49 +++++++++++-------- 3 files changed, 52 insertions(+), 64 deletions(-) diff --git a/pages/accountLists/[accountListId]/reports/financialAccounts/[[...financialAccount]].page.test.tsx b/pages/accountLists/[accountListId]/reports/financialAccounts/[[...financialAccount]].page.test.tsx index dd032cc24..d93066e79 100644 --- a/pages/accountLists/[accountListId]/reports/financialAccounts/[[...financialAccount]].page.test.tsx +++ b/pages/accountLists/[accountListId]/reports/financialAccounts/[[...financialAccount]].page.test.tsx @@ -26,6 +26,13 @@ const defaultRouter = { query: { accountListId }, isReady: true, }; +const entriesRouter = { + query: { + accountListId, + financialAccount: [financialAccountId, 'entries'], + }, + isReady: true, +}; const Components = ({ router = defaultRouter }: { router?: object }) => ( @@ -85,17 +92,7 @@ describe('Financial Accounts Page', () => { it('should show the transactions page for a financial account', async () => { const { findByText, findByRole, getByText, queryByText, queryByRole } = - render( - , - ); + render(); expect(await findByText('Account 1')).toBeInTheDocument(); @@ -112,17 +109,7 @@ describe('Financial Accounts Page', () => { }); it('should open filters on load and set initial date Range filter', async () => { - const { findByRole } = render( - , - ); + const { findByRole } = render(); expect( await findByRole('heading', { name: 'Filter (1)' }), @@ -131,15 +118,7 @@ describe('Financial Accounts Page', () => { it('should open and close filters and menu', async () => { const { findByRole, getByRole, queryByRole } = render( - , + , ); // Filters diff --git a/src/components/Reports/FinancialAccountsReport/AccountTransactions/AccountTransactions.test.tsx b/src/components/Reports/FinancialAccountsReport/AccountTransactions/AccountTransactions.test.tsx index 08ac0fb3e..0b2936960 100644 --- a/src/components/Reports/FinancialAccountsReport/AccountTransactions/AccountTransactions.test.tsx +++ b/src/components/Reports/FinancialAccountsReport/AccountTransactions/AccountTransactions.test.tsx @@ -3,6 +3,7 @@ import { ThemeProvider } from '@mui/material/styles'; import { LocalizationProvider } from '@mui/x-date-pickers'; import { AdapterLuxon } from '@mui/x-date-pickers/AdapterLuxon'; import { fireEvent, render, waitFor } from '@testing-library/react'; +import { Settings } from 'luxon'; import { SnackbarProvider } from 'notistack'; import { I18nextProvider } from 'react-i18next'; import TestRouter from '__tests__/util/TestRouter'; @@ -20,11 +21,7 @@ import { FinancialAccountContext, FinancialAccountType, } from '../Context/FinancialAccountsContext'; -import { - AccountTransactions, - defaultEndDate, - defaultStartDate, -} from './AccountTransactions'; +import { AccountTransactions } from './AccountTransactions'; import { financialAccountEntriesMock } from './AccountTransactionsMocks'; import { FinancialAccountEntriesQuery } from './financialAccountTransactions.generated'; @@ -96,6 +93,9 @@ const Components = ({ ); describe('Financial Account Transactions', () => { + beforeEach(() => { + Settings.now = () => new Date(2024, 7, 31).valueOf(); + }); describe('Resetting filters', () => { it('should reset the activeFilters with the filters from the url filters', () => { render( @@ -113,8 +113,8 @@ describe('Financial Account Transactions', () => { expect(setActiveFilters).not.toHaveBeenCalledWith({ dateRange: { - min: defaultStartDate, - max: defaultEndDate, + min: '2024-08-01', + max: '2024-08-31', }, }); @@ -129,8 +129,8 @@ describe('Financial Account Transactions', () => { expect(setActiveFilters).toHaveBeenCalledWith({ dateRange: { - min: defaultStartDate, - max: defaultEndDate, + min: '2024-08-01', + max: '2024-08-31', }, }); }); @@ -143,6 +143,7 @@ describe('Financial Account Transactions', () => { expect(getAllByText('category1Code')).toHaveLength(2); // Header + expect(getByText('8/31/2024')).toBeInTheDocument(); expect(getByText('Closing Balance')).toBeInTheDocument(); expect(getByText('UAH 280,414')).toBeInTheDocument(); @@ -165,6 +166,7 @@ describe('Financial Account Transactions', () => { expect(getByText('UAH 37')).toBeInTheDocument(); // Footer + expect(getByText('8/1/2024')).toBeInTheDocument(); expect(getByText('Opening Balance')).toBeInTheDocument(); expect(getByText('UAH 202,240')).toBeInTheDocument(); @@ -312,9 +314,9 @@ describe('Financial Account Transactions', () => { const csvContentArray = [ ['Date', 'Payee', 'Memo', 'Outflow', 'Inflow'], - ['8/9/2024', 'description1', 'category1Name', '', 'UAH 7,048'], - ['8/8/2024', 'description2', 'category1Name', 'UAH 15,008', ''], - ['8/7/2024', 'description3', 'category2Name', '', 'UAH 37'], + ['8/9/2024', 'description1', 'category1Name', '', '7047.28'], + ['8/8/2024', 'description2', 'category1Name', '15008', ''], + ['8/7/2024', 'description3', 'category2Name', '', '36.2'], ]; const csvContent = diff --git a/src/components/Reports/FinancialAccountsReport/AccountTransactions/AccountTransactions.tsx b/src/components/Reports/FinancialAccountsReport/AccountTransactions/AccountTransactions.tsx index 3a60253bf..b6538c4f0 100644 --- a/src/components/Reports/FinancialAccountsReport/AccountTransactions/AccountTransactions.tsx +++ b/src/components/Reports/FinancialAccountsReport/AccountTransactions/AccountTransactions.tsx @@ -9,8 +9,7 @@ import { headerHeight } from 'src/components/Shared/Header/ListHeader'; import { useDebouncedValue } from 'src/hooks/useDebounce'; import useGetAppSettings from 'src/hooks/useGetAppSettings'; import { useLocale } from 'src/hooks/useLocale'; -import { currencyFormat, dateFormatShort } from 'src/lib/intlFormat'; -import { formatNumber } from '../AccountSummary/AccountSummaryHelper'; +import { dateFormatShort } from 'src/lib/intlFormat'; import { FinancialAccountContext, FinancialAccountType, @@ -27,19 +26,31 @@ const Container = styled(Box)(() => ({ overflowY: 'auto', })); -const formatDateRange = (startDate?: DateTime, endDate?: DateTime) => { - if (!startDate) { - startDate = DateTime.local().minus({ months: 1 }).plus({ days: 1 }); +/** + * Converts the "amount" string to a number to remove ".0" + * If the value is 0 or isExpense is true, it returns the value as is. + * Otherwise, it removes the '-' character if present, or prepends it if absent. + */ +const formatAmountForExport = ( + amount?: string | null, + isExpense?: boolean, +): number => { + if (!amount) { + return 0; } - if (!endDate) { - endDate = DateTime.local(); + + if (amount === '0' || isExpense) { + return Number(amount); } - return `${startDate.toISODate()}..${endDate.toISODate()}`; + return -Number(amount); }; -const defaultDateRange = formatDateRange(); -export const defaultStartDate = defaultDateRange.split('..')[0]; -export const defaultEndDate = defaultDateRange.split('..')[1]; +const formatDateRange = (startDate?: DateTime, endDate?: DateTime) => { + const minDate = + startDate ?? DateTime.local().minus({ months: 1 }).plus({ days: 1 }); + const maxDate = endDate ?? DateTime.local(); + return `${minDate.toISODate()}..${maxDate.toISODate()}`; +}; export const AccountTransactions: React.FC = () => { const { query } = useRouter(); @@ -69,6 +80,10 @@ export const AccountTransactions: React.FC = () => { }; }, [query?.filters]); + const defaultDateRange = useMemo(() => formatDateRange(), []); + const defaultStartDate = defaultDateRange.split('..')[0]; + const defaultEndDate = defaultDateRange.split('..')[1]; + useEffect(() => { if (!hasActiveFilters && !query?.filters) { setActiveFilters({ @@ -129,18 +144,10 @@ export const AccountTransactions: React.FC = () => { entry.description ?? '', entry.category?.name ?? entry.category?.code ?? '', entry.type === FinancialAccountEntryTypeEnum.Debit - ? currencyFormat( - formatNumber(entry.amount), - entry.currency, - locale, - ).replace(/[\xA0\u2000-\u200B\uFEFF]/g, ' ') + ? `${formatAmountForExport(entry.amount, true)}` : '', entry.type === FinancialAccountEntryTypeEnum.Credit - ? currencyFormat( - formatNumber(entry.amount), - entry.currency, - locale, - ).replace(/[\xA0\u2000-\u200B\uFEFF]/g, ' ') + ? `${formatAmountForExport(entry.amount)}` : '', ], ];