<commit_before>import { Db } from 'mongodb';
import * as _ from 'underscore';
import { CanvasCalendarWithUser } from './canvas';
import * as canvas from './canvas';
import { PortalCalendarWithUser } from './portal';
import * as portal from './portal';
import { UserDoc } from './users';
import * as users from './users';

/**
 * Updates a user's Canvas cache data.
 * @param db Database connection.
 * @param user Username.
 */
export async function updateCanvasCache(db: Db, user: string) {
	if (typeof db !== 'object') { throw new Error('Invalid database connection!'); }

	const canvasdata = db.collection('canvasFeeds');

	const { isUser, userDoc } = await users.get(db, user);
	if (!isUser) { throw new Error('User doesn\'t exist!'); }

	const { events } = await canvas.getUserCal(db, userDoc!.user);

	try {
		await canvasdata.deleteMany({ user: userDoc!._id });
	} catch (e) {
		throw new Error('There was an error removing the old events from the database!');
	}

	for (const ev of events!) {
		(ev as any).user = userDoc!._id;
	}

	try {
		await canvasdata.insertMany(events as CanvasCalendarWithUser[]);
	} catch (e) {
		throw new Error('There was an error inserting events into the database!');
	}
}

/**
 * Adds a user to the Portal classes cache update queue.
 * @param db Database connection.
 * @param user Username.
 * @returns The events that are *currently* in the Portal classes cache.
 */
export async function addPortalQueueClasses(db: Db, user: string) {
	if (typeof db !== 'object') { throw new Error('Invalid database connection!'); }

	const { isUser, userDoc } = await users.get(db, user);
	if (!isUser) { throw new Error('User doesn\'t exist!'); }

	const portaldata = db.collection<PortalCalendarWithUser>('portalFeedsClasses');
	const userdata   = db.collection<UserDoc>('users');

	const { cal: events } = await portal.getFromCalClasses(db, user);

	if (_.isEmpty(events!)) {
		try {
			await userdata.updateOne({ user }, { $set: { inPortalQueueClasses: true } });
		} catch (e) {
			throw new Error('There was an error adding the user to the queue!');
		}

		return events!;
	}

	try {
		await portaldata.deleteMany({ user: userDoc!._id });
	} catch (e) {
		throw new Error('There was an error removing the old events from the database!');
	}

	for (const ev of events!) {
		(ev as any).user = userDoc!._id;
	}

	const newEvents = events as PortalCalendarWithUser[];

	try {
		await portaldata.insertMany(newEvents);
	} catch (e) {
		throw new Error(`There was an error inserting events into the database! (${e})`);
	}

	try {
		await userdata.updateOne({ user }, { $set: { inPortalQueueClasses: false } });
	} catch (e) {
		throw new Error('There was an error removing the user from the queue!');
	}

	return newEvents;
}

/**
 * Adds a user to the Portal calendar cache update queue.
 * @param db Database connection.
 * @param user Username.
 * @returns The events that are *currently* in the Portal calendar cache.
 */
export async function addPortalQueueCalendar(db: Db, user: string) {
	if (typeof db !== 'object') { throw new Error('Invalid database connection!'); }

	const { isUser, userDoc } = await users.get(db, user);
	if (!isUser) { throw new Error('User doesn\'t exist!'); }

	const portaldata = db.collection<PortalCalendarWithUser>('portalFeedsCalendar');
	const userdata   = db.collection<UserDoc>('users');

	const { cal: events } = await portal.getFromCalCalendar(db, user);

	if (_.isEmpty(events!)) {
		try {
			await userdata.updateOne({ user }, { $set: { inPortalQueueCalendar: true } });
		} catch (e) {
			throw new Error('There was an error adding the user to the queue!');
		}

		return events!;
	}

	try {
		await portaldata.deleteMany({ user: userDoc!._id });
	} catch (e) {
		throw new Error('There was an error removing the old events from the database!');
	}

	for (const ev of events!) {
		(ev as any).user = userDoc!._id;
	}

	const newEvents = events as PortalCalendarWithUser[];

	try {
		await portaldata.insertMany(newEvents);
	} catch (e) {
		throw new Error(`There was an error inserting events into the database! (${e})`);
	}

	try {
		await userdata.updateOne({ user }, { $set: { inPortalQueueCalendar: false } });
	} catch (e) {
		throw new Error('There was an error removing the user from the queue!');
	}

	return newEvents;
}

/**
 * Processes the queue for updating cached Portal data.
 * @param db Database connection.
 */
