summaryrefslogtreecommitdiff
path: root/internal/handlers/timeline_logic.go
diff options
context:
space:
mode:
authorClaude <agent@claude.ai>2026-03-18 10:04:57 +0000
committerClaude <agent@claude.ai>2026-03-18 10:04:57 +0000
commite85b42d373de55781af9d699b246c0d6a492aec1 (patch)
tree50e40abda62e60144186c1916ddd0f683533c2f4 /internal/handlers/timeline_logic.go
parente3195a6534bae000a63e884ff647fac95d9d2498 (diff)
refactor: RF-03/06 extract groupMeals helper, eliminate convertSyncItemToTask wrapper
RF-03: Extract shared groupMeals helper into internal/handlers/meals.go. Both HandleTabMeals and BuildTimeline now call groupMeals instead of duplicating the date+mealType grouping algorithm inline. CombinedMeal gains ID and Meals fields to carry the first-meal ID and original records needed by BuildTimeline when constructing TimelineItems. RF-06: Add api.ConvertSyncItemToTask for single-item conversion. ConvertSyncItemsToTasks now delegates to it, eliminating duplication. The Handler.convertSyncItemToTask wrapper (which allocated a one-element slice just to unwrap it) is deleted; its caller uses api.ConvertSyncItemToTask directly. Covered by TestConvertSyncItemToTask in internal/api/todoist_test.go. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Diffstat (limited to 'internal/handlers/timeline_logic.go')
-rw-r--r--internal/handlers/timeline_logic.go44
1 files changed, 7 insertions, 37 deletions
diff --git a/internal/handlers/timeline_logic.go b/internal/handlers/timeline_logic.go
index 4334f6d..adfa406 100644
--- a/internal/handlers/timeline_logic.go
+++ b/internal/handlers/timeline_logic.go
@@ -48,32 +48,9 @@ func BuildTimeline(ctx context.Context, s *store.Store, tasksClient api.GoogleTa
return nil, err
}
- // Group meals by date+mealType key
- type mealKey struct {
- date string
- mealType string
- }
- mealGroups := make(map[mealKey][]models.Meal)
- for _, meal := range meals {
- key := mealKey{
- date: meal.Date.Format("2006-01-02"),
- mealType: meal.MealType,
- }
- mealGroups[key] = append(mealGroups[key], meal)
- }
-
- // Create combined timeline items for each group
- for _, group := range mealGroups {
- if len(group) == 0 {
- continue
- }
-
- // Use first meal as base
- firstMeal := group[0]
- mealTime := firstMeal.Date
-
- // Apply Meal time defaults
- switch firstMeal.MealType {
+ for _, cm := range groupMeals(meals) {
+ mealTime := cm.Date
+ switch cm.MealType {
case "breakfast":
mealTime = time.Date(mealTime.Year(), mealTime.Month(), mealTime.Day(), config.BreakfastHour, 0, 0, 0, mealTime.Location())
case "lunch":
@@ -84,20 +61,13 @@ func BuildTimeline(ctx context.Context, s *store.Store, tasksClient api.GoogleTa
mealTime = time.Date(mealTime.Year(), mealTime.Month(), mealTime.Day(), config.LunchHour, 0, 0, 0, mealTime.Location())
}
- // Combine recipe names with " + "
- var names []string
- for _, m := range group {
- names = append(names, m.RecipeName)
- }
- combinedTitle := strings.Join(names, " + ")
-
item := models.TimelineItem{
- ID: firstMeal.ID,
+ ID: cm.ID,
Type: models.TimelineItemTypeMeal,
- Title: combinedTitle,
+ Title: strings.Join(cm.RecipeNames, " + "),
Time: mealTime,
- URL: firstMeal.RecipeURL, // Use first meal's URL
- OriginalItem: group, // Store all meals
+ URL: cm.RecipeURL,
+ OriginalItem: cm.Meals,
IsCompleted: false,
Source: "plantoeat",
}