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)
This commit is contained in:
@@ -3,10 +3,13 @@ package commands
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"math"
|
||||
"time"
|
||||
|
||||
"apocapoc-api/internal/domain/entities"
|
||||
"apocapoc-api/internal/domain/repositories"
|
||||
"apocapoc-api/internal/domain/value_objects"
|
||||
"apocapoc-api/internal/shared/errors"
|
||||
)
|
||||
|
||||
type MarkHabitCommand struct {
|
||||
@@ -44,7 +47,61 @@ func (h *MarkHabitHandler) Handle(ctx context.Context, cmd MarkHabitCommand) err
|
||||
return fmt.Errorf("habit is archived")
|
||||
}
|
||||
|
||||
entry := entities.NewHabitEntry(cmd.HabitID, cmd.ScheduledDate, cmd.Value)
|
||||
if habit.Type == value_objects.HabitTypeCounter && cmd.Value != nil {
|
||||
if *cmd.Value != math.Floor(*cmd.Value) {
|
||||
return errors.ErrInvalidInput
|
||||
}
|
||||
}
|
||||
|
||||
var finalValue *float64
|
||||
|
||||
if habit.Type == value_objects.HabitTypeCounter {
|
||||
startOfDay := time.Date(cmd.ScheduledDate.Year(), cmd.ScheduledDate.Month(), cmd.ScheduledDate.Day(), 0, 0, 0, 0, cmd.ScheduledDate.Location())
|
||||
endOfDay := startOfDay.Add(24 * time.Hour).Add(-time.Nanosecond)
|
||||
|
||||
existingEntries, _ := h.entryRepo.FindByHabitIDAndDateRange(ctx, cmd.HabitID, startOfDay, endOfDay)
|
||||
|
||||
if len(existingEntries) > 0 {
|
||||
existingEntry := existingEntries[0]
|
||||
|
||||
var increment float64 = 1.0
|
||||
if cmd.Value != nil {
|
||||
increment = *cmd.Value
|
||||
}
|
||||
|
||||
var newValue float64
|
||||
if existingEntry.Value != nil {
|
||||
newValue = *existingEntry.Value + increment
|
||||
} else {
|
||||
newValue = increment
|
||||
}
|
||||
|
||||
if newValue < 0 {
|
||||
newValue = 0
|
||||
}
|
||||
|
||||
finalValue = &newValue
|
||||
existingEntry.Value = finalValue
|
||||
existingEntry.CompletedAt = time.Now()
|
||||
|
||||
return h.entryRepo.Update(ctx, existingEntry)
|
||||
} else {
|
||||
if cmd.Value != nil {
|
||||
value := *cmd.Value
|
||||
if value < 0 {
|
||||
value = 0
|
||||
}
|
||||
finalValue = &value
|
||||
} else {
|
||||
defaultValue := 1.0
|
||||
finalValue = &defaultValue
|
||||
}
|
||||
}
|
||||
} else {
|
||||
finalValue = cmd.Value
|
||||
}
|
||||
|
||||
entry := entities.NewHabitEntry(cmd.HabitID, cmd.ScheduledDate, finalValue)
|
||||
|
||||
return h.entryRepo.Create(ctx, entry)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user