All files / shared/utils getProgramItemValidity.ts

100% Statements 10/10
100% Branches 15/15
100% Functions 1/1
100% Lines 10/10

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                                563x     1374x     1374x     1374x     1374x     1374x     1374x           1374x           1374x           1374x                  
import dayjs from "dayjs";
import { config } from "shared/config";
import { ProgramItem, SignupType } from "shared/types/models/programItem";
import { isLotterySignupProgramItem } from "shared/utils/isLotterySignupProgramItem";
 
interface ProgramItemValidity {
  isValidMinAttendanceValue: boolean;
  isValidMaxAttendanceValue: boolean;
  minAttendanceBiggerThanMax: boolean;
  signupTypeMissing: boolean;
  lotteryItemNotStartingOnEvenHour: boolean;
  allValuesValid: boolean;
}
 
// Check if a program item is missing required info, like attendance limits.
// Invalid program items cannot be signed up to
export const getProgramItemValidity = (
  programItem: ProgramItem,
): ProgramItemValidity => {
  const { noKonstiSignupIds, startTimesByParentIds } = 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 signupTypeMissing = programItem.signupType === SignupType.MISSING;
 
  // Lottery batches sign-ups by start time, so lottery items must start at an
  // even hour. Items with a configured parent start time are exempt: the whole
  // batch is run as one lottery at that admin-configured time
  const lotteryItemNotStartingOnEvenHour =
    usesKonstiSignup &&
    isLotterySignupProgramItem(programItem) &&
    !startTimesByParentIds.has(programItem.parentId) &&
    dayjs(programItem.startTime).minute() !== 0;
 
  const allValuesValid =
    isValidMinAttendanceValue &&
    isValidMaxAttendanceValue &&
    !minAttendanceBiggerThanMax &&
    !signupTypeMissing &&
    !lotteryItemNotStartingOnEvenHour;
 
  return {
    isValidMinAttendanceValue,
    isValidMaxAttendanceValue,
    minAttendanceBiggerThanMax,
    signupTypeMissing,
    lotteryItemNotStartingOnEvenHour,
    allValuesValid,
  };
};