Skip to content

Commit

Permalink
add create mautic contact on update if it doesn't exists, make mautic…
Browse files Browse the repository at this point in the history
… update optional
  • Loading branch information
serdiukov-o-nordwhale committed Oct 15, 2020
1 parent 5c8d71d commit 3060ee7
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions src/server/verification/verificationAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,32 @@ const setup = (app: Router, verifier: VerificationAPI, gunPublic: StorageAPI, st
return false
})

if (verified === false) return
if (verified === false) {
return
}

const { mauticId, email } = user
const updateMautic = async () => {
const mobile = tempSavedMobile
const isContactExists = await Mautic.isContactExists(mauticId)

try {
if (!isContactExists) {
log.warn(`Mautic contact ${mauticId} doesnt exists for user ${email}, re-creating`, {
email,
mauticId,
mobile,
user
})

return await Mautic.createContact({ ...user, mobile })
}

return await Mautic.updateContact(user.mauticId, { mobile })
} catch (e) {
log.error(`Error updating mautic contact ${mauticId} for user ${email}`, e.message, e)
}
}

let updIndexPromise
if (currentMobile && currentMobile !== hashedNewMobile) {
Expand All @@ -297,9 +322,10 @@ const setup = (app: Router, verifier: VerificationAPI, gunPublic: StorageAPI, st
gunPublic.addUserToIndex('mobile', tempSavedMobile, user)
])
}

await Promise.all([
updIndexPromise,
user.mauticId && Mautic.updateContact(user.mauticId, { mobile: tempSavedMobile }),
mauticId && updateMautic(),
storage.updateUser({
identifier: user.loggedInAs,
smsValidated: true,
Expand Down

0 comments on commit 3060ee7

Please sign in to comment.