diff --git a/.changeset/sour-jokes-sneeze.md b/.changeset/sour-jokes-sneeze.md new file mode 100644 index 0000000000..5cafaea595 --- /dev/null +++ b/.changeset/sour-jokes-sneeze.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-notifications-backend': patch +--- + +Added an option to filter notifications by topic diff --git a/plugins/notifications-backend/src/database/DatabaseNotificationsStore.test.ts b/plugins/notifications-backend/src/database/DatabaseNotificationsStore.test.ts index 6204629821..c6df60c56e 100644 --- a/plugins/notifications-backend/src/database/DatabaseNotificationsStore.test.ts +++ b/plugins/notifications-backend/src/database/DatabaseNotificationsStore.test.ts @@ -271,6 +271,20 @@ describe.each(databases.eachSupportedId())( expect(notifications.length).toBe(1); expect(notifications.at(0)?.id).toEqual(id2); }); + + it('should filter notifications based on topic', async () => { + await storage.saveNotification(testNotification1); + await storage.saveNotification(testNotification2); + await storage.saveNotification(testNotification3); + + const notifications = await storage.getNotifications({ + user, + topic: 'efgh-topic', + }); + + expect(notifications.length).toBe(1); + expect(notifications.at(0)?.id).toEqual(id1); + }); }); describe('getNotifications filters on severity', () => { diff --git a/plugins/notifications-backend/src/database/DatabaseNotificationsStore.ts b/plugins/notifications-backend/src/database/DatabaseNotificationsStore.ts index f4bd25d1ba..3d027128ec 100644 --- a/plugins/notifications-backend/src/database/DatabaseNotificationsStore.ts +++ b/plugins/notifications-backend/src/database/DatabaseNotificationsStore.ts @@ -204,6 +204,10 @@ export class DatabaseNotificationsStore implements NotificationsStore { query.whereNull('read'); } // or match both if undefined + if (options.topic) { + query.where('topic', '=', options.topic); + } + if (options.saved) { query.whereNotNull('saved'); } else if (options.saved === false) { diff --git a/plugins/notifications-backend/src/database/NotificationsStore.ts b/plugins/notifications-backend/src/database/NotificationsStore.ts index 901ea1695d..3c57d0ac4a 100644 --- a/plugins/notifications-backend/src/database/NotificationsStore.ts +++ b/plugins/notifications-backend/src/database/NotificationsStore.ts @@ -35,6 +35,7 @@ export type NotificationGetOptions = { limit?: number; search?: string; orderField?: EntityOrder[]; + topic?: string; read?: boolean; saved?: boolean; createdAfter?: Date; diff --git a/plugins/notifications-backend/src/service/router.ts b/plugins/notifications-backend/src/service/router.ts index bb5c9e06ac..3ca3ce46bc 100644 --- a/plugins/notifications-backend/src/service/router.ts +++ b/plugins/notifications-backend/src/service/router.ts @@ -302,6 +302,11 @@ export async function createRouter( opts.read = false; // or keep undefined } + + if (req.query.topic) { + opts.topic = req.query.topic.toString(); + } + if (req.query.saved === 'true') { opts.saved = true; } else if (req.query.saved === 'false') {