Merge branch 'hotfix/21.56.9' into master
[gitter.git] / server / services / billing-service.js
blob75afbb07caf7f74df1a47f2bc92d4498721932f6
1 'use strict';
3 var persistence = require('gitter-web-persistence');
4 var mongoUtils = require('gitter-web-persistence-utils/lib/mongo-utils');
5 var Promise = require('bluebird');
6 const mongoReadPrefs = require('gitter-web-persistence-utils/lib/mongo-read-prefs');
8 function toLowerCase(value) {
9 return value && value.toLowerCase();
12 /**
13 * For exporting things
15 function getCursorByUserId(userId) {
16 const cursor = persistence.Subscription.find({
17 userId
19 .lean()
20 .read(mongoReadPrefs.secondaryPreferred)
21 .batchSize(100)
22 .cursor();
24 return cursor;
27 function findActiveOrgPlans(orgUris) {
28 if (!orgUris || !orgUris.length) return Promise.resolve([]);
30 var query = mongoUtils.fieldInPredicate('lcUri', orgUris.map(toLowerCase), {
31 subscriptionType: 'ORG',
32 status: 'CURRENT'
33 });
35 return persistence.Subscription.find(query).exec();
38 function findActivePlan(uri) {
39 var lcUri = toLowerCase(uri);
41 return persistence.Subscription.findOne({
42 lcUri: lcUri,
43 status: 'CURRENT'
44 }).exec();
47 function findActivePlans(uris) {
48 if (!uris || !uris.length) return Promise.resolve([]);
50 var query = mongoUtils.fieldInPredicate('lcUri', uris.map(toLowerCase), {
51 status: 'CURRENT'
52 });
54 return persistence.Subscription.find(query).exec();
57 module.exports = {
58 getCursorByUserId,
59 findActiveOrgPlans,
60 findActivePlan,
61 findActivePlans