diff --git a/CHANGELOG.md b/CHANGELOG.md index 5d2fc69..4ee5baa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,17 @@ # Changelog +## [Frontend 0.11.1] - 2026-08-21 + +### Fixed + +- Preserve the explicit «Без категории» analytics filter value when requesting data. + +## [Frontend 0.11.0 / Backend 0.10.1 / Shared 0.5.0] - 2026-08-21 + +### Fixed + +- Corrected analytics period boundaries for partial week/month ranges and added a usable «Без категории» filter with SQL coverage. + ## [Backend 0.9.2] - 2026-08-20 ### Added diff --git a/backend/package.json b/backend/package.json index abac951..1660344 100644 --- a/backend/package.json +++ b/backend/package.json @@ -1,6 +1,6 @@ { "name": "@family-budget/backend", - "version": "0.9.2", + "version": "0.10.1", "private": true, "scripts": { "dev": "tsx watch src/app.ts", diff --git a/backend/src/routes/analytics.ts b/backend/src/routes/analytics.ts index c5608ed..42065bf 100644 --- a/backend/src/routes/analytics.ts +++ b/backend/src/routes/analytics.ts @@ -8,7 +8,7 @@ const router = Router(); router.get( '/summary', asyncHandler(async (req, res) => { - const { from, to, accountId, onlyConfirmed } = req.query; + const { from, to, accountId, categoryId, onlyConfirmed } = req.query; if (!from || !to) { res.status(400).json({ error: 'BAD_REQUEST', message: 'from and to are required' }); return; @@ -18,6 +18,7 @@ router.get( from: from as string, to: to as string, accountId: accountId ? Number(accountId) : undefined, + categoryId: categoryId !== undefined ? Number(categoryId) : undefined, onlyConfirmed: onlyConfirmed === 'true', }); res.json(result); @@ -27,7 +28,7 @@ router.get( router.get( '/by-category', asyncHandler(async (req, res) => { - const { from, to, accountId, onlyConfirmed } = req.query; + const { from, to, accountId, categoryId, onlyConfirmed } = req.query; if (!from || !to) { res.status(400).json({ error: 'BAD_REQUEST', message: 'from and to are required' }); return; @@ -37,6 +38,7 @@ router.get( from: from as string, to: to as string, accountId: accountId ? Number(accountId) : undefined, + categoryId: categoryId !== undefined ? Number(categoryId) : undefined, onlyConfirmed: onlyConfirmed === 'true', }); res.json(result); @@ -62,7 +64,7 @@ router.get( from: from as string, to: to as string, accountId: accountId ? Number(accountId) : undefined, - categoryId: categoryId ? Number(categoryId) : undefined, + categoryId: categoryId !== undefined ? Number(categoryId) : undefined, onlyConfirmed: onlyConfirmed === 'true', granularity: granularity as Granularity, }); diff --git a/backend/src/services/analytics.integration.test.ts b/backend/src/services/analytics.integration.test.ts index cfbd548..7c4e3aa 100644 --- a/backend/src/services/analytics.integration.test.ts +++ b/backend/src/services/analytics.integration.test.ts @@ -28,15 +28,33 @@ async function testQueries(): Promise { ); await insert(otherAccountId, '2026-07-01T12:00:00+03:00', -99_000, categoryId.expense, 'analytics-test-6'); await insert(accountId, '2026-08-01T12:00:00+03:00', -7_000, categoryId.expense, 'analytics-test-7'); + await client.query( + "INSERT INTO transactions (account_id, operation_at, amount_signed, commission, description, direction, fingerprint, category_id, is_category_confirmed) VALUES ($1, '2026-07-07T12:00:00+03:00', -2_500, 0, 'uncategorized', 'expense', 'analytics-test-uncategorized', NULL, FALSE)", + [accountId], + ); const params = { from: '2026-07-01', to: '2026-07-31', accountId, onlyConfirmed: true }; const summary = await getSummary(params, client); assert.deepEqual({ expense: summary.totalExpense, income: summary.totalIncome, net: summary.net, transferOut: summary.transferOutflow, interest: summary.interestIncome }, { expense: 6_000, income: 15_000, net: 9_000, transferOut: 3_000, interest: 1_500 }); + const categorySummary = await getSummary({ ...params, categoryId: categoryId.expense }, client); + assert.deepEqual({ expense: categorySummary.totalExpense, income: categorySummary.totalIncome }, { expense: 6_000, income: 0 }); + const uncategorized = await getSummary({ ...params, categoryId: 0, onlyConfirmed: false }, client); + assert.equal(uncategorized.totalExpense, 2_500); + assert.equal((await getSummary({ ...params, categoryId: 0, onlyConfirmed: true }, client)).totalExpense, 0); + assert.deepEqual((await getByCategory({ ...params, categoryId: 0, onlyConfirmed: false }, client)).map((item) => item.amount), [2_500]); + assert.deepEqual((await getByCategory({ ...params, categoryId: 0, onlyConfirmed: true }, client)), []); assert.equal((await getSummary({ ...params, from: '2026-08-01', to: '2026-08-31' }, client)).totalExpense, 7_000); assert.deepEqual((await getByCategory(params, client)).map((item) => item.amount), [6_000]); const timeseries = await getTimeseries({ ...params, granularity: 'month' }, client); assert.equal(timeseries[0].expenseAmount, 6_000); assert.equal(timeseries[0].incomeAmount, 15_000); + const partial = await getTimeseries({ ...params, from: '2026-07-02', to: '2026-07-03', granularity: 'month' }, client); + assert.equal(partial[0].expenseAmount, 0); + assert.equal(partial[0].incomeAmount, 20_000); + const uncategorizedSeries = await getTimeseries({ ...params, categoryId: 0, onlyConfirmed: false, granularity: 'month' }, client); + assert.equal(uncategorizedSeries[0].expenseAmount, 2_500); + const confirmedUncategorizedSeries = await getTimeseries({ ...params, categoryId: 0, onlyConfirmed: true, granularity: 'month' }, client); + assert.equal(confirmedUncategorizedSeries[0].expenseAmount, 0); } finally { await client.query('ROLLBACK'); client.release(); diff --git a/backend/src/services/analytics.ts b/backend/src/services/analytics.ts index fdbf0e2..a34dc75 100644 --- a/backend/src/services/analytics.ts +++ b/backend/src/services/analytics.ts @@ -14,6 +14,7 @@ interface BaseParams { from: string; to: string; accountId?: number; + categoryId?: number; onlyConfirmed?: boolean; } @@ -61,6 +62,13 @@ function buildBaseConditions( values.push(params.accountId); idx++; } + if (params.categoryId != null) { + conditions.push(params.categoryId === 0 ? 't.category_id IS NULL' : `t.category_id = $${idx}`); + if (params.categoryId !== 0) { + values.push(params.categoryId); + idx++; + } + } if (params.onlyConfirmed) { conditions.push('t.is_category_confirmed = TRUE'); } @@ -196,6 +204,8 @@ export async function getTimeseries( } const txConditions: string[] = [ + 't.operation_at::date >= $1::date', + 't.operation_at::date <= $2::date', 't.operation_at::date >= p.period_start', 't.operation_at::date <= p.period_end', ]; @@ -207,8 +217,11 @@ export async function getTimeseries( values.push(params.accountId); } if (params.categoryId != null) { - txConditions.push(`t.category_id = $${idx++}`); - values.push(params.categoryId); + if (params.categoryId === 0) txConditions.push('t.category_id IS NULL'); + else { + txConditions.push(`t.category_id = $${idx++}`); + values.push(params.categoryId); + } } if (params.onlyConfirmed) { txConditions.push('t.is_category_confirmed = TRUE'); diff --git a/frontend/package.json b/frontend/package.json index 261e832..5bc25f4 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -1,6 +1,6 @@ { "name": "@family-budget/frontend", - "version": "0.10.2", + "version": "0.11.1", "private": true, "type": "module", "scripts": { diff --git a/frontend/src/pages/AnalyticsPage.tsx b/frontend/src/pages/AnalyticsPage.tsx index 5783aac..46e9ec6 100644 --- a/frontend/src/pages/AnalyticsPage.tsx +++ b/frontend/src/pages/AnalyticsPage.tsx @@ -1,12 +1,14 @@ import { useState, useEffect, useCallback } from 'react'; import type { Account, + Category, AnalyticsSummaryResponse, ByCategoryItem, TimeseriesItem, Granularity, } from '@family-budget/shared'; import { getAccounts } from '../api/accounts'; +import { getCategories } from '../api/categories'; import { getSummary, getByCategory, getTimeseries } from '../api/analytics'; import { PeriodSelector, @@ -29,8 +31,10 @@ function getDefaultPeriod(): PeriodState { export function AnalyticsPage() { const [period, setPeriod] = useState(getDefaultPeriod); const [accountId, setAccountId] = useState(''); + const [categoryId, setCategoryId] = useState(''); const [onlyConfirmed, setOnlyConfirmed] = useState(false); const [accounts, setAccounts] = useState([]); + const [categories, setCategories] = useState([]); const [summary, setSummary] = useState( null, ); @@ -40,6 +44,7 @@ export function AnalyticsPage() { useEffect(() => { getAccounts().then(setAccounts).catch(() => {}); + getCategories({ isActive: true }).then(setCategories).catch(() => {}); }, []); const fetchAll = useCallback(async () => { @@ -63,6 +68,7 @@ export function AnalyticsPage() { from: period.from, to: period.to, ...(accountId ? { accountId: Number(accountId) } : {}), + ...(categoryId !== '' ? { categoryId: Number(categoryId) } : {}), ...(onlyConfirmed ? { onlyConfirmed: true } : {}), }; @@ -80,7 +86,7 @@ export function AnalyticsPage() { } finally { setLoading(false); } - }, [period, accountId, onlyConfirmed]); + }, [period, accountId, categoryId, onlyConfirmed]); useEffect(() => { fetchAll(); @@ -122,6 +128,14 @@ export function AnalyticsPage() { Только подтверждённые +
+ + +
diff --git a/shared/package.json b/shared/package.json index a065070..d3fd683 100644 --- a/shared/package.json +++ b/shared/package.json @@ -1,6 +1,6 @@ { "name": "@family-budget/shared", - "version": "0.4.0", + "version": "0.5.0", "private": true, "main": "dist/index.js", "types": "dist/index.d.ts", diff --git a/shared/src/types/analytics.ts b/shared/src/types/analytics.ts index 1b03b35..8f35548 100644 --- a/shared/src/types/analytics.ts +++ b/shared/src/types/analytics.ts @@ -4,6 +4,7 @@ export interface AnalyticsSummaryParams { from: string; to: string; accountId?: number; + categoryId?: number; onlyConfirmed?: boolean; } @@ -31,6 +32,7 @@ export interface ByCategoryParams { from: string; to: string; accountId?: number; + categoryId?: number; onlyConfirmed?: boolean; }