domain.protocols.balance_snapshot_repository¶
src.domain.protocols.balance_snapshot_repository
¶
BalanceSnapshotRepository protocol for balance snapshot persistence.
Port (interface) for hexagonal architecture. Infrastructure layer implements this protocol.
Reference
- docs/architecture/balance-tracking-architecture.md
Classes¶
BalanceSnapshotRepository
¶
Bases: Protocol
Balance snapshot repository protocol (port).
Defines the interface for balance snapshot persistence operations. Infrastructure layer provides concrete implementation.
This is a Protocol (not ABC) for structural typing. Implementations don't need to inherit from this.
Methods:
| Name | Description |
|---|---|
find_by_id |
Retrieve snapshot by ID |
find_by_account_id |
Retrieve all snapshots for an account |
find_by_account_id_in_range |
Retrieve snapshots within date range |
find_latest_by_account_id |
Get most recent snapshot for account |
find_by_user_id_in_range |
Retrieve snapshots across all user accounts |
save |
Create snapshot (no update - immutable) |
delete |
Remove snapshot |
Example Implementation
class PostgresBalanceSnapshotRepository: ... async def find_by_id(self, id: UUID) -> BalanceSnapshot | None: ... # Database logic here ... pass
Source code in src/domain/protocols/balance_snapshot_repository.py
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 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 | |
Functions¶
find_by_id
async
¶
Find snapshot by ID.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
snapshot_id
|
UUID
|
Snapshot's unique identifier. |
required |
Returns:
| Type | Description |
|---|---|
BalanceSnapshot | None
|
BalanceSnapshot if found, None otherwise. |
Example
snapshot = await repo.find_by_id(snapshot_id) if snapshot: ... print(snapshot.balance)
Source code in src/domain/protocols/balance_snapshot_repository.py
find_by_account_id
async
¶
find_by_account_id(
account_id: UUID,
source: SnapshotSource | None = None,
limit: int | None = None,
) -> list[BalanceSnapshot]
Find all snapshots for an account.
Results are ordered by captured_at descending (most recent first).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
account_id
|
UUID
|
Account's unique identifier. |
required |
source
|
SnapshotSource | None
|
Optional filter by snapshot source. |
None
|
limit
|
int | None
|
Optional maximum number of results. |
None
|
Returns:
| Type | Description |
|---|---|
list[BalanceSnapshot]
|
List of snapshots (empty if none found). |
Example
snapshots = await repo.find_by_account_id(account_id, limit=30) for s in snapshots: ... print(f"{s.captured_at}: {s.balance}")
Source code in src/domain/protocols/balance_snapshot_repository.py
find_by_account_id_in_range
async
¶
find_by_account_id_in_range(
account_id: UUID,
start_date: datetime,
end_date: datetime,
source: SnapshotSource | None = None,
) -> list[BalanceSnapshot]
Find snapshots for an account within date range.
Results are ordered by captured_at ascending (oldest first) for charting.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
account_id
|
UUID
|
Account's unique identifier. |
required |
start_date
|
datetime
|
Start of date range (inclusive). |
required |
end_date
|
datetime
|
End of date range (inclusive). |
required |
source
|
SnapshotSource | None
|
Optional filter by snapshot source. |
None
|
Returns:
| Type | Description |
|---|---|
list[BalanceSnapshot]
|
List of snapshots within range (empty if none found). |
Example
from datetime import datetime, timedelta end = datetime.now(UTC) start = end - timedelta(days=30) snapshots = await repo.find_by_account_id_in_range( ... account_id, start, end ... )
Source code in src/domain/protocols/balance_snapshot_repository.py
find_latest_by_account_id
async
¶
Find most recent snapshot for an account.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
account_id
|
UUID
|
Account's unique identifier. |
required |
Returns:
| Type | Description |
|---|---|
BalanceSnapshot | None
|
Most recent BalanceSnapshot if found, None otherwise. |
Example
latest = await repo.find_latest_by_account_id(account_id) if latest: ... print(f"Current balance: {latest.balance}")
Source code in src/domain/protocols/balance_snapshot_repository.py
find_by_user_id_in_range
async
¶
find_by_user_id_in_range(
user_id: UUID,
start_date: datetime,
end_date: datetime,
source: SnapshotSource | None = None,
) -> list[BalanceSnapshot]
Find snapshots across all accounts for a user within date range.
Aggregates snapshots from all user's accounts. Results are ordered by captured_at ascending.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_id
|
UUID
|
User's unique identifier. |
required |
start_date
|
datetime
|
Start of date range (inclusive). |
required |
end_date
|
datetime
|
End of date range (inclusive). |
required |
source
|
SnapshotSource | None
|
Optional filter by snapshot source. |
None
|
Returns:
| Type | Description |
|---|---|
list[BalanceSnapshot]
|
List of snapshots across all accounts (empty if none found). |
Note
This requires joining through account → connection → user.
Example
snapshots = await repo.find_by_user_id_in_range( ... user_id, start, end ... )
Group by account for portfolio breakdown¶
Source code in src/domain/protocols/balance_snapshot_repository.py
find_latest_by_user_id
async
¶
Find most recent snapshot for each of user's accounts.
Returns one snapshot per account (the latest for each). Useful for current portfolio summary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_id
|
UUID
|
User's unique identifier. |
required |
Returns:
| Type | Description |
|---|---|
list[BalanceSnapshot]
|
List of latest snapshots, one per account. |
Example
latest_snapshots = await repo.find_latest_by_user_id(user_id) total = sum(s.balance.amount for s in latest_snapshots)
Source code in src/domain/protocols/balance_snapshot_repository.py
save
async
¶
Create snapshot in database.
Snapshots are immutable - this only creates, never updates. Use delete() to remove incorrect snapshots.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
snapshot
|
BalanceSnapshot
|
BalanceSnapshot entity to persist. |
required |
Raises:
| Type | Description |
|---|---|
DatabaseError
|
If database operation fails. |
IntegrityError
|
If snapshot ID already exists. |
Example
snapshot = BalanceSnapshot(...) await repo.save(snapshot)
Source code in src/domain/protocols/balance_snapshot_repository.py
delete
async
¶
Remove snapshot from database.
Hard delete - permanently removes the record. Used to remove erroneous snapshots.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
snapshot_id
|
UUID
|
Snapshot's unique identifier. |
required |
Raises:
| Type | Description |
|---|---|
NotFoundError
|
If snapshot doesn't exist. |
DatabaseError
|
If database operation fails. |
Note
Use with caution - deleting snapshots breaks historical continuity.
Example
await repo.delete(snapshot_id)
Source code in src/domain/protocols/balance_snapshot_repository.py
count_by_account_id
async
¶
Count total snapshots for an account.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
account_id
|
UUID
|
Account's unique identifier. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Total number of snapshots. |
Example
count = await repo.count_by_account_id(account_id) print(f"Account has {count} balance snapshots")