Files
finance/backend/app/categorization/models.py
T
k1nq d582f15681 feat: add tests for import polling, login page, money utilities, and query keys
- Implemented tests for import polling functionality, ensuring correct behavior for active and terminal import statuses.
- Created tests for the login page, validating user interactions and error handling.
- Added unit tests for money utility functions, covering parsing, formatting, and summation of monetary values.
- Developed tests for query keys, ensuring proper scoping and invalidation roots.

chore: setup testing environment and configuration

- Added setup file for testing with Vitest and Testing Library.
- Configured TypeScript and Vite for the frontend project.
- Created Docker and Nginx configurations for local development and production environments.
- Established a PostgreSQL initialization script for setting up application roles.

docs: add README and environment configuration for infra

- Included README for the infra directory, detailing local development setup and service descriptions.
- Created an example environment configuration file for local development with placeholders for sensitive information.
2026-09-14 09:26:33 +05:00

114 lines
4.0 KiB
Python

import uuid
from sqlalchemy import (
Boolean,
ForeignKey,
ForeignKeyConstraint,
Integer,
String,
UniqueConstraint,
Uuid,
)
from sqlalchemy.orm import Mapped, mapped_column
from app.db.base import Base, PkMixin, TimestampMixin
class IncomeSource(Base, PkMixin, TimestampMixin):
__tablename__ = "income_sources"
__table_args__ = (
UniqueConstraint("space_id", "name", name="uq_income_sources_space_name"),
UniqueConstraint("space_id", "id", name="uq_income_sources_space_id"),
)
space_id: Mapped[uuid.UUID] = mapped_column(
Uuid, ForeignKey("financial_spaces.id", ondelete="CASCADE"), nullable=False, index=True
)
name: Mapped[str] = mapped_column(String(200), nullable=False)
archived: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False)
class Payee(Base, PkMixin, TimestampMixin):
__tablename__ = "payees"
__table_args__ = (
UniqueConstraint("space_id", "name", name="uq_payees_space_name"),
UniqueConstraint("space_id", "id", name="uq_payees_space_id"),
)
space_id: Mapped[uuid.UUID] = mapped_column(
Uuid, ForeignKey("financial_spaces.id", ondelete="CASCADE"), nullable=False, index=True
)
name: Mapped[str] = mapped_column(String(200), nullable=False)
archived: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False)
class PayeeAlias(Base, PkMixin, TimestampMixin):
__tablename__ = "payee_aliases"
__table_args__ = (
UniqueConstraint("space_id", "alias", name="uq_payee_aliases_space_alias"),
ForeignKeyConstraint(
["space_id", "payee_id"],
["payees.space_id", "payees.id"],
ondelete="CASCADE",
name="fk_payee_aliases_payee_tenant",
),
)
space_id: Mapped[uuid.UUID] = mapped_column(
Uuid, ForeignKey("financial_spaces.id", ondelete="CASCADE"), nullable=False, index=True
)
payee_id: Mapped[uuid.UUID] = mapped_column(Uuid, nullable=False, index=True)
alias: Mapped[str] = mapped_column(String(300), nullable=False)
class CategorizationRule(Base, PkMixin, TimestampMixin):
"""Confirmed payee rule: auto-applies category to matching future operations."""
__tablename__ = "categorization_rules"
__table_args__ = (
UniqueConstraint("space_id", "payee_id", name="uq_rules_space_payee"),
ForeignKeyConstraint(
["space_id", "payee_id"],
["payees.space_id", "payees.id"],
ondelete="CASCADE",
name="fk_rules_payee_tenant",
),
ForeignKeyConstraint(
["space_id", "category_id"],
["categories.space_id", "categories.id"],
name="fk_rules_category_tenant",
),
)
space_id: Mapped[uuid.UUID] = mapped_column(
Uuid, ForeignKey("financial_spaces.id", ondelete="CASCADE"), nullable=False, index=True
)
payee_id: Mapped[uuid.UUID] = mapped_column(Uuid, nullable=False)
category_id: Mapped[uuid.UUID] = mapped_column(Uuid, nullable=False)
created_by_user_id: Mapped[uuid.UUID | None] = mapped_column(
Uuid, ForeignKey("users.id", ondelete="SET NULL"), nullable=True
)
class MccCode(Base):
"""Global versioned MCC dictionary (not space-scoped, outside RLS)."""
__tablename__ = "mcc_codes"
code: Mapped[int] = mapped_column(Integer, primary_key=True)
title: Mapped[str] = mapped_column(String(300), nullable=False)
dictionary_version: Mapped[int] = mapped_column(nullable=False, default=1)
class BankCategoryMapping(Base, PkMixin):
"""Maps a bank's raw category string to an MCC for suggestion purposes."""
__tablename__ = "bank_category_mappings"
__table_args__ = (
UniqueConstraint("bank_code", "bank_category", name="uq_bank_mappings_bank_category"),
)
bank_code: Mapped[str] = mapped_column(String(32), nullable=False, index=True)
bank_category: Mapped[str] = mapped_column(String(200), nullable=False)
mcc_code: Mapped[int | None] = mapped_column(Integer, nullable=True)