38 lines
1.3 KiB
TypeScript
38 lines
1.3 KiB
TypeScript
|
|
import { Injectable, NotFoundException, Logger } from '@nestjs/common';
|
||
|
|
import { NotificationsRepository } from './notifications.repository';
|
||
|
|
import { RedisService } from '../../infrastructure/redis/redis.service';
|
||
|
|
import { QueueService } from '../../infrastructure/queue/queue.service';
|
||
|
|
|
||
|
|
@Injectable()
|
||
|
|
export class NotificationsService {
|
||
|
|
private readonly logger = new Logger(NotificationsService.name);
|
||
|
|
|
||
|
|
constructor(
|
||
|
|
private readonly repository: NotificationsRepository,
|
||
|
|
private readonly redis: RedisService,
|
||
|
|
private readonly queue: QueueService,
|
||
|
|
) {}
|
||
|
|
|
||
|
|
async list() {
|
||
|
|
return this.repository.findAll();
|
||
|
|
}
|
||
|
|
|
||
|
|
async markRead(id: string) {
|
||
|
|
const notification = await this.repository.markRead(id);
|
||
|
|
if (!notification) throw new NotFoundException(`Notification ${id} not found`);
|
||
|
|
return notification;
|
||
|
|
}
|
||
|
|
|
||
|
|
async send(data: { userId: string; type: string; title: string; body: string }) {
|
||
|
|
const notification = await this.repository.create(data);
|
||
|
|
this.queue.add('notification', { notificationId: notification.id, ...data });
|
||
|
|
this.redis.set(
|
||
|
|
`session:notifications:${data.userId}:last_sent`,
|
||
|
|
new Date().toISOString(),
|
||
|
|
86400,
|
||
|
|
);
|
||
|
|
this.logger.log(`Notification ${notification.id} sent to user ${data.userId}`);
|
||
|
|
return notification;
|
||
|
|
}
|
||
|
|
}
|