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, MembersProfile 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] sub_count = MembersProducts.get_subscription_counts(0) dashboard_data = { "subscription": subscription_data, "loans": { "currency": "Naira", "currency_text": "\u20a6", "text": "this week", "value": sub_count }, "payments": { "currency": "Dollars", "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): logger.info("ENTER :: Getting office custom templates") templates = CustomTemplates.get_custom_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): logger.info('ENTER API::get office account view') try: # member_uid = filters.member_uid member_uid = filters.get('member_uid') account_result = Members.get_member_by_uid(member_uid) account_data = { "id": account_result.id, "uid": str(account_result.uid), "username": account_result.username, "country": account_result.country, "added": account_result.added, "email": account_result.email, "account_name": account_result.account_name, "firstname": account_result.firstname, "lastname": account_result.lastname, "trial_end": account_result.trial_end, } member_id = account_result.id logger.info(f"member_id :: member_uid: {member_id} :: {member_uid} ") # "profile_comp leted": account_result.profile_completed, # "next_billing": account_result.next_billing, membersSubList = MembersProducts.get_member_productlist_by_member_id(account_result.id) member_sub_data = [] if membersSubList: for subs in membersSubList: member_sub_data.append({ 'id': subs.id, 'member_id': subs.member_id, 'subscription_uid': str(subs.uid), 'product_id': subs.product_id, 'internal_url': subs.internal_url, 'external_url': subs.external_url, 'dns_group': subs.dns_group, 'status': subs.status, 'product_template': subs.product_template, 'custom_template': subs.custom_template, 'updated': subs.updated, "added": subs.added, }) member_payments = Payments.get_member_payments_by_member_id(account_result.id) member_payments_data = [] for t in member_payments: member_payments_data.append({ 'id': t.id, 'uid': t.uid, 'option_name': t.option_name, 'option_type': t.option_type, 'payment_uid': t.payment_uid, 'amount': round(t.amount * 0.01, 2), 'status': t.status, 'added': t.added }) account_profile_data = [] current_profile = MembersProfile.get_member_profile_by_member_id(member_id) if current_profile is None: account_profile_data = [] else: account_profile_data = { 'id': current_profile.id, 'profile_uid': current_profile.uid, 'member_id': current_profile.member_id, 'practice': current_profile.practice, 'specialization': current_profile.specialization, 'url_name': current_profile.url_name } account_result = { "account": account_data, "account_profile": account_profile_data, "subscriptions": member_sub_data, "payments": member_payments_data } logger.info('RETURN API::get office account view') logger.info(account_result) return account_result except Exception as e: logger.error(f"An error occurred while getting cusomer data: {str(e)}", exc_info=True) return jsonify({"message": "Internal Server Error"}), 500 @staticmethod def get_subscription_view_data(filters=None): try: if filters is None: filters = {} # Extract filters subscription_uid = filters.get('subscription_uid') member_sub = [] product_templates_data = [] templates_data = [] membersSubResult = MembersProducts.get_member_product_by_subscription_uid(subscription_uid) if membersSubResult: member_sub = { 'id': membersSubResult.id, 'subscription_uid': str(membersSubResult.uid), 'member_id': membersSubResult.member_id, 'product_id': membersSubResult.product_id, 'internal_url': membersSubResult.internal_url, 'external_url': membersSubResult.external_url, 'dns_group': membersSubResult.dns_group, 'product_template': membersSubResult.product_template, 'custom_template': membersSubResult.custom_template, 'status': membersSubResult.status, 'updated': membersSubResult.updated, "added": membersSubResult.added, } templates = CustomTemplates.get_custom_template_for_office(filters) 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, }) product_templates = ProductsTemplates.get_template_by_product_id(membersSubResult.product_id) if product_templates: for t in product_templates: product_templates_data.append({ 'id': t.id, 'template_uid': t.uid, 'product_id': t.product_id, 'provision_name': t.provision_name, 'added': t.added.isoformat() if t.added else None, }) response_data = { 'subscription': member_sub, 'available_templates': product_templates_data, 'available_custom_templates': templates_data } 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