dataCollect.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. 'use strict'
  2. import express from 'express'
  3. import boom from '@hapi/boom'
  4. import userAgentParser from 'ua-parser-js'
  5. import { TEST_MODE } from '../../config'
  6. import { COLLECT_DATA } from '../../config.messagesId'
  7. import DataController from '../database/controllers/Data'
  8. import { asyncMiddleware, checkRequiredParameters } from '../functions'
  9. const router = express.Router()
  10. /**
  11. * @api {post} /dataCollect /dataCollect
  12. * @apiVersion 0.1.11
  13. * @apiName dataCollect
  14. * @apiGroup API
  15. *
  16. * @apiDescription Collect user's data
  17. *
  18. * @apiParam {String} uuid The unique user identifier
  19. * @apiParam {String} userId The user ID
  20. * @apiParam {String} experimentId The experiment ID
  21. * @apiParam {String} uuid The unique user identifier
  22. * @apiParam {Object} screen Screen data, `window.screen` @see https://developer.mozilla.org/en-US/docs/Web/API/Screen
  23. *
  24. * @apiExample Usage example
  25. * curl -i -L -H "Content-Type: application/json" -X POST "https://diran.univ-littoral.fr/api/dataCollect" -d {"uuid":"test","userId":"rigwild","experimentId":"expe-test","screen":{"width":1920,"height":1024}}
  26. *
  27. * @apiSuccessExample {string} Success response example
  28. * HTTP/1.1 200 OK /api/dataCollect
  29. * OK
  30. *
  31. * @apiError (Error 4xx) 400_[1] Missing parameter(s)
  32. * @apiErrorExample {json} Missing parameter
  33. * HTTP/1.1 400 Bad Request
  34. * {
  35. * "message": "Missing parameter(s). Required parameters : uuid, userId, experimentId, screen."
  36. * }
  37. *
  38. * @apiError (Error 4xx) 400_[2] Invalid query parameter
  39. * @apiErrorExample {json} Invalid query parameter(s)
  40. * HTTP/1.1 400 Bad Request
  41. * {
  42. * "message": "Invalid body parameter(s).",
  43. * "data": [
  44. * "\"uuid\" must be a string.",
  45. * "\"userId\" must be a string.",
  46. * "\"experimentId\" must be a string.",
  47. * "\"screen\" must be a valid object."
  48. * ]
  49. * }
  50. *
  51. */
  52. router.post('/', asyncMiddleware(async (req, res) => {
  53. // Check the request contains all the required body parameters
  54. const b = req.body
  55. checkRequiredParameters(['uuid', 'screen', 'userId', 'experimentId'], b)
  56. let errorList = []
  57. if (typeof b.uuid !== 'string')
  58. errorList.push('"uuid" must be a string.')
  59. if (b.userId && typeof b.userId !== 'string')
  60. errorList.push('"userId" must be a string.')
  61. if (b.experimentId && typeof b.experimentId !== 'string')
  62. errorList.push('"experimentId" must be a string.')
  63. if (typeof b.screen !== 'object' || Object.keys(b.screen).length > 30)
  64. errorList.push('"screen" must be a valid object.')
  65. // Check there is no errors with parameters
  66. if (errorList.length > 0)
  67. throw boom.badRequest('Invalid body parameter(s).', errorList)
  68. const userAgent = userAgentParser(req.headers['user-agent'])
  69. // Collected data object
  70. const data = {
  71. uuid: b.uuid,
  72. msgId: COLLECT_DATA,
  73. msg: {
  74. screen: b.screen,
  75. userAgent,
  76. ip: req.ip
  77. },
  78. userid: b.userId || null,
  79. experimentId: b.experimentId || null
  80. }
  81. if (!TEST_MODE) await DataController.add(data)
  82. res.send({ message: 'OK' })
  83. }))
  84. export default router