Skip to content

Commit

Permalink
(Bug) #662 User don't get the verification email during wallet creati…
Browse files Browse the repository at this point in the history
…on (#98)

* #662 User don't get the verification email during wallet creation

* add test and  prevent duplicate creation of mauticId

* fixed for comments

* Revision of all get methods in the user model
  • Loading branch information
StanislavShevchenko authored and sirpy committed Oct 16, 2019
1 parent bb20981 commit b6be611
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 17 deletions.
8 changes: 4 additions & 4 deletions src/server/db/mongo/user-privat-provider.js
Expand Up @@ -95,7 +95,7 @@ class UserPrivate {
* @returns {object || null}
*/
async getByIdentifier(identifier) {
return await this.model.findOne({ identifier })
return await this.model.findOne({ identifier }).lean()
}

/**
Expand Down Expand Up @@ -123,7 +123,7 @@ class UserPrivate {
* @returns {Promise<*>}
*/
async getUserByEmail(email: string): Promise<UserRecord> {
return await this.model.findOne({ email })
return await this.model.findOne({ email }).lean()
}

/**
Expand All @@ -134,7 +134,7 @@ class UserPrivate {
* @returns {Promise<*>}
*/
async getUserByMobile(mobile: string): Promise<UserRecord> {
return await this.model.findOne({ mobile })
return await this.model.findOne({ mobile }).lean()
}

/**
Expand All @@ -143,7 +143,7 @@ class UserPrivate {
* @returns {Promise<*>}
*/
async listUsers(): Promise<UserRecord> {
return await this.model.find()
return await this.model.find().lean()
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/server/login/login-middleware.js
Expand Up @@ -10,7 +10,7 @@ import { wrapAsync, lightLogs } from '../utils/helpers'
import UserDBPrivate from '../db/mongo/user-privat-provider'
import SEA from 'gun/sea'
import Config from '../server.config.js'
import { recoverPublickey } from "../utils/eth";
import { recoverPublickey } from '../utils/eth'
// const ExtractJwt = passportJWT.ExtractJwt
// const JwtStrategy = passportJWT.Strategy

Expand Down
22 changes: 22 additions & 0 deletions src/server/storage/__tests__/storageAPI.js
Expand Up @@ -3,6 +3,7 @@ import request from 'supertest'
import makeServer from '../../server-test'
import { getToken } from '../../__util__/'
import type { UserRecord } from '../../../imports/types'
import UserDBPrivate from '../../db/mongo/user-privat-provider'

jest.setTimeout(30000)
describe('storageAPI', () => {
Expand Down Expand Up @@ -31,6 +32,27 @@ describe('storageAPI', () => {
expect(res).toMatchObject({ status: 200, body: { ok: 1 } })
})

test('/user/add creds dont update mauticId', async () => {
const token = await getToken(server)
const mauticId = '111'
await UserDBPrivate.updateUser({ identifier: '0x7ac080f6607405705aed79675789701a48c76f55', mauticId: mauticId })
const user: UserRecord = {
identifier: '0x7ac080f6607405705aed79675789701a48c76f55',
email: 'useraddtest@gooddollar.org' // required for mautic create contact
}
let res = await request(server)
.post('/user/add')
.set('Authorization', `Bearer ${token}`)
.send({ user })
expect(res).toMatchObject({ status: 200, body: { ok: 1 } })

const mauticIdAfterAddUser = await UserDBPrivate.getUserField(
'0x7ac080f6607405705aed79675789701a48c76f55',
'mauticId'
)
expect(mauticIdAfterAddUser === mauticId).toBeTruthy()
})

test('/user/add false creds', async () => {
const token = await getToken(server)
const user: UserRecord = { identifier: '0x7ac080f6607405705aed79675789701a48c76f56' }
Expand Down
18 changes: 11 additions & 7 deletions src/server/storage/storageAPI.js
Expand Up @@ -49,12 +49,16 @@ const setup = (app: Router, storage: StorageAPI) => {
AdminWallet.whitelistUser(userRecord.gdAddress, userRecord.profilePublickey)
}

const mauticRecordPromise =
process.env.NODE_ENV === 'development'
? Promise.resolve({})
: Mautic.createContact(user).catch(e => {
log.error('Create Mautic Record Failed', e)
})
let mauticRecordPromise = Promise.resolve({})

if (!userRecord.mauticId) {
mauticRecordPromise =
process.env.NODE_ENV === 'development'
? Promise.resolve({})
: Mautic.createContact(user).catch(e => {
log.error('Create Mautic Record Failed', e)
})
}

const secureHash = md5(user.email + conf.secure_key)

Expand Down Expand Up @@ -82,7 +86,7 @@ const setup = (app: Router, storage: StorageAPI) => {
log.debug('Web3 user record', web3Record)

//mautic contact should already exists since it is first created during the email verification we update it here
const mauticId = get(mauticRecord, 'contact.fields.all.id', -1)
const mauticId = !userRecord.mauticId ? get(mauticRecord, 'contact.fields.all.id', -1) : userRecord.mauticId
logger.debug('User mautic record', { mauticId, mauticRecord })

const updateUserObj = {
Expand Down
8 changes: 3 additions & 5 deletions src/server/verification/verificationAPI.js
Expand Up @@ -283,8 +283,7 @@ const setup = (app: Router, verifier: VerificationAPI, storage: StorageAPI) => {
if (conf.allowDuplicateUserData === false && (await storage.isDupUserData(userRec))) {
return res.json({ ok: 0, error: 'Email already exists, please use a different one' })
}

if (!user.mauticId) {
if (!user.mauticId || user.email !== body.email) {
//first time create contact for user in mautic
const mauticContact = await Mautic.createContact(userRec)
userRec.mauticId = mauticContact.contact.fields.all.id
Expand All @@ -293,15 +292,14 @@ const setup = (app: Router, verifier: VerificationAPI, storage: StorageAPI) => {

if (conf.skipEmailVerification === false) {
const code = generateOTP(6)

if (!user.isEmailConfirmed || email !== savedEmail) {
Mautic.sendVerificationEmail(userRec, code)
log.debug('send new user email validation code', code)
}

// updates/adds user with the emailVerificationCode to be used for verification later and with mauticId
storage.updateUser({
identifier: user.loggedInAs,
await storage.updateUser({
identifier: user.identifier,
mauticId: userRec.mauticId,
emailVerificationCode: code,
otp: {
Expand Down

0 comments on commit b6be611

Please sign in to comment.