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

Commit

Permalink
changes in prisma model and project controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
Manas2403 committed Jun 19, 2023
1 parent e8b97cd commit 3aa5116
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 60 deletions.
11 changes: 10 additions & 1 deletion controllers/form.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ export async function createForm(req, res) {
export async function getForm(req, res) {
try {
const { formId } = req.params;
const form = await Form.aggregate([
let form = await Form.aggregate([
{
$match: {
formId: formId,
Expand Down Expand Up @@ -193,8 +193,17 @@ export async function getForm(req, res) {
},
},
]);
form = form[0];
if (!form) return response_400(res, 'Form not found');
const formSubmissions = await prisma.formSubmission.findMany({
select: {
id: true,
data: true,
createdAt: true,
},
orderBy: {
createdAt: 'desc',
},
where: {
formId: formId,
},
Expand Down
127 changes: 70 additions & 57 deletions controllers/project.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,10 @@ export async function projectDashboard(req, res) {
project = project.toJSON();
project.is_owner = String(project.owner._id) === String(req.user._id);
delete project.owner._id;
project.allowRecaptcha = project.hasRecaptcha;
project.hasRecaptcha = project.allowRecaptcha;
delete project.allowRecaptcha;
project.id = project.projectId;
delete project.projectId;
delete project.hasRecaptcha;
project.form_count = project?.forms?.length;
project.forms.forEach((form) => {
form.submission_count = form?.submission?.length;
Expand All @@ -187,6 +187,9 @@ export async function projectDashboard(req, res) {
delete form.updatedAt;
delete form.createdAt;
});
project.forms.sort((a, b) => {
return b.date_created - a.date_created;
});
return response_200(res, 'Project Dashboard', project);
} catch (error) {
console.log(error);
Expand All @@ -195,59 +198,69 @@ export async function projectDashboard(req, res) {
}

export async function updateCollaborator(req, res) {
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
})
}
})
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;

}
} catch (error) {
console.log(error);
return response_500(res, 'Server error', error);
}
}
// 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,
});
}
});
}
} catch (error) {
console.log(error);
return response_500(res, 'Server error', error);
}
}
2 changes: 0 additions & 2 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,4 @@ model FormSubmission {
data Json
formId String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
deletedAt DateTime?
}

0 comments on commit 3aa5116

Please sign in to comment.