feat: add notification badge counts
This commit is contained in:
@@ -10,9 +10,18 @@ export const NOTIFICATION_CATEGORIES = [
|
||||
'messages',
|
||||
'follows',
|
||||
'follow_requests',
|
||||
'collaboration',
|
||||
'system',
|
||||
] as const;
|
||||
export type NotificationCategory = (typeof NOTIFICATION_CATEGORIES)[number];
|
||||
|
||||
export class NotificationUnreadCountQueryDto {
|
||||
@ApiPropertyOptional({ enum: NOTIFICATION_CATEGORIES })
|
||||
@IsOptional()
|
||||
@IsEnum(NOTIFICATION_CATEGORIES)
|
||||
category?: NotificationCategory;
|
||||
}
|
||||
|
||||
export class NotificationQueryDto extends PaginationQueryDto {
|
||||
@ApiPropertyOptional({ default: false })
|
||||
@IsOptional()
|
||||
|
||||
@@ -7,7 +7,10 @@ import { SuperAdminJwtAuthGuard } from '../../common/guards/super-admin-jwt-auth
|
||||
import { SuperAdminPermissionsGuard } from '../../common/guards/superadmin-permissions.guard';
|
||||
import { JwtPayload } from '../../common/interfaces/jwt-payload.interface';
|
||||
import { SUPERADMIN_PERMISSIONS } from '../superadmin/superadmin-permissions';
|
||||
import { NotificationQueryDto } from './dto/notification-query.dto';
|
||||
import {
|
||||
NotificationQueryDto,
|
||||
NotificationUnreadCountQueryDto,
|
||||
} from './dto/notification-query.dto';
|
||||
import { NotificationsService } from './notifications.service';
|
||||
|
||||
@ApiTags('Notifications')
|
||||
@@ -32,8 +35,17 @@ export class NotificationsController {
|
||||
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Get('unread-count')
|
||||
async getUnreadCount(@CurrentUser() user: JwtPayload) {
|
||||
return this.notificationsService.getUnreadCount(user.sub);
|
||||
async getUnreadCount(
|
||||
@CurrentUser() user: JwtPayload,
|
||||
@Query() query: NotificationUnreadCountQueryDto,
|
||||
) {
|
||||
return this.notificationsService.getUnreadCountByCategory(user.sub, query.category);
|
||||
}
|
||||
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Get('unread-counts')
|
||||
async getUnreadCounts(@CurrentUser() user: JwtPayload) {
|
||||
return this.notificationsService.getUnreadCounts(user.sub);
|
||||
}
|
||||
|
||||
@UseGuards(JwtAuthGuard)
|
||||
|
||||
@@ -78,6 +78,19 @@ export class NotificationsRepository {
|
||||
.exec();
|
||||
}
|
||||
|
||||
async countUnreadByFilter(
|
||||
recipientId: string,
|
||||
filter: FilterQuery<NotificationDocument> = {},
|
||||
): Promise<number> {
|
||||
return this.notificationModel
|
||||
.countDocuments({
|
||||
recipientId: new Types.ObjectId(recipientId),
|
||||
read: false,
|
||||
...filter,
|
||||
})
|
||||
.exec();
|
||||
}
|
||||
|
||||
async count(filter: FilterQuery<NotificationDocument>): Promise<number> {
|
||||
return this.notificationModel.countDocuments(filter).exec();
|
||||
}
|
||||
|
||||
@@ -1,7 +1,19 @@
|
||||
import { NotFoundException } from '@nestjs/common';
|
||||
import { plainToInstance } from 'class-transformer';
|
||||
import { validate } from 'class-validator';
|
||||
import { NotificationUnreadCountQueryDto } from './dto/notification-query.dto';
|
||||
import { NotificationsService } from './notifications.service';
|
||||
|
||||
describe('NotificationsService', () => {
|
||||
it('rejects invalid unread-count category through DTO validation', async () => {
|
||||
const dto = plainToInstance(NotificationUnreadCountQueryDto, { category: 'badges' });
|
||||
|
||||
const errors = await validate(dto);
|
||||
|
||||
expect(errors.length).toBeGreaterThan(0);
|
||||
expect(errors[0].property).toBe('category');
|
||||
});
|
||||
|
||||
it('creates mention notifications with mention type', async () => {
|
||||
const notificationsRepository = {
|
||||
create: jest.fn().mockResolvedValue({ toJSON: () => ({ _id: 'notification-1' }) }),
|
||||
@@ -107,6 +119,139 @@ describe('NotificationsService', () => {
|
||||
expect(notificationsRepository.countUnread).toHaveBeenCalledWith('507f1f77bcf86cd799439011');
|
||||
});
|
||||
|
||||
it('returns total unread count without category for backward compatibility', async () => {
|
||||
const notificationsRepository = {
|
||||
countUnread: jest.fn().mockResolvedValue(10),
|
||||
};
|
||||
const notificationsGateway = {};
|
||||
|
||||
const service = new NotificationsService(
|
||||
notificationsRepository as any,
|
||||
notificationsGateway as any,
|
||||
);
|
||||
|
||||
await expect(service.getUnreadCountByCategory('507f1f77bcf86cd799439011')).resolves.toEqual({
|
||||
unreadCount: 10,
|
||||
});
|
||||
expect(notificationsRepository.countUnread).toHaveBeenCalledWith('507f1f77bcf86cd799439011');
|
||||
});
|
||||
|
||||
it('returns unread count for interactions category only', async () => {
|
||||
const notificationsRepository = {
|
||||
countUnreadByFilter: jest.fn().mockResolvedValue(4),
|
||||
};
|
||||
const notificationsGateway = {};
|
||||
|
||||
const service = new NotificationsService(
|
||||
notificationsRepository as any,
|
||||
notificationsGateway as any,
|
||||
);
|
||||
|
||||
await expect(
|
||||
service.getUnreadCountByCategory('507f1f77bcf86cd799439011', 'interactions'),
|
||||
).resolves.toEqual({
|
||||
unreadCount: 4,
|
||||
category: 'interactions',
|
||||
});
|
||||
expect(notificationsRepository.countUnreadByFilter).toHaveBeenCalledWith(
|
||||
'507f1f77bcf86cd799439011',
|
||||
{
|
||||
type: {
|
||||
$in: [
|
||||
'like',
|
||||
'comment',
|
||||
'reply',
|
||||
'mention',
|
||||
'save',
|
||||
'share',
|
||||
'collaboration_request',
|
||||
'system',
|
||||
],
|
||||
},
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
it('returns unread count for messages category only', async () => {
|
||||
const notificationsRepository = {
|
||||
countUnreadByFilter: jest.fn().mockResolvedValue(3),
|
||||
};
|
||||
const notificationsGateway = {};
|
||||
|
||||
const service = new NotificationsService(
|
||||
notificationsRepository as any,
|
||||
notificationsGateway as any,
|
||||
);
|
||||
|
||||
await expect(
|
||||
service.getUnreadCountByCategory('507f1f77bcf86cd799439011', 'messages'),
|
||||
).resolves.toEqual({
|
||||
unreadCount: 3,
|
||||
category: 'messages',
|
||||
});
|
||||
expect(notificationsRepository.countUnreadByFilter).toHaveBeenCalledWith(
|
||||
'507f1f77bcf86cd799439011',
|
||||
{ type: { $in: ['message'] } },
|
||||
);
|
||||
});
|
||||
|
||||
it('returns unread count for follow requests category only', async () => {
|
||||
const notificationsRepository = {
|
||||
countUnreadByFilter: jest.fn().mockResolvedValue(2),
|
||||
};
|
||||
const notificationsGateway = {};
|
||||
|
||||
const service = new NotificationsService(
|
||||
notificationsRepository as any,
|
||||
notificationsGateway as any,
|
||||
);
|
||||
|
||||
await expect(
|
||||
service.getUnreadCountByCategory('507f1f77bcf86cd799439011', 'follow_requests'),
|
||||
).resolves.toEqual({
|
||||
unreadCount: 2,
|
||||
category: 'follow_requests',
|
||||
});
|
||||
expect(notificationsRepository.countUnreadByFilter).toHaveBeenCalledWith(
|
||||
'507f1f77bcf86cd799439011',
|
||||
{
|
||||
type: {
|
||||
$in: ['follow_request', 'follow_request_approved', 'follow_request_rejected'],
|
||||
},
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
it('returns grouped unread counts for badge hydration', async () => {
|
||||
const notificationsRepository = {
|
||||
countUnread: jest.fn().mockResolvedValue(10),
|
||||
countUnreadByFilter: jest
|
||||
.fn()
|
||||
.mockResolvedValueOnce(4)
|
||||
.mockResolvedValueOnce(3)
|
||||
.mockResolvedValueOnce(1)
|
||||
.mockResolvedValueOnce(2)
|
||||
.mockResolvedValueOnce(1)
|
||||
.mockResolvedValueOnce(0),
|
||||
};
|
||||
const notificationsGateway = {};
|
||||
|
||||
const service = new NotificationsService(
|
||||
notificationsRepository as any,
|
||||
notificationsGateway as any,
|
||||
);
|
||||
|
||||
await expect(service.getUnreadCounts('507f1f77bcf86cd799439011')).resolves.toEqual({
|
||||
total: 10,
|
||||
interactions: 4,
|
||||
messages: 3,
|
||||
follows: 1,
|
||||
followRequests: 2,
|
||||
collaboration: 1,
|
||||
system: 0,
|
||||
});
|
||||
});
|
||||
|
||||
it('filters notifications by interactions category', async () => {
|
||||
const notificationsRepository = {
|
||||
findMine: jest.fn().mockResolvedValue([]),
|
||||
|
||||
@@ -22,6 +22,8 @@ const NOTIFICATION_CATEGORY_TYPES: Record<NotificationCategory, NotificationType
|
||||
messages: ['message'],
|
||||
follows: ['follow'],
|
||||
follow_requests: ['follow_request', 'follow_request_approved', 'follow_request_rejected'],
|
||||
collaboration: ['collaboration_request'],
|
||||
system: ['system'],
|
||||
};
|
||||
|
||||
@Injectable()
|
||||
@@ -244,6 +246,49 @@ export class NotificationsService {
|
||||
};
|
||||
}
|
||||
|
||||
async getUnreadCountByCategory(recipientId: string, category?: NotificationCategory) {
|
||||
if (!category) {
|
||||
return this.getUnreadCount(recipientId);
|
||||
}
|
||||
|
||||
return {
|
||||
unreadCount: await this.notificationsRepository.countUnreadByFilter(recipientId, {
|
||||
type: { $in: NOTIFICATION_CATEGORY_TYPES[category] },
|
||||
}),
|
||||
category,
|
||||
};
|
||||
}
|
||||
|
||||
async getUnreadCounts(recipientId: string) {
|
||||
const [
|
||||
total,
|
||||
interactions,
|
||||
messages,
|
||||
follows,
|
||||
followRequests,
|
||||
collaboration,
|
||||
system,
|
||||
] = await Promise.all([
|
||||
this.notificationsRepository.countUnread(recipientId),
|
||||
this.countUnreadForCategory(recipientId, 'interactions'),
|
||||
this.countUnreadForCategory(recipientId, 'messages'),
|
||||
this.countUnreadForCategory(recipientId, 'follows'),
|
||||
this.countUnreadForCategory(recipientId, 'follow_requests'),
|
||||
this.countUnreadForCategory(recipientId, 'collaboration'),
|
||||
this.countUnreadForCategory(recipientId, 'system'),
|
||||
]);
|
||||
|
||||
return {
|
||||
total,
|
||||
interactions,
|
||||
messages,
|
||||
follows,
|
||||
followRequests,
|
||||
collaboration,
|
||||
system,
|
||||
};
|
||||
}
|
||||
|
||||
async markRead(recipientId: string, notificationId: string) {
|
||||
if (!Types.ObjectId.isValid(notificationId)) {
|
||||
throw new NotFoundException('Notification not found');
|
||||
@@ -376,4 +421,10 @@ export class NotificationsService {
|
||||
filter.type = { $in: NOTIFICATION_CATEGORY_TYPES[query.category] };
|
||||
}
|
||||
}
|
||||
|
||||
private countUnreadForCategory(recipientId: string, category: NotificationCategory) {
|
||||
return this.notificationsRepository.countUnreadByFilter(recipientId, {
|
||||
type: { $in: NOTIFICATION_CATEGORY_TYPES[category] },
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user