All files resendVerifySignup.js

0% Statements 0/15
100% Branches 0/0
0% Functions 0/9
0% Lines 0/15
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52                                                                                                       
 
/* eslint-env node */
 
const debug = require('debug')('verify-reset:resendVerifySignup');
 
const {
  getUserData,
  ensureObjPropsValid,
  sanitizeUserForClient,
  getLongToken,
  getShortToken,
  notifier
} = require('./helpers');
 
// {email}, {verifyToken}, {verifyShortToken},
// {email, verifyToken, verifyShortToken}
module.exports = function resendVerifySignup (options, emailOrToken, notifierOptions) {
  debug('resendVerifySignup', emailOrToken);
  const users = options.app.service(options.service);
  const usersIdName = users.id;
 
  return Promise.resolve()
    .then(() => {
      ensureObjPropsValid(emailOrToken, ['email', 'verifyToken', 'verifyShortToken']);
 
      return emailOrToken;
    })
    .then(query =>
      Promise.all([
        users.find({ query })
          .then(data => getUserData(data, ['isNotVerified'])),
        getLongToken(options.longTokenLen),
        getShortToken(options.shortTokenLen, options.shortTokenDigits)
      ])
    )
    .then(([user, longToken, shortToken]) =>
      patchUser(user, {
        isVerified: false,
        verifyExpires: Date.now() + options.delay,
        verifyToken: longToken,
        verifyShortToken: shortToken
      })
    )
    .then(user => notifier(options.notifier, 'resendVerifySignup', user, notifierOptions))
    .then(user => sanitizeUserForClient(user));
 
  function patchUser (user, patchToUser) {
    return users.patch(user[usersIdName], patchToUser, {}) // needs users from closure
      .then(() => Object.assign(user, patchToUser));
  }
};