<commit_before>"use strict";

module.exports = function(app, util, schemas) {

	let ObjectId = require("mongoose").Types.ObjectId;

	let requireLogin = util.requireLogin;
	let requireLeader = util.requireLeader;
	let requireAdmin = util.requireAdmin;

	let User = schemas.User;
	let Event = schemas.Event;
	let AttendanceHandler = schemas.AttendanceHandler;

	//assign variables to util functions(and objects) and database schemas
	for (key in util) {
		eval("var " + key + " = util." + key + ";");
	}
	for (key in schemas) {
		eval("var " + key + " = schemas." + key + ";");
	}

	app.post("/f/getEventsForUserInTeamInMonth", requireLogin, function(req, res) {
		let userSubdivisionIds = util.activeSubdivisionIds(req.user.subdivisions);
		let numberOfDays = new Date(req.body.year, req.body.month, 0).getDate(); //month is 1 based
		let start = new Date(req.body.year, req.body.month-1, 1, 0, 0, 0); //month is 0 based
		let end = new Date(req.body.year, req.body.month-1, numberOfDays, 23, 59, 59); //month is 0 based
		Event.find({
			team: req.user.current_team.id,
			$or: [
				{ entireTeam: true },
				{ userAttendees: req.user._id },
				{ subdivisionAttendees: { "$in": userSubdivisionIds } }
			],
			date: {$gte: start, $lte: end}
		}, function(err, events) {
			if (err) {
				console.error(err);
				res.end("fail");
			} else {
				res.json(events)
			}
		});
	});
	app.post("/f/getUpcomingEventsForUser", requireLogin, function(req, res) {
		let userSubdivisionIds = util.activeSubdivisionIds(req.user.subdivisions);
		Event.find({
			team: req.user.current_team.id,
			$or: [
				{ entireTeam: true },
				{ userAttendees: req.user._id },
				{ subdivisionAttendees: { "$in": userSubdivisionIds } }
			],
			date: {$gte: new Date()}
		}).sort("date").exec(function(err, events) {
			if (err) {
				console.error(err);
				res.end("fail");
			} else {
				res.end( JSON.stringify(events) );
			}
		});
	});
	app.post("/f/createEvent", requireLogin, requireLeader, function(req, res) {

		if (req.body.userAttendees == undefined) {
			req.body.userAttendees = []
		}
		if (req.body.subdivisionAttendees == undefined) {
			req.body.subdivisionAttendees = []
		}

		if (req.body.hasAttendance == "true") {
			req.body.hasAttendance = true;
		} else {
			req.body.hasAttendance = false;
		}

		if (req.body.sendEmail == "true") {
			req.body.sendEmail = true;
		} else {
			req.body.sendEmail = false;
		}


		if (req.body.description != "") {
			if (req.body.entireTeam == "true") {
				Event.create({
					name: req.body.name,
					description: req.body.description,
					entireTeam: true,
					date: new Date(req.body.date),
					team: req.user.current_team.id,
					creator: req.user._id,
					hasAttendance: req.body.hasAttendance
				}, function(err, event) {
					if (err) {
						console.error(err);
						res.end("fail");
					} else {
						User.find({ teams: {$elemMatch: {id: req.user.current_team.id }} }, "-password", function(err, users) {
							if (err) {
								console.error(err);
								res.end("fail");
							} else {
								if (req.body.sendEmail) {
									let list = util.createRecepientList(users);
									util.notify.sendMail({
											from: "MorTeam Notification <<EMAIL>>",
											to: list,
											subject: "New Event on " + readableDate(event.date) + " - " + event.name,
											html: req.user.firstname + " " + req.user.lastname + " has created an event on " + readableDate(event.date) + ",<br><br>" + event.name + "<br>" + event.description
									});
								}
								if (req.body.hasAttendance) {<commit_msg>Begin promisifying chat.js<commit_after>"use strict";

module.exports = function(app, util, schemas) {

	let ObjectId = require("mongoose").Types.ObjectId;
	let Promise = require("bluebird");

	let requireLogin = util.requireLogin;
	let requireLeader = util.requireLeader;
	let requireAdmin = util.requireAdmin;

	let User = schemas.User;
	let Event = schemas.Event;
	let AttendanceHandler = schemas.AttendanceHandler;

	app.post("/f/getEventsForUserInTeamInMonth", requireLogin, Promise.coroutine(function*(req, res) {
		let userSubdivisionIds = util.activeSubdivisionIds(req.user.subdivisions);

		let numberOfDays = new Date(req.body.year, req.body.month, 0).getDate(); // month is 1 based
		let start = new Date(req.body.year, req.body.month - 1, 1, 0, 0, 0); // month is 0 based
		let end = new Date(req.body.year, req.body.month - 1, numberOfDays, 23, 59, 59); // month is 0 based

		try {

			let events = yield Event.find({
				team: req.user.current_team.id,
				$or: [
					{ entireTeam: true },
					{ userAttendees: req.user._id },
					{ subdivisionAttendees: { "$in": userSubdivisionIds } }
				],
				date: {$gte: start, $lte: end}
			});
			
			res.json(events);

		} catch (err) {
			console.error(err);
			res.end("fail");
		}
	}));

	app.post("/f/getUpcomingEventsForUser", requireLogin, Promise.coroutine(function*(req, res) {
		let userSubdivisionIds = util.activeSubdivisionIds(req.user.subdivisions);

		try {

			let events = yield Event.find({
				team: req.user.current_team.id,
				$or: [
					{ entireTeam: true },
					{ userAttendees: req.user._id },
					{ subdivisionAttendees: { "$in": userSubdivisionIds } }
				],
				date: {$gte: new Date()}
			}).sort("date").exec();
			
			res.end( JSON.stringify(events) );

		} catch (err) {
			console.error(err);
			res.end("fail");
		}
	}));

	app.post("/f/createEvent", requireLogin, requireLeader, Promise.coroutine(function*(req, res) {

		if (req.body.userAttendees == undefined) {
			req.body.userAttendees = []
		}
		if (req.body.subdivisionAttendees == undefined) {
			req.body.subdivisionAttendees = []
		}

		if (req.body.hasAttendance == "true") {
			req.body.hasAttendance = true;
		} else {
			req.body.hasAttendance = false;
		}

		if (req.body.sendEmail == "true") {
			req.body.sendEmail = true;
		} else {
			req.body.sendEmail = false;
		}

		if (req.body.description != "") {
			if (req.body.entireTeam == "true") {
				Event.create({
					name: req.body.name,
					description: req.body.description,
					entireTeam: true,
					date: new Date(req.body.date),
					team: req.user.current_team.id,
					creator: req.user._id,
					hasAttendance: req.body.hasAttendance
				}, function(err, event) {
					if (err) {
						console.error(err);
						res.end("fail");
					} else {
						User.find({ teams: {$elemMatch: {id: req.user.current_team.id }} }, "-password", function(err, users) {
							if (err) {
								console.error(err);
								res.end("fail");
							} else {
								if (req.body.sendEmail) {
									let list = util.createRecepientList(users);
									util.notify.sendMail({
											from: "MorTeam Notification <<EMAIL>>",
											to: list,
											subject: "New Event on " + readableDate(event.date) + " - " + event.name,
											html: req.user.firstname + " " + req.user.lastname + " has created an event on " + readableDate(event.date) + ",<br><br>" + event.name + "<br>" + event.description
									});
								}
								if (req.body.hasAttendance) {
{'fraction_non_alphanumeric': 0.16182513816069152, 'fraction_numerical': 0.004534504747059657, 'mean_word_length': 2.940815187046343, 'pattern_counts': {'":': 4, '<': 13, '<?xml version=': 0, '>': 13, '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': '2080434', 'n_tokens_mistral': 3043, 'n_tokens_neox': 2437, 'n_words': 491}