Make some notifications be scoped to certain orgs to prevent redirect

issues.
This commit is contained in:
x032205
2025-09-05 22:39:47 -04:00
parent ad42fb4721
commit 67b02612ed
9 changed files with 54 additions and 13 deletions
@@ -15,13 +15,14 @@ export const notificationServiceFactory = ({
notificationQueue,
userNotificationDAL
}: TNotificationServiceFactoryDep) => {
const listUserNotifications = async ({ userId }: { userId: string }) => {
const listUserNotifications = async ({ userId, orgId }: { userId: string; orgId: string }) => {
const now = new Date();
const threeMonthsAgo = new Date();
threeMonthsAgo.setMonth(threeMonthsAgo.getMonth() - 3);
const notifications = await userNotificationDAL.find({
userId,
orgId,
startDate: threeMonthsAgo.toISOString(),
endDate: now.toISOString()
});
@@ -43,8 +44,8 @@ export const notificationServiceFactory = ({
return deletedNotifications[0];
};
const markUserNotificationsAsRead = async ({ userId }: { userId: string }) => {
await userNotificationDAL.markAllNotificationsAsRead(userId);
const markUserNotificationsAsRead = async ({ userId, orgId }: { userId: string; orgId: string }) => {
await userNotificationDAL.markAllNotificationsAsRead(userId, orgId);
};
const updateUserNotification = async ({
@@ -5,6 +5,9 @@ export enum NotificationType {
export interface TCreateUserNotificationDTO {
userId: string;
// Adding an orgId will make the notification only show up when a user is in a certain org. Otherwise, it shows up in all orgs.
// Keep in mind that org-scoped links for a notification will break if orgId is missing and the user is in the wrong org
orgId?: string;
type: NotificationType;
title: string;
body?: string;
@@ -19,12 +19,14 @@ export const userNotificationDALFactory = (db: TDbClient) => {
const find = async (
{
userId,
orgId,
startDate,
endDate,
limit = 1000,
offset = 0
}: {
userId: string;
orgId: string;
startDate: string;
endDate: string;
limit?: number;
@@ -35,6 +37,11 @@ export const userNotificationDALFactory = (db: TDbClient) => {
try {
const docs = await (tx || db.replicaNode())(TableName.UserNotifications)
.where(`${TableName.UserNotifications}.userId`, userId)
.andWhere((qb) => {
void qb
.where(`${TableName.UserNotifications}.orgId`, orgId)
.orWhereNull(`${TableName.UserNotifications}.orgId`);
})
.whereRaw(`"${TableName.UserNotifications}"."createdAt" >= ?::timestamptz`, [startDate])
.andWhereRaw(`"${TableName.UserNotifications}"."createdAt" < ?::timestamptz`, [endDate])
.select(selectAllTableCols(TableName.UserNotifications))
@@ -110,8 +117,13 @@ export const userNotificationDALFactory = (db: TDbClient) => {
}
};
const markAllNotificationsAsRead = async (userId: string) => {
await db(TableName.UserNotifications).where({ userId }).update({ isRead: true });
const markAllNotificationsAsRead = async (userId: string, orgId: string) => {
await db(TableName.UserNotifications)
.where({ userId })
.andWhere((qb) => {
void qb.where({ orgId }).orWhereNull("orgId");
})
.update({ isRead: true });
};
return { ...notificationOrm, pruneNotifications, find, markAllNotificationsAsRead };