feat: apply category rules on creation

This commit is contained in:
2026-08-20 09:18:17 +03:00
parent 5f282d199d
commit 6515e73ead
7 changed files with 43 additions and 15 deletions

View File

@@ -1,5 +1,11 @@
# Changelog # Changelog
## [Backend 0.7.5 / Shared 0.3.1] - 2026-08-20
### Added
- Creating a category rule now applies it atomically to all matching unconfirmed transactions and returns the affected count.
## [Backend 0.7.4] - 2026-08-20 ## [Backend 0.7.4] - 2026-08-20
### Fixed ### Fixed

View File

@@ -1,6 +1,6 @@
{ {
"name": "@family-budget/backend", "name": "@family-budget/backend",
"version": "0.7.4", "version": "0.7.5",
"private": true, "private": true,
"scripts": { "scripts": {
"dev": "tsx watch src/app.ts", "dev": "tsx watch src/app.ts",

View File

@@ -55,22 +55,38 @@ export async function getRules(params: GetCategoryRulesParams): Promise<Category
export async function createRule( export async function createRule(
body: CreateCategoryRuleRequest, body: CreateCategoryRuleRequest,
): Promise<CategoryRule> { ): Promise<CategoryRule & { applied: number }> {
const pattern = body.pattern.trim(); const pattern = body.pattern.trim();
const matchType = body.matchType ?? 'contains'; const matchType = body.matchType ?? 'contains';
const priority = body.priority ?? 100; const priority = body.priority ?? 100;
const requiresConfirmation = body.requiresConfirmation ?? false; const requiresConfirmation = body.requiresConfirmation ?? false;
const { rows } = await pool.query( const client = await pool.connect();
`INSERT INTO category_rules (pattern, match_type, category_id, priority, requires_confirmation) try {
VALUES ($1, $2, $3, $4, $5) await client.query('BEGIN');
RETURNING *`, const { rows } = await client.query(
[pattern, matchType, body.categoryId, priority, requiresConfirmation], `INSERT INTO category_rules (pattern, match_type, category_id, priority, requires_confirmation)
); VALUES ($1, $2, $3, $4, $5) RETURNING *`,
[pattern, matchType, body.categoryId, priority, requiresConfirmation],
const catResult = await pool.query('SELECT name FROM categories WHERE id = $1', [body.categoryId]); );
rows[0].category_name = catResult.rows[0].name; const match = matchType === 'starts_with'
return toRule(rows[0]); ? `t.description ILIKE $1 || '%'`
: `t.description ILIKE '%' || $1 || '%'`;
const applied = await client.query(
`UPDATE transactions t SET category_id = $2, is_category_confirmed = $3, updated_at = NOW()
WHERE (t.category_id IS NULL OR t.is_category_confirmed = FALSE) AND ${match}`,
[pattern, body.categoryId, !requiresConfirmation],
);
const catResult = await client.query('SELECT name FROM categories WHERE id = $1', [body.categoryId]);
rows[0].category_name = catResult.rows[0].name;
await client.query('COMMIT');
return { ...toRule(rows[0]), applied: applied.rowCount ?? 0 };
} catch (error) {
await client.query('ROLLBACK');
throw error;
} finally {
client.release();
}
} }
export async function updateRule( export async function updateRule(

View File

@@ -1,5 +1,6 @@
import type { import type {
CategoryRule, CategoryRule,
CreateCategoryRuleResponse,
GetCategoryRulesParams, GetCategoryRulesParams,
CreateCategoryRuleRequest, CreateCategoryRuleRequest,
UpdateCategoryRuleRequest, UpdateCategoryRuleRequest,
@@ -24,8 +25,8 @@ export async function getCategoryRules(
export async function createCategoryRule( export async function createCategoryRule(
data: CreateCategoryRuleRequest, data: CreateCategoryRuleRequest,
): Promise<CategoryRule> { ): Promise<CreateCategoryRuleResponse> {
return api.post<CategoryRule>('/api/category-rules', data); return api.post<CreateCategoryRuleResponse>('/api/category-rules', data);
} }
export async function updateCategoryRule( export async function updateCategoryRule(

View File

@@ -1,6 +1,6 @@
{ {
"name": "@family-budget/shared", "name": "@family-budget/shared",
"version": "0.3.0", "version": "0.3.1",
"private": true, "private": true,
"main": "dist/index.js", "main": "dist/index.js",
"types": "dist/index.d.ts", "types": "dist/index.d.ts",

View File

@@ -26,6 +26,10 @@ export interface CreateCategoryRuleRequest {
requiresConfirmation?: boolean; requiresConfirmation?: boolean;
} }
export interface CreateCategoryRuleResponse extends CategoryRule {
applied: number;
}
export interface UpdateCategoryRuleRequest { export interface UpdateCategoryRuleRequest {
pattern?: string; pattern?: string;
categoryId?: number; categoryId?: number;

View File

@@ -24,6 +24,7 @@ export type {
export type { export type {
CategoryRule, CategoryRule,
CreateCategoryRuleResponse,
GetCategoryRulesParams, GetCategoryRulesParams,
CreateCategoryRuleRequest, CreateCategoryRuleRequest,
UpdateCategoryRuleRequest, UpdateCategoryRuleRequest,