All files / src/application/notification mark-notification-as-read.use-case.ts

100% Statements 7/7
100% Branches 4/4
100% Functions 2/2
100% Lines 7/7

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26                    25x     6x   6x 4x     2x 2x   2x      
import { Notification } from '../../domain/notification/notification';
import { NotificationRepository } from './notification-repository';
import { NotificationNotFoundError } from './errors/notification-not-found.error';
 
export interface MarkNotificationAsReadInput {
  notificationId: string;
  requestingUserId: string;
}
 
export class MarkNotificationAsReadUseCase {
  constructor(private readonly notificationRepository: NotificationRepository) {}
 
  async execute(input: MarkNotificationAsReadInput): Promise<Notification> {
    const notification = await this.notificationRepository.findById(input.notificationId);
 
    if (!notification || notification.userId !== input.requestingUserId) {
      throw new NotificationNotFoundError();
    }
 
    const readNotification = notification.markAsRead();
    await this.notificationRepository.save(readNotification);
 
    return readNotification;
  }
}