export async function processPortalQueue(db: Db) {
	if (typeof db !== 'object') { throw new Error('Invalid database connection!'); }<commit_msg>null checks to fix some prod errors we just noticed<commit_after>import { Db } from 'mongodb';
import * as _ from 'underscore';
import { CanvasCalendarWithUser } from './canvas';
import * as canvas from './canvas';
import { PortalCalendarWithUser } from './portal';
import * as portal from './portal';
import { UserDoc } from './users';
import * as users from './users';

/**
 * Updates a user's Canvas cache data.
 * @param db Database connection.
 * @param user Username.
 */
export async function updateCanvasCache(db: Db, user: string) {
	if (typeof db !== 'object') { throw new Error('Invalid database connection!'); }

	const canvasdata = db.collection('canvasFeeds');

	const { isUser, userDoc } = await users.get(db, user);
	if (!isUser) { throw new Error('User doesn\'t exist!'); }

	const { events } = await canvas.getUserCal(db, userDoc!.user);

	try {
		await canvasdata.deleteMany({ user: userDoc!._id });
	} catch (e) {
		throw new Error('There was an error removing the old events from the database!');
	}

	if (events === null) { return; }

	for (const ev of events) {
		(ev as any).user = userDoc!._id;
	}

	try {
		await canvasdata.insertMany(events as CanvasCalendarWithUser[]);
	} catch (e) {
		throw new Error('There was an error inserting events into the database!');
	}
}

/**
 * Adds a user to the Portal classes cache update queue.
 * @param db Database connection.
 * @param user Username.
 * @returns The events that are *currently* in the Portal classes cache, or null if there's no URL.
 */
export async function addPortalQueueClasses(db: Db, user: string) {
	if (typeof db !== 'object') { throw new Error('Invalid database connection!'); }

	const { isUser, userDoc } = await users.get(db, user);
	if (!isUser) { throw new Error('User doesn\'t exist!'); }

	const portaldata = db.collection<PortalCalendarWithUser>('portalFeedsClasses');
	const userdata   = db.collection<UserDoc>('users');

	const { cal: events } = await portal.getFromCalClasses(db, user);

	if (events === null) {
		return events;
	} else if (_.isEmpty(events)) {
		try {
			await userdata.updateOne({ user }, { $set: { inPortalQueueClasses: true } });
		} catch (e) {
			throw new Error('There was an error adding the user to the queue!');
		}

		return events;
	}

	try {
		await portaldata.deleteMany({ user: userDoc!._id });
	} catch (e) {
		throw new Error('There was an error removing the old events from the database!');
	}

	for (const ev of events) {
		(ev as any).user = userDoc!._id;
	}

	const newEvents = events as PortalCalendarWithUser[];

	try {
		await portaldata.insertMany(newEvents);
	} catch (e) {
		throw new Error(`There was an error inserting events into the database! (${e})`);
	}

	try {
		await userdata.updateOne({ user }, { $set: { inPortalQueueClasses: false } });
	} catch (e) {
		throw new Error('There was an error removing the user from the queue!');
	}

	return newEvents;
}

/**
 * Adds a user to the Portal calendar cache update queue.
 * @param db Database connection.
 * @param user Username.
 * @returns The events that are *currently* in the Portal calendar cache, or null if there's no URL.
 */
export async function addPortalQueueCalendar(db: Db, user: string) {
	if (typeof db !== 'object') { throw new Error('Invalid database connection!'); }

	const { isUser, userDoc } = await users.get(db, user);
	if (!isUser) { throw new Error('User doesn\'t exist!'); }

	const portaldata = db.collection<PortalCalendarWithUser>('portalFeedsCalendar');
	const userdata   = db.collection<UserDoc>('users');

	const { cal: events } = await portal.getFromCalCalendar(db, user);

	if (events === null) {
		return events;
	} else if (_.isEmpty(events)) {
		try {
			await userdata.updateOne({ user }, { $set: { inPortalQueueCalendar: true } });
		} catch (e) {
			throw new Error('There was an error adding the user to the queue!');
		}

		return events!;
	}

	try {
		await portaldata.deleteMany({ user: userDoc!._id });
	} catch (e) {
		throw new Error('There was an error removing the old events from the database!');
	}

	for (const ev of events) {
		(ev as any).user = userDoc!._id;
	}

	const newEvents = events as PortalCalendarWithUser[];

	try {
		await portaldata.insertMany(newEvents);
	} catch (e) {
		throw new Error(`There was an error inserting events into the database! (${e})`);
	}

	try {
		await userdata.updateOne({ user }, { $set: { inPortalQueueCalendar: false } });
	} catch (e) {
		throw new Error('There was an error removing the user from the queue!');
	}

	return newEvents;
}

/**
 * Processes the queue for updating cached Portal data.
 * @param db Database connection.
 */
export async function processPortalQueue(db: Db) {
	if (typeof db !== 'object') { throw new Error('Invalid database connection!'); }
{'fraction_non_alphanumeric': 0.1368274084416282, 'fraction_numerical': 0.0, 'mean_word_length': 4.03623580313683, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2594626', 'n_tokens_mistral': 2982, 'n_tokens_neox': 2858, 'n_words': 1025}