@@ -59,6 +59,8 @@ export async function populateAdmin(configs) {
5959}
6060
6161export async function createConfig ( args , admin ) {
62+ args . key = formatInput ( args . key , { toUpper : true , autoUnderscore : true } ) ;
63+
6264 // validates account plan permissions
6365 const group = await getGroupConfigById ( args . group ) ;
6466 await checkSwitcher ( group ) ;
@@ -70,7 +72,7 @@ export async function createConfig(args, admin) {
7072 } ) ;
7173
7274 if ( config ) {
73- throw new BadRequestError ( `Config ${ args . key } already exists` ) ;
75+ throw new BadRequestError ( `Config ${ config . key } already exists` ) ;
7476 }
7577
7678 config = new Config ( {
@@ -79,8 +81,6 @@ export async function createConfig(args, admin) {
7981 owner : admin . _id
8082 } ) ;
8183
82- config . key = formatInput ( config . key , { toUpper : true , autoUnderscore : true } ) ;
83-
8484 // validates permissions
8585 config = await verifyOwnership ( admin , config , group . domain , ActionTypes . CREATE , RouterTypes . CONFIG ) ;
8686
@@ -112,19 +112,12 @@ export async function updateConfig(id, args, admin) {
112112
113113 // validates existing switcher key
114114 if ( args . key ) {
115- const duplicatedKey = await getConfig ( {
116- key : args . key ,
117- group : config . group ,
118- domain : config . domain
119- } ) ;
120-
121- if ( duplicatedKey ) {
122- throw new BadRequestError ( `Config ${ args . key } already exists` ) ;
123- }
115+ args = await updateConfigKey ( args , config ) ;
116+ }
124117
125- // resets permission cache
126- permissionCache . permissionReset ( config . domain , ActionTypes . ALL , RouterTypes . CONFIG , config . group ) ;
127- permissionCache . permissionReset ( config . domain , ActionTypes . ALL , RouterTypes . CONFIG , config . key ) ;
118+ // validates existing group
119+ if ( args . group ) {
120+ await getGroupConfigById ( args . group ) ;
128121 }
129122
130123 // validates existing environment
@@ -139,13 +132,32 @@ export async function updateConfig(id, args, admin) {
139132 config . updatedBy = admin . email ;
140133 const updates = Object . keys ( args ) ;
141134 updates . forEach ( ( update ) => config [ update ] = args [ update ] ) ;
142- config . key = formatInput ( config . key , { toUpper : true , autoUnderscore : true } ) ;
135+
143136 await config . save ( ) ;
144137 updateDomainVersion ( config . domain ) ;
145138
146139 return config ;
147140}
148141
142+ async function updateConfigKey ( args , config ) {
143+ args . key = formatInput ( args . key , { toUpper : true , autoUnderscore : true } ) ;
144+
145+ const duplicatedKey = await getConfig ( {
146+ key : args . key ,
147+ domain : config . domain
148+ } ) ;
149+
150+ if ( duplicatedKey ) {
151+ throw new BadRequestError ( `Config ${ duplicatedKey . key } already exists` ) ;
152+ }
153+
154+ // resets permission cache
155+ permissionCache . permissionReset ( config . domain , ActionTypes . ALL , RouterTypes . CONFIG , config . group ) ;
156+ permissionCache . permissionReset ( config . domain , ActionTypes . ALL , RouterTypes . CONFIG , config . key ) ;
157+
158+ return args ;
159+ }
160+
149161export async function updateConfigRelay ( id , args , admin ) {
150162 let config = await getConfigById ( id ) ;
151163 isRelayValid ( args ) ;
0 commit comments