All files / src/utils meal.ts

99.25% Statements 133/134
93.33% Branches 14/15
100% Functions 4/4
99.25% Lines 133/134

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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 1951x 1x                               1x                   1x 3x 3x 3x 3x 3x   3x 3x 3x 3x 3x 3x                           1x 3x 3x 3x 3x 3x 3x 3x 12x 12x 12x 12x   12x 12x 12x 12x 12x 12x 12x 3x   3x 3x 3x 3x 3x 3x 3x 3x 3x                   1x 3x 3x 3x 3x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x   1x 1x 1x   1x 2x 2x   2x 2x 2x 2x 2x 2x 2x   2x 2x 2x 2x 2x 2x 2x 2x 2x 2x     1x 1x   1x 4x 4x 4x 4x 4x     4x 4x 4x   4x 4x 3x 3x 3x 3x 3x 3x 3x 3x 3x   3x 3x 3x 3x 3x 3x 3x 3x 4x   4x 4x 4x 4x 4x 4x 4x 4x 4x 4x  
import { toZonedTime } from 'date-fns-tz';
import { formatISO } from 'date-fns';
 
// Types
import {
  LunchPlansResponse,
  FormattedLunchPlan,
  OfficeStats,
  MealMenu,
  Meal,
  MealPlansResponse,
  FormattedMealPlan,
  OfficeMealStats,
  Meals,
} from '@/types';
 
// Utils
import { formatDateToISODateString } from '@/utils';
 
/**
 * Format a list of lunch menus by converting their dates to a specific time zone
 * and formatting them into ISO 8601 strings.
 *
 * @param plan - The array of MealMenu objects to format. Defaults to an empty array.
 * @param timeZone - The IANA time zone string to convert dates into. Defaults to 'Asia/Ho_Chi_Minh'.
 * @returns A new array of MealMenu objects with `date` formatted to the specified time zone.
 */
export const formatLunchMenus = (
  plan: MealMenu[] = [],
  timeZone: string = 'Asia/Ho_Chi_Minh',
): MealMenu[] => {
  return plan.map(({ date, ...rest }) => {
    const zonedDate = formatISO(toZonedTime(date, timeZone));
 
    return {
      date: zonedDate,
      ...rest,
    };
  });
};
 
/**
 * Formats raw lunch plan data into a normalized structure for easier consumption.
 *
 * @param plan - Array of lunch plan responses from the backend API.
 *   Each object contains total counts and per-office meal values.
 *
 * @returns Array of FormattedLunchPlan objects, where each contains:
 *   - date: The date of the lunch plan.
 *   - totalVegan: Total vegan meals planned.
 *   - totalNormal: Total regular meals planned.
 *   - stats: Per-office stats including actual meal values and changes.
 */
export const formatLunchPlans = (
  plan: LunchPlansResponse[] = [],
  timeZone: string = 'Asia/Ho_Chi_Minh',
): FormattedLunchPlan[] => {
  return plan.map(({ date, totalVegan, totalNormal, data, isHoliday }) => {
    const zonedDate = formatISO(toZonedTime(date, timeZone));
    const stats: OfficeStats[] = Object.entries(data).map(
      ([office, values]) => {
        const {
          vegan: { value: veganValue = 0, diff: veganDiff = 0 } = {},
          normal: { value: normalValue = 0, diff: normalDiff = 0 } = {},
        } = values;
 
        return {
          office,
          veganMeals: veganValue + veganDiff,
          normalMeals: normalValue + normalDiff,
          changeNumber: veganDiff + normalDiff,
        };
      },
    );
 
    return {
      date: zonedDate,
      totalVeganMeals: totalVegan,
      totalNormalMeals: totalNormal,
      stats,
      isHoliday,
    };
  });
};
 
/**
 * Merge raw meal menu data with formatted meal plans by matching dates.
 *
 * @param meals - Array of MealMenu objects, typically fetched from the kitchen/menu service.
 * @param plans - Array of FormattedLunchPlan objects generated from `formatLunchPlan`.
 *
 * @returns Array of enriched Meal objects
 */
export const mergePlansIntoMeals = (
  menus: MealMenu[] = [],
  plans: FormattedMealPlan[] = [],
): Meals[] => {
  if (!menus.length) {
    return plans.map(
      ({
        date,
        totalVeganMeals,
        totalLunchNormalMeals,
        totalBreakfastNormalMeals,
        stats,
        isHoliday,
      }) => ({
        date,
        dailyStats: {
          totalLunchNormalMeals,
          totalVeganMeals,
          totalBreakfastNormalMeals,
          stats,
        },
        isHoliday,
      }),
    ) as Meals[];
  }
 
  const plansMap = new Map(
    plans.map((plan) => [formatDateToISODateString(plan.date), plan]),
  );
 
  return menus.map((meal) => {
    const mealDate = formatDateToISODateString(meal.date);
    const matchingPlans = plansMap.get(mealDate);
 
    if (matchingPlans) {
      const {
        totalVeganMeals,
        totalLunchNormalMeals,
        totalBreakfastNormalMeals,
        stats,
      } = matchingPlans;
 
      return {
        ...meal,
        dailyStats: {
          totalLunchNormalMeals,
          totalVeganMeals,
          totalBreakfastNormalMeals,
          stats,
        },
      };
    }
 
    return meal;
  });
};
 
export const formatMealPlans = (
  plan: MealPlansResponse[] = [],
  timeZone: string = 'Asia/Ho_Chi_Minh',
): FormattedMealPlan[] => {
  return plan.map(({ date, total, data, isHoliday }) => {
    const zonedDate = formatISO(toZonedTime(date, timeZone));
 
    // Extract totals properly from nested structure
    const totalVeganMeals = total.lunch.vegan;
    const totalLunchNormalMeals = total.lunch.normal;
    const totalBreakfastNormalMeals = total.breakfast.normal;
 
    const stats: OfficeMealStats[] = Object.entries(data).map(
      ([office, values]) => {
        const {
          lunch: {
            vegan: { value: veganValue = 0, diff: veganDiff = 0 } = {},
            normal: { value: normalValue = 0, diff: normalDiff = 0 } = {},
          },
          breakfast: {
            normal: { value: breakfastValue = 0, diff: breakfastDiff = 0 } = {},
          },
        } = values;
 
        return {
          office,
          veganMeals: veganValue + veganDiff,
          normalMeals: normalValue + normalDiff,
          normaBreakfastNlMeals: breakfastValue + breakfastDiff,
          changeNumber: veganDiff + normalDiff + breakfastDiff,
        };
      },
    );
 
    return {
      date: zonedDate,
      totalVeganMeals,
      totalLunchNormalMeals,
      totalBreakfastNormalMeals,
      stats,
      isHoliday,
    };
  });
};