feat(savings): Add savings module with categories, transactions, recurring plans

- Categories: regular, deposits, credits, recurring, multi-user, accounts
- Transactions: deposits and withdrawals with user tracking
- Recurring plans: monthly payment obligations per user
- Stats: overdues calculation with allocation algorithm
- Excludes is_account categories from total sums
- Documentation: docs/SAVINGS.md
This commit is contained in:
Cosmo
2026-02-16 06:48:09 +00:00
parent 9e90aa6d95
commit 2a50e50771
18 changed files with 2910 additions and 162 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, reminder_time)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
INSERT INTO tasks (user_id, title, description, icon, color, due_date, priority, reminder_time, is_recurring, recurrence_type, recurrence_interval, recurrence_end_date, parent_task_id)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13)
RETURNING id, created_at, updated_at`
return r.db.QueryRow(query,
@@ -34,6 +34,11 @@ func (r *TaskRepository) Create(task *model.Task) error {
task.DueDate,
task.Priority,
task.ReminderTime,
task.IsRecurring,
task.RecurrenceType,
task.RecurrenceInterval,
task.RecurrenceEndDate,
task.ParentTaskID,
).Scan(&task.ID, &task.CreatedAt, &task.UpdatedAt)
}
@@ -41,13 +46,17 @@ 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, reminder_time, completed_at, created_at, updated_at
SELECT id, user_id, title, description, icon, color, due_date, priority, reminder_time, completed_at,
is_recurring, recurrence_type, recurrence_interval, recurrence_end_date, parent_task_id,
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.ReminderTime, &task.CompletedAt, &task.CreatedAt, &task.UpdatedAt,
&task.ReminderTime, &task.CompletedAt,
&task.IsRecurring, &task.RecurrenceType, &task.RecurrenceInterval, &task.RecurrenceEndDate, &task.ParentTaskID,
&task.CreatedAt, &task.UpdatedAt,
)
if err != nil {
@@ -63,7 +72,9 @@ 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, reminder_time, completed_at, created_at, updated_at
SELECT id, user_id, title, description, icon, color, due_date, priority, reminder_time, completed_at,
is_recurring, recurrence_type, recurrence_interval, recurrence_end_date, parent_task_id,
created_at, updated_at
FROM tasks WHERE user_id = $1`
if completed != nil {
@@ -88,7 +99,9 @@ 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.ReminderTime, &task.CompletedAt, &task.CreatedAt, &task.UpdatedAt,
&task.ReminderTime, &task.CompletedAt,
&task.IsRecurring, &task.RecurrenceType, &task.RecurrenceInterval, &task.RecurrenceEndDate, &task.ParentTaskID,
&task.CreatedAt, &task.UpdatedAt,
); err != nil {
return nil, err
}
@@ -104,7 +117,9 @@ 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, reminder_time, completed_at, created_at, updated_at
SELECT id, user_id, title, description, icon, color, due_date, priority, reminder_time, completed_at,
is_recurring, recurrence_type, recurrence_interval, recurrence_end_date, parent_task_id,
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`
@@ -122,7 +137,9 @@ 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.ReminderTime, &task.CompletedAt, &task.CreatedAt, &task.UpdatedAt,
&task.ReminderTime, &task.CompletedAt,
&task.IsRecurring, &task.RecurrenceType, &task.RecurrenceInterval, &task.RecurrenceEndDate, &task.ParentTaskID,
&task.CreatedAt, &task.UpdatedAt,
); err != nil {
return nil, err
}
@@ -136,12 +153,14 @@ func (r *TaskRepository) GetTodayTasks(userID int64) ([]model.Task, error) {
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
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.is_recurring, t.recurrence_type, t.recurrence_interval, t.recurrence_end_date, t.parent_task_id,
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 t.due_date = $2
AND u.telegram_chat_id IS NOT NULL
AND u.notifications_enabled = true`
@@ -157,7 +176,9 @@ func (r *TaskRepository) GetTasksWithReminder(reminderTime string, date string)
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,
&task.ReminderTime, &task.CompletedAt,
&task.IsRecurring, &task.RecurrenceType, &task.RecurrenceInterval, &task.RecurrenceEndDate, &task.ParentTaskID,
&task.CreatedAt, &task.UpdatedAt,
); err != nil {
return nil, err
}
@@ -171,8 +192,10 @@ func (r *TaskRepository) GetTasksWithReminder(reminderTime string, date string)
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, reminder_time = $8, updated_at = CURRENT_TIMESTAMP
WHERE id = $1 AND user_id = $9
SET title = $2, description = $3, icon = $4, color = $5, due_date = $6, priority = $7, reminder_time = $8,
is_recurring = $9, recurrence_type = $10, recurrence_interval = $11, recurrence_end_date = $12,
updated_at = CURRENT_TIMESTAMP
WHERE id = $1 AND user_id = $13
RETURNING updated_at`
return r.db.QueryRow(query,
@@ -184,6 +207,10 @@ func (r *TaskRepository) Update(task *model.Task) error {
task.DueDate,
task.Priority,
task.ReminderTime,
task.IsRecurring,
task.RecurrenceType,
task.RecurrenceInterval,
task.RecurrenceEndDate,
task.UserID,
).Scan(&task.UpdatedAt)
}