AnalyticsController.coffee 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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. {resource_id, start_date, end_date, lag} = req.query
  25. InstitutionsAPI.getInstitutionLicences resource_id, start_date, end_date, lag, (error, licences) ->
  26. if error?
  27. next(error)
  28. else
  29. res.send licences
  30. respondWith = (error, res, next) ->
  31. if error instanceof Errors.ServiceNotConfiguredError
  32. # ignore, no-op
  33. res.send(204)
  34. else if error?
  35. next(error)
  36. else
  37. res.send 204