1- import Switcher from 'switcher-client' ;
2- import Domain from '../models/domain' ;
3- import GroupConfig from '../models/group-config' ;
4- import { Config } from '../models/config' ;
5- import Component from '../models/component' ;
6- import { Environment , EnvType } from '../models/environment' ;
7- import { Team } from '../models/team' ;
1+ import { Switcher , checkNumeric , checkValue } from 'switcher-client' ;
2+ import { EnvType } from '../models/environment' ;
83import { FeatureUnavailableError } from '../exceptions' ;
4+ import { getDomainById , getTotalDomainsByOwner } from '../controller/domain' ;
5+ import { getGroupsByDomainId } from '../controller/group-config' ;
6+ import { getTotalConfigsByDomainId } from '../controller/config' ;
7+ import { getTotalComponentsByDomainId } from '../controller/component' ;
8+ import { getTotalEnvByDomainId } from '../controller/environment' ;
9+ import { getTotalTeamsByDomainId } from '../controller/team' ;
910
1011const apiKey = process . env . SWITCHER_API_KEY ;
1112const environment = process . env . SWITCHER_API_ENVIRONMENT ;
@@ -26,79 +27,73 @@ export async function checkDomain(req) {
2627 if ( process . env . SWITCHER_API_ENABLE != 'true' )
2728 return ;
2829
29- const total = await Domain . find ( { owner : req . admin . _id } ) . countDocuments ( ) ;
30+ const total = await getTotalDomainsByOwner ( req . admin . _id ) ;
3031 switcherFlagResult ( await switcher . isItOn ( 'ELEMENT_CREATION' , [
31- Switcher . StrategiesType . VALUE , `domain#${ req . admin . _id } ` ,
32- Switcher . StrategiesType . NUMERIC , total ]
33- ) , 'Domain limit has been reached.' ) ;
32+ checkValue ( `domain#${ req . admin . _id } ` ) ,
33+ checkNumeric ( total ) ] ) , 'Domain limit has been reached.' ) ;
3434}
3535
3636export async function checkGroup ( domain ) {
3737 if ( process . env . SWITCHER_API_ENABLE != 'true' )
3838 return ;
3939
40- const total = await GroupConfig . find ( { domain : domain . _id } ) . countDocuments ( ) ;
40+ const total = await getGroupsByDomainId ( domain . _id ) ;
4141 switcherFlagResult ( await switcher . isItOn ( 'ELEMENT_CREATION' , [
42- Switcher . StrategiesType . VALUE , `group#${ domain . owner } ` ,
43- Switcher . StrategiesType . NUMERIC , total ]
44- ) , 'Group limit has been reached.' ) ;
42+ checkValue ( `group#${ domain . owner } ` ) ,
43+ checkNumeric ( total ) ] ) , 'Group limit has been reached.' ) ;
4544}
4645
4746export async function checkSwitcher ( group ) {
4847 if ( process . env . SWITCHER_API_ENABLE != 'true' )
4948 return ;
5049
51- const total = await Config . find ( { domain : group . domain } ) . countDocuments ( ) ;
52- const { owner } = await Domain . findById ( group . domain ) . lean ( ) ;
50+ const total = await getTotalConfigsByDomainId ( group . domain ) ;
51+ const { owner } = await getDomainById ( group . domain ) ;
5352 switcherFlagResult ( await switcher . isItOn ( 'ELEMENT_CREATION' , [
54- Switcher . StrategiesType . VALUE , `switcher#${ owner } ` ,
55- Switcher . StrategiesType . NUMERIC , total ]
56- ) , 'Switcher limit has been reached.' ) ;
53+ checkValue ( `switcher#${ owner } ` ) ,
54+ checkNumeric ( total ) ] ) , 'Switcher limit has been reached.' ) ;
5755}
5856
5957export async function checkComponent ( domain ) {
6058 if ( process . env . SWITCHER_API_ENABLE != 'true' )
6159 return ;
6260
63- const total = await Component . find ( { domain } ) . countDocuments ( ) ;
64- const { owner } = await Domain . findById ( domain ) . lean ( ) ;
61+ const total = await getTotalComponentsByDomainId ( domain ) ;
62+ const { owner } = await getDomainById ( domain ) ;
6563 switcherFlagResult ( await switcher . isItOn ( 'ELEMENT_CREATION' , [
66- Switcher . StrategiesType . VALUE , `component#${ owner } ` ,
67- Switcher . StrategiesType . NUMERIC , total ]
68- ) , 'Component limit has been reached.' ) ;
64+ checkValue ( `component#${ owner } ` ) ,
65+ checkNumeric ( total ) ] ) , 'Component limit has been reached.' ) ;
6966}
7067
7168export async function checkEnvironment ( domain ) {
7269 if ( process . env . SWITCHER_API_ENABLE != 'true' )
7370 return ;
7471
75- const total = await Environment . find ( { domain } ) . countDocuments ( ) ;
76- const { owner } = await Domain . findById ( domain ) . lean ( ) ;
72+ const total = await getTotalEnvByDomainId ( domain ) ;
73+ const { owner } = await getDomainById ( domain ) ;
7774 switcherFlagResult ( await switcher . isItOn ( 'ELEMENT_CREATION' , [
78- Switcher . StrategiesType . VALUE , `environment#${ owner } ` ,
79- Switcher . StrategiesType . NUMERIC , total ]
80- ) , 'Environment limit has been reached.' ) ;
75+ checkValue ( `environment#${ owner } ` ) ,
76+ checkNumeric ( total ) ] ) , 'Environment limit has been reached.' ) ;
8177}
8278
8379export async function checkTeam ( domain ) {
8480 if ( process . env . SWITCHER_API_ENABLE != 'true' )
8581 return ;
8682
87- const total = await Team . find ( { domain } ) . countDocuments ( ) ;
88- const { owner } = await Domain . findById ( domain ) . lean ( ) ;
83+ const total = await getTotalTeamsByDomainId ( domain ) ;
84+ const { owner } = await getDomainById ( domain ) ;
8985 switcherFlagResult ( await switcher . isItOn ( 'ELEMENT_CREATION' , [
90- Switcher . StrategiesType . VALUE , `team#${ owner } ` ,
91- Switcher . StrategiesType . NUMERIC , total ]
92- ) , 'Team limit has been reached.' ) ;
86+ checkValue ( `team#${ owner } ` ) ,
87+ checkNumeric ( total ) ] ) , 'Team limit has been reached.' ) ;
9388}
9489
9590export async function checkMetrics ( config ) {
9691 if ( process . env . SWITCHER_API_ENABLE != 'true' )
9792 return true ;
9893
99- const { owner } = await Domain . findById ( config . domain ) . lean ( ) ;
94+ const { owner } = await getDomainById ( config . domain ) ;
10095 if ( ! await switcher . isItOn ( 'ELEMENT_CREATION' , [
101- Switcher . StrategiesType . VALUE , `metrics#${ owner } ` ] ) ) {
96+ checkValue ( `metrics#${ owner } ` ) ] ) ) {
10297
10398 if ( ! config . disable_metrics ) {
10499 config . disable_metrics = new Map ( ) ;
@@ -115,32 +110,31 @@ export async function checkHistory(domain) {
115110 if ( process . env . SWITCHER_API_ENABLE != 'true' )
116111 return true ;
117112
118- const { owner } = await Domain . findById ( domain ) . lean ( ) ;
113+ const { owner } = await getDomainById ( domain ) ;
119114 return switcher . isItOn ( 'ELEMENT_CREATION' , [
120- Switcher . StrategiesType . VALUE , `history#${ owner } ` ] ) ;
115+ checkValue ( `history#${ owner } ` ) ] ) ;
121116}
122117
123118export async function checkAdmin ( login ) {
124119 if ( process . env . SWITCHER_API_ENABLE != 'true' )
125120 return ;
126121
127122 switcherFlagResult ( await switcher . isItOn ( 'ACCOUNT_CREATION' , [
128- Switcher . StrategiesType . VALUE , login ]
129- ) , 'Account not released to use the API.' ) ;
123+ checkValue ( login ) ] ) , 'Account not released to use the API.' ) ;
130124}
131125
132126export function notifyAcCreation ( adminid ) {
133127 if ( process . env . SWITCHER_API_ENABLE != 'true' )
134128 return ;
135129
136130 switcher . isItOn ( 'ACCOUNT_IN_NOTIFY' , [
137- Switcher . StrategiesType . VALUE , adminid ] ) ;
131+ checkValue ( adminid ) ] ) ;
138132}
139133
140134export function notifyAcDeletion ( adminid ) {
141135 if ( process . env . SWITCHER_API_ENABLE != 'true' )
142136 return ;
143137
144138 switcher . isItOn ( 'ACCOUNT_OUT_NOTIFY' , [
145- Switcher . StrategiesType . VALUE , adminid ] ) ;
139+ checkValue ( adminid ) ] ) ;
146140}
0 commit comments