Fix today's habits timezone calculation and include entry data

- Calculate today based on user's timezone instead of always using UTC
- Include habit entry in response if it exists for the current day
- Update GetTodaysHabitsHandler to fetch and return entry information
- Add entry field to TodaysHabitDTO and TodaysHabitResponse
- Update tests to reflect new behavior of including completed habits
- Add test for habits with value entries
This commit is contained in:
2025-11-29 02:09:30 +01:00
parent 9a0637aa4c
commit 90d5628a42
6 changed files with 130 additions and 32 deletions
@@ -9,6 +9,12 @@ import (
"apocapoc-api/internal/shared/utils"
)
type TodaysHabitEntryDTO struct {
ID string
Value *float64
CompletedAt time.Time
}
type TodaysHabitDTO struct {
ID string
Name string
@@ -17,6 +23,7 @@ type TodaysHabitDTO struct {
IsNegative bool
ScheduledDate time.Time
IsCarriedOver bool
Entry *TodaysHabitEntryDTO
}
type GetTodaysHabitsQuery struct {
@@ -66,29 +73,29 @@ func (h *GetTodaysHabitsHandler) Handle(
entries, _ := h.entryRepo.FindByHabitIDAndDateRange(
ctx,
habit.ID,
query.Date.AddDate(0, 0, -30),
query.Date,
query.Date,
)
isCompleted := false
for _, entry := range entries {
if entry.ScheduledDate.Equal(query.Date) {
isCompleted = true
break
var entryDTO *TodaysHabitEntryDTO
if len(entries) > 0 && entries[0].ScheduledDate.Format("2006-01-02") == query.Date.Format("2006-01-02") {
entryDTO = &TodaysHabitEntryDTO{
ID: entries[0].ID,
Value: entries[0].Value,
CompletedAt: entries[0].CompletedAt,
}
}
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,
})
}
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,
Entry: entryDTO,
})
}
return result, nil