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 | 58x 441x 441x 441x 441x 58x 427x 427x 427x 427x 427x 427x 427x 427x 427x 427x 427x 6123x 6123x 427x 427x 427x 427x 1421x 427x 427x 427x 427x 6123x 68x 6123x 6069x 5022x 427x 368x 368x 427x 58x 1373x 1373x 1373x 1373x 16410x 16410x 16410x 1373x 58x 371x 371x 371x 371x 9x 367x 367x 367x 58x 19x 3x 19x 19x 19x 19x | import { MongoDbError } from "shared/types/api/errors";
import { Popularity, ProgramItem } from "shared/types/models/programItem";
import { differenceBy } from "shared/utils/remedaExtend";
import {
Result,
makeErrorResult,
makeSuccessResult,
} from "shared/utils/result";
import { removeCancelledDeletedProgramItemsFromUsers } from "server/features/assignment/utils/removeInvalidProgramItemsFromUsers";
import { updateMovedProgramItems } from "server/features/assignment/utils/updateMovedProgramItems";
import {
createEmptyDirectSignupDocumentForProgramItems,
findDirectSignups,
} from "server/features/direct-signup/directSignupRepository";
import {
ProgramItemModel,
ProgramItemSchemaDb,
} from "server/features/program-item/programItemSchema";
import { handleCancelledDeletedProgramItems } from "server/features/program-item/programItemUtils";
import { logger } from "server/utils/logger";
export const removeProgramItems = async (
programItemIds?: string[],
): Promise<Result<void, MongoDbError>> => {
logger.info(
`MongoDB: remove program items from db: ${programItemIds ? programItemIds.join(", ") : "ALL"}`,
);
try {
await ProgramItemModel.deleteMany(
programItemIds ? { programItemId: { $in: programItemIds } } : {},
);
return makeSuccessResult();
} catch (error) {
logger.error(
new Error("MongoDB: Error removing program items", { cause: error }),
);
return makeErrorResult(MongoDbError.UNKNOWN_ERROR);
}
};
export const saveProgramItems = async (
updatedProgramItems: readonly ProgramItem[],
): Promise<Result<void, MongoDbError>> => {
logger.info("MongoDB: Store program items to DB");
const currentProgramItemsResult = await findProgramItems();
Iif (!currentProgramItemsResult.ok) {
return currentProgramItemsResult;
}
const currentProgramItems = currentProgramItemsResult.value;
// If program item was cancelled or deleted, remove program item and direct sign-ups
const deletedProgramItemsResult = await handleCancelledDeletedProgramItems(
updatedProgramItems,
currentProgramItems,
);
Iif (!deletedProgramItemsResult.ok) {
return deletedProgramItemsResult;
}
// If program item was cancelled or deleted, remove lottery sign-ups and favorite program items
const removeCancelledDeletedProgramItemsFromUsersResult =
await removeCancelledDeletedProgramItemsFromUsers({
programItems: updatedProgramItems,
currentProgramItems,
notifyAffectedDirectSignups: deletedProgramItemsResult.value,
notify: true,
});
Iif (!removeCancelledDeletedProgramItemsFromUsersResult.ok) {
return removeCancelledDeletedProgramItemsFromUsersResult;
}
const updateMovedProgramItemsResult = await updateMovedProgramItems(
updatedProgramItems,
currentProgramItems,
);
Iif (!updateMovedProgramItemsResult.ok) {
return updateMovedProgramItemsResult;
}
const bulkOps = updatedProgramItems.map((programItem) => {
const newProgramItem: Omit<ProgramItem, "popularity"> = {
programItemId: programItem.programItemId,
parentId: programItem.parentId,
title: programItem.title,
description: programItem.description,
location: programItem.location,
startTime: programItem.startTime,
mins: programItem.mins,
tags: programItem.tags,
ageGroups: programItem.ageGroups,
genres: programItem.genres,
styles: programItem.styles,
languages: programItem.languages,
endTime: programItem.endTime,
people: programItem.people,
minAttendance: programItem.minAttendance,
maxAttendance: programItem.maxAttendance,
gameSystem: programItem.gameSystem,
shortDescription: programItem.shortDescription,
revolvingDoor: programItem.revolvingDoor,
programType: programItem.programType,
contentWarnings: programItem.contentWarnings,
otherAuthor: programItem.otherAuthor,
accessibilityValues: programItem.accessibilityValues,
otherAccessibilityInformation: programItem.otherAccessibilityInformation,
entryFee: programItem.entryFee,
signupType: programItem.signupType,
state: programItem.state,
};
return {
updateOne: {
filter: {
programItemId: programItem.programItemId,
},
update: {
...newProgramItem,
},
upsert: true,
},
};
});
try {
await ProgramItemModel.bulkWrite(bulkOps);
logger.debug("MongoDB: Program items saved to DB successfully");
} catch (error) {
logger.error(
new Error("Error saving program items to DB", { cause: error }),
);
return makeErrorResult(MongoDbError.UNKNOWN_ERROR);
}
const newProgramItems = differenceBy(
updatedProgramItems,
currentProgramItems,
(programItem) => programItem.programItemId,
);
logger.info(`MongoDB: Found ${newProgramItems.length} new program items`);
// Create sign-up document for all program items missing sign-up document
const directSignupsResult = await findDirectSignups();
Iif (!directSignupsResult.ok) {
return directSignupsResult;
}
const directSignupDocMissingProgramItemIds = updatedProgramItems.flatMap(
(updatedProgramItem) => {
const found = directSignupsResult.value.some(
(directSignup) =>
directSignup.programItemId === updatedProgramItem.programItemId,
);
if (!found) {
return updatedProgramItem.programItemId;
}
return [];
},
);
if (directSignupDocMissingProgramItemIds.length > 0) {
const createEmptySignupResult =
await createEmptyDirectSignupDocumentForProgramItems(
directSignupDocMissingProgramItemIds,
);
Iif (!createEmptySignupResult.ok) {
return createEmptySignupResult;
}
}
return makeSuccessResult();
};
export const findProgramItems = async (): Promise<
Result<ProgramItem[], MongoDbError>
> => {
try {
const response = await ProgramItemModel.find({}).lean();
logger.debug("MongoDB: Find all program items");
const programItems = response.flatMap((programItem) => {
const result = ProgramItemSchemaDb.safeParse(programItem);
Iif (!result.success) {
logger.error(
new Error(
`Error validating findProgramItems DB value: programItemId: ${programItem.programItemId}`,
{ cause: result.error },
),
);
return [];
}
return result.data;
});
return makeSuccessResult(programItems);
} catch (error) {
logger.error(
new Error("MongoDB: Error fetching program items", { cause: error }),
);
return makeErrorResult(MongoDbError.UNKNOWN_ERROR);
}
};
export const findProgramItemById = async (
programItemId: string,
): Promise<Result<ProgramItem, MongoDbError>> => {
logger.debug(`MongoDB: Find program item with id ${programItemId}`);
try {
const response = await ProgramItemModel.findOne({
programItemId,
}).lean();
if (!response) {
return makeErrorResult(MongoDbError.PROGRAM_ITEM_NOT_FOUND);
}
const result = ProgramItemSchemaDb.safeParse(response);
Iif (!result.success) {
logger.error(
new Error(`Error validating findProgramItemById DB value`, {
cause: result.error,
}),
);
return makeErrorResult(MongoDbError.UNKNOWN_ERROR);
}
return makeSuccessResult(result.data);
} catch (error) {
logger.error(
new Error("MongoDB: Error fetching programItemId", { cause: error }),
);
return makeErrorResult(MongoDbError.UNKNOWN_ERROR);
}
};
interface PopularityUpdate {
programItemId: string;
popularity: Popularity;
}
export const saveProgramItemPopularity = async (
popularityUpdates: PopularityUpdate[],
): Promise<Result<void, MongoDbError>> => {
const bulkOps = popularityUpdates.map((popularityUpdate) => {
return {
updateOne: {
filter: {
programItemId: popularityUpdate.programItemId,
},
update: {
popularity: popularityUpdate.popularity,
},
},
};
});
try {
await ProgramItemModel.bulkWrite(bulkOps);
logger.info(
`MongoDB: Updated popularity for ${popularityUpdates.length} program items`,
);
return makeSuccessResult();
} catch (error) {
logger.error(
new Error("Error updating program item popularity", { cause: error }),
);
return makeErrorResult(MongoDbError.UNKNOWN_ERROR);
}
};
|