| Total Complexity | 9 |
| Complexity/F | 1.5 |
| Lines of Code | 55 |
| Function Count | 6 |
| Duplicated Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
| 1 | var express = require('express'); |
||
| 2 | var router = express.Router(); |
||
| 3 | const sqlite3 = require('sqlite3').verbose(); |
||
|
|
|||
| 4 | // const db = new sqlite3.Database('./db/texts.sqlite'); |
||
| 5 | const db = require("../db/database.js"); |
||
| 6 | const jwt = require('jsonwebtoken'); |
||
| 7 | |||
| 8 | let config; |
||
| 9 | |||
| 10 | try { |
||
| 11 | config = require('../config.js'); |
||
| 12 | } catch (error) { |
||
| 13 | console.error(error); |
||
| 14 | } |
||
| 15 | |||
| 16 | const jwtSecret = process.env.JWT_SECRET || config.secret; |
||
| 17 | |||
| 18 | router.post("/", |
||
| 19 | (req, res, next) => checkToken(req, res, next), |
||
| 20 | (req, res) => reportedit(res, req.body)); |
||
| 21 | |||
| 22 | function checkToken(req, res, next) { |
||
| 23 | const token = req.headers['x-access-token']; |
||
| 24 | |||
| 25 | jwt.verify(token, jwtSecret, function(err, decoded) { |
||
| 26 | if (err) { |
||
| 27 | return res.status(401).json({ |
||
| 28 | data: { |
||
| 29 | message: "No valid token." |
||
| 30 | } |
||
| 31 | }); |
||
| 32 | } |
||
| 33 | // Valid token send on the request |
||
| 34 | next(); |
||
| 35 | }); |
||
| 36 | } |
||
| 37 | |||
| 38 | function reportedit(res, body) { |
||
| 39 | const week = body.week; |
||
| 40 | const text = body.text; |
||
| 41 | |||
| 42 | db.run("UPDATE reports SET text = '" + text + "' WHERE week = " + week, |
||
| 43 | (err) => { |
||
| 44 | if (err) { |
||
| 45 | // console.log(err); |
||
| 46 | } |
||
| 47 | return res.status(201).json({ |
||
| 48 | data: { |
||
| 49 | message: "Report successfully updated." |
||
| 50 | } |
||
| 51 | }); |
||
| 52 | }); |
||
| 53 | } |
||
| 54 | |||
| 55 | module.exports = router; |
||
| 56 |