File size: 7,847 Bytes
45efbb3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import resend
from typing import List
from Backend.core.config import settings
from Backend.core.logging import get_logger

logger = get_logger(__name__)


class EmailService:
    def __init__(self):
        self.sender_email = settings.sender_email
        if settings.resend_api_key:
            resend.api_key = settings.resend_api_key
        else:
            logger.warning("Resend API key not configured")
    
    async def send_email(
        self,
        to: List[str],
        subject: str,
        body: str,
        html: bool = False
    ) -> bool:
        if not settings.resend_api_key:
            logger.warning("Resend API key not configured. Email not sent.")
            logger.info(f"Would send email to {to}: {subject}")
            return False
        
        try:
            for recipient in to:
                try:
                    params = {
                        "from": self.sender_email,
                        "to": [recipient],
                        "subject": subject,
                    }
                    
                    if html:
                        params["html"] = body
                    else:
                        params["text"] = body
                    
                    resend.Emails.send(params)
                    logger.info(f"Email sent successfully to {recipient}")
                except Exception as e:
                    logger.error(f"Failed to send email to {recipient}: {e}")
                    return False
            
            return True
            
        except Exception as e:
            logger.error(f"Email service error: {e}")
            return False
    
    async def send_assignment_email(
        self,
        worker_email: str,
        worker_name: str,
        issue_id: str,
        category: str,
        priority: str,
        location: str,
        description: str
    ):
        subject = f"πŸ”” New Task Assigned: {category} [{priority}]"
        
        body = f"""
Hello {worker_name},

You have been assigned a new task in CityTrack.

ISSUE DETAILS
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Issue ID: {issue_id}
Category: {category}
Priority: {priority}
Location: {location}
Description: {description or 'No description provided'}

NEXT STEPS
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
1. Review the issue details in your worker dashboard
2. Navigate to the location
3. Resolve the issue
4. Upload proof of resolution

Thank you for your service!

CityTrack Team
"Governance at the Speed of Software"
"""
        
        return await self.send_email([worker_email], subject, body)
    
    async def send_manual_review_email(
        self,
        issue_id: str,
        reason: str,
        category: str,
        location: str,
        image_url: str
    ):
        subject = f"⚠️ Manual Review Required: {category}"
        
        body = f"""
Admin Team,

An issue requires manual review in CityTrack.

ISSUE DETAILS
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Issue ID: {issue_id}
Category: {category}
Location: {location}
Reason: {reason}

Image: {image_url}

ACTION REQUIRED
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Please review this issue in the admin dashboard and take appropriate action.

CityTrack System
"""
        
        return await self.send_email([settings.admin_email], subject, body)
    
    async def send_completion_email(
        self,
        user_email: str,
        issue_id: str,
        category: str,
        location: str,
        resolution_notes: str
    ):
        subject = f"βœ… Your Report Has Been Resolved: {category}"
        
        body = f"""
Dear Citizen,

Great news! Your reported issue has been resolved.

ISSUE DETAILS
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Issue ID: {issue_id}
Category: {category}
Location: {location}
Resolution: {resolution_notes or 'Issue has been successfully addressed'}

FEEDBACK
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
We value your input! Please confirm if the issue has been resolved by checking the app.

Thank you for making our city better!

CityTrack Team
"Governance at the Speed of Software"
"""
        
        return await self.send_email([user_email], subject, body)
    
    async def send_escalation_email(
        self,
        admin_email: str,
        issue_id: str,
        category: str,
        priority: str,
        reason: str,
        escalation_level: int
    ):
        subject = f"🚨 ESCALATION LEVEL {escalation_level}: {category}"
        
        body = f"""
URGENT: Issue Escalation

An issue has been escalated and requires immediate attention.

ISSUE DETAILS
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Issue ID: {issue_id}
Category: {category}
Priority: {priority}
Escalation Level: {escalation_level}
Reason: {reason}

IMMEDIATE ACTION REQUIRED
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Please review and address this issue immediately in the admin dashboard.

CityTrack System
"""
        
        return await self.send_email([admin_email], subject, body)
    
    async def send_confirmation_request_email(
        self,
        user_email: str,
        issue_id: str,
        category: str,
        confirmation_link: str
    ):
        subject = f"πŸ” Please Confirm: Is This Issue Resolved?"
        
        body = f"""
Dear Citizen,

Your reported issue has been marked as resolved by our team.

ISSUE DETAILS
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Issue ID: {issue_id}
Category: {category}

CONFIRMATION NEEDED
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Please confirm if the issue has been properly resolved:
{confirmation_link}

Your feedback helps us improve our service quality.

Thank you!

CityTrack Team
"""
        
        return await self.send_email([user_email], subject, body)
    
    async def send_issue_accepted_email(
        self,
        user_email: str,
        issue_id: str,
        category: str,
        priority: str,
        location: str,
        accepted_by: str = "automatic",
        tracking_url: str = None
    ):
        acceptance_type = "automatically" if accepted_by == "automatic" else "manually by our team"
        subject = f"βœ“ Your Report Has Been Accepted: {category}"
        
        body = f"""
Dear Citizen,

Thank you for reporting an issue! Your report has been accepted {acceptance_type}.

ISSUE DETAILS
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Issue ID: {issue_id}
Category: {category}
Priority: {priority}
Location: {location}

WHAT HAPPENS NEXT
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
1. Your issue has been assigned to the appropriate department
2. A field worker will be dispatched to address it
3. You will receive updates on the progress
4. Once resolved, you'll get a confirmation notification

TRACK YOUR REPORT
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
{tracking_url or 'Check the CityTrack app for real-time updates'}

Thank you for helping make our city better!

CityTrack Team
"Governance at the Speed of Software"
"""
        
        return await self.send_email([user_email], subject, body)


email_service = EmailService()