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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 | 44x 63x 63x 63x 63x 63x 63x 63x 63x 63x 63x 329x 23x 17x 63x 63x 63x 329x 63x 63x 63x 63x 63x 63x 329x 11x 63x 329x 63x 63x 63x 63x 329x 63x 63x 63x 63x 63x 63x 63x 3432x 155966x 3432x 63x 986x 63x 368x 6499x 368x 46x 325x 63x 20x 46x 20x 20x 20x 20x 46x 20x 63x | import dayjs from "dayjs";
import { unique } from "remeda";
import { logger } from "server/utils/logger";
import {
getGlobalNotificationQueueService,
NotificationTaskType,
} from "server/utils/notificationQueue";
import { UserAssignmentResult } from "shared/types/models/result";
import {
delDirectSignups,
delAssignmentDirectSignupsByStartTime,
findDirectSignupsByStartTime,
saveDirectSignups,
} from "server/features/direct-signup/directSignupRepository";
import {
Result,
makeErrorResult,
makeSuccessResult,
} from "shared/utils/result";
import { MongoDbError, QueueError } from "shared/types/api/errors";
import {
addEventLogItems,
deleteEventLogItemsByStartTime,
} from "server/features/user/event-log/eventLogRepository";
import { EventLogAction } from "shared/types/models/eventLog";
import { getLotterySignups } from "server/features/assignment/utils/getLotterySignups";
import { User } from "shared/types/models/user";
import { getGroupCreators } from "server/features/assignment/utils/getGroupCreators";
import { getGroupMembersWithCreatorLotterySignups } from "server/features/assignment/utils/getGroupMembers";
import { getStartingProgramItems } from "server/features/assignment/utils/getStartingProgramItems";
import { ProgramItem } from "shared/types/models/programItem";
import { SignupRepositoryAddSignup } from "server/features/direct-signup/directSignupTypes";
import { isStartTimeMatch } from "server/utils/isStartTimeMatch";
import { EmailNotificationTrigger } from "shared/types/emailNotification";
import { findSettings } from "server/features/settings/settingsRepository";
import { Settings } from "shared/types/models/settings";
interface SaveUserSignupResultsParams {
assignmentTime: string;
results: readonly UserAssignmentResult[];
users: User[];
programItems: ProgramItem[];
}
export const saveUserSignupResults = async ({
assignmentTime,
results,
users,
programItems,
}: SaveUserSignupResultsParams): Promise<
Result<void, MongoDbError | QueueError>
> => {
const queueService = getGlobalNotificationQueueService();
const settingsResult = await findSettings();
let settings: Settings | null = null;
if (settingsResult.ok) {
settings = settingsResult.value;
} else E{
logger.error(
"Failed to find settings. Do not queue notification for this assignment.",
);
}
// Remove previous lottery result for the same start time
// This does not remove non-lottery signups or previous signups from moved program items
const delAssignmentSignupsByStartTimeResult =
await delAssignmentDirectSignupsByStartTime(assignmentTime, programItems);
Iif (!delAssignmentSignupsByStartTimeResult.ok) {
return delAssignmentSignupsByStartTimeResult;
}
// Only non-lottery signups and previous signups from moved program items should be remaining
const directSignupsByStartTimeResult = await findDirectSignupsByStartTime(
assignmentTime,
programItems,
);
Iif (!directSignupsByStartTimeResult.ok) {
return directSignupsByStartTimeResult;
}
// Resolve conflicting existing direct signups
// If user has existing signups...
// ... and new assignment result -> remove existing
// ... and no new assignment result -> keep existing
// A user can hold several signups at the same start time (e.g. an always-open item plus a
// moved-in one), so remove every one of theirs, not just the first
const signupsToDelete = results.flatMap((result) =>
directSignupsByStartTimeResult.value
.filter((signup) => signup.username === result.username)
.map((signup) => ({
username: signup.username,
directSignupProgramItemId: signup.programItemId,
})),
);
const delDirectSignupsResult = await delDirectSignups(signupsToDelete);
Iif (!delDirectSignupsResult.ok) {
return delDirectSignupsResult;
}
// Save new assignment results
const newSignups: SignupRepositoryAddSignup[] = results.map((result) => {
return {
username: result.username,
directSignupProgramItemId: result.assignmentSignup.programItemId,
// assignmentTime can be parent-resolved; direct signups store parent time for lottery re-run cleanup
signedToStartTime: assignmentTime,
signupTime: dayjs().toISOString(),
// Signups received from assignment don't have signup messages
message: "",
priority: result.assignmentSignup.priority,
};
});
// This might drop some signups if by some error too many signups are passed for a program item
const saveSignupsResult = await saveDirectSignups(newSignups, programItems);
Iif (!saveSignupsResult.ok) {
return saveSignupsResult;
}
const { droppedSignups } = saveSignupsResult.value;
// Remove eventLog items from same start time
const deleteEventLogItemsByStartTimeResult =
await deleteEventLogItemsByStartTime(assignmentTime, [
EventLogAction.NEW_ASSIGNMENT,
EventLogAction.NO_ASSIGNMENT,
]);
Iif (!deleteEventLogItemsByStartTimeResult.ok) {
return deleteEventLogItemsByStartTimeResult;
}
// Filter out possible dropped results
const finalResults = results.filter((result) => {
return droppedSignups.every(
(signup) =>
signup.directSignupProgramItemId !==
result.assignmentSignup.programItemId ||
signup.username !== result.username,
);
});
// Add NEW_ASSIGNMENT to user event logs
const newAssignmentEventLogItemsResult = await addEventLogItems(
finalResults.map((result) => ({
username: result.username,
programItemId: result.assignmentSignup.programItemId,
programItemStartTime: assignmentTime,
createdAt: dayjs().toISOString(),
action: EventLogAction.NEW_ASSIGNMENT,
})),
);
Iif (!newAssignmentEventLogItemsResult.ok) {
return newAssignmentEventLogItemsResult;
}
// Add SEND_EMAIL_ACCEPTED to notification queue
Eif (
settings?.emailNotificationTrigger.includes(
EmailNotificationTrigger.ACCEPTED,
)
) {
Iif (queueService === null) {
return makeErrorResult(QueueError.QUEUE_NOT_INITIALIZED);
}
const newAssignmentEmailNotificationsResult =
queueService.addNotificationsBulk(
finalResults.map((result) => ({
type: NotificationTaskType.SEND_EMAIL_ACCEPTED,
username: result.username,
programItemId: result.assignmentSignup.programItemId,
programItemStartTime: result.assignmentSignup.signedToStartTime,
})),
);
Iif (!newAssignmentEmailNotificationsResult.ok) {
return newAssignmentEmailNotificationsResult;
}
}
// Get users who didn't get a seat in lottery
const startingProgramItems = getStartingProgramItems(
programItems,
assignmentTime,
);
const groupCreators = getGroupCreators(users, startingProgramItems);
const groupMembers = getGroupMembersWithCreatorLotterySignups(
groupCreators,
users,
);
const allAttendees = [...groupCreators, ...groupMembers];
const lotterySignups = getLotterySignups(allAttendees);
const lotterySignupsForStartingTime = lotterySignups.filter(
(lotterySignup) => {
const programItem = startingProgramItems.find(
(startingProgramItem) =>
startingProgramItem.programItemId === lotterySignup.programItemId,
);
return isStartTimeMatch(
lotterySignup.signedToStartTime,
assignmentTime,
programItem?.parentId,
);
},
);
const lotterySignupUsernames = unique(
lotterySignupsForStartingTime.map(
(lotterySignup) => lotterySignup.username,
),
);
const noAssignmentLotterySignupUsernames = lotterySignupUsernames.flatMap(
(lotterySignupUsername) => {
// Use finalResults so users whose signup was dropped are treated as not assigned
const userGotAssignment = finalResults.some(
(result) => result.username === lotterySignupUsername,
);
if (!userGotAssignment) {
return lotterySignupUsername;
}
return [];
},
);
// Add NO_ASSIGNMENT to user event logs
if (noAssignmentLotterySignupUsernames.length > 0) {
const noAssignmentEventLogItemsResult = await addEventLogItems(
noAssignmentLotterySignupUsernames.map(
(noAssignmentLotterySignupUsername) => ({
username: noAssignmentLotterySignupUsername,
programItemId: "",
programItemStartTime: assignmentTime,
createdAt: dayjs().toISOString(),
action: EventLogAction.NO_ASSIGNMENT,
}),
),
);
Iif (!noAssignmentEventLogItemsResult.ok) {
return noAssignmentEventLogItemsResult;
}
// Add SEND_EMAIL_REJECTED to notification queue
Eif (
settings?.emailNotificationTrigger.includes(
EmailNotificationTrigger.REJECTED,
)
) {
Iif (queueService === null) {
return makeErrorResult(QueueError.QUEUE_NOT_INITIALIZED);
}
const noAssignmentEmailNotificationsResult =
queueService.addNotificationsBulk(
noAssignmentLotterySignupUsernames.map(
(noAssignmentLotterySignupUsername) => ({
type: NotificationTaskType.SEND_EMAIL_REJECTED,
username: noAssignmentLotterySignupUsername,
programItemId: "",
programItemStartTime: assignmentTime,
}),
),
);
Iif (!noAssignmentEmailNotificationsResult.ok) {
return noAssignmentEmailNotificationsResult;
}
}
}
return makeSuccessResult();
};
|