2026-05-09 18:25:04 +08:00
|
|
|
import { Injectable, NotFoundException, Logger } from '@nestjs/common';
|
|
|
|
|
import { NotificationsRepository } from './notifications.repository';
|
2026-05-17 19:08:07 +08:00
|
|
|
import type { PaginationDto } from '../../common/dto/pagination.dto';
|
2026-05-09 18:25:04 +08:00
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
|
export class NotificationsService {
|
|
|
|
|
private readonly logger = new Logger(NotificationsService.name);
|
|
|
|
|
|
2026-05-17 00:39:46 +08:00
|
|
|
constructor(private readonly repository: NotificationsRepository) {}
|
2026-05-09 18:25:04 +08:00
|
|
|
|
2026-05-17 19:08:07 +08:00
|
|
|
async list(userId: string, pagination: PaginationDto) {
|
|
|
|
|
return this.repository.findAll(userId, pagination);
|
2026-05-09 18:25:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async markRead(id: string) {
|
2026-05-17 00:39:46 +08:00
|
|
|
try {
|
|
|
|
|
return await this.repository.markRead(id);
|
|
|
|
|
} catch {
|
|
|
|
|
throw new NotFoundException(`Notification ${id} not found`);
|
|
|
|
|
}
|
2026-05-09 18:25:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async send(data: { userId: string; type: string; title: string; body: string }) {
|
|
|
|
|
const notification = await this.repository.create(data);
|
|
|
|
|
this.logger.log(`Notification ${notification.id} sent to user ${data.userId}`);
|
|
|
|
|
return notification;
|
|
|
|
|
}
|
|
|
|
|
}
|