experimentCheck.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. 'use strict'
  2. import express from 'express'
  3. import boom from '@hapi/boom'
  4. import { TEST_MODE } from '../../config'
  5. import DataController from '../database/controllers/Data'
  6. import { asyncMiddleware, checkRequiredParameters } from '../functions'
  7. const router = express.Router()
  8. /**
  9. * @api {post} /experimentCheck /experimentCheck
  10. * @apiVersion 0.1.11
  11. * @apiName experimentCheck
  12. * @apiGroup API
  13. *
  14. * @apiDescription Collect user's data
  15. *
  16. * @apiParam {String} msgId The type of message to store
  17. * @apiParam {any} Any data that needs to be stored
  18. *
  19. * @apiExample Usage example
  20. * curl -i -L -H "Content-Type: application/json" -X POST "https://diran.univ-littoral.fr/api/experimentCheck" -d {"msgId":"test","msg":{}}
  21. *
  22. * @apiSuccessExample {string} Success response example
  23. * HTTP/1.1 204 OK /api/experimentCheck
  24. *
  25. * @apiError (Error 4xx) 400_[1] Missing parameter(s)
  26. * @apiErrorExample {json} Missing parameter
  27. * HTTP/1.1 400 Bad Request
  28. * {
  29. * "message": "Missing parameter(s). Required parameters : msgId, msg."
  30. * }
  31. *
  32. * @apiError (Error 4xx) 400_[2] Invalid query parameter
  33. * @apiErrorExample {json} Invalid query parameter(s)
  34. * HTTP/1.1 400 Bad Request
  35. * {
  36. * "message": "Invalid body parameter(s).",
  37. * "data": [
  38. * "\"msgId\" must be a string."
  39. * ]
  40. * }
  41. *
  42. */
  43. router.post('/', asyncMiddleware(async (req, res) => {
  44. // Check the request contains all the required body parameters
  45. const b = req.body
  46. checkRequiredParameters(['msgId', 'msg'], b)
  47. const {
  48. msgId,
  49. msg,
  50. userId = null,
  51. experimentId = null
  52. } = req.body
  53. console.log('Trying to find data')
  54. // Check there is no errors with parameters
  55. if (typeof b.msgId !== 'string')
  56. throw boom.badRequest('Invalid body parameter(s).', ['"msgId" must be a string.'])
  57. let status = 204
  58. console.log('Trying to find data')
  59. // Add data to the database
  60. if (!TEST_MODE) {
  61. console.log({ msgId, msg, userId, experimentId })
  62. const findDoc = await DataController.findOne({ msgId, msg, userId, experimentId })
  63. console.log(findDoc)
  64. if (findDoc)
  65. status = 208
  66. }
  67. res.status(status).send()
  68. }))
  69. export default router