Skip to content

Commit

Permalink
Merge pull request #30 from fga-eps-mds/bugfix-97-interface
Browse files Browse the repository at this point in the history
BUGFIX - Interface #97
  • Loading branch information
GeovanaRamos authored Nov 20, 2020
2 parents 36082d0 + 7bb4e07 commit e3516ef
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 11 deletions.
25 changes: 19 additions & 6 deletions server/controllers/LearnerController.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ module.exports = {
await learner.save();
if (learner.mentor) throw new Error('Learner already has a mentor');
let mentor = (await Mentor.aggregate()
.match({ isAvailable: true, isValidated: true })
.match({ isAvailable: true, isValidated: true, _id: { $nin: learner.noAssociations } })
.group({
_id: '$_id',
size: { $max: '$learners' },
Expand Down Expand Up @@ -77,10 +77,12 @@ module.exports = {
if (!learner.mentor) throw new Error('Learner does not have a mentor');
await Mentor.findByIdAndUpdate(learner.mentor, {
$pull: { learners: learner._id },
$push: { noAssociations: learner._id },
isAvailable: false,
});
const oldMentor = learner.mentor;
const mentorMail = await Mentor.findById(learner.mentor);
learner.noAssociations.push(learner.mentor);
learner.mentor = null;
await learner.save();
const data = mail.unassignMentor(req.user.email, req.user.name, mentorMail.name, mentorMail.gender); // eslint-disable-line max-len
Expand All @@ -101,13 +103,24 @@ module.exports = {
const hasLearnerCertificate = reqUser.courseCertificates.length > 0;

if (!hasLearnerCertificate) throw new Error('User did not conclude Tutorial');

const user = await User.findOneAndUpdate(
{ _id },
{ $set: { userType: 'Mentor' } }, { new: true },
if (reqUser.mentor) {
await User.findOneAndUpdate({ _id }, {
$pull: { learners: reqUser._id },
$push: { noAssociations: reqUser._id },
isAvailable: false,
});
reqUser.noAssociations.push(reqUser.mentor);
reqUser.mentor = null;
reqUser.save();
}
const user = await User.findByIdAndUpdate(
_id,
{
$set: { userType: 'Mentor' },
mentor_request: false,
}, { new: true },
);
user.isValidated = true;
user.mentor_request = false;
user.save();
const data = mail.learnerPromotion(user.email, user.name);
await transport.sendMail(data);
Expand Down
14 changes: 12 additions & 2 deletions server/controllers/MentorController.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,21 @@ module.exports = {

async assignLearner(req, res) {
const { user } = req;
if (!user.isValidated) throw new Error('Mentor not validated');
user.isAvailable = true;
try {
await user.save();
const learner = (
await Learner.find({ mentor_request: true, mentor: null }).sort({
createdAt: 'asc',
await Learner.find({
mentor_request: true,
mentor: null,
_id: {
$nin: user.noAssociations,
},
})
.sort({
createdAt: 'asc',
})
)[0];
if (!learner) throw new Error("There's no available learners");
learner.mentor = user._id;
Expand Down Expand Up @@ -66,8 +74,10 @@ module.exports = {
const learner = await Learner.findByIdAndUpdate(learnerID, {
mentor: null,
mentor_request: false,
$push: { noAssociations: user._id },
},
{ new: true });
user.noAssociations.push(learnerID);
await user.save();
await user.execPopulate('learners');
const data = mail.unassignMentor(learner.email, learner.name, user.name, user.gender);
Expand Down
7 changes: 4 additions & 3 deletions server/models/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,11 @@ const UserSchema = new Schema(
type: Boolean,
select: false,
},
noAssociations: {
type: [String],
noAssociations: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'blacklistedUser',
select: false,
},
}],
courseCertificates: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'courseCertificate',
Expand Down

0 comments on commit e3516ef

Please sign in to comment.