From 3aa51166bdd914f241693dc2e56189a7e4467df6 Mon Sep 17 00:00:00 2001 From: "guptamanas149@gmail.com" Date: Mon, 19 Jun 2023 18:06:31 +0530 Subject: [PATCH] changes in prisma model and project controllers --- controllers/form.controller.js | 11 ++- controllers/project.controller.js | 127 ++++++++++++++++-------------- prisma/schema.prisma | 2 - 3 files changed, 80 insertions(+), 60 deletions(-) diff --git a/controllers/form.controller.js b/controllers/form.controller.js index 9b295e0..e00ce4e 100644 --- a/controllers/form.controller.js +++ b/controllers/form.controller.js @@ -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, @@ -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, }, diff --git a/controllers/project.controller.js b/controllers/project.controller.js index d226f93..4794c5e 100644 --- a/controllers/project.controller.js +++ b/controllers/project.controller.js @@ -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; @@ -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); @@ -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); - } -} \ No newline at end of file + // 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); + } +} diff --git a/prisma/schema.prisma b/prisma/schema.prisma index ab176d0..19c42e5 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -16,6 +16,4 @@ model FormSubmission { data Json formId String createdAt DateTime @default(now()) - updatedAt DateTime @updatedAt - deletedAt DateTime? }