completed the task

This commit was merged in pull request #18.
This commit is contained in:
Chinenye Nmoh
2025-05-28 12:45:17 +01:00
parent 2c46a4390c
commit bbb919d933
4 changed files with 33 additions and 36 deletions
+2 -2
View File
@@ -19,6 +19,6 @@ ENV FLASK_RUN_HOST=0.0.0.0
#COPY scripts/enterypointone.sh scripts/enterypointone.sh #COPY scripts/enterypointone.sh scripts/enterypointone.sh
RUN chmod +x scripts/enterypointone.sh RUN chmod +x scripts/entry.sh
ENTRYPOINT ["scripts/enterypointone.sh"] ENTRYPOINT ["scripts/entry.sh"]
+30 -34
View File
@@ -4,34 +4,30 @@ from app.api.services.base_service import BaseService
from app.models.transaction import Transaction from app.models.transaction import Transaction
from app.models.loan import Loan from app.models.loan import Loan
from sqlalchemy import func, desc from sqlalchemy import func, desc
from datetime import datetime, timedelta from datetime import datetime, timedelta, timezone
from app.extensions import db from app.extensions import db
from app.api.enums.transaction_type import TransactionType
class DashboardService(BaseService): class DashboardService(BaseService):
@staticmethod @staticmethod
def get_dashboard_data(): def get_dashboard_data():
"""
Get dashboard summary data.
Returns:
dict: A standardized response with dashboard data.
"""
try: try:
# Get current date and start of the week
now = datetime.now() now = datetime.now()
start_of_week = now - timedelta(days=now.weekday()) start_of_week = now - timedelta(days=now.weekday())
start_of_week = start_of_week.replace(hour=0, minute=0, second=0, microsecond=0) start_of_week = start_of_week.replace(hour=0, minute=0, second=0, microsecond=0)
# Get loans data for the current week # Calculate 24 hours ago
last_24_hours = datetime.now(timezone.utc) - timedelta(hours=24)
# Loans this week
loans_this_week = db.session.query( loans_this_week = db.session.query(
func.sum(Loan.initial_loan_amount) func.sum(Loan.initial_loan_amount)
).filter( ).filter(
Loan.created_at >= start_of_week Loan.created_at >= start_of_week
).scalar() or 0 ).scalar() or 0
# Get payments data for the current week # Payments this week
# Assuming payments are transactions with type 'PAYMENT'
payments_this_week = db.session.query( payments_this_week = db.session.query(
func.count(Transaction.id) func.count(Transaction.id)
).filter( ).filter(
@@ -39,51 +35,53 @@ class DashboardService(BaseService):
Transaction.type == 'PAYMENT' Transaction.type == 'PAYMENT'
).scalar() or 0 ).scalar() or 0
# Get request summary counts
# These are placeholders - needed to adjust based on your actual data model # Request summary for the last 24 hours
eligibility_check_count = db.session.query( eligibility_check_count = db.session.query(
func.count(Transaction.id) func.count(Transaction.id)
).filter( ).filter(
Transaction.type == 'ELIGIBILITY_CHECK' Transaction.type == TransactionType.ELIGIBILITY_CHECK.value,
Transaction.created_at >= last_24_hours
).scalar() or 0 ).scalar() or 0
select_offer_count = db.session.query( select_offer_count = db.session.query(
func.count(Transaction.id) func.count(Transaction.id)
).filter( ).filter(
Transaction.type == 'SELECT_OFFER' Transaction.type == TransactionType.SELECT_OFFER.value,
Transaction.created_at >= last_24_hours
).scalar() or 0 ).scalar() or 0
provide_loan_count = db.session.query( provide_loan_count = db.session.query(
func.count(Transaction.id) func.count(Transaction.id)
).filter( ).filter(
Transaction.type == 'PROVIDE_LOAN' Transaction.type == TransactionType.PROVIDE_LOAN.value,
Transaction.created_at >= last_24_hours
).scalar() or 0 ).scalar() or 0
repayment_count = db.session.query( repayment_count = db.session.query(
func.count(Transaction.id) func.count(Transaction.id)
).filter( ).filter(
Transaction.type == 'REPAYMENT' Transaction.type == TransactionType.REPAYMENT.value,
Transaction.created_at >= last_24_hours
).scalar() or 0 ).scalar() or 0
# Get recent transactions # Recent transactions (not limited to 24 hrs, just latest 15)
recent_transactions = Transaction.query.order_by( recent_transactions = Transaction.query.order_by(
Transaction.id.desc() Transaction.id.desc()
).limit(15).all() ).limit(15).all()
# Format recent transactions recent_transactions_data = [{
recent_transactions_data = [] 'id': t.id,
for transaction in recent_transactions: 'transaction_id': t.transaction_id,
recent_transactions_data.append({ 'account_id': t.account_id,
'id': transaction.id, 'type': t.type,
'transaction_id': transaction.transaction_id, 'channel': t.channel,
'account_id': transaction.account_id, 'created_at': t.created_at.isoformat() if t.created_at else None,
'type': transaction.type, 'updated_at': t.updated_at.isoformat() if t.updated_at else None
'channel': transaction.channel, } for t in recent_transactions]
'created_at': transaction.created_at.isoformat() if transaction.created_at else None,
'updated_at': transaction.updated_at.isoformat() if transaction.updated_at else None
})
# Prepare response data # Final response
dashboard_data = { dashboard_data = {
"loans": { "loans": {
"value": float(loans_this_week), "value": float(loans_this_week),
@@ -110,6 +108,4 @@ class DashboardService(BaseService):
except Exception as e: except Exception as e:
logger.error(f"An error occurred while getting dashboard data: {str(e)}", exc_info=True) logger.error(f"An error occurred while getting dashboard data: {str(e)}", exc_info=True)
return jsonify({ return jsonify({"message": "Internal Server Error"}), 500
"message": "Internal Server Error"
}), 500
+1
View File
@@ -72,6 +72,7 @@ class TransactionService:
'transaction_id': transaction.transaction_id, 'transaction_id': transaction.transaction_id,
'account_id': transaction.account_id, 'account_id': transaction.account_id,
'type': transaction.type, 'type': transaction.type,
'customer_id': transaction.customer_id,
'channel': transaction.channel, 'channel': transaction.channel,
'created_at': transaction.created_at.isoformat(), 'created_at': transaction.created_at.isoformat(),
'updated_at': transaction.updated_at.isoformat() 'updated_at': transaction.updated_at.isoformat()