Skip to content

Commit

Permalink
Merge pull request #49 from codexjr-dev/iss#25
Browse files Browse the repository at this point in the history
Iss#25
  • Loading branch information
ManoMax authored Sep 7, 2023
2 parents c33c542 + ffe2b41 commit aa91554
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions src/modules/Member/MemberService.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ module.exports = {
department,
} = memberData;

await verifyEmail(email);

const psw = await bcrypt.hash(
`${password}`,
parseInt(process.env.SALT_ROUNDS)
);

const member = await Member.create({
const newMember = await Member.create({
name,
email,
role,
Expand All @@ -36,7 +38,7 @@ module.exports = {
department,
});

return getDTOmember(member);
return getDTOmember(newMember);
},

// only for test purposes
Expand Down Expand Up @@ -71,21 +73,31 @@ module.exports = {
);
data.password = psw;
}

const member = await Member.findOne({ _id: memberId });

if (member.email !== data.email) {
await verifyEmail(data.email);
}

if (
member.role !== data.role &&
!["Presidente", "Diretor(a)"].includes(data.role)
)
await checkMinimumQuantity(member);

await member.updateOne(data);

return getDTOmember(member);
const updatedMember = await member.updateOne(data);
return getDTOmember(updatedMember);
},
};

async function verifyEmail(memberEmail) {
const emailInUse = await Member.findOne({ email: memberEmail });
if (emailInUse) {
throw new Error('Já existe um membro cadastrado para esse email!');
}
}

function getDTOmember(member) {
return {
_id: member._id,
Expand Down

0 comments on commit aa91554

Please sign in to comment.