diff --git a/services/api/src/resources/fact/resolvers.ts b/services/api/src/resources/fact/resolvers.ts index 1af7eef09e..e94dad24dd 100644 --- a/services/api/src/resources/fact/resolvers.ts +++ b/services/api/src/resources/fact/resolvers.ts @@ -382,18 +382,22 @@ export const deleteFact: ResolverFn = async ( export const deleteFactsFromSource: ResolverFn = async ( root, - { input: { environment: environmentId, source } }, + { input: { environment: environmentId, source, service } }, { sqlClientPool, hasPermission, userActivityLogger } ) => { const environment = await environmentHelpers( sqlClientPool ).getEnvironmentById(environmentId); + if(environment == null) { + throw new Error(`Unable to find environment with id: ${environmentId}`); + } + await hasPermission('fact', 'delete', { project: environment.project }); - await query(sqlClientPool, Sql.deleteFactsFromSource(environmentId, source)); + await query(sqlClientPool, Sql.deleteFactsFromSource(environmentId, source, service)); userActivityLogger(`User deleted facts`, { project: '', @@ -401,7 +405,8 @@ export const deleteFactsFromSource: ResolverFn = async ( payload: { data: { environment: environmentId, - source + source, + service } } }); diff --git a/services/api/src/resources/fact/sql.ts b/services/api/src/resources/fact/sql.ts index 1908719edb..33a06ce918 100644 --- a/services/api/src/resources/fact/sql.ts +++ b/services/api/src/resources/fact/sql.ts @@ -48,11 +48,18 @@ export const Sql = { }) .del() .toString(), - deleteFactsFromSource: (environment, source) => - knex('environment_fact') - .where({ environment, source }) - .del() - .toString(), + deleteFactsFromSource: (environment, source, service) => { + let query = knex('environment_fact') + .where({ environment, source }); + + if(service != null) { + query = query.andWhere("service", "=", service) + } + + query = query.del(); + + return query.toString(); + }, selectFactReferenceByDatabaseId: (id: number) => knex('environment_fact_reference') .where({ id }) diff --git a/services/api/src/typeDefs.js b/services/api/src/typeDefs.js index 12f577e019..d0d913c47d 100644 --- a/services/api/src/typeDefs.js +++ b/services/api/src/typeDefs.js @@ -378,6 +378,7 @@ const typeDefs = gql` input DeleteFactsFromSourceInput { environment: Int! source: String! + service: String } type FactReference {