Skip to content
This repository has been archived by the owner on Dec 10, 2023. It is now read-only.

Commit

Permalink
Merge pull request #143 from chandra1899/issue_142
Browse files Browse the repository at this point in the history
Issue 142
  • Loading branch information
BuddyLongLegs committed Jun 18, 2023
2 parents 8307088 + 3ef5e6e commit c5a6388
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 74 deletions.
123 changes: 55 additions & 68 deletions controllers/project.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
2 changes: 1 addition & 1 deletion templates/collaborator.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@
<h5 class="card-title">Invitation to project {projectName}</h5>
<p class="card-text">
You have been invited to the project{projectName} by
<a href="mailto://{userEmail}" style="color: #006dfb">{userName}</a>.
<a href="mailto://{userEmail}" style="color: #006dfb">{userEmail}</a>.
Kindly click on the following link !
</p>
<a href="{{url}}" class="c ard-link">
Expand Down
7 changes: 2 additions & 5 deletions utils/mailer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -88,8 +86,7 @@ export function sendCollabInvitationLink(
context: {
url: link,
projectName,
userName,
userEmail,
userEmail:email
},
};
transporter.sendMail(mailOptions, (err, info) => {
Expand Down

0 comments on commit c5a6388

Please sign in to comment.