Skip to content

Commit eb3110a

Browse files
committed
✨ [feat][backend] Extend read txn API to support txn id filter
1 parent 9d2fdba commit eb3110a

4 files changed

Lines changed: 25 additions & 2 deletions

File tree

backend/kayman/crud/transaction.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ def create_transactions(
2727

2828
def read_transactions(
2929
session: Session,
30+
transaction_id: int | None = None,
3031
account_id: int | None = None,
3132
start: datetime | None = None,
3233
end: datetime | None = None,
@@ -35,6 +36,8 @@ def read_transactions(
3536
descending: bool = False,
3637
) -> Sequence[Transaction]:
3738
scalar = select(Transaction)
39+
if transaction_id is not None:
40+
scalar = scalar.where(Transaction.id == transaction_id)
3841
if account_id:
3942
scalar = scalar.where(Transaction.account_id == account_id)
4043
if event_id == "empty":

backend/kayman/logics/transaction.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ def get_transactions_with_running_balance(
1919
if start is not None
2020
else Decimal(0)
2121
)
22-
txns = read_transactions(session, account_id, start, end, order_by="created_at")
22+
txns = read_transactions(
23+
session, account_id=account_id, start=start, end=end, order_by="created_at"
24+
)
2325
running = opening
2426
results: list[TransactionReadWithBalance] = []
2527
for txn in txns:

backend/kayman/routers/transaction.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,17 @@ def create(
4444
def reads(
4545
*,
4646
session: Session = Depends(get_session),
47+
transaction_id: int | None = None,
4748
account_id: int | None = None,
4849
start: datetime | None = None,
4950
end: datetime | None = None,
5051
event_id: int | Literal["empty"] | None = None,
5152
) -> Sequence[TransactionBase]:
52-
return read_transactions(session, account_id, start, end, event_id=event_id)
53+
return read_transactions(
54+
session,
55+
transaction_id=transaction_id,
56+
account_id=account_id,
57+
start=start,
58+
end=end,
59+
event_id=event_id,
60+
)

backend/kayman/tests/crud/test_transaction.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,16 @@ def test_read_transactions_all(session: Session):
107107
assert len(read_transactions(session)) == 10
108108

109109

110+
def test_read_transactions_by_id(session: Session):
111+
txn_1 = TransactionFactory()
112+
TransactionFactory()
113+
114+
results = read_transactions(session, transaction_id=txn_1.id)
115+
116+
assert len(results) == 1
117+
assert results[0].id == txn_1.id
118+
119+
110120
def test_read_transactions_by_account(session: Session):
111121
account_1 = AccountFactory()
112122
account_2 = AccountFactory()

0 commit comments

Comments
 (0)