This repository was archived by the owner on Mar 17, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathdb.py
More file actions
173 lines (145 loc) · 5.34 KB
/
Copy pathdb.py
File metadata and controls
173 lines (145 loc) · 5.34 KB
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
import logging
from datetime import datetime
from config import supabase
from utils import decrypt, encrypt
logger = logging.getLogger(__name__)
async def get_user_settings_by_telegram_chat_id(chat_id: str):
logger.info(f"Looking up user settings for chat_id: {chat_id}")
try:
response = (
supabase.table("UserSettings")
.select("user_id, timezone_offset, timetable_start, timetable_end")
.eq("telegram_id", chat_id)
.limit(1)
.single()
.execute()
)
if response.data:
logger.info(f"Found settings for chat_id: {chat_id}")
return response.data
else:
logger.warning(f"No user settings found for chat_id: {chat_id}")
return None
except Exception as e:
logger.error(f"Error fetching user settings for chat_id {chat_id}: {e}")
return None
async def fetch_schedule_for_user_in_range(
user_id: str, range_start: datetime, range_end: datetime
):
try:
logger.info(
f"Fetching tasks for user {user_id} between {range_start.isoformat()} and {range_end.isoformat()}"
)
response = (
supabase.table("tasks")
.select("*")
.eq("user_id", user_id)
.gte("scheduled_time", range_start.isoformat())
.lt("scheduled_time", range_end.isoformat())
.order("scheduled_time", desc=False)
.execute()
)
if response.data:
for task in response.data:
if "title" in task:
task["title"] = decrypt(task["title"])
return response.data
except Exception as e:
logger.error(f"Error fetching tasks for user {user_id}: {e}")
return []
async def update_user_timezone(chat_id: str, offset_str: str):
try:
response = (
supabase.table("UserSettings")
.update({"timezone_offset": offset_str})
.eq("telegram_id", chat_id)
.execute()
)
logger.info(f"Updated timezone for chat_id {chat_id}: {response.data}")
return response.data
except Exception as e:
logger.error(f"Error updating timezone for chat_id {chat_id}: {e}")
return None
async def create_task(new_task: dict):
try:
if "title" in new_task:
new_task["title"] = encrypt(new_task["title"])
response = supabase.table("tasks").insert(new_task).execute()
if response.data:
for task in response.data:
if "title" in task:
task["title"] = decrypt(task["title"])
return response.data
except Exception as e:
logger.error(f"Error creating task: {e}")
return None
async def get_users_for_reminders():
try:
response = (
supabase.table("UserSettings")
.select("user_id, telegram_id, timezone_offset")
.eq("notifications", True)
.not_.is_("telegram_id", "null")
.execute()
)
return response.data
except Exception as e:
logger.error(f"Error fetching users for reminders: {e}")
return []
async def get_tasks_for_reminder(
user_id: str, start_naive: datetime, end_naive: datetime
):
try:
response = (
supabase.table("tasks")
.select("title, scheduled_time")
.eq("user_id", user_id)
.eq("is_completed", False)
.gte("scheduled_time", start_naive.isoformat())
.lt("scheduled_time", end_naive.isoformat())
.execute()
)
if response.data:
for task in response.data:
if "title" in task:
task["title"] = decrypt(task["title"])
return response.data
except Exception as e:
logger.error(f"Error fetching tasks for reminder: {e}")
return []
async def fetch_external_events_for_user_in_range(
user_id: str, range_start: datetime, range_end: datetime
):
"""Fetch external calendar events for a user within a date range.
External events are stored in UTC and need to be converted to local time for display.
The title is NOT encrypted unlike regular tasks.
"""
try:
logger.info(
f"Fetching external events for user {user_id} between {range_start.isoformat()} and {range_end.isoformat()}"
)
# First get all calendar sources for this user
sources_response = (
supabase.table("calendar_sources")
.select("id")
.eq("user_id", user_id)
.execute()
)
if not sources_response.data:
logger.info(f"No calendar sources found for user {user_id}")
return []
source_ids = [source["id"] for source in sources_response.data]
# Now fetch external events for these sources within the time range
events_response = (
supabase.table("external_events")
.select("*")
.in_("source_id", source_ids)
.gte("start_time", range_start.isoformat())
.lt("start_time", range_end.isoformat())
.order("start_time", desc=False)
.execute()
)
return events_response.data or []
except Exception as e:
logger.error(f"Error fetching external events for user {user_id}: {e}")
return []