Skip to content

Commit 6febf8b

Browse files
Fixing Multipage Error & Adding Completion Stamps (#43)
* Initial Fix * Fixed multipage surveys * update readme * test csv * Corrected numbering The curr page is 1 forward. * package-lock.json will cause install errors. Just keep package.json and then it will be auto generated * Setting response insertion to have right type * Adds completion stamp, queries supported in encrypted url Co-authored-by: Sumant R Shringari <[email protected]>
1 parent c08ae45 commit 6febf8b

File tree

11 files changed

+224
-392
lines changed

11 files changed

+224
-392
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,7 @@ dist
106106
# Application outputs
107107
.data
108108
built
109-
*.pem
109+
*.pem
110+
111+
# package lock
112+
package-lock.json

README.md

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,19 @@ Surveyor generates data driven surveys for online participants. It imports surve
33

44
## Development
55
Create an `.env` file in the root directory and add:
6-
7-
For local mongo development
6+
Do not use any of the following config variables with KEY in production.
87
```PowerShell
98
PORT=4000
10-
PROD=False
11-
MONGO=True
12-
TEST_URI=mongodb://localhost:27017/
9+
PROD=FALSE
10+
URI=mongodb://localhost:27017
1311
TEST_DB=testDB
14-
PROD_URI=nothing
15-
PROD_DB=nothing
16-
```
17-
18-
For local nedb development
19-
```PowerShell
20-
PORT=4000
21-
PROD=False
22-
MONGO=False
23-
TEST_URI=nothing
24-
TEST_DB=nothing
25-
PROD_URI=nothing
26-
PROD_DB=nothing
12+
PROD_DB=prodDB
13+
RANDOM=TESTISRANDOM
14+
TOKEN_KEY=TESTISRANDOM
15+
SECRET_KEY=TESTISRANDOM
16+
ENCRYPT_KEY=TESTISRANDOM
17+
IV_KEY=a0f5c2b327abb291c7cecdea1b2f8cc5
18+
DOMAIN=http://localhost:4000/
2719
```
2820

2921
Run `npm install` to install all dependencies. Run `npm test` to launch continuous Typescript compilation, watching for changes in any of the `ts` files. Leave this shell alone and, in a new one, run `npm start` to launch the application on the port in `.env`. With this setting it would start a server at [`localhost:4000`](http://localhost:4000).

config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import exp = require("constants");
77

88
const env_config = {
99
PORT: parseInt(process.env.PORT),
10-
URI: process.env.PROD.toLowerCase() == "true" ? process.env.PROD_URI : process.env.TEST_URI,
10+
URI: process.env.URI,
1111
DB: process.env.PROD.toLowerCase() == "true" ? process.env.PROD_DB : process.env.TEST_DB,
1212
RANDOM: process.env.RANDOM,
1313
TOKEN_KEY: process.env.TOKEN_KEY,

util.ts renamed to helpers/encrypt_utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import crypto = require("crypto")
2-
import {Db_Wrapper, env_config} from "./config"
2+
import {Db_Wrapper, env_config} from "../config"
33

44
const private_key = crypto.scryptSync(env_config.ENCRYPT_KEY, "salt", 32)
55
const iv = Buffer.from(env_config.IV_KEY, 'hex')

helpers/survey_helpers.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { response } from "express";
2+
import { Request, Response } from "express-serve-static-core";
3+
import {Db_Wrapper, env_config} from "../config"
4+
5+
export const setPageNums = (survey) => {
6+
var pagefinal = 0
7+
8+
survey.forEach((elem) => {
9+
elem["page"] = Number(elem["page"])
10+
pagefinal = Math.max(Number(elem["page"]), pagefinal)
11+
})
12+
13+
return pagefinal
14+
}
15+
16+
export const setSurveyResponse = (req: Request<{}>) => {
17+
let response = {...req.body}
18+
if (response.start_time) {
19+
response["start_time"] = new Date(response["start_time"])
20+
}
21+
22+
if (response.sent) {
23+
response["sent"] = new Date(response["sent"])
24+
}
25+
26+
response["raw_data"] = "Surveyor_Incomplete"
27+
28+
delete response["_csrf"]
29+
delete response["start"]
30+
delete response["check"]
31+
delete response["final"]
32+
delete response["curr_page"]
33+
return response
34+
}
35+
36+
export const setSurveyCompleted = () => {
37+
const completion_stamp = {}
38+
completion_stamp["paid"] = false
39+
completion_stamp["end_time"] = new Date()
40+
completion_stamp["raw_data"] = "Surveyor"
41+
42+
return completion_stamp
43+
}
44+
45+
export const isSurveyCompleted = async (parsed) => {
46+
47+
let queries: any[] = await Db_Wrapper.find({
48+
"url": parsed.url,
49+
"WorkerId": parsed.WorkerId,
50+
"sent": new Date(parsed.sent),
51+
"paid": {"$exists": 1}
52+
}, "responses")
53+
54+
return !(queries.length == 0)
55+
}
56+

package-lock.json

Lines changed: 19 additions & 64 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

routes/encrypt.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const express = require('express')
22
const bycrpyt = require('bcryptjs')
33
const jwt = require('jsonwebtoken')
44
import {Db_Wrapper, env_config} from "../config"
5-
import { encrypt, decrypt } from "../util"
5+
import { encrypt, decrypt } from "../helpers/encrypt_utils"
66
import { verifyAdminToken } from "../middlewares/auth.middleware"
77
const router = express.Router()
88

0 commit comments

Comments
 (0)