All files / client/src/views/program-item programItemUtils.ts

87.8% Statements 36/41
75% Branches 15/20
100% Functions 12/12
87.8% Lines 36/41

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                                609x       1736x 626x       609x       3567x 447x             609x       1577x 269x                             609x 3312x 3312x   3312x         2991x     321x       321x 321x     609x           3312x 3279x     33x     609x       470x 470x 470x               609x           459x           459x       459x               609x       217x   217x           217x             217x             217x               217x    
import { TZDate, tz } from "@date-fns/tz";
import { isSameWeek } from "date-fns";
import { TFunction } from "i18next";
import { config } from "shared/config";
import { Locale } from "shared/types/locale";
import { ProgramItem, Tag } from "shared/types/models/programItem";
import { DirectSignup, LotterySignup } from "shared/types/models/user";
import { localeFor } from "shared/utils/setLocale";
import { isSameTime } from "shared/utils/timeComparison";
import {
  getDateAndTime,
  getTime,
  getWeekdayAndTime,
} from "shared/utils/timeFormatter";
import { TIMEZONE } from "shared/utils/timezone";
 
export const isAlreadyLotterySigned = (
  programItemToCheck: ProgramItem,
  lotterySignups: readonly LotterySignup[],
): boolean => {
  return lotterySignups.some(
    (g) => g.programItemId === programItemToCheck.programItemId,
  );
};
 
export const isAlreadyDirectySigned = (
  programItemToCheck: ProgramItem,
  directSignups: readonly DirectSignup[],
): boolean => {
  return directSignups.some(
    (g) => g.programItemId === programItemToCheck.programItemId,
  );
};
 
// Matched through each sign-up's own program item rather than its stored signedToStartTime,
// which is never rewritten when a program item moves. Both sides are the hour the attendee
// would turn up, so a batched program item's parent time does not come into it.
export const getDirectSignupForSlot = <T extends { programItem: ProgramItem }>(
  directSignups: readonly T[],
  programItem: ProgramItem,
): T | undefined =>
  directSignups.find((signup) =>
    isSameTime(signup.programItem.startTime, programItem.startTime),
  );
 
// Bucketed in the event timezone so a viewer further east doesn't enter event week a day early.
// Cached because every visible row asks the same question and the timezone-aware isSameWeek is
// the expensive part of formatting one.
let eventWeekCache:
  | {
      timeNowMs: number;
      eventStartTime: string;
      locale: Locale;
      result: boolean;
    }
  | undefined;
 
const isEventWeek = (timeNow: Date, locale: Locale): boolean => {
  const { eventStartTime } = config.event();
  const timeNowMs = timeNow.getTime();
 
  if (
    eventWeekCache?.timeNowMs === timeNowMs &&
    eventWeekCache.eventStartTime === eventStartTime &&
    eventWeekCache.locale === locale
  ) {
    return eventWeekCache.result;
  }
 
  const result = isSameWeek(timeNow, new Date(eventStartTime), {
    in: tz(TIMEZONE),
    locale: localeFor(locale),
  });
  eventWeekCache = { timeNowMs, eventStartTime, locale, result };
  return result;
};
 
export const getFormattedTime = (
  time: Date,
  timeNow: Date,
  locale: Locale,
): string => {
  // Show weekday and time on event week
  if (isEventWeek(timeNow, locale)) {
    return getWeekdayAndTime(time.toISOString(), locale);
  }
  // Show full time before event week
  return getDateAndTime(time.toISOString(), locale);
};
 
export const isSameDayInEventTimezone = (
  time: Date,
  compared: Date,
): boolean => {
  const zoned = new TZDate(time, TIMEZONE);
  const zonedCompared = new TZDate(compared, TIMEZONE);
  return (
    zoned.getFullYear() === zonedCompared.getFullYear() &&
    zoned.getMonth() === zonedCompared.getMonth() &&
    zoned.getDate() === zonedCompared.getDate()
  );
};
 
/** Format a time interval in a human-friendly way for showing in the UI. */
export const getFormattedInterval = (
  startTime: Date,
  endTime: Date,
  timeNow: Date,
  locale: Locale,
): string => {
  const startFormatted = getFormattedTime(startTime, timeNow, locale);
 
  // Same day in the event timezone: an item running 23:00 -> 01:00 Helsinki
  // crosses midnight for everyone, whatever calendar day the viewer is on.
  // Compared as calendar fields rather than with a timezone-aware isSameDay,
  // which costs ~45us against ~10us here because each of its setters re-syncs.
  const endFormatted = isSameDayInEventTimezone(startTime, endTime)
    ? getTime(endTime.toISOString(), locale)
    : getFormattedTime(endTime, timeNow, locale);
 
  return `${startFormatted} – ${endFormatted}`;
};
 
interface EntryCondition {
  label: string;
  id: string;
}
 
export const getEntryCondition = (
  programItem: ProgramItem,
  t: TFunction,
): EntryCondition | null => {
  const { entryConditions } = config.event();
 
  const foundCondition = entryConditions.find((entryCondition) => {
    if (entryCondition.programItemIds.includes(programItem.programItemId)) {
      return entryCondition;
    }
  });
 
  Iif (foundCondition) {
    return {
      label: t(`signup.signupCondition.${foundCondition.conditionText}`),
      id: "signup-condition-agree-checkbox",
    };
  }
 
  Iif (programItem.tags.includes(Tag.K16)) {
    return {
      label: t("signup.signupCondition.k16"),
      id: "signup-condition-agree-checkbox",
    };
  }
 
  Iif (programItem.entryFee) {
    return {
      label: t("signup.signupCondition.entryFeeInfo", {
        ENTRY_FEE: programItem.entryFee,
      }),
      id: "entry-fee-agree-checkbox",
    };
  }
  return null;
};