From bcd7a740ec7d31b1479ae371abbec81366513c40 Mon Sep 17 00:00:00 2001 From: Hugh O'Brien Date: Fri, 16 Nov 2018 09:02:57 +0000 Subject: [PATCH] Merge pull request #1154 from sharelatex/ta-display-institution-name display institution name on managment and metrics pages GitOrigin-RevId: 302bffee358d9d5090877ca833584f50afc0ff7b --- .../UserMembershipController.coffee | 20 ++++++++++--------- .../UserMembershipEntityConfigs.coffee | 1 + .../web/app/coffee/models/Institution.coffee | 12 +++++++++++ .../web/app/coffee/models/Subscription.coffee | 4 ++++ .../UserMembershipControllerTests.coffee | 13 ++++++++++-- 5 files changed, 39 insertions(+), 11 deletions(-) diff --git a/services/web/app/coffee/Features/UserMembership/UserMembershipController.coffee b/services/web/app/coffee/Features/UserMembership/UserMembershipController.coffee index a1add2139f..3a8e26ca1d 100644 --- a/services/web/app/coffee/Features/UserMembership/UserMembershipController.coffee +++ b/services/web/app/coffee/Features/UserMembership/UserMembershipController.coffee @@ -8,16 +8,18 @@ logger = require("logger-sharelatex") module.exports = index: (req, res, next)-> { entity, entityConfig } = req - UserMembershipHandler.getUsers entity, entityConfig, (error, users)-> + entity.fetchV1Data (error, entity) -> return next(error) if error? - entityPrimaryKey = entity[entityConfig.fields.primaryKey].toString() - entityName = entity[entityConfig.fields.name] if entityConfig.fields.name - res.render "user_membership/index", - name: entityName - users: users - groupSize: entity.membersLimit if entityConfig.hasMembersLimit - translations: entityConfig.translations - paths: entityConfig.pathsFor(entityPrimaryKey) + UserMembershipHandler.getUsers entity, entityConfig, (error, users)-> + return next(error) if error? + entityPrimaryKey = entity[entityConfig.fields.primaryKey].toString() + entityName = entity[entityConfig.fields.name] if entityConfig.fields.name + res.render "user_membership/index", + name: entityName + users: users + groupSize: entity.membersLimit if entityConfig.hasMembersLimit + translations: entityConfig.translations + paths: entityConfig.pathsFor(entityPrimaryKey) add: (req, res, next)-> { entity, entityConfig } = req diff --git a/services/web/app/coffee/Features/UserMembership/UserMembershipEntityConfigs.coffee b/services/web/app/coffee/Features/UserMembership/UserMembershipEntityConfigs.coffee index e0e670f571..733a82f66d 100644 --- a/services/web/app/coffee/Features/UserMembership/UserMembershipEntityConfigs.coffee +++ b/services/web/app/coffee/Features/UserMembership/UserMembershipEntityConfigs.coffee @@ -54,6 +54,7 @@ module.exports = read: ['managerIds'] write: 'managerIds' access: 'managerIds' + name: 'name' translations: title: 'institution_account' subtitle: 'managers_management' diff --git a/services/web/app/coffee/models/Institution.coffee b/services/web/app/coffee/models/Institution.coffee index decf114a96..6af1d43470 100644 --- a/services/web/app/coffee/models/Institution.coffee +++ b/services/web/app/coffee/models/Institution.coffee @@ -1,11 +1,23 @@ mongoose = require 'mongoose' Schema = mongoose.Schema ObjectId = Schema.ObjectId +settings = require 'settings-sharelatex' +request = require 'request' InstitutionSchema = new Schema v1Id: { type: Number, required: true } managerIds: [ type:ObjectId, ref:'User' ] +# fetch institution's data from v1 API. Errors are ignored +InstitutionSchema.method 'fetchV1Data', (callback = (error, institution)->) -> + url = "#{settings.apis.v1.url}/universities/list/#{this.v1Id}" + request.get url, (error, response, body) => + try parsedBody = JSON.parse(body) catch e + this.name = parsedBody?.name + this.countryCode = parsedBody?.country_code + this.departments = parsedBody?.departments + callback(null, this) + mongoose.model 'Institution', InstitutionSchema exports.Institution = mongoose.model 'Institution' exports.InstitutionSchema = InstitutionSchema diff --git a/services/web/app/coffee/models/Subscription.coffee b/services/web/app/coffee/models/Subscription.coffee index aad4638def..ac3043e3f6 100644 --- a/services/web/app/coffee/models/Subscription.coffee +++ b/services/web/app/coffee/models/Subscription.coffee @@ -36,6 +36,10 @@ SubscriptionSchema.statics.findAndModify = (query, update, callback)-> this.update query, update, -> self.findOne query, callback +# Subscriptions have no v1 data to fetch +SubscriptionSchema.method 'fetchV1Data', (callback = (error, subscription)->) -> + callback(null, this) + conn = mongoose.createConnection(Settings.mongo.url, { server: {poolSize: Settings.mongo.poolSize || 10}, config: {autoIndex: false} diff --git a/services/web/test/unit/coffee/UserMembership/UserMembershipControllerTests.coffee b/services/web/test/unit/coffee/UserMembership/UserMembershipControllerTests.coffee index 34339211d2..32df7c85ad 100644 --- a/services/web/test/unit/coffee/UserMembership/UserMembershipControllerTests.coffee +++ b/services/web/test/unit/coffee/UserMembership/UserMembershipControllerTests.coffee @@ -18,8 +18,16 @@ describe "UserMembershipController", -> @req.params.id = 'mock-entity-id' @user = _id: 'mock-user-id' @newUser = _id: 'mock-new-user-id', email: 'new-user-email@foo.bar' - @subscription = { _id: 'mock-subscription-id'} - @institution = _id: 'mock-institution-id', v1Id: 123 + @subscription = + _id: 'mock-subscription-id' + fetchV1Data: (callback) => callback(null, @subscription) + @institution = + _id: 'mock-institution-id' + v1Id: 123 + fetchV1Data: (callback) => + institution = Object.assign({}, @institution) + institution.name = 'Test Institution Name' + callback(null, institution) @users = [ { _id: 'mock-member-id-1', email: 'mock-email-1@foo.com' } { _id: 'mock-member-id-2', email: 'mock-email-2@foo.com' } @@ -79,6 +87,7 @@ describe "UserMembershipController", -> @req.entityConfig = EntityConfigs.institution @UserMembershipController.index @req, render: (viewPath, viewParams) => expect(viewPath).to.equal 'user_membership/index' + expect(viewParams.name).to.equal 'Test Institution Name' expect(viewParams.groupSize).to.equal undefined expect(viewParams.translations.title).to.equal 'institution_account' expect(viewParams.paths.exportMembers).to.be.undefined