Files
MermsCoreFlask/app/api/services/office_dashboard.py
T
CHIEFSOFT\ameye 3282815627 account profile
2025-09-22 09:26:23 -04:00

203 lines
6.6 KiB
Python

from urllib import request
from flask import jsonify
from app.utils.logger import logger
from app.api.services.base_service import BaseService
from sqlalchemy import func, desc
from datetime import datetime, timedelta, timezone
from app.extensions import db
from app.models import MembersProducts, Products, Payments, Members, CustomTemplates, ProductsTemplates
class OfficeDashboardService(BaseService):
@staticmethod
def get_payments_data(filters):
try:
if filters is None:
filters = {}
# Extract filters
option_name = filters.get('option_name')
# member_id = filters.get('member_id')
# Extract pagination parameters
page = int(filters.get('page', 1))
limit = int(filters.get('limit', 20))
# Ensure page and limit are valid
if page < 1:
page = 1
if limit < 1 or limit > 100:
limit = 20
membersPayList, total_count = Payments.get_all_payments(option_name, None, page, limit)
# Convert loans to dictionary format
member_sub_data = []
for subs in membersPayList:
member_sub_data.append({
'id': subs.id,
'member_id': subs.member_id,
'option_name': subs.option_name,
'option_type': subs.option_type,
'payment_uid': subs.payment_uid,
'amount': subs.amount * 0.01,
'status': subs.status,
'sub_start': subs.sub_start,
'sub_stop': subs.sub_stop,
"added": subs.added,
})
# Calculate total pages
total_pages = (total_count + limit - 1) // limit
response_data = {
'payments': member_sub_data,
'count': len(member_sub_data),
'pagination': {
'total_count': total_count,
'total_pages': total_pages,
'current_page': page,
'limit': limit,
'has_next': page < total_pages,
'has_prev': page > 1
}
}
return response_data
except Exception as e:
logger.error(f"An error occurred while getting dashboard data: {str(e)}", exc_info=True)
return jsonify({"message": "Internal Server Error"}), 500
@staticmethod
def get_dashboard_data():
try:
subscription_data = []
subscription = MembersProducts.get_dash_recent_subscription(15)
if subscription:
subscription_data = [{
'id': t.id,
'uid': t.uid,
'product_id': t.product_id,
'internal_url': t.internal_url,
'external_url': t.external_url,
'dns_group': t.dns_group,
'status': t.status,
'added': t.added,
'updated': t.updated
} for t in subscription]
dashboard_data = {
"subscription": subscription_data,
"loans": {
"currency": "Naira",
"currency_text": "\u20a6",
"text": "this week",
"value": 159999.0
},
"payments": {
"currency": "Naira",
"currency_text": "\u0024",
"text": "this week",
"value": 0
},
"request_summary": {
"eligibility_check": {
"Started": 6
},
"provide_loan": {
"Scheduled": 2
},
"repayment": {
"Processing": 0
},
"select_offer": {
"Completed": 3
}
}
}
return dashboard_data
except Exception as e:
logger.error(f"An error occurred while getting dashboard data: {str(e)}", exc_info=True)
return jsonify({"message": "Internal Server Error"}), 500
@staticmethod
def get_office_products(filters):
products = Products.get_user_product_list(0)
product_data = []
for t in products:
product_data.append({
'id': t.id,
'uid': t.uid,
'product_id': t.product_id,
'description': t.description,
'name': t.name,
'status': t.status,
'added': t.added.isoformat() if t.added else None,
'updated': t.updated.isoformat() if t.updated else None,
'banner': t.banner,
})
products_result = {
"products": product_data,
}
return products_result
@staticmethod
def get_office_product_templates(filters):
templates = ProductsTemplates.get_template_for_office(filters)
templates_data = []
if templates:
for t in templates:
templates_data.append({
'id': t.id,
'uid': t.uid,
'product_id': t.product_id,
'provision_name': t.provision_name,
'added': t.added.isoformat() if t.added else None,
})
templates_result = {
"templates": templates_data,
}
return templates_result
@staticmethod
def get_office_custom_templates(filters):
templates = CustomTemplates.get_template_for_office(filters)
templates_data = []
if templates:
for t in templates:
templates_data.append({
'id': t.id,
'uid': t.uid,
'custom_id': t.custom_id,
'provision_name': t.provision_name,
'added': t.added.isoformat() if t.added else None,
})
templates_result = {
"templates": templates_data,
}
return templates_result
@staticmethod
def get_office_account_view(filters):
member_uid = filters.member_uid
account_data = Members.get_member_by_uid(member_uid)
account_result = {
"account": account_data,
"account_profile": [],
"subscriptions": [],
"payments": []
}
return account_result