Merge pull request #2219 from overleaf/jel-institution-email-already-exists

Check for email before adding institution identifier

GitOrigin-RevId: 71f498116c8c8df2c3c1a33eafce3e56c87e7ee0
This commit is contained in:
Eric Mc Sween
2019-10-14 13:37:50 +00:00
committed by sharelatex
parent ed7b5a9450
commit 526d4982a1
2 changed files with 48 additions and 3 deletions
@@ -6,7 +6,19 @@ const { User } = require('../../models/User')
const UserGetter = require('../User/UserGetter')
const UserUpdater = require('../User/UserUpdater')
function _addIdentifier(userId, externalUserId, providerId, hasEntitlement) {
async function _addIdentifier(
userId,
externalUserId,
providerId,
hasEntitlement,
institutionEmail
) {
// first check if institutionEmail linked to another account
// before adding the identifier for the email
const user = await UserGetter.promises.getUserByAnyEmail(institutionEmail)
if (user && user._id.toString() !== userId.toString()) {
throw new Errors.EmailExistsError()
}
providerId = providerId.toString()
hasEntitlement = !!hasEntitlement
const query = {
@@ -137,7 +149,13 @@ async function linkAccounts(
providerName,
hasEntitlement
) {
await _addIdentifier(userId, externalUserId, providerId, hasEntitlement)
await _addIdentifier(
userId,
externalUserId,
providerId,
hasEntitlement,
institutionEmail
)
await _addInstitutionEmail(userId, institutionEmail, providerId)
await _sendLinkedEmail(userId, providerName)
}