fix: force broadcast notification user field to be null always

Signed-off-by: Hellgren Heikki <heikki.hellgren@op.fi>
This commit is contained in:
Hellgren Heikki
2025-06-11 12:21:20 +03:00
parent 58cb0452a9
commit ef9ab8267e
3 changed files with 22 additions and 7 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-notifications-backend': patch
---
Notifications API will now return user as null always for broadcast notifications
@@ -228,6 +228,13 @@ describe.each(databases.eachSupportedId())(
]);
});
it('should return null value for user for broadcast notifications', async () => {
await storage.saveBroadcast({ ...testNotification1, read: new Date() });
const notifications = await storage.getNotifications({ user });
expect(notifications).toHaveLength(1);
expect(notifications[0].user).toBeNull();
});
it('should return read notifications for user', async () => {
await storage.saveNotification(testNotification1);
await storage.saveBroadcast(testNotification2);
@@ -298,7 +305,7 @@ describe.each(databases.eachSupportedId())(
user: otherUser,
});
expect(otherUserNotifications.map(idOnly)).toEqual([id0, id2]);
expect(otherUserNotifications[1].user).toBe(otherUser);
expect(otherUserNotifications[1].user).toBeNull();
});
it('should allow searching for notifications', async () => {
@@ -635,7 +642,7 @@ describe.each(databases.eachSupportedId())(
expect(notification?.user).toBeNull();
await storage.markRead({ ids: [id1], user });
notification = await storage.getNotification({ id: id1, user });
expect(notification?.user).toBe(user);
expect(notification?.user).toBeNull();
const otherNotification = await storage.getNotification({
id: id1,
@@ -149,7 +149,7 @@ export class DatabaseNotificationsStore implements NotificationsStore {
private mapToNotifications = (rows: any[]): Notification[] => {
return rows.map(row => ({
id: row.id,
user: row.user,
user: row.type === 'broadcast' ? null : row.user,
created: new Date(row.created),
saved: row.saved,
read: row.read,
@@ -252,7 +252,7 @@ export class DatabaseNotificationsStore implements NotificationsStore {
join.andOnVal('user', '=', user);
}
})
.select(NOTIFICATION_COLUMNS);
.select([...NOTIFICATION_COLUMNS, this.db.raw("'broadcast' as type")]);
};
private getNotificationsBaseQuery = (
@@ -261,7 +261,7 @@ export class DatabaseNotificationsStore implements NotificationsStore {
const { user, orderField } = options;
const subQuery = this.db<NotificationRowType>('notification')
.select(NOTIFICATION_COLUMNS)
.select([...NOTIFICATION_COLUMNS, this.db.raw("'entity' as type")])
.unionAll([this.getBroadcastUnion(user)])
.as('notifications');
@@ -331,7 +331,10 @@ export class DatabaseNotificationsStore implements NotificationsStore {
async getNotifications(options: NotificationGetOptions) {
const notificationQuery = this.getNotificationsBaseQuery(options);
const notifications = await notificationQuery.select(NOTIFICATION_COLUMNS);
const notifications = await notificationQuery.select([
...NOTIFICATION_COLUMNS,
'type',
]);
return this.mapToNotifications(notifications);
}
@@ -462,7 +465,7 @@ export class DatabaseNotificationsStore implements NotificationsStore {
.select('*')
.from(
this.db<NotificationRowType>('notification')
.select(NOTIFICATION_COLUMNS)
.select([...NOTIFICATION_COLUMNS, this.db.raw("'entity' as type")])
.unionAll([this.getBroadcastUnion(options.user)])
.as('notifications'),
)