Skip to content

Commit

Permalink
add handle updateContact also in /verify/email
Browse files Browse the repository at this point in the history
  • Loading branch information
serdiukov-o-nordwhale committed Oct 19, 2020
1 parent d454d38 commit f925224
Showing 1 changed file with 22 additions and 14 deletions.
36 changes: 22 additions & 14 deletions src/server/verification/verificationAPI.js
Expand Up @@ -519,38 +519,46 @@ const setup = (app: Router, verifier: VerificationAPI, gunPublic: StorageAPI, st
const log = req.log
const { user, body } = req
const verificationData: { code: string } = body.verificationData
const tempSavedEmail = user.otp && user.otp.email
const hashedNewEmail = sha3(tempSavedEmail)
const tempSavedMauticId = user.otp && user.otp.tempMauticId
const { email, tempMauticId: tempSavedMauticId } = user.otp || {}
const hashedNewEmail = sha3(email)
const currentEmail = user.email

log.debug('email verified', { user, body, verificationData, tempSavedMauticId, tempSavedEmail, currentEmail })
log.debug('email verified', {
user,
body,
verificationData,
tempSavedMauticId,
tempSavedEmail: email,
currentEmail
})

if (!user.isEmailConfirmed || currentEmail !== hashedNewEmail) {
if (runInEnv && conf.skipEmailVerification === false)
const { mauticId } = user

if (runInEnv && conf.skipEmailVerification === false) {
await verifier.verifyEmail({ identifier: user.loggedInAs }, verificationData)
}

const updateUserUbj = {
identifier: user.loggedInAs,
isEmailConfirmed: true,
email: hashedNewEmail
}

if (runInEnv && user.mauticId) {
await Promise.all([
Mautic.deleteContact({
mauticId: tempSavedMauticId
}),
Mautic.updateContact(user.mauticId, { email: tempSavedEmail })
])
if (runInEnv && mauticId) {
const updMauticPromise = Mautic.updateContact(mauticId, { email }).catch(e =>
log.error('Error updating Mautic contact', e.message, e, { mauticId, email })
)

await Promise.all([Mautic.deleteContact({ mauticId: tempSavedMauticId }), updMauticPromise])
} else {
updateUserUbj.mauticId = tempSavedMauticId
}

//update indexes, if new user, indexes are set in /adduser
if (currentEmail && currentEmail !== tempSavedEmail) {
if (currentEmail && currentEmail !== email) {
gunPublic.removeUserFromIndex('email', currentEmail)
gunPublic.addUserToIndex('email', tempSavedEmail, user)
gunPublic.addUserToIndex('email', email, user)
}

const [, , signedEmail] = await Promise.all([
Expand Down

0 comments on commit f925224

Please sign in to comment.