const models = require('vn-loopback/server/server').models; const maxTTL = ((60 * 1000) * 5); // 5 min module.exports = authCode = async(vnUser, options) => { const myOptions = {}; if (typeof options == 'object') Object.assign(myOptions, options); const min = 100000; const max = 999999; const code = String(Math.floor(Math.random() * (max - min + 1)) + min); await models.AuthCode.upsertWithWhere({userFk: vnUser.id}, { userFk: vnUser.id, code, expires: Date.vnNow() + maxTTL }, myOptions); return code; };