<commit_before>UserGetter = require '../User/UserGetter'
UserUpdater = require '../User/UserUpdater'
OneTimeTokenHandler = require '../Security/OneTimeTokenHandler'
logger = require 'logger-sharelatex'
Settings = require 'settings-sharelatex'


module.exports = AccountMergeEmailController =

	confirmMergeFromEmail: (req, res, next) ->
		token = req.query.token
		if !token
			return res.status(400).send()
		OneTimeTokenHandler.getValueFromTokenAndExpire 'account-merge-email-to-ol', token, (err, data) ->
			return next(err) if err?
			if !data
				return res.status(404).send()
			if data.origin != 'sl'
				logger.log {}, "Only sharelatex origin supported"
				# TODO: better error handling
				return res.status(501).send()
			{ sl_id, v1_id, final_email } = data
			UserGetter.getUser sl_id, {_id: 1, overleaf: 1}, (err, user) ->
				return next(err) if err?
				if !user?
					logger.err {userId: sl_id}, 'SL user not found for account-merge'
					# TODO: better error handling
					return res.status(400).send()
				if user?.overleaf?.id?
					logger.err {userId: sl_id}, 'SL user is already linked to overleaf'
					return res.status(400).send()
				logger.log {sl_id, v1_id, final_email, origin: data.origin},
					"[AccountMergeEmailController] about to merge sharelatex and overleaf-v1 accounts"
				UserUpdater.updateUser sl_id, {
					'$set': {
						'overleaf.id': v1_id,
						email: final_email
					}
				}, (err) ->
					return next(err) if err?
					res.render 'account_merge/finish', {
						finalEmail: final_email,
						continueUrl: "#{Settings.accountMerge.sharelatexHost}/user/login_to_ol_v2"
					}<commit_msg>Also add the final email to the array of emails on the account<commit_after>UserGetter = require '../User/UserGetter'
UserUpdater = require '../User/UserUpdater'
OneTimeTokenHandler = require '../Security/OneTimeTokenHandler'
logger = require 'logger-sharelatex'
Settings = require 'settings-sharelatex'


module.exports = AccountMergeEmailController =

	confirmMergeFromEmail: (req, res, next) ->
		token = req.query.token
		if !token
			return res.status(400).send()
		OneTimeTokenHandler.getValueFromTokenAndExpire 'account-merge-email-to-ol', token, (err, data) ->
			return next(err) if err?
			if !data
				return res.status(404).send()
			if data.origin != 'sl'
				logger.log {}, "Only sharelatex origin supported"
				# TODO: better error handling
				return res.status(501).send()
			{ sl_id, v1_id, final_email } = data
			UserGetter.getUser sl_id, {_id: 1, overleaf: 1}, (err, user) ->
				return next(err) if err?
				if !user?
					logger.err {userId: sl_id}, 'SL user not found for account-merge'
					# TODO: better error handling
					return res.status(400).send()
				if user?.overleaf?.id?
					logger.err {userId: sl_id}, 'SL user is already linked to overleaf'
					return res.status(400).send()
				logger.log {sl_id, v1_id, final_email, origin: data.origin},
					"[AccountMergeEmailController] about to merge sharelatex and overleaf-v1 accounts"
				UserUpdater.updateUser sl_id, {
					'$set': {
						'overleaf.id': v1_id,
						email: final_email
					},
					'$push': {
						emails: {
							email: final_email, createdAt: new Date()
						}
					}
				}, (err) ->
					return next(err) if err?
					res.render 'account_merge/finish', {
						finalEmail: final_email,
						continueUrl: "#{Settings.accountMerge.sharelatexHost}/user/login_to_ol_v2"
					}
{'fraction_non_alphanumeric': 0.12653419053185272, 'fraction_numerical': 0.012857977790765635, 'mean_word_length': 3.4396887159533076, 'pattern_counts': {'":': 0, '<': 3, '<?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': '6964234', 'n_tokens_mistral': 1368, 'n_tokens_neox': 1109, 'n_words': 285}