Files
gridpilot.gg/packages/notifications/application/use-cases/GetUnreadNotificationsQuery.ts
2025-12-09 22:22:06 +01:00

89 lines
2.5 KiB
TypeScript

/**
* Application Query: GetUnreadNotificationsQuery
*
* Retrieves unread notifications for a recipient.
*/
import type { Notification } from '../../domain/entities/Notification';
import type { INotificationRepository } from '../../domain/repositories/INotificationRepository';
export interface UnreadNotificationsResult {
notifications: Notification[];
totalCount: number;
}
export class GetUnreadNotificationsQuery {
constructor(
private readonly notificationRepository: INotificationRepository,
) {}
async execute(recipientId: string): Promise<UnreadNotificationsResult> {
const notifications = await this.notificationRepository.findUnreadByRecipientId(recipientId);
return {
notifications,
totalCount: notifications.length,
};
}
}
/**
* Application Query: GetNotificationsQuery
*
* Retrieves all notifications for a recipient with optional filtering.
*/
export interface GetNotificationsOptions {
includeRead?: boolean;
includeDismissed?: boolean;
limit?: number;
offset?: number;
}
export class GetNotificationsQuery {
constructor(
private readonly notificationRepository: INotificationRepository,
) {}
async execute(recipientId: string, options: GetNotificationsOptions = {}): Promise<Notification[]> {
const allNotifications = await this.notificationRepository.findByRecipientId(recipientId);
let filtered = allNotifications;
// Filter by status
if (!options.includeRead && !options.includeDismissed) {
filtered = filtered.filter(n => n.isUnread());
} else if (!options.includeDismissed) {
filtered = filtered.filter(n => !n.isDismissed());
} else if (!options.includeRead) {
filtered = filtered.filter(n => n.isUnread() || n.isDismissed());
}
// Sort by creation date (newest first)
filtered.sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime());
// Apply pagination
if (options.offset !== undefined) {
filtered = filtered.slice(options.offset);
}
if (options.limit !== undefined) {
filtered = filtered.slice(0, options.limit);
}
return filtered;
}
}
/**
* Application Query: GetUnreadCountQuery
*
* Gets the count of unread notifications for a recipient.
*/
export class GetUnreadCountQuery {
constructor(
private readonly notificationRepository: INotificationRepository,
) {}
async execute(recipientId: string): Promise<number> {
return this.notificationRepository.countUnreadByRecipientId(recipientId);
}
}