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

82.14% Statements 23/28
62.5% Branches 10/16
100% Functions 3/3
82.14% Lines 23/28

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                                    44x     23x 23x 4x     19x           44x 19x 19x 4x             15x     15x               15x   15x 15x                 15x 15x                 15x             44x     809x 809x             809x       809x             809x            
import { config } from "shared/config";
import { KompassiError } from "shared/types/api/errors";
import {
  GetProgramItemsResponse,
  PostUpdateProgramItemsResponse,
} from "shared/types/api/programItems";
import { ProgramItem } from "shared/types/models/programItem";
import { UserGroup } from "shared/types/models/user";
import { Result, makeSuccessResult } from "shared/utils/result";
import { updateProgramItemPopularity } from "server/features/program-item-popularity/updateProgramItemPopularity";
import {
  findProgramItems,
  saveProgramItems,
} from "server/features/program-item/programItemRepository";
import { getProgramItemsFromKompassi } from "server/kompassi/getProgramItemsFromKompassi";
import { kompassiProgramItemMapper } from "server/kompassi/kompassiProgramItemMapper";
import { enrichProgramItems } from "./programItemUtils";
 
export const getProgramItemsForEvent = async (): Promise<
  Result<readonly ProgramItem[], KompassiError>
> => {
  const kompassiProgramItemsResult = await getProgramItemsFromKompassi();
  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,
  };
};