system-prompts-and-models-o.../salesflow-saas/backend/app/api/v1/dashboard.py
Claude f1852c1121
Add SalesMatic AI Sales SaaS Platform - Complete Foundation
Full-stack AI-powered sales automation platform for Saudi SMEs:

Backend (FastAPI + PostgreSQL):
- Multi-tenant architecture with row-level isolation
- JWT auth with RBAC (owner/manager/agent/admin)
- Lead, Customer, Deal, Pipeline, Activity, Message, Proposal models
- Dashboard analytics API (overview, pipeline, revenue)
- WhatsApp Business API, Email (SMTP/SendGrid), SMS (Unifonic) integrations
- Celery + Redis workers for automated follow-ups and scheduled messages
- Property model for Real Estate module (Riyadh districts)
- Hijri date utilities, Arabic/English localization

Frontend (Next.js + Tailwind):
- Professional Arabic RTL landing page with 10 sections
- Brand identity: SalesMatic (سيلزماتك) with custom SVG logo
- Color system: Trust Blue #0F4C81, Growth Teal #00BFA6, CTA Orange #FF6B35
- IBM Plex Sans Arabic + Inter typography
- Responsive design, dark hero section, pricing table, FAQ

Industry Templates:
- Healthcare/Clinics: pipeline stages, WhatsApp message templates, auto-workflows
- Real Estate Riyadh: 20 districts, property tours, payment plans, matching

Infrastructure:
- Docker Compose (PostgreSQL, Redis, Backend, Celery, Frontend, Nginx)
- Nginx reverse proxy config
- Makefile for common operations

https://claude.ai/code/session_01LLR7jzpyNRwDA9kojtT3CW
2026-03-28 03:06:53 +00:00

79 lines
2.9 KiB
Python

from datetime import datetime, timezone, timedelta
from decimal import Decimal
from fastapi import APIRouter, Depends
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy import select, func
from app.database import get_db
from app.api.deps import get_current_user
from app.models.user import User
from app.models.lead import Lead
from app.models.deal import Deal
from app.models.message import Message
from app.schemas.dashboard import DashboardOverview, PipelineSummary
router = APIRouter()
@router.get("/overview", response_model=DashboardOverview)
async def dashboard_overview(
current_user: User = Depends(get_current_user),
db: AsyncSession = Depends(get_db),
):
tid = current_user.tenant_id
today_start = datetime.now(timezone.utc).replace(hour=0, minute=0, second=0, microsecond=0)
total_leads = (await db.execute(select(func.count()).where(Lead.tenant_id == tid))).scalar() or 0
new_today = (await db.execute(select(func.count()).where(Lead.tenant_id == tid, Lead.created_at >= today_start))).scalar() or 0
total_deals = (await db.execute(select(func.count()).where(Deal.tenant_id == tid))).scalar() or 0
open_value = (await db.execute(
select(func.coalesce(func.sum(Deal.value), 0)).where(Deal.tenant_id == tid, Deal.stage.notin_(["closed_won", "closed_lost"]))
)).scalar() or Decimal("0")
won_value = (await db.execute(
select(func.coalesce(func.sum(Deal.value), 0)).where(Deal.tenant_id == tid, Deal.stage == "closed_won")
)).scalar() or Decimal("0")
won_count = (await db.execute(
select(func.count()).where(Deal.tenant_id == tid, Deal.stage == "closed_won")
)).scalar() or 0
msgs_today = (await db.execute(
select(func.count()).where(Message.tenant_id == tid, Message.created_at >= today_start, Message.direction == "outbound")
)).scalar() or 0
conversion = (won_count / total_leads * 100) if total_leads > 0 else 0
return DashboardOverview(
total_leads=total_leads,
new_leads_today=new_today,
total_deals=total_deals,
open_deals_value=open_value,
closed_won_value=won_value,
closed_won_count=won_count,
messages_sent_today=msgs_today,
conversion_rate=round(conversion, 2),
active_workflows=0,
)
@router.get("/pipeline", response_model=PipelineSummary)
async def pipeline_summary(
current_user: User = Depends(get_current_user),
db: AsyncSession = Depends(get_db),
):
tid = current_user.tenant_id
result = await db.execute(
select(Deal.stage, func.count(), func.coalesce(func.sum(Deal.value), 0))
.where(Deal.tenant_id == tid)
.group_by(Deal.stage)
)
stages = {}
values = {}
for stage, count, value in result.all():
stages[stage] = count
values[stage] = float(value)
return PipelineSummary(stages=stages, total_value_by_stage=values)