Merge branch 'hotfix/21.56.9' into master
[gitter.git] / server / serializers / rest / github-org-strategy.js
blobad2ac6c8f073aef866715a2dc251a4a1a0dd5b5f
1 'use strict';
3 var Promise = require('bluebird');
4 var TroupeUriStrategy = require('./troupe-uri-strategy');
5 var billingService = require('../../services/billing-service');
7 function OrgPlanStrategy() {
8 var orgsWithPlans;
10 this.preload = function(orgUris) {
11 if (orgUris.isEmpty()) return;
13 return billingService.findActiveOrgPlans(orgUris.toArray()).then(function(subscriptions) {
14 orgsWithPlans = subscriptions.reduce(function(memo, s) {
15 memo[s.uri.toLowerCase()] = s.plan;
16 return memo;
17 }, {});
19 return true;
20 });
23 this.map = function(orgUri) {
24 return orgsWithPlans[orgUri.toLowerCase()];
28 OrgPlanStrategy.prototype = {
29 name: 'OrgPlanStrategy'
32 function GitHubOrgStrategy(options) {
33 var troupeUriStrategy = new TroupeUriStrategy(options);
34 var planStrategy = new OrgPlanStrategy();
36 this.preload = function(orgs) {
37 var orgUris = orgs.map(function(org) {
38 return org.login;
39 });
41 return Promise.join(troupeUriStrategy.preload(orgUris), planStrategy.preload(orgUris));
44 this.map = function(item) {
45 var plan = planStrategy.map(item.login);
46 return {
47 type: 'GH_ORG',
48 id: item.id,
49 name: item.login,
50 avatar_url: item.avatar_url,
51 uri: item.login,
52 absoluteUri: item.absoluteUri,
53 room: troupeUriStrategy.map(item.login),
54 premium: !!plan
59 GitHubOrgStrategy.prototype = {
60 name: 'GithubOrgStrategy'
63 module.exports = GitHubOrgStrategy;