AnalyticsController.coffee 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. AnalyticsManager = require "./AnalyticsManager"
  2. Errors = require "../Errors/Errors"
  3. AuthenticationController = require("../Authentication/AuthenticationController")
  4. InstitutionsAPI = require("../Institutions/InstitutionsAPI")
  5. GeoIpLookup = require '../../infrastructure/GeoIpLookup'
  6. module.exports = AnalyticsController =
  7. updateEditingSession: (req, res, next) ->
  8. userId = AuthenticationController.getLoggedInUserId(req)
  9. projectId = req.params.projectId
  10. countryCode = null
  11. if userId?
  12. GeoIpLookup.getDetails req.ip, (err, geoDetails) ->
  13. if geoDetails?.country_code? and geoDetails.country_code != ""
  14. countryCode = geoDetails.country_code
  15. AnalyticsManager.updateEditingSession userId, projectId, countryCode, (error) ->
  16. respondWith(error, res, next)
  17. else
  18. res.send 204
  19. recordEvent: (req, res, next) ->
  20. user_id = AuthenticationController.getLoggedInUserId(req) or req.sessionID
  21. AnalyticsManager.recordEvent user_id, req.params.event, req.body, (error) ->
  22. respondWith(error, res, next)
  23. licences: (req, res, next) ->
  24. AuthenticationController.getLoggedInUserId(req) or req.sessionID
  25. {resource_id, start_date, end_date, lag} = req.query
  26. InstitutionsAPI.getInstitutionLicences resource_id, start_date, end_date, lag, (error, licences) ->
  27. if error?
  28. res.send 503
  29. else
  30. res.send licences
  31. respondWith = (error, res, next) ->
  32. if error instanceof Errors.ServiceNotConfiguredError
  33. # ignore, no-op
  34. res.send(204)
  35. else if error?
  36. next(error)
  37. else
  38. res.send 204