From 89f10edac0f4d99d06f2d38a3cd9cabbe4abaa22 Mon Sep 17 00:00:00 2001 From: cgoodwin90 Date: Thu, 7 Mar 2024 12:36:20 +1100 Subject: [PATCH 1/4] Initial commit --- services/api/src/resolvers.js | 4 +- .../src/resources/notification/resolvers.ts | 43 +++++++++++++++++++ .../api/src/resources/notification/sql.ts | 11 ++++- services/api/src/typeDefs.js | 4 ++ 4 files changed, 60 insertions(+), 2 deletions(-) diff --git a/services/api/src/resolvers.js b/services/api/src/resolvers.js index 8c192b3174..1e21eab469 100644 --- a/services/api/src/resolvers.js +++ b/services/api/src/resolvers.js @@ -152,6 +152,7 @@ const { deleteNotificationWebhook, getNotificationsByProjectId, getNotificationsByOrganizationId, + getNotificationByNameAndType, removeNotificationFromProject, updateNotificationMicrosoftTeams, updateNotificationRocketChat, @@ -595,7 +596,8 @@ const resolvers = { getGroupProjectOrganizationAssociation, getProjectGroupOrganizationAssociation, getEnvVariablesByProjectEnvironmentName, - checkBulkImportProjectsAndGroupsToOrganization + checkBulkImportProjectsAndGroupsToOrganization, + notificationByNameAndType: getNotificationByNameAndType }, Mutation: { addProblem, diff --git a/services/api/src/resources/notification/resolvers.ts b/services/api/src/resources/notification/resolvers.ts index f714feac37..d82f5b2128 100644 --- a/services/api/src/resources/notification/resolvers.ts +++ b/services/api/src/resources/notification/resolvers.ts @@ -705,3 +705,46 @@ export const removeAllNotificationsFromAllProjects: ResolverFn = async ( // TODO: Check rows for success return 'success'; }; + +export const getNotificationByNameAndType: ResolverFn = async ( + root, + args, + { sqlClientPool, hasPermission } +) => { + + let notificationName: string; + let notificationType: string; + + if (args) { + notificationName = args.name; + notificationType = args.type; + } + + const row = await query(sqlClientPool, Sql.selectNotificationByNameAndType(notificationName, notificationType)); + let res = row[0]; + + const nid = await Helpers(sqlClientPool).getAssignedNotificationIds({ + name: notificationName, + type: notificationType + }); + + if (nid == res.id) { + res = await query( + sqlClientPool, + Sql.selectProjectNotificationByNameAndType(notificationName, notificationType), + ); + } + + if (!res) { + return null; + } + + const result = res[0] + result.type = notificationType; + + await hasPermission('notification', 'view', { + notification: result.id, + }); + console.log(result); + return result; +}; \ No newline at end of file diff --git a/services/api/src/resources/notification/sql.ts b/services/api/src/resources/notification/sql.ts index 75c2c4731f..f6f3829079 100644 --- a/services/api/src/resources/notification/sql.ts +++ b/services/api/src/resources/notification/sql.ts @@ -227,5 +227,14 @@ export const Sql = { truncateProjectNotification: () => knex('project_notification') .truncate() - .toString() + .toString(), + selectNotificationByNameAndType: (name: string, type: string) => + knex(`notification_${type}`) + .where('name', '=', name) + .toString(), + selectProjectNotificationByNameAndType: (name: string, type: string) => + knex(`notification_${type}`) + .join('project_notification', `notification_${type}.id`, '=', 'project_notification.nid' ) + .where('name', '=', name) + .toString(), }; diff --git a/services/api/src/typeDefs.js b/services/api/src/typeDefs.js index 835ec5d846..9a32b8e490 100644 --- a/services/api/src/typeDefs.js +++ b/services/api/src/typeDefs.js @@ -1456,6 +1456,10 @@ const typeDefs = gql` deployTargetConfigsByDeployTarget(deployTarget: Int!) : [DeployTargetConfig] @deprecated(reason: "Unstable API, subject to breaking changes in any release. Use at your own risk") allDeployTargetConfigs: [DeployTargetConfig] @deprecated(reason: "Unstable API, subject to breaking changes in any release. Use at your own risk") """ + Get a notification by its name and type + """ + notificationByNameAndType(name: String, type: NotificationType) : Notification + """ List all organizations """ allOrganizations: [Organization] @deprecated(reason: "Unstable API, subject to breaking changes in any release. Use at your own risk") From b392e53d22b423c4e3e13c53238f0f3d8fdb7a58 Mon Sep 17 00:00:00 2001 From: cgoodwin90 Date: Thu, 7 Mar 2024 15:52:22 +1100 Subject: [PATCH 2/4] Incorporated pid --- services/api/src/resources/notification/resolvers.ts | 12 ++++++------ services/api/src/typeDefs.js | 5 +++++ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/services/api/src/resources/notification/resolvers.ts b/services/api/src/resources/notification/resolvers.ts index d82f5b2128..11b44c6ba0 100644 --- a/services/api/src/resources/notification/resolvers.ts +++ b/services/api/src/resources/notification/resolvers.ts @@ -721,30 +721,30 @@ export const getNotificationByNameAndType: ResolverFn = async ( } const row = await query(sqlClientPool, Sql.selectNotificationByNameAndType(notificationName, notificationType)); - let res = row[0]; + let notification = row[0]; const nid = await Helpers(sqlClientPool).getAssignedNotificationIds({ name: notificationName, type: notificationType }); - if (nid == res.id) { - res = await query( + if (nid == notification.id) { + const projectNotification = await query( sqlClientPool, Sql.selectProjectNotificationByNameAndType(notificationName, notificationType), ); + notification = projectNotification[0] } - if (!res) { + if (!notification) { return null; } - const result = res[0] + const result = notification result.type = notificationType; await hasPermission('notification', 'view', { notification: result.id, }); - console.log(result); return result; }; \ No newline at end of file diff --git a/services/api/src/typeDefs.js b/services/api/src/typeDefs.js index 9a32b8e490..4d3bc504f2 100644 --- a/services/api/src/typeDefs.js +++ b/services/api/src/typeDefs.js @@ -579,6 +579,7 @@ const typeDefs = gql` contentType: String notificationSeverityThreshold: ProblemSeverityRating organization: Int + pid: Int } type NotificationRocketChat { @@ -589,6 +590,7 @@ const typeDefs = gql` contentType: String notificationSeverityThreshold: ProblemSeverityRating organization: Int + pid: Int } type NotificationSlack { @@ -599,6 +601,7 @@ const typeDefs = gql` contentType: String notificationSeverityThreshold: ProblemSeverityRating organization: Int + pid: Int } type NotificationEmail { @@ -608,6 +611,7 @@ const typeDefs = gql` contentType: String notificationSeverityThreshold: ProblemSeverityRating organization: Int + pid: Int } type NotificationWebhook { @@ -617,6 +621,7 @@ const typeDefs = gql` contentType: String notificationSeverityThreshold: ProblemSeverityRating organization: Int + pid: Int } type UnassignedNotification { From c264aa9444cd75967c5f8eb6174d15df762b8956 Mon Sep 17 00:00:00 2001 From: cgoodwin90 Date: Wed, 20 Mar 2024 10:25:55 +1100 Subject: [PATCH 3/4] Updated error handling --- services/api/src/resources/notification/resolvers.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/services/api/src/resources/notification/resolvers.ts b/services/api/src/resources/notification/resolvers.ts index 11b44c6ba0..3a8ecb11a0 100644 --- a/services/api/src/resources/notification/resolvers.ts +++ b/services/api/src/resources/notification/resolvers.ts @@ -728,6 +728,10 @@ export const getNotificationByNameAndType: ResolverFn = async ( type: notificationType }); + if (!notification) { + throw new Error(`No notification found for ${notificationName}`) + } + if (nid == notification.id) { const projectNotification = await query( sqlClientPool, @@ -736,10 +740,6 @@ export const getNotificationByNameAndType: ResolverFn = async ( notification = projectNotification[0] } - if (!notification) { - return null; - } - const result = notification result.type = notificationType; From d9013f02b3c5477e7cc4544e5d13f7728bdbe93d Mon Sep 17 00:00:00 2001 From: cgoodwin90 Date: Fri, 7 Jun 2024 16:26:39 +1000 Subject: [PATCH 4/4] Removed pid --- .../src/resources/notification/resolvers.ts | 28 +++---------------- services/api/src/typeDefs.js | 5 ---- 2 files changed, 4 insertions(+), 29 deletions(-) diff --git a/services/api/src/resources/notification/resolvers.ts b/services/api/src/resources/notification/resolvers.ts index 3a8ecb11a0..0d72a87047 100644 --- a/services/api/src/resources/notification/resolvers.ts +++ b/services/api/src/resources/notification/resolvers.ts @@ -712,39 +712,19 @@ export const getNotificationByNameAndType: ResolverFn = async ( { sqlClientPool, hasPermission } ) => { - let notificationName: string; - let notificationType: string; - - if (args) { - notificationName = args.name; - notificationType = args.type; - } + const { name: notificationName, type: notificationType } = args; const row = await query(sqlClientPool, Sql.selectNotificationByNameAndType(notificationName, notificationType)); let notification = row[0]; - const nid = await Helpers(sqlClientPool).getAssignedNotificationIds({ - name: notificationName, - type: notificationType - }); - if (!notification) { throw new Error(`No notification found for ${notificationName}`) } - if (nid == notification.id) { - const projectNotification = await query( - sqlClientPool, - Sql.selectProjectNotificationByNameAndType(notificationName, notificationType), - ); - notification = projectNotification[0] - } - - const result = notification - result.type = notificationType; + notification.type = notificationType; await hasPermission('notification', 'view', { - notification: result.id, + notification: notification.id, }); - return result; + return notification; }; \ No newline at end of file diff --git a/services/api/src/typeDefs.js b/services/api/src/typeDefs.js index d64d9b2b72..4f3367b19e 100644 --- a/services/api/src/typeDefs.js +++ b/services/api/src/typeDefs.js @@ -579,7 +579,6 @@ const typeDefs = gql` contentType: String notificationSeverityThreshold: ProblemSeverityRating organization: Int - pid: Int } type NotificationRocketChat { @@ -590,7 +589,6 @@ const typeDefs = gql` contentType: String notificationSeverityThreshold: ProblemSeverityRating organization: Int - pid: Int } type NotificationSlack { @@ -601,7 +599,6 @@ const typeDefs = gql` contentType: String notificationSeverityThreshold: ProblemSeverityRating organization: Int - pid: Int } type NotificationEmail { @@ -611,7 +608,6 @@ const typeDefs = gql` contentType: String notificationSeverityThreshold: ProblemSeverityRating organization: Int - pid: Int } type NotificationWebhook { @@ -621,7 +617,6 @@ const typeDefs = gql` contentType: String notificationSeverityThreshold: ProblemSeverityRating organization: Int - pid: Int } type UnassignedNotification {