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

Commit

Permalink
Merge branch 'main' into issue_142
Browse files Browse the repository at this point in the history
  • Loading branch information
BuddyLongLegs committed Jun 18, 2023
2 parents ae249b6 + 8307088 commit 3ef5e6e
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 36 deletions.
3 changes: 3 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ app.use('/project', projectRoutes);
import userRoutes from './routes/user.routes.js';
app.use('/user', userRoutes);

import formSubmissionRoutes from './routes/formSubmission.routes.js';
app.use('/main', formSubmissionRoutes);

app.listen(
process.env.PORT ? process.env.PORT : 8080,
process.env.HOST ? process.env.HOST : '127.0.0.1',
Expand Down
9 changes: 5 additions & 4 deletions controllers/form.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export async function createForm(req, res) {
return response_400(res, 'Name cannot be an empty string');
const projectId = req.params.projectId;
const project = await Project.findOne({ projectId });
console.log(project);
if (!project) return response_400(res, 'No project found with this id');

//Mongoose object id cannot be equated directly so i converted them into string and checked that.
Expand All @@ -109,18 +110,17 @@ export async function createForm(req, res) {
submisssionLinkGeneratedAt,
}),
);
let url = `${hostUrl}/main/submit/?formRef=${encryptedStr}`;
let url = `${hostUrl}/main/submit?formRef=${encryptedStr}`;
let newForm = await Form.create({
name: req.body.name,
project: project._id,
schema: req.body.schema,
hasFileField: req.body.hasFileField,
hasRecaptchaVerification: req.body.hasRecaptcha,
submissions: [],
formId: generateRandomString(16),
formId: formId,
submisssionLinkGeneratedAt,
});

