File size: 4,944 Bytes
50c20bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
DB Service Configuration Registration

Add this to your main.py or app initialization:
"""

from services.db_service import DBServiceConfig
from core.models import (
    Base,  # SQLAlchemy declarative base
    User, GeminiJob, PaymentTransaction, Contact,
    RateLimit, ApiKeyUsage, ClientUser, AuditLog
)


def register_db_service_config():
    """Register DB Service configuration at application startup."""
    DBServiceConfig.register(
        # Database metadata (for table creation)
        db_base=Base,
        all_models=[
            User, GeminiJob, PaymentTransaction, Contact,
            RateLimit, ApiKeyUsage, ClientUser, AuditLog
        ],
        
        # Column names
        user_filter_column="user_id",      # Column name for user ownership
        user_id_column="id",               # Column name for user ID
        soft_delete_column="deleted_at",   # Column name for soft delete tracking
        
        # Special models
        special_user_model=User,  # Model that uses 'id' instead of 'user_id'
        
        # ================================================================
        # USER SCOPES (Regular authenticated users)
        # ================================================================
        user_read_scoped=[
            User,               # Users can read own profile
            GeminiJob,          # Users can read own jobs
            PaymentTransaction, # Users can read own payments
            Contact,            # Users can read own contacts
        ],
        
        user_create_scoped=[
            GeminiJob,          # Users can create jobs
            PaymentTransaction, # Users can create payments (via API)
            Contact,            # Users can submit contact forms
        ],
        
        user_update_scoped=[
            User,               # Users can update own profile
            GeminiJob,          # Users can update own jobs
        ],
        
        user_delete_scoped=[
            GeminiJob,          # Users can delete own jobs
            Contact,            # Users can delete own contacts
        ],
        
        # ================================================================
        # ADMIN SCOPES (Administrators only - via ADMIN_EMAILS env var)
        # ================================================================
        admin_read_only=[
            RateLimit,    # Only admins view rate limits
            ApiKeyUsage,  # Only admins view API usage
            ClientUser,   # Only admins view client  mappings
            AuditLog,     # Only admins view audit logs
        ],
        
        admin_create_only=[
            RateLimit,    # Only admins create rate limits
            ApiKeyUsage,  # Only admins create API usage entries
            ClientUser,   # Only admins create client mappings
            AuditLog,     # Only admins create audit entries
        ],
        
        admin_update_only=[
            RateLimit,         # Only admins update rate limits
            ApiKeyUsage,       # Only admins update API settings
            ClientUser,        # Only admins modify client mappings
            PaymentTransaction,# Only admins refund/adjust payments
        ],
        
        admin_delete_only=[
            RateLimit,    # Only admins delete rate limits
            ApiKeyUsage,  # Only admins remove API tracking
            User,         # Only admins delete user accounts
        ],
        
        # ================================================================
        # SYSTEM SCOPES (Background processes - OAuth, webhooks, workers)
        # ================================================================
        system_read_scoped=[
            User, GeminiJob, PaymentTransaction, RateLimit,
            ApiKeyUsage, ClientUser, AuditLog,
        ],
        
        system_create_scoped=[
            User,              # OAuth creates users
            ClientUser,        # Middleware creates client mappings
            AuditLog,          # System creates audit entries
            PaymentTransaction,# Webhooks create transactions
            ApiKeyUsage,       # Middleware tracks API usage
            GeminiJob,         # System creates jobs
            RateLimit,         # Middleware creates rate limits
        ],
        
        system_update_scoped=[
            User,              # OAuth updates profiles
            GeminiJob,         # Workers update job status
            PaymentTransaction,# Webhooks update verification
            ApiKeyUsage,       # System updates API stats
            RateLimit,         # System updates rate counters
            ClientUser,        # System updates mappings
        ],
        
        system_delete_scoped=[
            GeminiJob,         # Cleanup deletes expired jobs
            RateLimit,         # Cleanup deletes old limits
            ApiKeyUsage,       # Cleanup deletes old usage
        ],
    )