From ac5baae4daa37e481f0420029be25946e39aa5d6 Mon Sep 17 00:00:00 2001 From: Blaize M Kaye Date: Mon, 18 Dec 2023 09:12:12 +1300 Subject: [PATCH] Adds service to deleteFactsFromSource --- services/api/src/resources/fact/resolvers.ts | 11 ++++++++--- services/api/src/resources/fact/sql.ts | 17 ++++++++++++----- services/api/src/typeDefs.js | 1 + 3 files changed, 21 insertions(+), 8 deletions(-) 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 {