Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 27f93f7

Browse files
creating the unread notification use case and the test
1 parent 7aac7f3 commit 27f93f7

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { makeNotification } from '@test/factories/notification-factory';
2+
import { InMemoryNotificationsRepository } from '../../../test/repositories/in-memory-notifications-repository';
3+
import { NotificationNotFound } from './errors/notification-not-found';
4+
import { UnreadNotification } from './unread-notification';
5+
6+
describe('Unread notification', () => {
7+
it('should be able to unread a notification', async () => {
8+
const notificationsRepository = new InMemoryNotificationsRepository();
9+
const unreadNotification = new UnreadNotification(notificationsRepository);
10+
11+
// para eu conseguir marcar uma notificação como não lida, ela precisa estar
12+
// como lida antes
13+
const notification = makeNotification({
14+
readAt: new Date(),
15+
});
16+
17+
await notificationsRepository.create(notification);
18+
19+
await unreadNotification.execute({
20+
notificationId: notification.id,
21+
});
22+
23+
expect(notificationsRepository.notifications[0].readAt).toBeNull();
24+
});
25+
26+
it('should not be able to unread a non existing notification', async () => {
27+
const notificationsRepository = new InMemoryNotificationsRepository();
28+
const unreadNotification = new UnreadNotification(notificationsRepository);
29+
30+
expect(() => {
31+
return unreadNotification.execute({
32+
notificationId: 'fake-notification-id',
33+
}); // por conta de ser uma Promise, o rejects quer dizer que deu erro
34+
}).rejects.toThrow(NotificationNotFound);
35+
});
36+
});
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { Injectable } from '@nestjs/common';
2+
3+
import { NotificationsRepository } from '../repositories/notifications-repository';
4+
import { NotificationNotFound } from './errors/notification-not-found';
5+
6+
interface UnreadNotificationRequest {
7+
notificationId: string;
8+
}
9+
10+
type UnreadNotificationResponse = void;
11+
12+
@Injectable()
13+
export class UnreadNotification {
14+
constructor(private notificationsRepository: NotificationsRepository) {}
15+
16+
async execute(
17+
request: UnreadNotificationRequest,
18+
): Promise<UnreadNotificationResponse> {
19+
const { notificationId } = request;
20+
21+
const notification = await this.notificationsRepository.findById(
22+
notificationId,
23+
);
24+
25+
if (!notification) {
26+
throw new NotificationNotFound();
27+
}
28+
29+
notification.unread();
30+
31+
await this.notificationsRepository.save(notification);
32+
}
33+
}

0 commit comments

Comments
 (0)