domain.entities.account¶
src.domain.entities.account
¶
Account domain entity.
Represents a financial account aggregated from a provider connection. Multiple accounts can belong to a single connection (e.g., IRA and brokerage).
Architecture
- Pure domain entity (no infrastructure dependencies)
- Primarily a data container with query methods
- State changes come from provider sync operations
- NO domain events (sync operations handled at application layer)
Reference
- docs/architecture/account-domain-model.md
Usage
from uuid_extensions import uuid7 from src.domain.entities import Account from src.domain.enums import AccountType from src.domain.value_objects import Money from decimal import Decimal
account = Account( id=uuid7(), connection_id=connection.id, provider_account_id="SCHWAB-12345", account_number_masked="**1234", name="Individual Brokerage", account_type=AccountType.BROKERAGE, balance=Money(Decimal("10000.00"), "USD"), currency="USD", )
Classes¶
Account
dataclass
¶
Financial account from a provider connection.
Represents an individual account (brokerage, checking, IRA, etc.) aggregated from an external provider. Accounts are data containers reflecting provider state, not user-managed entities.
Provider Agnostic
Account structure works for any provider (Schwab, Chase, Fidelity). Provider-specific data stored in provider_metadata.
Financial Precision
All monetary values use Money value object with Decimal precision. Never store money as float.
Attributes:
| Name | Type | Description |
|---|---|---|
id |
UUID
|
Unique account identifier (internal). |
connection_id |
UUID
|
FK to ProviderConnection. |
provider_account_id |
str
|
Provider's unique account identifier. |
account_number_masked |
str
|
Masked account number for display (**1234). |
name |
str
|
Account name from provider. |
account_type |
AccountType
|
Type classification (BROKERAGE, CHECKING, etc.). |
balance |
Money
|
Current account balance. |
available_balance |
Money | None
|
Available balance if different (e.g., pending). |
currency |
str
|
ISO 4217 currency code. |
is_active |
bool
|
Whether account is active on provider. |
last_synced_at |
datetime | None
|
Last successful sync timestamp. |
provider_metadata |
dict[str, Any] | None
|
Provider-specific data (unstructured). |
created_at |
datetime
|
Record creation timestamp. |
updated_at |
datetime
|
Last modification timestamp. |
Example
account = Account( ... id=uuid7(), ... connection_id=connection.id, ... provider_account_id="ACC-12345", ... account_number_masked="**1234", ... name="Schwab Brokerage", ... account_type=AccountType.BROKERAGE, ... balance=Money(Decimal("10000.00"), "USD"), ... currency="USD", ... ) account.is_investment_account() True
Source code in src/domain/entities/account.py
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 | |
Functions¶
__post_init__
¶
Validate account after initialization.
Raises:
| Type | Description |
|---|---|
ValueError
|
If required fields are invalid. |
Note
post_init raises ValueError for construction errors. These are programming errors, not business logic failures.
Source code in src/domain/entities/account.py
is_investment_account
¶
Check if account type is investment-related.
Investment accounts can hold securities like stocks, bonds, mutual funds, and ETFs.
Returns:
| Type | Description |
|---|---|
bool
|
True if account type is in investment category. |
Example
account.account_type = AccountType.BROKERAGE account.is_investment_account() True
Source code in src/domain/entities/account.py
is_bank_account
¶
Check if account type is banking-related.
Bank accounts are traditional deposit accounts like checking and savings.
Returns:
| Type | Description |
|---|---|
bool
|
True if account type is in banking category. |
Example
account.account_type = AccountType.CHECKING account.is_bank_account() True
Source code in src/domain/entities/account.py
is_retirement_account
¶
Check if account type is retirement-related.
Retirement accounts have special tax treatment (IRA, 401k, etc.).
Returns:
| Type | Description |
|---|---|
bool
|
True if account type is in retirement category. |
Example
account.account_type = AccountType.IRA account.is_retirement_account() True
Source code in src/domain/entities/account.py
is_credit_account
¶
Check if account type is credit-related.
Credit accounts represent money owed (credit cards, loans).
Returns:
| Type | Description |
|---|---|
bool
|
True if account type is in credit category. |
Example
account.account_type = AccountType.CREDIT_CARD account.is_credit_account() True
Source code in src/domain/entities/account.py
has_available_balance
¶
Check if available balance differs from current balance.
Some accounts distinguish between current balance and available balance (e.g., pending transactions).
Returns:
| Type | Description |
|---|---|
bool
|
True if available_balance is set and differs from balance. |
Example
account.balance = Money(Decimal("1000.00"), "USD") account.available_balance = Money(Decimal("900.00"), "USD") account.has_available_balance() True
Source code in src/domain/entities/account.py
needs_sync
¶
Check if account hasn't been synced within threshold.
Used to identify accounts that need data refresh from provider.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
threshold
|
timedelta
|
Maximum time since last sync. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if last_synced_at is None or older than threshold. |
Example
account.needs_sync(timedelta(hours=1)) True # If not synced in last hour
Source code in src/domain/entities/account.py
get_display_name
¶
Get user-friendly display name for account.
Combines account name with masked number for identification.
Returns:
| Type | Description |
|---|---|
str
|
Display string like "Schwab Brokerage (**1234)". |
Example
account.get_display_name() 'Individual Brokerage (**1234)'
Source code in src/domain/entities/account.py
update_balance
¶
Update balance from provider sync.
Called when syncing account data from provider. Validates currency matches before updating.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
balance
|
Money
|
New current balance from provider. |
required |
available_balance
|
Money | None
|
Optional available balance if different. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
Success |
None
|
Update successful. |
Failure |
error
|
Currency mismatch. |
Side Effects (on success): - Updates balance - Updates available_balance - Updates updated_at timestamp
Source code in src/domain/entities/account.py
update_from_provider
¶
update_from_provider(
name: str | None = None,
is_active: bool | None = None,
provider_metadata: dict[str, Any] | None = None,
) -> Result[None, str]
Update account details from provider sync.
Called when syncing account metadata from provider. Only updates provided fields (None values ignored).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str | None
|
New account name from provider. |
None
|
is_active
|
bool | None
|
Whether account is still active on provider. |
None
|
provider_metadata
|
dict[str, Any] | None
|
Updated provider-specific data. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
Success |
None
|
Update successful. |
Failure |
error
|
Invalid name provided. |
Side Effects (on success): - Updates provided fields - Updates updated_at timestamp
Source code in src/domain/entities/account.py
mark_synced
¶
Record successful sync timestamp.
Called after successful data synchronization with provider.
Returns:
| Name | Type | Description |
|---|---|---|
Success |
None
|
Always succeeds. |
Side Effects
- Updates last_synced_at to current time
- Updates updated_at timestamp
Source code in src/domain/entities/account.py
deactivate
¶
Mark account as inactive.
Called when account is removed from provider or user requests account removal.
Returns:
| Name | Type | Description |
|---|---|---|
Success |
None
|
Always succeeds. |
Side Effects
- Sets is_active to False
- Updates updated_at timestamp
Source code in src/domain/entities/account.py
activate
¶
Mark account as active.
Called when previously inactive account becomes available again.
Returns:
| Name | Type | Description |
|---|---|---|
Success |
None
|
Always succeeds. |
Side Effects
- Sets is_active to True
- Updates updated_at timestamp