<commit_before>const mongoose = require('mongoose');
// eslint-disable-next-line no-unused-vars
const { info, error } = require('../src/logger');

const { connect, close } = require('../src/utils/database');


const { userModel: UserModel } = require('../src/services/user/model'); // mongoose.model('user');
const { teamsModel: TeamsModel } = require('../src/services/teams/model'); // mongoose.model('team');

const equal = (id1, id2) => id1.toString() === id2.toString();

const filterTeamWithMissingSchoolIds = (teams) => teams.filter((team) => team.userIds.some((user) => !user.schoolId));

const selectTeamUserWithoutSchoolIds = (teams) => {
	const usersIds = [];
	teams.forEach((team) => {
		team.userIds.forEach(({ userId, schoolId }) => {
			if (!schoolId) {
				usersIds.push(userId);
			}
		});
	});
	// map to uniq id array
	return [...new Set(usersIds)];
};

const idsToOrCondition = (ids) => ids.map((_id) => ({ _id }));


module.exports = {
	up: async function up() {
		await connect();
		let teamsForFixing; let users;
		// pre conditions and data selects
		try {
			// TeamsModel.find({ 'userIds.0.schoolId': { $exists: true } })
			const teams = await TeamsModel.find({}).lean().exec();
			teamsForFixing = filterTeamWithMissingSchoolIds(teams);
			const userIdsForFixing = selectTeamUserWithoutSchoolIds(teamsForFixing);
			const $or = idsToOrCondition(userIdsForFixing);
			users = await UserModel.find({ $or }).select('schoolId').lean().exec();
		} catch (err) {
			error('Pre select for data failed.', err);
		}

		// pre fix team data
		info(`Found ${teamsForFixing.length} teams for fixing.`, teamsForFixing.map((team) => team._id));
		teamsForFixing.forEach((team) => {
			team.userIds.forEach((user) => {
				if (!user.schoolId) {
					const baseUser = users.find(({ _id }) => equal(user.userId, _id));
					user.schoolId = baseUser.schoolId;
				}
			});
		});

		// create querys for fixes
		const promises = teamsForFixing.map(({ _id, userIds }) => {
			const $set = { userIds };
			return TeamsModel.update({ _id }, { $set }).lean().exec();
		});

		// querying fixes
		const erg = await Promise.all(promises).catch((err) => {
			error('Something go wrong!', err);
		});

		info(erg);

		await close();
	},

	down: async function down() {
		await connect();
		info('Nothing to do!');
		await close();
	},
};<commit_msg>remove unused connections<commit_after>const { info, error } = require('../src/logger');

const { connect, close } = require('../src/utils/database');


const { userModel: UserModel } = require('../src/services/user/model'); // mongoose.model('user');
const { teamsModel: TeamsModel } = require('../src/services/teams/model'); // mongoose.model('team');

const equal = (id1, id2) => id1.toString() === id2.toString();

const filterTeamWithMissingSchoolIds = (teams) => teams.filter((team) => team.userIds.some((user) => !user.schoolId));

const selectTeamUserWithoutSchoolIds = (teams) => {
	const usersIds = [];
	teams.forEach((team) => {
		team.userIds.forEach(({ userId, schoolId }) => {
			if (!schoolId) {
				usersIds.push(userId);
			}
		});
	});
	// map to uniq id array
	return [...new Set(usersIds)];
};

const idsToOrCondition = (ids) => ids.map((_id) => ({ _id }));


module.exports = {
	up: async function up() {
		await connect();
		let teamsForFixing; let users;
		// pre conditions and data selects
		try {
			// TeamsModel.find({ 'userIds.0.schoolId': { $exists: true } })
			const teams = await TeamsModel.find({}).lean().exec();
			teamsForFixing = filterTeamWithMissingSchoolIds(teams);
			const userIdsForFixing = selectTeamUserWithoutSchoolIds(teamsForFixing);
			const $or = idsToOrCondition(userIdsForFixing);
			users = await UserModel.find({ $or }).select('schoolId').lean().exec();
		} catch (err) {
			error('Pre select for data failed.', err);
		}

		// pre fix team data
		info(`Found ${teamsForFixing.length} teams for fixing.`, teamsForFixing.map((team) => team._id));
		teamsForFixing.forEach((team) => {
			team.userIds.forEach((user) => {
				if (!user.schoolId) {
					const baseUser = users.find(({ _id }) => equal(user.userId, _id));
					user.schoolId = baseUser.schoolId;
				}
			});
		});

		// create querys for fixes
		const promises = teamsForFixing.map(({ _id, userIds }) => {
			const $set = { userIds };
			return TeamsModel.update({ _id }, { $set }).lean().exec();
		});

		// querying fixes
		const erg = await Promise.all(promises).catch((err) => {
			error('Something go wrong!', err);
		});

		info(erg);

		await close();
	},

	down: async function down() {
		info('Nothing to do!');
	},
};
{'fraction_non_alphanumeric': 0.18876551273677336, 'fraction_numerical': 0.002177226213803614, 'mean_word_length': 3.8004179728317657, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 33, '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': '4936096', 'n_tokens_mistral': 1674, 'n_tokens_neox': 1526, 'n_words': 333}