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

Commit

Permalink
updated form model
Browse files Browse the repository at this point in the history
  • Loading branch information
Manas2403 committed Jun 17, 2023
1 parent 152df38 commit 3a12c21
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 12 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
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 3a12c21

Please sign in to comment.