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 149 150 151 152 153 154 155 156 157 158 | 47x 103x 103x 103x 103x 103x 103x 103x 103x 57x 103x 103x 103x 99x 103x 47x 99x 99x 99x 99x | import { AssignmentAlgorithm } from "shared/config/eventConfigTypes";
import { MongoDbError } from "shared/types/api/errors";
import { ProgramItem } from "shared/types/models/programItem";
import { UserAssignmentResult } from "shared/types/models/result";
import { User } from "shared/types/models/user";
import { Result, makeSuccessResult } from "shared/utils/result";
import { addAssignmentNotifications } from "server/features/assignment/utils/addAssignmentNotifications";
import { getAssignmentResultGroups } from "server/features/assignment/utils/getAssignmentResultGroups";
import { saveUserSignupResults } from "server/features/assignment/utils/saveUserSignupResults";
import {
saveLotteryRanForStartTime,
savePassedOverForLottery,
} from "server/features/program-item/programItemRepository";
import { saveResult } from "server/features/results/resultsRepository";
import { logger } from "server/utils/logger";
interface SaveResultsParams {
results: readonly UserAssignmentResult[];
assignmentTime: string;
algorithm: AssignmentAlgorithm;
message: string;
users: User[];
programItems: ProgramItem[];
lotteriedProgramItems: readonly ProgramItem[];
passedOverProgramItems: readonly ProgramItem[];
}
// The writes below run in order of how much rides on them: the spots first, then the mark that
// closes this start time's lottery, then the record-keeping and the messages, neither of which
// can cost anybody a spot by failing
export const saveResults = async ({
results,
assignmentTime,
algorithm,
message,
users,
programItems,
lotteriedProgramItems,
passedOverProgramItems,
// Returns the results that actually landed, so the caller acts on those rather than on what
// the algorithm proposed
}: SaveResultsParams): Promise<
Result<readonly UserAssignmentResult[], MongoDbError>
> => {
// The lottery runs on a timer, so most runs reach a start time with nothing to do at all
Iif (
lotteriedProgramItems.length === 0 &&
passedOverProgramItems.length === 0
) {
logger.info(
`Assignment ${assignmentTime}: nothing was lotteried, nothing to save`,
);
return makeSuccessResult([]);
}
logger.info(`Save user signup results for assignment time ${assignmentTime}`);
const saveUserSignupResultsResult = await saveUserSignupResults({
assignmentTime,
results,
users,
programItems,
});
Iif (!saveUserSignupResultsResult.ok) {
return saveUserSignupResultsResult;
}
const finalResults = saveUserSignupResultsResult.value;
// The program items that went through the lottery, placed or not: their lottery has happened.
// Written only once the spots exist, because nothing clears the mark - marking before a
// failed save would strand them out of the lottery with nobody placed.
const saveLotteryRanResult = await saveLotteryRanForStartTime(
lotteriedProgramItems,
);
Iif (!saveLotteryRanResult.ok) {
// The spots are saved, so returning here would cost the winners their notifications over
// a mark that only bookkeeping needs
logger.error(
new Error(
`Assignment ${assignmentTime}: failed to mark the lotteried program items: ${saveLotteryRanResult.error}`,
),
);
}
const savePassedOverResult = await savePassedOverForLottery(
passedOverProgramItems.map((programItem) => programItem.programItemId),
);
Iif (!savePassedOverResult.ok) {
logger.error(
new Error(
`Assignment ${assignmentTime}: failed to record the passed over program items: ${savePassedOverResult.error}`,
),
);
}
// The spots are saved at this point, so nothing below can fail the run - it logs and carries on
await addAssignmentNotifications({
assignmentTime,
finalResults,
users,
programItems,
});
// Recorded only when a program item actually went through a lottery. A run that skipped
// every one of them decided nothing, and a record for it would read as a lottery that ran.
if (lotteriedProgramItems.length > 0) {
await storeResultsSnapshot({
results: finalResults,
assignmentTime,
algorithm,
message,
users,
programItems,
});
}
return makeSuccessResult(finalResults);
};
interface StoreResultsSnapshotParams {
results: readonly UserAssignmentResult[];
assignmentTime: string;
algorithm: AssignmentAlgorithm;
message: string;
users: User[];
programItems: ProgramItem[];
}
const storeResultsSnapshot = async ({
results,
assignmentTime,
algorithm,
message,
users,
programItems,
}: StoreResultsSnapshotParams): Promise<void> => {
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) {
logger.error(
new Error(
`Assignment ${assignmentTime}: failed to store the results snapshot: ${saveResultResult.error}`,
),
);
}
};
|