mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-26 19:04:23 +00:00
feat(notification): add unread sidebar count badge and fix project scope upload authorization check
This commit is contained in:
@@ -310,4 +310,50 @@ describe('NotificationService', () => {
|
||||
expect(resCustom.data.some((n) => n.id === uploadNotifOther.id)).toBe(false) // otherUploads is now false
|
||||
expect(resCustom.data.some((n) => n.id === uploadNotifSelf.id)).toBe(true) // yourUploads is now true
|
||||
})
|
||||
|
||||
it('getUnreadCount retrieves correct count of unread notifications', async () => {
|
||||
const tm = await prisma.team.create({ data: { name: 'Team 2' } })
|
||||
const u1 = await prisma.user.create({
|
||||
data: { name: 'u3', email: `u3-${Date.now()}@example.com`, password: 'p' },
|
||||
})
|
||||
const u2 = await prisma.user.create({
|
||||
data: { name: 'u4', email: `u4-${Date.now()}@example.com`, password: 'p' },
|
||||
})
|
||||
const member = await prisma.teamMember.create({
|
||||
data: { teamId: tm.id, userId: u1.id, role: 'owner' },
|
||||
})
|
||||
|
||||
// No notifications initially
|
||||
let count = await notificationService.getUnreadCount(tm.id, u1.id)
|
||||
expect(count).toBe(0)
|
||||
|
||||
// Create unread notifications from other user
|
||||
const n1 = await prisma.notification.create({
|
||||
data: { id: '101', teamId: tm.id, type: NotificationType.comment_created, creatorId: u2.id },
|
||||
})
|
||||
const n2 = await prisma.notification.create({
|
||||
data: { id: '102', teamId: tm.id, type: NotificationType.reply_created, userId: u1.id, creatorId: u2.id },
|
||||
})
|
||||
|
||||
count = await notificationService.getUnreadCount(tm.id, u1.id)
|
||||
expect(count).toBe(2)
|
||||
|
||||
// Mark up to n1 as read
|
||||
await prisma.teamMember.update({
|
||||
where: { id: member.id },
|
||||
data: { lastReadNotificationId: n1.id },
|
||||
})
|
||||
|
||||
count = await notificationService.getUnreadCount(tm.id, u1.id)
|
||||
expect(count).toBe(1) // n2 is still unread
|
||||
|
||||
// Mark all as read (up to n2)
|
||||
await prisma.teamMember.update({
|
||||
where: { id: member.id },
|
||||
data: { lastReadNotificationId: n2.id },
|
||||
})
|
||||
|
||||
count = await notificationService.getUnreadCount(tm.id, u1.id)
|
||||
expect(count).toBe(0)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -97,28 +97,12 @@ export class NotificationService {
|
||||
}
|
||||
}
|
||||
|
||||
async list(
|
||||
private async getListWhere(
|
||||
teamId: string,
|
||||
userId: string,
|
||||
params: ListNotificationParams,
|
||||
): Promise<PaginatedData<NotificationInfo[]>> {
|
||||
// Get user's last read notification
|
||||
const teamMember = await prisma.teamMember.findUnique({
|
||||
where: {
|
||||
teamIdUserId: {
|
||||
teamId,
|
||||
userId,
|
||||
},
|
||||
},
|
||||
include: {
|
||||
lastReadNotification: true,
|
||||
},
|
||||
})
|
||||
|
||||
if (!teamMember) {
|
||||
throw new Error('failed to get team member')
|
||||
}
|
||||
|
||||
teamMember: Prisma.TeamMemberGetPayload<{ include: { lastReadNotification: true } }>,
|
||||
unreadOnly: boolean,
|
||||
): Promise<Prisma.NotificationWhereInput> {
|
||||
// Permission Filtering based on Scope
|
||||
let projectFilter: string[] = []
|
||||
if (teamMember.scope === 'project') {
|
||||
@@ -215,7 +199,7 @@ export class NotificationService {
|
||||
] as Prisma.NotificationWhereInput[]
|
||||
}
|
||||
|
||||
if (params.unreadOnly && teamMember.lastReadNotification) {
|
||||
if (unreadOnly && teamMember.lastReadNotification) {
|
||||
// We want notifications NEWER than the last read one.
|
||||
// String comparison works for ULID/KSUID.
|
||||
const currentAnd = Array.isArray(where.AND) ? where.AND : where.AND ? [where.AND] : []
|
||||
@@ -225,6 +209,54 @@ export class NotificationService {
|
||||
] as Prisma.NotificationWhereInput[]
|
||||
}
|
||||
|
||||
return where
|
||||
}
|
||||
|
||||
async getUnreadCount(teamId: string, userId: string): Promise<number> {
|
||||
const teamMember = await prisma.teamMember.findUnique({
|
||||
where: {
|
||||
teamIdUserId: {
|
||||
teamId,
|
||||
userId,
|
||||
},
|
||||
},
|
||||
include: {
|
||||
lastReadNotification: true,
|
||||
},
|
||||
})
|
||||
|
||||
if (!teamMember) {
|
||||
return 0
|
||||
}
|
||||
|
||||
const where = await this.getListWhere(teamId, userId, teamMember, true)
|
||||
return prisma.notification.count({ where })
|
||||
}
|
||||
|
||||
async list(
|
||||
teamId: string,
|
||||
userId: string,
|
||||
params: ListNotificationParams,
|
||||
): Promise<PaginatedData<NotificationInfo[]>> {
|
||||
// Get user's last read notification
|
||||
const teamMember = await prisma.teamMember.findUnique({
|
||||
where: {
|
||||
teamIdUserId: {
|
||||
teamId,
|
||||
userId,
|
||||
},
|
||||
},
|
||||
include: {
|
||||
lastReadNotification: true,
|
||||
},
|
||||
})
|
||||
|
||||
if (!teamMember) {
|
||||
throw new Error('failed to get team member')
|
||||
}
|
||||
|
||||
const where = await this.getListWhere(teamId, userId, teamMember, !!params.unreadOnly)
|
||||
|
||||
return paginateQuery<NotificationInfo>(
|
||||
async (skip, take) => {
|
||||
const notifications = await prisma.notification.findMany({
|
||||
|
||||
@@ -2,6 +2,7 @@ import { prisma } from '@/db'
|
||||
import { Prisma } from '@/generated/prisma/client'
|
||||
import { PaginatedData, paginateQuery } from '@/services/pagination'
|
||||
import { ProviderService, providerService } from '@/services/provider/provider'
|
||||
import { notificationService } from '@/services/notification/notification'
|
||||
import {
|
||||
ServiceCreateTeamRequest,
|
||||
ServiceGetUserTeamsRequest,
|
||||
@@ -138,11 +139,17 @@ export class TeamService {
|
||||
|
||||
if (!member) throw new Error('user is not a team member')
|
||||
|
||||
const unreadNotificationCount = await notificationService.getUnreadCount(
|
||||
req.teamId,
|
||||
req.user.id,
|
||||
)
|
||||
|
||||
return {
|
||||
id: req.user.id,
|
||||
name: req.user.name,
|
||||
email: undefined,
|
||||
role: member.role,
|
||||
unreadNotificationCount,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user