console.log(newForm);
Project.findByIdAndUpdate(
project._id,
{ forms: [...project.forms, newForm._id] },
Expand All @@ -136,6 +136,7 @@ export async function createForm(req, res) {
submisssionUrl: url,
});
} catch (error) {
console.log(error);
return response_500(res, 'Server Error', error);
}
}
Expand Down
2 changes: 1 addition & 1 deletion controllers/formSubmission.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
import Form from '../models/form.model.js';
export async function createFormSubmission(req, res) {
try {
const encryptedStr = req.params.encryptedStr;
const encryptedStr = req.query.formRef;
const decryptedStr = await dcryptString(encryptedStr);
const { formId, submisssionLinkGeneratedAt } = JSON.parse(decryptedStr);
const form = await Form.findOne({ formId: formId });
Expand Down
32 changes: 19 additions & 13 deletions controllers/project.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ export async function createProject(req, res) {
if (!req.body.name) return response_400(res, 'Project name is required');
const user = await User.findById(req.user._id);
if (user.projects.length >= 5) {
return response_400(res, 'Maximum number of 5 projects reached for this user.');
return response_400(
res,
'Maximum number of 5 projects reached for this user.',
);
}
const newProject = Project({
name: req.body.name,
Expand All @@ -39,13 +42,11 @@ export async function createProject(req, res) {
console.log(newProject);
//number of origins of project greater than 3 not allowed
if (newProject.allowedOrigins.length > 3) {
return response_400(res, 'Number of allowed origins cannot be greater than 3');
}
//number of collaborators of the project greater than 5 not allowed
if (req.body.collaborators.length > 5) {
return response_400(res, 'Number of collaborators cannot be greater than 5');
return response_400(
res,
'Number of allowed origins cannot be greater than 3',
);
}

try {
await newProject.save();
req.user.projects.push(newProject._id);
Expand Down Expand Up @@ -115,14 +116,20 @@ export async function updateProject(req, res) {
}
if (req.body.allowedOrigins) {
if (req.body.allowedOrigins.length > 3) {
return response_400(res, 'Number of allowed origins cannot be greater than 3');
return response_400(
res,
'Number of allowed origins cannot be greater than 3',
);
}
updatedProject.allowedOrigins = req.body.allowedOrigins;
}

if (req.body.collaborators) {
if (req.body.collaborators.length > 5) {
return response_400(res, 'Number of collaborators cannot be greater than 5');
return response_400(
res,
'Number of collaborators cannot be greater than 5',
);
}
inviteCollaborators(
req.body.collaborators,
Expand All @@ -133,7 +140,6 @@ export async function updateProject(req, res) {
);
}


// updating the project details in DB
const finalProject = await Project.findOneAndUpdate(
{ projectId: projectId },
Expand All @@ -160,7 +166,7 @@ export async function projectDashboard(req, res) {

try {
project = await Project.findOne({ projectId: req.params.id })
.populate('forms', 'formId name submissions createdAt updatedAt -_id')
.populate('forms', 'formId name submission createdAt updatedAt -_id')
.populate('owner', 'name email')
.populate('collaborators', 'name email -_id')
.select('-_id -createdAt -updatedAt -__v');
Expand All @@ -173,7 +179,7 @@ export async function projectDashboard(req, res) {
delete project.hasRecaptcha;
project.form_count = project?.forms?.length;
project.forms.forEach((form) => {
form.submission_count = form?.submissions?.length;
form.submission_count = form?.submission?.length;
form.last_updated = form.updatedAt;
form.date_created = form.createdAt;
form.id = form.formId;
Expand Down
16 changes: 5 additions & 11 deletions controllers/user.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,11 @@ export function getVerificationLink(req, res) {
}

export async function updateUser(req, res) {
if (!(req.body.name && req.body.email && req.body.password))
return response_400(res, 'All required fields not present');
if (req.user.passwordHash !== '') {
try {
if (!(req.body.name && req.body.email))
return response_400(res, 'All required fields not present');
if (!verifycaptcha(req.body.recaptcha_token))
return response_400(res, 'Captcha not verified');
const password = await hash_password(req.body.password);
if (password !== req.user.passwordHash)
return response_400(res, 'Wrong Password');
if (req.body.email !== req.user.email) {
if (!validator.isEmail(req.body.email))
return response_400(res, 'Invalid email id');
Expand All @@ -42,11 +39,8 @@ export async function updateUser(req, res) {
name: updatedUser.name,
email: updatedUser.email,
});
} else {
return response_400(
res,
'You will need to create a password before changing these settings',
);
} catch (err) {
return response_400(res, 'Error updating user info');
}
}

Expand Down
13 changes: 8 additions & 5 deletions models/form.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,14 @@ const formSchema = new Schema(
// ref: 'formSubmission',
// },
// ],
submission: [
{
type: String,
},
],
submission: {
type: [
{
type: String,
},
],
default: void 0,
},
hasRecaptchaVerification: {
type: Boolean,
default: false,
Expand Down
2 changes: 0 additions & 2 deletions routes/form.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
updateForm,
getForm,
} from '../controllers/form.controller.js';
import { createFormSubmission } from '../controllers/formSubmission.controller.js';
const router = Router();

// All routes configured here
Expand All @@ -17,5 +16,4 @@ router.post('/new/:projectId', verifiedMiddleware, createForm);
router.patch('/update/:id', verifiedMiddleware, updateForm);
router.delete('/', verifiedMiddleware, deleteForm);
router.get('/:formId', getForm);
router.post('/main/submit/:encryptedStr', createFormSubmission);
export default router;
7 changes: 7 additions & 0 deletions routes/formSubmission.routes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { greet } from '../controllers/auth.controller.js';
import verifiedMiddleware from '../middlewares/verify.middleware.js';
import { Router } from 'express';
import { createFormSubmission } from '../controllers/formSubmission.controller.js';
const router = Router();
router.post('/submit', verifiedMiddleware, createFormSubmission);
export default router;

0 comments on commit 3ef5e6e

Please sign in to comment.