All files resendVerifySignup.js

100% Statements 15/15
100% Branches 0/0
100% Functions 9/9
100% Lines 15/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 53 54      1x                         1x 76x 76x 76x   76x   76x       68x   68x     68x         220x               44x 44x     44x 44x      
 
/* eslint-env node */
 
const debug = require('debug')('authManagement:resendVerifySignup');
 
const {
  getUserData,
  ensureObjPropsValid,
  sanitizeUserForClient,
  getLongToken,
  getShortToken,
  notifier
} = require('./helpers');
 
// {email}, {cellphone}, {verifyToken}, {verifyShortToken},
// {email, cellphone, verifyToken, verifyShortToken}
module.exports = function resendVerifySignup (options, identifyUser, notifierOptions) {
  debug('resendVerifySignup', identifyUser);
  const users = options.app.service(options.service);
  const usersIdName = users.id;
 
  return Promise.resolve()
    .then(() => {
      ensureObjPropsValid(identifyUser,
        options.identifyUserProps.concat('verifyToken', 'verifyShortToken')
      );
 
      return identifyUser;
    })
    .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));
  }
};