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

83.33% Statements 25/30
58.33% Branches 7/12
100% Functions 10/10
83.33% Lines 25/30

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                        521x       1585x 567x       521x       3265x 351x           521x       421x 421x 120x       521x   2934x 2906x     28x       521x         267x   267x         267x               521x       193x   193x           193x             193x             193x               193x    
import { Dayjs } from "dayjs";
import { TFunction } from "i18next";
import { config } from "shared/config";
import { ProgramItem, Tag } from "shared/types/models/programItem";
import { DirectSignup, LotterySignup } from "shared/types/models/user";
import { getProgramItemStartTime } from "shared/utils/signupTimes";
import {
  getDateAndTime,
  getTime,
  getWeekdayAndTime,
} from "shared/utils/timeFormatter";
 
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,
  );
};
 
// Find the user's existing direct sign-up that occupies the same time slot as a lottery item.
// Direct sign-ups store the parent-resolved start time, so match against that, not the item's own
export const getDirectSignupForSlot = <T extends { signedToStartTime: string }>(
  directSignups: readonly T[],
  programItem: ProgramItem,
): T | undefined => {
  const programItemStartTime = getProgramItemStartTime(programItem);
  return directSignups.find(
    (signup) => signup.signedToStartTime === programItemStartTime,
  );
};
 
export const getFormattedTime = (time: Dayjs, timeNow: Dayjs): string => {
  // Show weekday and time on event week
  if (timeNow.isSame(config.event().eventStartTime, "week")) {
    return getWeekdayAndTime(time.toISOString());
  }
  // Show full time before event week
  return getDateAndTime(time.toISOString());
};
 
/** Format a time interval in a human-friendly way for showing in the UI. */
export const getFormattedInterval = (
  startTime: Dayjs,
  endTime: Dayjs,
  timeNow: Dayjs,
): string => {
  const startFormatted = getFormattedTime(startTime, timeNow);
 
  const endFormatted = startTime.isSame(endTime, "day")
    ? getTime(endTime.toISOString())
    : getFormattedTime(endTime, timeNow);
 
  // Note that the dash should be an en dash
  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;
};