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

86.84% Statements 33/38
76.19% Branches 16/21
100% Functions 11/11
86.84% Lines 33/38

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                                        357x     572x     572x     572x     572x     572x       572x       572x               357x       792x 174x       357x       1653x 175x           357x       235x 235x 12x       357x   1949x 1919x     30x       357x         151x   151x         151x               357x       159x   159x           159x             159x             159x               159x    
import { Dayjs } from "dayjs";
import { TFunction } from "i18next";
import { ProgramItem, SignupType, Tag } from "shared/types/models/programItem";
import { DirectSignup, LotterySignup } from "shared/types/models/user";
import {
  getDateAndTime,
  getTime,
  getWeekdayAndTime,
} from "shared/utils/timeFormatter";
import { config } from "shared/config";
import { getProgramItemStartTime } from "shared/utils/signupTimes";
 
interface ProgramItemValidity {
  isValidMinAttendanceValue: boolean;
  isValidMaxAttendanceValue: boolean;
  minAttendanceBiggerThanMax: boolean;
  allValuesValid: boolean;
}
 
// Check if a program item is missing required info, like attendance limits
export const getProgramItemValidity = (
  programItem: ProgramItem,
): ProgramItemValidity => {
  const { noKonstiSignupIds } = config.event();
 
  const usesKonstiSignup =
    programItem.signupType === SignupType.KONSTI &&
    !noKonstiSignupIds.includes(programItem.programItemId);
 
  const isValidMinAttendanceValue = programItem.minAttendance > 0;
 
  const isValidMaxAttendanceValue =
    !usesKonstiSignup || programItem.maxAttendance > 0;
 
  const minAttendanceBiggerThanMax =
    programItem.minAttendance > programItem.maxAttendance &&
    programItem.maxAttendance > 0;
 
  const allValuesValid =
    isValidMinAttendanceValue &&
    isValidMaxAttendanceValue &&
    !minAttendanceBiggerThanMax;
 
  return {
    isValidMinAttendanceValue,
    isValidMaxAttendanceValue,
    minAttendanceBiggerThanMax,
    allValuesValid,
  };
};
 
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 signup that occupies the same time slot as a lottery item.
// Direct signups 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;
};