All files / server/src/features/assignment/utils saveResults.ts

84.61% Statements 11/13
66.66% Branches 4/6
100% Functions 1/1
84.61% Lines 11/13

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                                        43x               53x 35x         35x           35x             35x       18x         53x 53x           53x       53x    
import { logger } from "server/utils/logger";
import { saveUserSignupResults } from "server/features/assignment/utils/saveUserSignupResults";
import { UserAssignmentResult } from "shared/types/models/result";
import { saveResult } from "server/features/results/resultsRepository";
import { getAssignmentResultGroups } from "server/features/assignment/utils/getAssignmentResultGroups";
import { Result, makeSuccessResult } from "shared/utils/result";
import { MongoDbError, QueueError } from "shared/types/api/errors";
import { User } from "shared/types/models/user";
import { ProgramItem } from "shared/types/models/programItem";
import { AssignmentAlgorithm } from "shared/config/eventConfigTypes";
 
interface SaveResultsParams {
  results: readonly UserAssignmentResult[];
  assignmentTime: string;
  algorithm: AssignmentAlgorithm;
  message: string;
  users: User[];
  programItems: ProgramItem[];
}
 
export const saveResults = async ({
  results,
  assignmentTime,
  algorithm,
  message,
  users,
  programItems,
}: SaveResultsParams): Promise<Result<void, MongoDbError | QueueError>> => {
  if (results.length > 0) {
    logger.info(
      `Save all signup results to separate collection for assignment time ${assignmentTime}`,
    );
 
    // Snapshot the groups as they were when this lottery ran
    const groups = getAssignmentResultGroups(
      users,
      programItems,
      assignmentTime,
    );
 
    const saveResultResult = await saveResult(
      results,
      groups,
      assignmentTime,
      algorithm,
      message,
    );
    Iif (!saveResultResult.ok) {
      return saveResultResult;
    }
  } else {
    logger.info(
      `No results, skip saving signup results to separate collection for assignment time ${assignmentTime}`,
    );
  }
 
  logger.info(`Save user signup results for assignment time ${assignmentTime}`);
  const saveUserSignupResultsResult = await saveUserSignupResults({
    assignmentTime,
    results,
    users,
    programItems,
  });
  Iif (!saveUserSignupResultsResult.ok) {
    return saveUserSignupResultsResult;
  }
 
  return makeSuccessResult();
};