dataCollect.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 {Object} screen Screen data, `window.screen` @see https://developer.mozilla.org/en-US/docs/Web/API/Screen
  20. *
  21. * @apiExample Usage example
  22. * curl -i -L -H "Content-Type: application/json" -X POST "http://diran.univ-littoral.fr/api/dataCollect" -d {"uuid":"test","screen":{"width":1920,"height":1024}}
  23. *
  24. * @apiSuccessExample {string} Success response example
  25. * HTTP/1.1 200 OK /api/dataCollect
  26. * OK
  27. *
  28. * @apiError (Error 4xx) 400_[1] Missing parameter(s)
  29. * @apiErrorExample {json} Missing parameter
  30. * HTTP/1.1 400 Bad Request
  31. * {
  32. * "message": "Missing parameter(s). Required parameters : uuid, screen."
  33. * }
  34. *
  35. * @apiError (Error 4xx) 400_[2] Invalid query parameter
  36. * @apiErrorExample {json} Invalid query parameter(s)
  37. * HTTP/1.1 400 Bad Request
  38. * {
  39. * "message": "Invalid body parameter(s).",
  40. * "data": [
  41. * "\"uuid\" must be a string.",
  42. * "\"screen\" must be a valid object."
  43. * ]
  44. * }
  45. *
  46. */
  47. router.post('/', asyncMiddleware(async (req, res) => {
  48. // Check the request contains all the required body parameters
  49. const b = req.body
  50. checkRequiredParameters(['uuid', 'screen', 'userId', 'experimentId'], b)
  51. let errorList = []
  52. if (typeof b.uuid !== 'string')
  53. errorList.push('"uuid" must be a string.')
  54. if (b.userId && typeof b.userId !== 'string')
  55. errorList.push('"userId" must be a string.')
  56. if (b.experimentId && typeof b.experimentId !== 'string')
  57. errorList.push('"experimentId" must be a string.')
  58. if (typeof b.screen !== 'object' || Object.keys(b.screen).length > 30)
  59. errorList.push('"screen" must be a valid object.')
  60. // Check there is no errors with parameters
  61. if (errorList.length > 0)
  62. throw boom.badRequest('Invalid body parameter(s).', errorList)
  63. const userAgent = userAgentParser(req.headers['user-agent'])
  64. // Collected data object
  65. const data = {
  66. uuid: b.uuid,
  67. msgId: COLLECT_DATA,
  68. msg: {
  69. screen: b.screen,
  70. userAgent,
  71. ip: req.ip
  72. },
  73. userid: b.userId || null,
  74. experimentId: b.experimentId || null
  75. }
  76. if (!TEST_MODE) await DataController.add(data)
  77. res.send({ message: 'OK' })
  78. }))
  79. export default router