All files / server/src/features/program-item programItemService.ts

82.75% Statements 24/29
62.5% Branches 10/16
100% Functions 3/3
82.75% Lines 24/29

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                                    40x     15x   15x 15x 4x     11x           40x 11x 11x 4x             7x     7x               7x   7x 7x                 7x 7x                 7x             40x     434x 434x             434x       434x             434x            
import { updateProgramItemPopularity } from "server/features/program-item-popularity/updateProgramItemPopularity";
import { config } from "shared/config";
import {
  PostUpdateProgramItemsResponse,
  GetProgramItemsResponse,
} from "shared/types/api/programItems";
import {
  findProgramItems,
  saveProgramItems,
} from "server/features/program-item/programItemRepository";
import { enrichProgramItems } from "./programItemUtils";
import { Result, makeSuccessResult } from "shared/utils/result";
import { KompassiError } from "shared/types/api/errors";
import { ProgramItem } from "shared/types/models/programItem";
import { getProgramItemsFromKompassi } from "server/kompassi/getProgramItemsFromKompassi";
import { kompassiProgramItemMapper } from "server/kompassi/kompassiProgramItemMapper";
import { UserGroup } from "shared/types/models/user";
 
export const getProgramItemsForEvent = async (): Promise<
  Result<readonly ProgramItem[], KompassiError>
> => {
  const eventName = config.event().eventName;
  const kompassiProgramItemsResult =
    await getProgramItemsFromKompassi(eventName);
  if (!kompassiProgramItemsResult.ok) {
    return kompassiProgramItemsResult;
  }
 
  return makeSuccessResult(
    kompassiProgramItemMapper(kompassiProgramItemsResult.value),
  );
};
 
export const updateProgramItems =
  async (): Promise<PostUpdateProgramItemsResponse> => {
    const programItemsResult = await getProgramItemsForEvent();
    if (!programItemsResult.ok) {
      return {
        message: "Loading program items from Kompassi failed",
        status: "error",
        errorId: "kompassiError",
      };
    }
 
    const saveProgramItemsResult = await saveProgramItems(
      programItemsResult.value,
    );
    Iif (!saveProgramItemsResult.ok) {
      return {
        message: "Program items db update failed: Saving program items failed",
        status: "error",
        errorId: "unknown",
      };
    }
 
    Eif (config.server().updateProgramItemPopularityEnabled) {
      const updateProgramItemPopularityResult =
        await updateProgramItemPopularity();
      Iif (!updateProgramItemPopularityResult.ok) {
        return {
          message: "Program item popularity update failed",
          status: "error",
          errorId: "unknown",
        };
      }
    }
 
    const updatedProgramItemsResult = await findProgramItems();
    Iif (!updatedProgramItemsResult.ok) {
      return {
        message:
          "Program items db update failed: Error loading updated program items",
        status: "error",
        errorId: "unknown",
      };
    }
 
    return {
      message: "Program items db updated",
      status: "success",
      programItems: updatedProgramItemsResult.value,
    };
  };
 
export const fetchProgramItems = async (
  userGroup: UserGroup | null,
): Promise<GetProgramItemsResponse> => {
  const programItemsResult = await findProgramItems();
  Iif (!programItemsResult.ok) {
    return {
      message: "Downloading program items failed",
      status: "error",
      errorId: "databaseError",
    };
  }
  const programItemsWithAttendeesResult = await enrichProgramItems(
    programItemsResult.value,
    userGroup,
  );
  Iif (!programItemsWithAttendeesResult.ok) {
    return {
      message: "Enriching program items failed",
      status: "error",
      errorId: "unknown",
    };
  }
  return {
    message: "Program items downloaded",
    status: "success",
    programItems: programItemsWithAttendeesResult.value,
  };
};