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 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 | 42x 126x 126x 126x 126x 126x 126x 1x 1x 1x 125x 125x 1x 124x 1x 123x 1x 122x 122x 122x 122x 1x 121x 121x 2x 2x 2x 119x 119x 119x 1x 118x 118x 118x 118x 118x 100x 118x 124x 118x 124x 118x 109x 109x 24x 24x 24x 109x 105x 42x 12x 12x 12x 12x 12x 1x 1x 1x 11x 11x 11x 11x 11x 11x 1x 10x 10x 10x 10x 9x 10x 9x | import { config } from "shared/config";
import { DIRECT_SIGNUP_PRIORITY } from "shared/constants/signups";
import {
DeleteDirectSignupRequest,
DeleteDirectSignupResponse,
PostDirectSignupRequest,
PostDirectSignupResponse,
} from "shared/types/api/myProgramItems";
import { SignupType, State } from "shared/types/models/programItem";
import { getProgramItemValidity } from "shared/utils/getProgramItemValidity";
import { isLotterySignupProgramItem } from "shared/utils/isLotterySignupProgramItem";
import {
getDirectSignupEndTime,
getDirectSignupStartTime,
getDirectSignupStarted,
} from "shared/utils/signupTimes";
import { getTimeNow } from "server/features/assignment/utils/getTimeNow";
import {
delDirectSignup,
saveDirectSignup,
} from "server/features/direct-signup/directSignupRepository";
import { SignupRepositoryAddSignup } from "server/features/direct-signup/directSignupTypes";
import { findProgramItemById } from "server/features/program-item/programItemRepository";
import { getSignupMessage } from "server/features/program-item/programItemUtils";
import { findOrCreateSettings } from "server/features/settings/settingsRepository";
import { leaveOrCloseGroup } from "server/features/user/group/groupService";
import { hasSignupEnded } from "server/features/user/userUtils";
import { logger } from "server/utils/logger";
export const storeDirectSignup = async (
signupRequest: PostDirectSignupRequest,
username: string,
): Promise<PostDirectSignupResponse> => {
const { directSignupProgramItemId } = signupRequest;
const timeNowResult = await getTimeNow();
Iif (!timeNowResult.ok) {
return {
message: "Unable to get current time",
status: "error",
errorId: "unknown",
};
}
const timeNow = timeNowResult.value;
const programItemResult = await findProgramItemById(
directSignupProgramItemId,
);
if (!programItemResult.ok) {
const message = `Signed program item ${directSignupProgramItemId} not found`;
logger.warn(message);
return {
message,
status: "error",
errorId: "unknown",
};
}
const programItem = programItemResult.value;
if (
programItem.signupType !== SignupType.KONSTI ||
config.event().noKonstiSignupIds.includes(directSignupProgramItemId)
) {
return {
message: "No Konsti signup for this program item",
status: "error",
errorId: "noKonstiSignup",
};
}
if (programItem.state === State.CANCELLED) {
return {
message: "Program item is cancelled",
status: "error",
errorId: "cancelled",
};
}
// Invalid program items have their sign-up disabled in the client, but a
// sign-up can still arrive from a stale or bugged page
if (!getProgramItemValidity(programItem).allValuesValid) {
return {
message: "Program item is missing required information",
status: "error",
errorId: "invalidProgramItem",
};
}
const settingsResult = await findOrCreateSettings();
Iif (!settingsResult.ok) {
return {
message: "Error loading settings",
status: "error",
errorId: "unknown",
};
}
const settings = settingsResult.value;
// Hidden program items are only filtered from the client's list view, so a
// sign-up for one can still arrive from a stale page or a direct link
if (settings.hiddenProgramItemIds.includes(directSignupProgramItemId)) {
return {
message: "Program item is hidden",
status: "error",
errorId: "hidden",
};
}
const directSignupStartTime = getDirectSignupStartTime(programItem);
if (!getDirectSignupStarted(programItem, timeNow)) {
// String rather than toISOString: this branch is reached when the start time
// may be unparseable, and toISOString throws on that, which would turn the
// structured error into a 500
const message = `Signup for program item ${directSignupProgramItemId} not open yet, opens ${String(directSignupStartTime)}`;
logger.warn(message);
return {
errorId: "signupNotOpenYet",
message,
status: "error",
};
}
const directSignupEndTime = getDirectSignupEndTime(programItem);
const signupEnded = hasSignupEnded({
signupEndTime: directSignupEndTime,
timeNow,
});
if (signupEnded) {
return {
errorId: "signupEnded",
message: "Signup time ended",
status: "error",
};
}
const newDirectSignup: SignupRepositoryAddSignup = {
...signupRequest,
username,
// User-made direct sign-ups are always first-come-first-served; the priority is set
// here rather than trusted from the request
priority: DIRECT_SIGNUP_PRIORITY,
// A spot belongs to the hour the attendee turns up, which for a batched program item is
// not the hour its lottery runs at
signedToStartTime: programItem.startTime,
signupTime: timeNow.toISOString(),
};
const signupResult = await saveDirectSignup(newDirectSignup);
Iif (!signupResult.ok) {
return {
message: "Store signup failure",
status: "error",
errorId: "unknown",
};
}
const signup = signupResult.value;
const signupQuestion = settings.signupQuestions.find(
(message) => message.programItemId === programItem.programItemId,
);
const allSignups = {
programItemId: signup.programItemId,
userSignups: signup.userSignups.map((userSignup) => ({
username: userSignup.username,
message: getSignupMessage(signupQuestion, userSignup.message),
})),
};
// The user missing from the stored sign-ups means the program item was full
const newSignup = signup.userSignups.find(
(userSignup) => userSignup.username === username,
);
if (newSignup) {
// Leaving the group only for a program item the lottery allocates: a group exists to enter
// those together, so taking one of their spots alone leaves it. The sign-up already
// persisted, so a group-leave failure is logged rather than failing the request.
let leftGroup = false;
if (isLotterySignupProgramItem(programItem)) {
const leaveOrCloseGroupResult = await leaveOrCloseGroup(username);
if (leaveOrCloseGroupResult.ok) {
leftGroup = leaveOrCloseGroupResult.value;
} else E{
logger.error(
new Error(
`leaveOrCloseGroup failed after direct signup for ${username}: ${leaveOrCloseGroupResult.error}`,
),
);
}
}
return {
message: "Store signup success",
status: "success",
allSignups,
directSignup: {
programItemId: signup.programItemId,
priority: newSignup.priority,
signedToStartTime: newSignup.signedToStartTime,
// A private sign-up question only hides the answer from other attendees:
// the user's own sign-up keeps it so the UI can show it back to them
message: newSignup.message,
},
leftGroup,
};
}
return {
message: "Program item full",
status: "success",
allSignups,
leftGroup: false,
};
};
export const removeDirectSignup = async (
signupRequest: DeleteDirectSignupRequest,
username: string,
): Promise<DeleteDirectSignupResponse> => {
const { directSignupProgramItemId } = signupRequest;
const timeNowResult = await getTimeNow();
Iif (!timeNowResult.ok) {
return {
message: "Unable to get current time",
status: "error",
errorId: "unknown",
};
}
const programItemResult = await findProgramItemById(
directSignupProgramItemId,
);
if (!programItemResult.ok) {
const message = `Signed program item ${directSignupProgramItemId} not found`;
logger.warn(message);
return {
message,
status: "error",
errorId: "unknown",
};
}
const programItem = programItemResult.value;
const directSignupEndTime = getDirectSignupEndTime(programItem);
const signupEnded = hasSignupEnded({
signupEndTime: directSignupEndTime,
timeNow: timeNowResult.value,
});
Iif (signupEnded) {
return {
errorId: "signupEnded",
message: "Signup failure",
status: "error",
};
}
const signupResult = await delDirectSignup({
directSignupProgramItemId: signupRequest.directSignupProgramItemId,
username,
});
if (!signupResult.ok) {
return {
message: "Delete signup failure",
status: "error",
errorId: "unknown",
};
}
const signup = signupResult.value;
const settingsResult = await findOrCreateSettings();
Iif (!settingsResult.ok) {
return {
message: "Error loading settings",
status: "error",
errorId: "unknown",
};
}
const signupQuestion = settingsResult.value.signupQuestions.find(
(message) => message.programItemId === programItem.programItemId,
);
return {
message: "Delete signup success",
status: "success",
allSignups: {
programItemId: signup.programItemId,
userSignups: signup.userSignups.map((userSignup) => ({
username: userSignup.username,
message: getSignupMessage(signupQuestion, userSignup.message),
})),
},
};
};
|