acupedia-api/app/features/admin/services/__init__.py

148 lines
5.8 KiB
Python

from __future__ import annotations
from sqlalchemy.ext.asyncio import AsyncSession
from fastapi.responses import HTMLResponse, StreamingResponse
from app.db.models import AppUser
from ..schema import (
AdminCreateSuperAdminRequest,
AdminPromoteUserRequest,
AdminRefundRequest,
AdminRefundResponse,
AdminSyncResponse,
AdminUserListResponse,
CreateCouponRequest,
CreatePlanRequest,
CustomerStatusUpdateRequest,
ModuleStatusUpdateRequest,
ModuleUpdateRequest,
PlanPricingUpdateRequest,
PlanStatusUpdateRequest,
UpdateCouponRequest,
)
from .module_service import AdminModuleService
from .plan_service import AdminPlanService
from .coupon_service import AdminCouponService
from .customer_service import AdminCustomerService
from .payment_service import AdminPaymentService
from .database_service import AdminDatabaseService
from .report_service import AdminReportService
__all__ = [
"AdminModuleService",
"AdminPlanService",
"AdminCouponService",
"AdminCustomerService",
"AdminPaymentService",
"AdminDatabaseService",
"AdminReportService",
"AdminService",
]
class AdminService:
"""
Unified Admin Service Facade aggregating specialized domain services:
- modules: AdminModuleService
- plans: AdminPlanService
- coupons: AdminCouponService
- customers: AdminCustomerService
- payments: AdminPaymentService
- database: AdminDatabaseService
- reports: AdminReportService
"""
def __init__(self, session: AsyncSession) -> None:
self.session = session
self.modules = AdminModuleService(session)
self.plans = AdminPlanService(session)
self.coupons = AdminCouponService(session)
self.customers = AdminCustomerService(session)
self.payments = AdminPaymentService(session)
self.database = AdminDatabaseService(session)
self.reports = AdminReportService(session)
# ── Modules Facade ──
async def list_modules(self) -> list[dict]:
return await self.modules.list_modules()
async def update_module_status(self, module_id: str, payload: ModuleStatusUpdateRequest) -> dict:
return await self.modules.update_status(module_id, payload)
async def update_module(self, module_id: str, payload: ModuleUpdateRequest) -> dict:
return await self.modules.update_details(module_id, payload)
# ── Plans Facade ──
async def list_plans(self) -> list[dict]:
return await self.plans.list_plans()
async def create_plan(self, payload: CreatePlanRequest, admin: AppUser) -> dict:
return await self.plans.create_plan(payload, admin)
async def update_plan_pricing(self, plan_code: str, payload: PlanPricingUpdateRequest) -> dict:
return await self.plans.update_pricing(plan_code, payload)
async def toggle_plan_status(self, plan_code: str, is_active: bool) -> dict:
return await self.plans.toggle_status(plan_code, is_active)
# ── Coupons Facade ──
async def list_coupons(self) -> list[dict]:
return await self.coupons.list_coupons()
async def create_coupon(self, payload: CreateCouponRequest, admin: AppUser) -> dict:
return await self.coupons.create_coupon(payload, admin)
async def update_coupon(self, coupon_id: str, payload: UpdateCouponRequest) -> dict:
return await self.coupons.update_coupon(coupon_id, payload)
async def toggle_coupon_status(self, coupon_id: str, is_active: bool) -> dict:
return await self.coupons.toggle_status(coupon_id, is_active)
async def delete_coupon(self, coupon_id: str) -> dict:
return await self.coupons.delete_coupon(coupon_id)
# ── Customer & Users Facade ──
async def list_users(self, super_admin_only: bool) -> list[AdminUserListResponse]:
return await self.customers.list_users(super_admin_only)
async def promote(self, request: AdminPromoteUserRequest, admin: AppUser) -> dict:
return await self.customers.promote(request, admin)
async def create_super_admin(self, request: AdminCreateSuperAdminRequest, admin: AppUser) -> dict:
return await self.customers.create_super_admin(request, admin)
async def get_customer_detail(self, user_id: str) -> dict:
return await self.customers.get_customer_detail(user_id)
async def update_customer_status(self, user_id: str, is_active: bool, admin: AppUser) -> dict:
return await self.customers.update_customer_status(user_id, is_active, admin)
# ── Payments & Invoicing Facade ──
async def process_refund(self, request: AdminRefundRequest, admin: AppUser) -> AdminRefundResponse:
return await self.payments.process_refund(request, admin)
async def force_sync(self) -> AdminSyncResponse:
return await self.payments.force_sync()
async def subscriptions(self) -> list[dict]:
return await self.payments.subscriptions()
async def get_or_create_invoice(self, payment_id: str) -> tuple[str | None, str, str]:
return await self.payments.get_or_create_invoice(payment_id)
async def share_invoice(self, payment_id: str) -> dict:
return await self.payments.share_invoice(payment_id)
async def render_invoice_html(self, payment_id: str) -> HTMLResponse:
return await self.payments.render_invoice_html(payment_id)
# ── Database & Telemetry Facade ──
async def get_database_info(self) -> dict:
return await self.database.get_database_info()
async def download_database_backup(self, scope: str = "full") -> StreamingResponse:
return await self.database.download_database_backup(scope)
# ── Reports Facade ──
async def export_report_csv(self, report_type: str, start_date: str | None, end_date: str | None) -> StreamingResponse:
return await self.reports.export_report_csv(report_type, start_date, end_date)