All files / munch-ease-frontend/src/domain decoders.ts

78.33% Statements 94/120
77.5% Branches 31/40
75% Functions 9/12
78.33% Lines 94/120

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 1211x 1x 1x 1x 44x 44x 44x 1x 1x 1x 16x 16x 16x 16x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 1x 1x 4x 4x 4x 4x 4x 4x 4x 1x 1x       1x 1x 9x 9x 2x 7x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 1x 1x             1x 1x 3x 3x 3x 3x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 1x 1x 12x 12x 12x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1x 1x 1x                                    
import type { RecipeOut, Recipe, MealOut, Meal, MealRecipe, Ingredient as DomainIngredient, ShoppingList, ShoppingListItem, MealInput } from './types'
import type { components } from '@/api/types'
 
export function toDate(value: string | Date | null | undefined): Date | null {
  if (value === null || value === undefined) return null
  return typeof value === 'string' ? new Date(value) : value
}
 
// Small helper to decode optional lookup maps without repeating loops everywhere
export function decodeLookup<TIn, TOut>(
  raw: Record<string, TIn> | null | undefined,
  decode: (v: TIn) => TOut | null
): Record<string, TOut> | undefined {
  if (!raw) return undefined
  const out: Record<string, TOut> = {}
  for (const key of Object.keys(raw)) {
    if (!Object.prototype.hasOwnProperty.call(raw, key)) continue
    const maybe = raw[key]
    if (maybe === undefined) continue
    const decoded = decode(maybe)
    if (decoded) out[String(key)] = decoded
  }
  return out
}
 
export function decodeRecipe(r: RecipeOut | null | undefined): Recipe | null {
  if (!r) return null
  return {
    ...r,
    dateCreated: toDate(r.dateCreated),
    dateHidden: toDate(r.dateHidden),
  }
}
 
export function decodeRecipes(list: RecipeOut[] | null | undefined): Recipe[] {
  if (!Array.isArray(list)) return []
  return list.map((r) => decodeRecipe(r)).filter((r): r is Recipe => !!r)
}
 
export function decodeMeal(m: MealOut | null | undefined): Meal | null {
  if (!m) return null
  const recipes = Array.isArray(m.recipes) 
    ? m.recipes.map(mr => decodeMealRecipe(mr)).filter((r): r is MealRecipe => !!r)
    : []
  
  return {
    ...m,
    suggestedDate: toDate(m.suggestedDate),
    purchaseDate: toDate(m.purchaseDate),
    consumedDate: toDate(m.consumedDate),
    recipes,
    chefs: m.chefs ?? [],
    consumers: m.consumers ?? [],
    cleanup: m.cleanup ?? [],
    extraIngredients: m.extraIngredients ?? [],
  }
}
 
export function decodeMealRecipe(mr: components['schemas']['MealRecipe-Output'] | null | undefined): MealRecipe | null {
  if (!mr) return null
  return {
    ...mr,
    recipe: mr.recipe ? decodeRecipe(mr.recipe) : null,
  }
}
 
export function decodeIngredient(i: components['schemas']['Ingredient'] | null | undefined): DomainIngredient | null {
  if (!i) return null
  // API guarantees quantity is a number; pass through
  return { ...i }
}
 
export function decodeIngredients(list: components['schemas']['Ingredient'][] | null | undefined): DomainIngredient[] {
  if (!Array.isArray(list)) return []
  return list.map((i) => decodeIngredient(i)).filter((x): x is DomainIngredient => !!x)
}
 
export function decodeShoppingListItem(i: components['schemas']['ShoppingListItem'] | null | undefined): ShoppingListItem | null {
  if (!i) return null
  return {
    ...i,
    createdDate: toDate(i.createdDate),
  }
}
 
export function decodeShoppingListItems(list: components['schemas']['ShoppingListItem'][] | null | undefined): ShoppingListItem[] {
  if (!Array.isArray(list)) return []
  return list.map((i) => decodeShoppingListItem(i)).filter((x): x is ShoppingListItem => !!x)
}
 
export function decodeShoppingList(v: components['schemas']['ShoppingList'] | null | undefined): ShoppingList | null {
  if (!v) return null
  const { items: rawItems, ...rest } = v
  const items = Array.isArray(rawItems) ? decodeShoppingListItems(rawItems) : undefined
  return {
    ...rest,
    createdDate: toDate(v.createdDate),
    ...(items ? { items } : {}),
  }
}
 
// Helper to convert domain Meal to MealInput, keeping Date→string conversion in boundary
export function toMealInput(meal: Meal): MealInput {
  return {
    id: meal.id,
    suggestedDate: meal.suggestedDate ? meal.suggestedDate.toISOString() : new Date().toISOString(),
    consumedDate: meal.consumedDate ? meal.consumedDate.toISOString() : null,
    purchaseDate: meal.purchaseDate ? meal.purchaseDate.toISOString() : null,
    chefs: meal.chefs,
    cleanup: meal.cleanup,
    consumers: meal.consumers,
    recipes: meal.recipes.map((r) => ({
      mealId: r.mealId,
      recipeId: r.recipeId,
      servings: r.servings,
      recipe: null,
    })),
    extraIngredients: meal.extraIngredients,
  }
}