File size: 3,775 Bytes
f3997d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Service for user settings management.
"""
from sqlalchemy.orm import Session
from typing import Optional, Dict
from datetime import datetime

from app.database.models import UserSettings


class SettingsService:
    """Service for managing user settings and preferences."""
    
    @staticmethod
    def get_or_create_settings(db: Session, user_id: str) -> UserSettings:
        """
        Get user settings or create default if not exists.
        
        Args:
            db: Database session
            user_id: User ID
            
        Returns:
            UserSettings object
        """
        settings = db.query(UserSettings).filter(UserSettings.user_id == user_id).first()
        
        if not settings:
            # Create default settings
            settings = UserSettings(user_id=user_id)
            db.add(settings)
            db.commit()
            db.refresh(settings)
        
        return settings
    
    @staticmethod
    def update_profile(
        db: Session,
        user_id: str,
        bio: Optional[str] = None,
        phone: Optional[str] = None,
        company: Optional[str] = None
    ) -> UserSettings:
        """
        Update user profile settings.
        
        Args:
            db: Database session
            user_id: User ID
            bio: User bio
            phone: Phone number
            company: Company name
            
        Returns:
            Updated UserSettings object
        """
        settings = SettingsService.get_or_create_settings(db, user_id)
        
        if bio is not None:
            settings.bio = bio
        if phone is not None:
            settings.phone = phone
        if company is not None:
            settings.company = company
        
        settings.updated_at = datetime.utcnow()
        db.commit()
        db.refresh(settings)
        
        return settings
    
    @staticmethod
    def update_appearance(
        db: Session,
        user_id: str,
        theme: str
    ) -> UserSettings:
        """
        Update appearance settings.
        
        Args:
            db: Database session
            user_id: User ID
            theme: Theme preference (light, dark, system)
            
        Returns:
            Updated UserSettings object
        """
        if theme not in ['light', 'dark', 'system']:
            raise ValueError("Theme must be 'light', 'dark', or 'system'")
        
        settings = SettingsService.get_or_create_settings(db, user_id)
        settings.theme = theme
        settings.updated_at = datetime.utcnow()
        
        db.commit()
        db.refresh(settings)
        
        return settings
    
    @staticmethod
    def update_notifications(
        db: Session,
        user_id: str,
        email_notifications: Optional[bool] = None,
        update_notifications: Optional[bool] = None
    ) -> UserSettings:
        """
        Update notification settings.
        
        Args:
            db: Database session
            user_id: User ID
            email_notifications: Enable/disable email notifications
            update_notifications: Enable/disable update notifications
            
        Returns:
            Updated UserSettings object
        """
        settings = SettingsService.get_or_create_settings(db, user_id)
        
        if email_notifications is not None:
            settings.email_notifications = 1 if email_notifications else 0
        if update_notifications is not None:
            settings.update_notifications = 1 if update_notifications else 0
        
        settings.updated_at = datetime.utcnow()
        db.commit()
        db.refresh(settings)
        
        return settings


# Global service instance
settings_service = SettingsService()