File size: 7,469 Bytes
4a2ab42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9b95c00
 
4a2ab42
 
 
 
 
 
 
 
9b95c00
 
4a2ab42
 
 
9b95c00
 
4a2ab42
 
 
 
 
 
 
 
 
9b95c00
 
4a2ab42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
"""
API Documentation Configuration using FastAPI/OpenAPI
Provides comprehensive API documentation with examples and schemas
"""

from fastapi import FastAPI
from fastapi.openapi.utils import get_openapi


def custom_openapi_schema(app: FastAPI):
    """Generate custom OpenAPI schema with comprehensive documentation"""
    if app.openapi_schema:
        return app.openapi_schema

    openapi_schema = get_openapi(
        title="Zenith Fraud Detection Platform API",
        version="2.0.0",
        description="""
## Zenith Fraud Detection Platform

### Overview
Comprehensive fraud detection and investigation platform with AI-powered analysis,
multi-modal evidence processing, and advanced network visualization capabilities.

### Key Features
- πŸ” **Secure Authentication**: JWT-based authentication with MFA support
- πŸ•΅οΈ **Fraud Detection**: Advanced ML algorithms for transaction analysis
- πŸ“Š **Case Management**: Complete investigation workflow management
- 🌐 **Network Analysis**: Entity relationship mapping and visualization
- πŸ“ **Evidence Processing**: Multi-modal file analysis (images, documents, videos)
- πŸ“ˆ **Analytics & Reporting**: Real-time dashboards and custom reports

### Authentication
All endpoints (except `/auth/login` and `/auth/register`) require Bearer token authentication:

```
Authorization: Bearer <your_access_token>
```

### Rate Limiting
- **Per Minute**: 60 requests
- **Per Hour**: 1000 requests

### Error Responses
All errors follow this format:
```json
{
    "detail": "Error message",
    "status_code": 400
}
```

### Support
- Documentation: https://docs.zenith.com
- Support Email: support@zenith.com
- GitHub: https://github.com/zenith/fraud-detection
        """,
        routes=app.routes,
        servers=[
            {"url": "https://api.zenith.com", "description": "Production server"},
            {"url": "https://staging.zenith.com", "description": "Staging server"},
            {"url": "http://localhost:8000", "description": "Development server"},
        ],
        tags=[
            {
                "name": "Authentication",
                "description": "User authentication and authorization endpoints. "
                "Includes login, registration, MFA setup, and token management.",
            },
            {
                "name": "Fraud Detection",
                "description": "Fraud detection and analysis endpoints. "
                "Real-time transaction analysis, risk scoring, and alert generation.",
            },
            {
                "name": "Case Management",
                "description": "Investigation case management. "
                "Create, update, assign, and close fraud investigation cases.",
            },
            {
                "name": "Evidence",
                "description": "Evidence upload and management. "
                "Multi-modal file analysis including OCR, image analysis, and metadata extraction.",
            },
            {
                "name": "Network Analysis",
                "description": "Entity and relationship network analysis. "
                "Graph-based fraud detection and pattern recognition.",
            },
            {
                "name": "Analytics",
                "description": "Analytics and reporting endpoints. "
                "Statistics, metrics, and custom report generation.",
            },
            {
                "name": "Admin",
                "description": "Administrative functions. "
                "User management, system configuration, and audit logs. "
                "**Requires ADMIN role**.",
            },
            {
                "name": "Health",
                "description": "System health and monitoring. "
                "Health checks, metrics, and system status.",
            },
        ],
    )

    # Add security schemes
    openapi_schema["components"]["security_schemes"] = {
        "BearerAuth": {
            "type": "http",
            "scheme": "bearer",
            "bearer_format": "JWT",
            "description": "JWT token obtained from /auth/login endpoint",
        }
    }

    # Add example responses
    openapi_schema["components"]["examples"] = {
        "SuccessfulLogin": {
            "summary": "Successful login response",
            "value": {
                "access_token": "eyJ0eXAiOiJKV1QiLCJhbGc...",
                "refresh_token": "eyJ0eXAiOiJKV1QiLCJhbGc...",
                "token_type": "bearer",
                "expires_in": 1800,
            },
        },
        "FraudAnalysis": {
            "summary": "Fraud analysis result",
            "value": {
                "transaction_id": "TXN001",
                "fraud_score": 0.85,
                "risk_level": "HIGH",
                "risk_factors": [
                    "large_amount",
                    "unusual_location",
                    "rapid_transactions",
                ],
                "recommendation": "REVIEW_REQUIRED",
            },
        },
        "CaseCreated": {
            "summary": "Newly created case",
            "value": {
                "case_id": "CASE-2025-001",
                "title": "Suspicious Transaction Investigation",
                "status": "OPEN",
                "priority": "HIGH",
                "created_at": "2025-12-17T05:00:00Z",
                "assigned_to": "investigator@example.com",
            },
        },
    }

    # Add common schemas
    openapi_schema["components"]["schemas"]["Error"] = {
        "type": "object",
        "properties": {
            "detail": {"type": "string", "description": "Error message"},
            "status_code": {"type": "integer", "description": "HTTP status code"},
        },
    }

    app.openapi_schema = openapi_schema
    return app.openapi_schema


def setup_api_documentation(app: FastAPI):
    """Setup comprehensive API documentation"""

    # Override default OpenAPI schema
    app.openapi = lambda: custom_openapi_schema(app)

    # Configure Swagger UI
    app.swagger_ui_parameters = {
        "deep_linking": True,
        "display_request_duration": True,
        "filter": True,
        "show_extensions": True,
        "show_common_extensions": True,
        "syntaxHighlight.theme": "monokai",
    }

    return app


# Example endpoint documentation
"""
@router.post(
    "/fraud/analyze",
    tags=["Fraud Detection"],
    summary="Analyze transaction for fraud",
    description=\"\"\"
    Analyzes a transaction using machine learning models to detect potential fraud.

    Returns a fraud score (0-1) and risk level classification.
    Scores above 0.75 are considered high risk and should be reviewed.
    \"\"\",
    response_description="Fraud analysis result with score and risk factors",
    responses={
        200: {
            "description": "Successful analysis",
            "content": {
                "application/json": {
                    "example": {
                        "transaction_id": "TXN001",
                        "fraud_score": 0.85,
                        "risk_level": "HIGH",
                        "risk_factors": ["large_amount", "unusual_location"]
                    }
                }
            }
        },
        401: {"description": "Unauthorized - Invalid or missing token"},
        422: {"description": "Validation Error - Invalid transaction data"}
    }
)
async def analyze_transaction(transaction: TransactionData):
    pass
"""