diff --git a/controllers/project.controller.js b/controllers/project.controller.js index e59488c..d226f93 100644 --- a/controllers/project.controller.js +++ b/controllers/project.controller.js @@ -195,72 +195,59 @@ export async function projectDashboard(req, res) { } export async function updateCollaborator(req, res) { - try { - let project = await Project.findById(req.params.projectId).populate( - 'owner', - 'name email', - ); - let is_owner = String(project.owner._id) === String(req.user._id); - if (!is_owner) { - response_401('The user is not the owner of project.'); - } else { - //storing emails we got in an array - const emails = req.body.collaborators; + try { + let project=await Project.findOne({ projectId: req.params.projectId }).populate('owner', 'name email') + let is_owner = String(project.owner._id) === String(req.user._id); + if(!is_owner){ + response_401('The user is not the owner of project.'); + }else{ + //storing emails we got in an array + const emails=req.body.collaborators; + + // finding all collaborators with projectId + const projectCollaborators=await Collaborators.find({projectId:req.params.projectId}); + + //store emails of every collaborator in an array + const collaboratorsEmails=await projectCollaborators.map((projectCollaborator)=>(projectCollaborator.email)) + + //Array of promises to send invitation mails + let sendMailsPromise; + + //iterating on every email we got + emails.forEach(async (email)=>{ + //check this email is present in array of collaborators list of this project + const isPresent= collaboratorsEmails.includes(email); + if(!isPresent){ + // creating collaborator in db + let newcollaborator=await Collaborators.create({ + email:email, + projectId:req.params.projectId, + status:'Invited' + }); + + //Invite new Collaborator with this email + sendMailsPromise.push(sendCollabInvitationLink(newcollaborator.email,newcollaborator._id,project.name)); + + } + }) + + //send mails + await Promise.all(sendMailsPromise); + + //checking if any removed collaborators and deleting + collaboratorsEmails.forEach(async (collaboratorEmail)=>{ + const isPresent= emails.includes(collaboratorEmail); + if(!isPresent){ + await Collaborators.findOneAndDelete({ + email:collaboratorEmail, + projectId:req.params.projectId + }) + } + }) - // finding all collaborators with projectId - const projectCollaborators = await Collaborators.find({ - projectId: req.params.projectId, - }); - - //store emails of every collaborator in an array - const collaboratorsEmails = await projectCollaborators.map( - (projectCollaborator) => projectCollaborator.email, - ); - - //iterating on every email we got - emails.forEach(async (email) => { - //check this email is present in array of collaborators list of this project - const isPresent = collaboratorsEmails.includes(email); - if (!isPresent) { - //Invite new Collaborator with this email - - // creating collaborator in db - await Collaborators.create({ - email: email, - projectId: req.params.projectId, - status: 'Invited', - }); - } - }); - - //checking if any removed collaborators and deleting - collaboratorsEmails.forEach(async (collaboratorEmail) => { - const isPresent = emails.includes(collaboratorEmail); - if (!isPresent) { - await Collaborators.findOneAndDelete({ - email: collaboratorEmail, - projectId: req.params.projectId, - }); - } - }); - } - } catch (error) { - console.log(error); - return response_500(res, 'Server error', error); - } -} - -function inviteCollaborators( - email, - projectId, - projectName, - userName, - userEmail, -) { - const obj = { - projectId, - collaborators: email.join(';'), - }; - const secret = getJwt(obj); - sendCollabInvitationLink(email, secret, projectName, userName, userEmail); -} + } + } catch (error) { + console.log(error); + return response_500(res, 'Server error', error); + } +} \ No newline at end of file diff --git a/templates/collaborator.handlebars b/templates/collaborator.handlebars index bab0c6a..260bf90 100644 --- a/templates/collaborator.handlebars +++ b/templates/collaborator.handlebars @@ -151,7 +151,7 @@
Invitation to project {projectName}

You have been invited to the project{projectName} by - {userName}. + {userEmail}. Kindly click on the following link !

diff --git a/utils/mailer.js b/utils/mailer.js index 3df37d5..2c542ae 100644 --- a/utils/mailer.js +++ b/utils/mailer.js @@ -67,9 +67,7 @@ export function sendVerificationLink(email, secret, ip) { export function sendCollabInvitationLink( email, secret, - projectName, - userName, - userEmail, + projectName ) { let link = BASE_URL + '/collab/' + secret; const handlebarOptions = { @@ -88,8 +86,7 @@ export function sendCollabInvitationLink( context: { url: link, projectName, - userName, - userEmail, + userEmail:email }, }; transporter.sendMail(mailOptions, (err, info) => {