Files
apocapoc-api/internal/application/queries/get_todays_habits.go
T
david 10d45fc34e Add COUNTER type with auto-increment and IsNegative field
COUNTER type:
- Only accepts integer values (rejects decimals)
- Auto-increment behavior: each mark adds to existing value
- Supports decrement via negative values (e.g., -2)
- Enforces minimum value of 0 (never negative)
- Default increment is 1 if no value provided

VALUE type:
- Accepts decimal values
- Replaces value (no auto-increment)

IsNegative field:
- New boolean field on Habit entity
- Persisted in database (is_negative column)
- Available in all DTOs and HTTP responses
- Marks habits as "negative" (e.g., candy consumption)

Examples:
- COUNTER: 5 + (-2) = 3, 2 + (-3) = 0, 0 + (-1) = 0
- VALUE: 5000 → 12000 = 12000 (replacement)
2025-11-27 08:58:39 +01:00

96 lines
1.9 KiB
Go

package queries
import (
"context"
"time"
"apocapoc-api/internal/domain/repositories"
"apocapoc-api/internal/domain/value_objects"
"apocapoc-api/internal/shared/utils"
)
type TodaysHabitDTO struct {
ID string
Name string
Type value_objects.HabitType
TargetValue *float64
IsNegative bool
ScheduledDate time.Time
IsCarriedOver bool
}
type GetTodaysHabitsQuery struct {
UserID string
Timezone string
Date time.Time
}
type GetTodaysHabitsHandler struct {
habitRepo repositories.HabitRepository
entryRepo repositories.HabitEntryRepository
}
func NewGetTodaysHabitsHandler(
habitRepo repositories.HabitRepository,
entryRepo repositories.HabitEntryRepository,
) *GetTodaysHabitsHandler {
return &GetTodaysHabitsHandler{
habitRepo: habitRepo,
entryRepo: entryRepo,
}
}
func (h *GetTodaysHabitsHandler) Handle(
ctx context.Context,
query GetTodaysHabitsQuery,
) ([]TodaysHabitDTO, error) {
habits, err := h.habitRepo.FindActiveByUserID(ctx, query.UserID)
if err != nil {
return nil, err
}
var result []TodaysHabitDTO
for _, habit := range habits {
shouldAppear := utils.ShouldAppearToday(
string(habit.Frequency),
habit.SpecificDays,
habit.SpecificDates,
query.Date,
)
if !shouldAppear && !habit.CarryOver {
continue
}
entries, _ := h.entryRepo.FindByHabitIDAndDateRange(
ctx,
habit.ID,
query.Date.AddDate(0, 0, -30),
query.Date,
)
isCompleted := false
for _, entry := range entries {
if entry.ScheduledDate.Equal(query.Date) {
isCompleted = true
break
}
}
if !isCompleted {
result = append(result, TodaysHabitDTO{
ID: habit.ID,
Name: habit.Name,
Type: habit.Type,
TargetValue: habit.TargetValue,
IsNegative: habit.IsNegative,
ScheduledDate: query.Date,
IsCarriedOver: !shouldAppear && habit.CarryOver,
})
}
}
return result, nil
}