const { getUsername } = require('./TSP');

const getInvalidatedUuid = (uuid) => `${uuid}/invalid!`;
const getInvalidatedEmail = (email) => `${email}.invalid`;

const invalidateUser = async (app, user) => {
	const userService = app.service('usersModel');
	const accountService = app.service('accountModel');

	const invalidatedUuid = getInvalidatedUuid(user.sourceOptions.tspUid);
	const userChanges = {
		sourceOptions: {
			tspUid: invalidatedUuid,
		},
		email: getInvalidatedEmail(user.email),
	};
	await userService.patch(user._id, userChanges);

	const accountChanges = {
		username: getUsername(userChanges),
	};
	await accountService.patch(null, accountChanges, { query: { userId: user._id } });
};

const deleteUser = (app, user) => {
	const userService = app.service('usersModel');
	const accountService = app.service('accountModel');
	return Promise.all([
		userService.remove({ _id: user._id }),
		accountService.remove(null, { query: { userId: user._id } }),
	]);
};

const grantAccessToPrivateFiles = async (app, oldUser, newUser) => {
	const fileService = app.service('/files');
	const searchParams = {
		query: {
			refOwnerModel: 'user',
			owner: oldUser._id,
		},
	};
	const updateData = {
		owner: newUser._id,
	};
	await fileService.patch(null, updateData, searchParams);
};

const grantAccessToSharedFiles = async (app, oldUser, newUser) => {
	/*
		- shared files are those with
			a) one item in the permissions array with (refPermModel === 'user' && refId === oldUser._id) AND
			b) creator must not be oldUser._id (the creator of a file used to be denoted by the first item in the
				permisions array; that is no longer the case -- we have the creator attribute now -- but files
				still have both => SC-3851)
	*/
};

const switchSchool = async (app, currentUser, createUserMethod) => {
	await invalidateUser(app, currentUser);
	const newUser = await createUserMethod();
	await Promise.all([
		grantAccessToPrivateFiles(app, currentUser, newUser),
		grantAccessToSharedFiles(app, currentUser, newUser),
	]);
	await deleteUser(app, currentUser);
	return newUser;
};

module.exports = {
	getInvalidatedUuid,
	invalidateUser,
	deleteUser,
	grantAccessToPrivateFiles,
	grantAccessToSharedFiles,
	switchSchool,
};

@@ -1357,16 +1357,599 @@
 r) =%3E %7B%0A
+%09console.log('here');%0A%09const fileModel = app.service('file');%0A%0A%09try %7B%0A%09%09const filesToUpdate = await fileModel.find(%0A%09%09%09%7B%0A%09%09%09%09'permissions.refPermModel': 'user',%0A%09%09%09%09'permissions.refId': oldUser._id,%0A%09%09%09%09owner: %7B%0A%09%09%09%09%09$ne: oldUser._id,%0A%09%09%09%09%7D%0A%09%09%09%7D);%0A%09%09const updateFilesRefIdResult = await Promise.all(filesToUpdate.map((file) =%3E %7B%0A%09%09%09return fileModel.update(%7B_id: file._id, 'permissions.refId': oldUser._id %7D,%0A%09%09%09%09%7B $set: %7B 'permissions.$.refId': newUser._id %7D%7D);%0A%09%09%7D));%0A%09%09console.log(updateFilesRefIdResult);%0A%09%09return updateFilesRefIdResult;%0A%0A%09%7D catch (err) %7B%0A%09%09console.log(err);%0A%09%7D%0A%0A
 %09/*%0A%09%09- 

Prepare first draft of the grantAccessToSharedFiles method