feat: add Telegram bot with notifications and scheduler

This commit is contained in:
Cosmo
2026-02-06 13:16:50 +00:00
parent 5a40127edd
commit 9e467b0448
19 changed files with 1007 additions and 110 deletions

View File

@@ -21,8 +21,8 @@ func NewTaskRepository(db *sqlx.DB) *TaskRepository {
func (r *TaskRepository) Create(task *model.Task) error {
query := `
INSERT INTO tasks (user_id, title, description, icon, color, due_date, priority)
VALUES ($1, $2, $3, $4, $5, $6, $7)
INSERT INTO tasks (user_id, title, description, icon, color, due_date, priority, reminder_time)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
RETURNING id, created_at, updated_at`
return r.db.QueryRow(query,
@@ -33,6 +33,7 @@ func (r *TaskRepository) Create(task *model.Task) error {
task.Color,
task.DueDate,
task.Priority,
task.ReminderTime,
).Scan(&task.ID, &task.CreatedAt, &task.UpdatedAt)
}
@@ -40,13 +41,13 @@ func (r *TaskRepository) GetByID(id, userID int64) (*model.Task, error) {
var task model.Task
query := `
SELECT id, user_id, title, description, icon, color, due_date, priority, completed_at, created_at, updated_at
SELECT id, user_id, title, description, icon, color, due_date, priority, reminder_time, completed_at, created_at, updated_at
FROM tasks WHERE id = $1 AND user_id = $2`
err := r.db.QueryRow(query, id, userID).Scan(
&task.ID, &task.UserID, &task.Title, &task.Description,
&task.Icon, &task.Color, &task.DueDate, &task.Priority,
&task.CompletedAt, &task.CreatedAt, &task.UpdatedAt,
&task.ReminderTime, &task.CompletedAt, &task.CreatedAt, &task.UpdatedAt,
)
if err != nil {
@@ -62,7 +63,7 @@ func (r *TaskRepository) GetByID(id, userID int64) (*model.Task, error) {
func (r *TaskRepository) ListByUser(userID int64, completed *bool) ([]model.Task, error) {
query := `
SELECT id, user_id, title, description, icon, color, due_date, priority, completed_at, created_at, updated_at
SELECT id, user_id, title, description, icon, color, due_date, priority, reminder_time, completed_at, created_at, updated_at
FROM tasks WHERE user_id = $1`
if completed != nil {
@@ -87,7 +88,7 @@ func (r *TaskRepository) ListByUser(userID int64, completed *bool) ([]model.Task
if err := rows.Scan(
&task.ID, &task.UserID, &task.Title, &task.Description,
&task.Icon, &task.Color, &task.DueDate, &task.Priority,
&task.CompletedAt, &task.CreatedAt, &task.UpdatedAt,
&task.ReminderTime, &task.CompletedAt, &task.CreatedAt, &task.UpdatedAt,
); err != nil {
return nil, err
}
@@ -103,7 +104,7 @@ func (r *TaskRepository) GetTodayTasks(userID int64) ([]model.Task, error) {
today := time.Now().Format("2006-01-02")
query := `
SELECT id, user_id, title, description, icon, color, due_date, priority, completed_at, created_at, updated_at
SELECT id, user_id, title, description, icon, color, due_date, priority, reminder_time, completed_at, created_at, updated_at
FROM tasks
WHERE user_id = $1 AND completed_at IS NULL AND due_date <= $2
ORDER BY priority DESC, due_date, created_at`
@@ -121,7 +122,7 @@ func (r *TaskRepository) GetTodayTasks(userID int64) ([]model.Task, error) {
if err := rows.Scan(
&task.ID, &task.UserID, &task.Title, &task.Description,
&task.Icon, &task.Color, &task.DueDate, &task.Priority,
&task.CompletedAt, &task.CreatedAt, &task.UpdatedAt,
&task.ReminderTime, &task.CompletedAt, &task.CreatedAt, &task.UpdatedAt,
); err != nil {
return nil, err
}
@@ -133,11 +134,45 @@ func (r *TaskRepository) GetTodayTasks(userID int64) ([]model.Task, error) {
return tasks, nil
}
func (r *TaskRepository) GetTasksWithReminder(reminderTime string, date string) ([]model.Task, error) {
query := `
SELECT t.id, t.user_id, t.title, t.description, t.icon, t.color, t.due_date, t.priority, t.reminder_time, t.completed_at, t.created_at, t.updated_at
FROM tasks t
JOIN users u ON t.user_id = u.id
WHERE t.reminder_time = $1
AND t.completed_at IS NULL
AND (t.due_date IS NULL OR t.due_date >= $2)
AND u.telegram_chat_id IS NOT NULL
AND u.notifications_enabled = true`
rows, err := r.db.Query(query, reminderTime, date)
if err != nil {
return nil, err
}
defer rows.Close()
var tasks []model.Task
for rows.Next() {
var task model.Task
if err := rows.Scan(
&task.ID, &task.UserID, &task.Title, &task.Description,
&task.Icon, &task.Color, &task.DueDate, &task.Priority,
&task.ReminderTime, &task.CompletedAt, &task.CreatedAt, &task.UpdatedAt,
); err != nil {
return nil, err
}
task.ProcessForJSON()
tasks = append(tasks, task)
}
return tasks, nil
}
func (r *TaskRepository) Update(task *model.Task) error {
query := `
UPDATE tasks
SET title = $2, description = $3, icon = $4, color = $5, due_date = $6, priority = $7, updated_at = CURRENT_TIMESTAMP
WHERE id = $1 AND user_id = $8
SET title = $2, description = $3, icon = $4, color = $5, due_date = $6, priority = $7, reminder_time = $8, updated_at = CURRENT_TIMESTAMP
WHERE id = $1 AND user_id = $9
RETURNING updated_at`
return r.db.QueryRow(query,
@@ -148,6 +183,7 @@ func (r *TaskRepository) Update(task *model.Task) error {
task.Color,
task.DueDate,
task.Priority,
task.ReminderTime,
task.UserID,
).Scan(&task.UpdatedAt)
}