All files / apps/host/src/utils mealReservation.ts

100% Statements 19/19
93.33% Branches 14/15
100% Functions 1/1
100% Lines 19/19

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    1x                     1x 6x 6x       6x         6x 1x       5x 4x 4x 4x 4x 4x 4x     4x 3x       1x 1x         1x    
import { isExpired } from '@repo/utils/date';
 
const NOTES = {
  IMMEDIATELY: 'The changes will be updated immediately.',
  TOMORROW_MORNING: 'The changes will be updated tomorrow morning.',
  ON_REQUESTED_DAY: 'The changes will be updated on your requested day.',
};
/**
 * Determines the update time message for meal reservation changes.
 * - Past or today before noon/current time: "immediately"
 * - Today after noon and current time: "tomorrow morning"
 * - Future date: Shows specific date and time
 */
export const getUpdateTimeMessage = (dateStr: string): string => {
  const reallocationDateTime = new Date(dateStr);
  const now = new Date();
 
  // Check if it's the same day
  const isSameDay =
    reallocationDateTime.getDate() === now.getDate() &&
    reallocationDateTime.getMonth() === now.getMonth() &&
    reallocationDateTime.getFullYear() === now.getFullYear();
 
  // If date is in the past, update immediately
  if (isExpired(dateStr)) {
    return NOTES.IMMEDIATELY;
  }
 
  // If it's today
  if (isSameDay) {
    const reallocationHour = reallocationDateTime.getHours();
    const currentHour = now.getHours();
    const currentMinutes = now.getMinutes();
    const reallocationMinutes = reallocationDateTime.getMinutes();
    const currentTimeInMinutes = currentHour * 60 + currentMinutes;
    const reallocationTimeInMinutes = reallocationHour * 60 + reallocationMinutes;
 
    // Before noon (12:00 PM) OR before current time → update immediately
    if (reallocationHour < 12 || reallocationTimeInMinutes <= currentTimeInMinutes) {
      return NOTES.IMMEDIATELY;
    }
 
    // After noon AND after current time → update tomorrow morning
    Eif (reallocationHour >= 12 && reallocationTimeInMinutes > currentTimeInMinutes) {
      return NOTES.TOMORROW_MORNING;
    }
  }
 
  // Future date → show specific date and time
  return NOTES.ON_REQUESTED_DAY;
};