@@ -14,6 +14,7 @@ import { buildTypes } from '@utils/schema';
1414import { validateApi } from '@utils/validators/validateApi' ;
1515import config from 'config' ;
1616import { logger } from '@services/logger.service' ;
17+ import { cloneDeep , isEmpty , omit } from 'lodash' ;
1718
1819/**
1920 * Edit the passed apiConfiguration if authorized.
@@ -41,42 +42,33 @@ export default {
4142 ) ;
4243 }
4344 const ability : AppAbility = user . ability ;
44- if (
45- ! args . name &&
46- ! args . status &&
47- ! args . authType &&
48- ! args . endpoint &&
49- ! args . pingUrl &&
50- ! args . graphQLEndpoint &&
51- ! args . settings &&
52- ! args . permissions
53- ) {
45+
46+ // Check if any of required arguments for a valid update are provided.
47+ // Else, send error
48+ // cloneDeep coupled with omit allows to filter out the 'id' field
49+ if ( isEmpty ( cloneDeep ( omit ( args , [ 'id' ] ) ) ) ) {
5450 throw new GraphQLError (
5551 context . i18next . t (
5652 'mutations.apiConfiguration.edit.errors.invalidArguments'
5753 )
5854 ) ;
5955 }
60- const update = { } ;
56+ // See if API name is usable
6157 if ( args . name ) {
6258 validateApi ( args . name ) ;
6359 }
64- Object . assign (
65- update ,
66- args . name && { name : args . name } ,
67- args . status && { status : args . status } ,
68- args . authType && { authType : args . authType } ,
69- args . endpoint && { endpoint : args . endpoint } ,
70- args . graphQLEndpoint && { graphQLEndpoint : args . graphQLEndpoint } ,
71- args . pingUrl && { pingUrl : args . pingUrl } ,
72- args . settings && {
60+ // Create the update document
61+ const update = {
62+ ...cloneDeep ( omit ( args , [ 'id' ] ) ) ,
63+ ...( args . settings && {
7364 settings : CryptoJS . AES . encrypt (
7465 JSON . stringify ( args . settings ) ,
7566 config . get ( 'encryption.key' )
7667 ) . toString ( ) ,
77- } ,
78- args . permissions && { permissions : args . permissions }
79- ) ;
68+ } ) ,
69+ } ;
70+
71+ // Find API configuration and update it using User permissions
8072 const filters = ApiConfiguration . accessibleBy ( ability , 'update' )
8173 . where ( { _id : args . id } )
8274 . getFilter ( ) ;
0 commit comments