diff --git a/services/web/app/coffee/Features/Analytics/AnalyticsController.coffee b/services/web/app/coffee/Features/Analytics/AnalyticsController.coffee index 38029219ee..e407feb488 100644 --- a/services/web/app/coffee/Features/Analytics/AnalyticsController.coffee +++ b/services/web/app/coffee/Features/Analytics/AnalyticsController.coffee @@ -1,6 +1,7 @@ AnalyticsManager = require "./AnalyticsManager" Errors = require "../Errors/Errors" AuthenticationController = require("../Authentication/AuthenticationController") +InstitutionsAPI = require("../Institutions/InstitutionsAPI") GeoIpLookup = require '../../infrastructure/GeoIpLookup' module.exports = AnalyticsController = @@ -23,6 +24,14 @@ module.exports = AnalyticsController = AnalyticsManager.recordEvent user_id, req.params.event, req.body, (error) -> respondWith(error, res, next) + licences: (req, res, next) -> + {resource_id, start_date, end_date, lag} = req.query + InstitutionsAPI.getInstitutionLicences resource_id, start_date, end_date, lag, (error, licences) -> + if error? + next(error) + else + res.send licences + respondWith = (error, res, next) -> if error instanceof Errors.ServiceNotConfiguredError # ignore, no-op diff --git a/services/web/app/coffee/Features/Analytics/AnalyticsRouter.coffee b/services/web/app/coffee/Features/Analytics/AnalyticsRouter.coffee index 06ca2bfa1c..57b131326f 100644 --- a/services/web/app/coffee/Features/Analytics/AnalyticsRouter.coffee +++ b/services/web/app/coffee/Features/Analytics/AnalyticsRouter.coffee @@ -23,4 +23,4 @@ module.exports = publicApiRouter.use '/analytics/uniExternalCollaboration', AuthenticationController.httpAuth, - AnalyticsProxy.call('/uniExternalCollaboration') \ No newline at end of file + AnalyticsProxy.call('/uniExternalCollaboration') diff --git a/services/web/app/coffee/Features/Institutions/InstitutionsAPI.coffee b/services/web/app/coffee/Features/Institutions/InstitutionsAPI.coffee index 3d8da2ee80..dfc04e63b1 100644 --- a/services/web/app/coffee/Features/Institutions/InstitutionsAPI.coffee +++ b/services/web/app/coffee/Features/Institutions/InstitutionsAPI.coffee @@ -11,6 +11,13 @@ module.exports = InstitutionsAPI = defaultErrorMessage: "Couldn't get institution affiliations" }, (error, body) -> callback(error, body or []) + getInstitutionLicences: (institutionId, startDate, endDate, lag, callback = (error, body) ->) -> + makeAffiliationRequest { + method: 'GET' + path: "/api/v2/institutions/#{institutionId.toString()}/institution_licences" + body: {start_date: startDate, end_date: endDate, lag} + defaultErrorMessage: "Couldn't get institution licences" + }, callback getUserAffiliations: (userId, callback = (error, body) ->) -> makeAffiliationRequest { diff --git a/services/web/test/unit/coffee/Analytics/AnalyticsControllerTests.coffee b/services/web/test/unit/coffee/Analytics/AnalyticsControllerTests.coffee index c3e3802f37..0aa0517074 100644 --- a/services/web/test/unit/coffee/Analytics/AnalyticsControllerTests.coffee +++ b/services/web/test/unit/coffee/Analytics/AnalyticsControllerTests.coffee @@ -17,9 +17,13 @@ describe 'AnalyticsController', -> updateEditingSession: sinon.stub().callsArgWith(3) recordEvent: sinon.stub().callsArgWith(3) + @InstitutionsAPI = + getInstitutionLicences: sinon.stub().callsArgWith(4) + @controller = SandboxedModule.require modulePath, requires: "./AnalyticsManager":@AnalyticsManager "../Authentication/AuthenticationController":@AuthenticationController + "../Institutions/InstitutionsAPI":@InstitutionsAPI "logger-sharelatex": log:-> '../../infrastructure/GeoIpLookup': @GeoIpLookup = @@ -66,3 +70,19 @@ describe 'AnalyticsController', -> @controller.recordEvent @req, @res @AnalyticsManager.recordEvent.calledWith(@req.sessionID, @req.params["event"], @req.body).should.equal true done() + + describe "licences", -> + beforeEach -> + @req = + query: + resource_id:1 + start_date:'1514764800' + end_date:'1530662400' + resource_type:'institution' + sessionID: "sessionIDHere" + session: {} + + it "should trigger institutions api to fetch licences graph data", (done)-> + @controller.licences @req, @res + @InstitutionsAPI.getInstitutionLicences.calledWith(@req.query["resource_id"], @req.query["start_date"], @req.query["end_date"], @req.query["lag"]).should.equal true + done() diff --git a/services/web/test/unit/coffee/Institutions/InstitutionsAPITests.coffee b/services/web/test/unit/coffee/Institutions/InstitutionsAPITests.coffee index 9588ea32e7..0c4e7ae445 100644 --- a/services/web/test/unit/coffee/Institutions/InstitutionsAPITests.coffee +++ b/services/web/test/unit/coffee/Institutions/InstitutionsAPITests.coffee @@ -40,7 +40,7 @@ describe "InstitutionsAPI", -> should.not.exist(requestOptions.body) body.should.equal responseBody done() - + it 'handle empty response', (done)-> @settings.apis = null @InstitutionsAPI.getInstitutionAffiliations @institutionId, (err, body) => @@ -49,6 +49,26 @@ describe "InstitutionsAPI", -> body.length.should.equal 0 done() + describe 'getInstitutionLicences', -> + it 'get licences', (done)-> + @institutionId = 123 + responseBody = {"lag":"monthly","data":[{"key":"users","values":[{"x":"2018-01-01","y":1}]}]} + @request.yields(null, { statusCode: 200 }, responseBody) + startDate = '1417392000' + endDate = '1420848000' + @InstitutionsAPI.getInstitutionLicences @institutionId, startDate, endDate, 'monthly', (err, body) => + should.not.exist(err) + @request.calledOnce.should.equal true + requestOptions = @request.lastCall.args[0] + expectedUrl = "v1.url/api/v2/institutions/#{@institutionId}/institution_licences" + requestOptions.url.should.equal expectedUrl + requestOptions.method.should.equal 'GET' + requestOptions.body['start_date'].should.equal startDate + requestOptions.body['end_date'].should.equal endDate + requestOptions.body.lag.should.equal 'monthly' + body.should.equal responseBody + done() + describe 'getUserAffiliations', -> it 'get affiliations', (done)-> responseBody = [{ foo: 'bar' }]