Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add get_recent_transactions option for USSD #26

Merged
merged 1 commit into from
Sep 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
from fastapi.responses import PlainTextResponse
from openg2p_fastapi_common.context import dbengine
from openg2p_fastapi_common.controller import BaseController
from openg2p_g2p_bridge_example_bank_models.models import Account
from openg2p_g2p_bridge_example_bank_models.models import Account, AccountingLog
from sqlalchemy import desc
from sqlalchemy.ext.asyncio import async_sessionmaker
from sqlalchemy.future import select

Expand Down Expand Up @@ -43,11 +44,13 @@ async def ussd(
response = "CON Welcome to Example Bank. What do you want to do? \n \n"
response += "1. Get account balance \n"
response += "2. Initiate transfer \n"
response += "3. Request account statement"
response += "3. See recent transactions"
elif text == "1":
response = await self.get_account_balance(phoneNumber)
elif text == "2":
response = "END Bye!"
elif text == "3":
response = await self.get_recent_transactions(phoneNumber)
else:
response = "END Invalid choice selected!"

Expand Down Expand Up @@ -75,3 +78,40 @@ async def get_account_balance(self, phone_number: str) -> str:
f"END Available balance in account ending with {account.account_number[-4:]} is"
f" ₹{account.available_balance:,.2f}"
)

async def get_recent_transactions(self, phone_number: str) -> str:
_logger.info("Fetching account transactions through USSD")
_logger.info(f"Phone Number: {phone_number}")
phone_number_parsed = phone_number[1:]
_logger.info(f"Parsed Phone Number: {phone_number_parsed}")

session_maker = async_sessionmaker(dbengine.get(), expire_on_commit=False)
async with session_maker() as session:
account_db_query = select(Account).where(
Account.account_holder_phone == phone_number_parsed
)
account_result = await session.execute(account_db_query)
account = account_result.scalars().first()

if not account:
_logger.error("Account not found")
return f"END Account not found for this phone number: {phone_number}"

accounting_logs_query = (
select(AccountingLog)
.where(AccountingLog.account_number == account.account_number)
.order_by(desc(AccountingLog.id))
.limit(3)
)
accounting_log_result = await session.execute(accounting_logs_query)
accounting_logs = accounting_log_result.scalars()
transaction_text = ""
for accounting_log in accounting_logs:
date_formatted = accounting_log.transaction_date.strftime(
"%d/%b"
).upper() # Format and convert to uppercase
credit_debit_type = (
"CR" if accounting_log.debit_credit == "CREDIT" else "DR"
)
transaction_text += f"{credit_debit_type} - ₹{accounting_log.transaction_amount:,.2f} - {date_formatted} - {accounting_log.narrative_1} \n"
return transaction_text
Loading