Skip to content

Commit

Permalink
Merge pull request #24 from codexjr-dev/iss#8
Browse files Browse the repository at this point in the history
fix: verify if user email already exists
  • Loading branch information
ManoMax committed Aug 28, 2023
2 parents e4003e6 + 555cd79 commit 00ff7df
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/modules/User/UserService.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@ const { remove } = require('../Ej/Ej');
module.exports = {
async save(userData, ejId) {
const { name, email, role, birthDate, password } = userData

const user = await User.findOne({ email: userData.email });
if (user) {
throw new Error('Já existe um usuário cadastrado para esse email!');
}

// password auto generated for first access with 6 digits, mockado por enquanto
const code = 123456 || Math.floor(Math.random() * (999999 - 100000) + 100000);
const psw = await bcrypt.hash(`${password || code}`, parseInt(process.env.SALT_ROUNDS))

const user = await User.create({
const newUser = await User.create({
name: name,
email: email,
birthDate: birthDate,
Expand All @@ -20,7 +25,7 @@ module.exports = {
})

userData.senhaGerada = password || code
userData._id = user._id
userData._id = newUser._id
return userData;
},

Expand Down

0 comments on commit 00ff7df

Please sign in to comment.