Skip to content

Commit

Permalink
Extends allNotifications with optional filters
Browse files Browse the repository at this point in the history
  • Loading branch information
CGoodwin90 authored and shreddedbacon committed Sep 8, 2024
1 parent 9a89260 commit 8d92eb8
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 10 deletions.
17 changes: 11 additions & 6 deletions services/api/src/resources/notification/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,32 +56,37 @@ export const Helpers = (sqlClientPool: Pool) => ({
await query(sqlClientPool, Sql.deleteProjectNotificationByProjectId(project, "email"));
await query(sqlClientPool, Sql.deleteProjectNotificationByProjectId(project, "webhook"));
},
selectAllNotifications: async () => {
selectAllNotifications: async (name: string = null) => {
let type = "slack"
// get all notifications
const slacks = await query(
sqlClientPool,
Sql.selectAllNotifications(type)
name ?
Sql.selectAllNotificationsByName(name, type) : Sql.selectAllNotifications(type)
);
type = "rocketchat"
const rcs = await query(
sqlClientPool,
Sql.selectAllNotifications(type)
name ?
Sql.selectAllNotificationsByName(name, type) : Sql.selectAllNotifications(type)
);
type = "microsoftteams"
const teams = await query(
sqlClientPool,
Sql.selectAllNotifications(type)
name ?
Sql.selectAllNotificationsByName(name, type) : Sql.selectAllNotifications(type)
);
type = "email"
const email = await query(
sqlClientPool,
Sql.selectAllNotifications(type)
name ?
Sql.selectAllNotificationsByName(name, type) : Sql.selectAllNotifications(type)
);
type = "webhook"
const webhook = await query(
sqlClientPool,
Sql.selectAllNotifications(type)
name ?
Sql.selectAllNotificationsByName(name, type) : Sql.selectAllNotifications(type)
);
let result = [...slacks, ...rcs, ...teams, ...email, ...webhook]

Expand Down
38 changes: 36 additions & 2 deletions services/api/src/resources/notification/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -701,9 +701,43 @@ export const getAllNotifications: ResolverFn = async (
args,
{ sqlClientPool, hasPermission }
) => {
await hasPermission('notification', 'viewAll');
let rows;

if (args.name && args.type) {
rows = await query(sqlClientPool, Sql.selectNotificationByNameAndType(args.name, args.type));

if (rows.length > 0) {
rows[0].type = args.type;
}

await hasPermission('notification', 'view', {
notification: rows[0].id,
});

return rows;
}

if (args.name) {
await hasPermission('notification', 'viewAll');

const rows = await Helpers(sqlClientPool).selectAllNotifications();
const rows = await Helpers(sqlClientPool).selectAllNotifications(args.name);

if (rows.length === 0) {
throw new Error(`No notification found for ${args.name}`);
}

return rows;
}

if (args.type) {
await hasPermission('notification', 'viewAll');

const rows = await query(sqlClientPool, Sql.selectAllNotifications(args.type));

return rows;
}

await hasPermission('notification', 'viewAll');
rows = await Helpers(sqlClientPool).selectAllNotifications();
return rows;
};
9 changes: 9 additions & 0 deletions services/api/src/resources/notification/sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ export const Sql = {
knex(`notification_${type}`)
.select('*', knex.raw(`'${type}' as type`))
.toString(),
selectAllNotificationsByName: (name: string, type: string) =>
knex(`notification_${type}`)
.select('*', knex.raw(`'${type}' as type`))
.where('name', '=', name)
.toString(),
selectNotificationByNameAndType: (name: string, type: string) =>
knex(`notification_${type}`)
.where('name', '=', name)
.toString(),
deleteProjectNotification: input => {
const deleteQuery = knex.raw(
`DELETE pn
Expand Down
4 changes: 2 additions & 2 deletions services/api/src/typeDefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -1417,9 +1417,9 @@ 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")
"""
List all notifications
List all notifications matching the given filters (name & type) | returns all if no filter defined
"""
allNotifications: [Notification]
allNotifications(name: String, type: NotificationType): [Notification]
"""
List all organizations
"""
Expand Down

0 comments on commit 8d92eb8

Please sign in to comment.