This commit is contained in:
@@ -8,6 +8,8 @@ import {
|
||||
PrimaryColumn, // ✅ [Fix] เพิ่ม Import นี้
|
||||
} from 'typeorm';
|
||||
import { User } from '../../user/entities/user.entity';
|
||||
import { UuidBaseEntity } from '../../../common/entities/uuid-base.entity';
|
||||
import { Exclude } from 'class-transformer';
|
||||
|
||||
export enum NotificationType {
|
||||
EMAIL = 'EMAIL',
|
||||
@@ -16,8 +18,9 @@ export enum NotificationType {
|
||||
}
|
||||
|
||||
@Entity('notifications')
|
||||
export class Notification {
|
||||
export class Notification extends UuidBaseEntity {
|
||||
@PrimaryGeneratedColumn()
|
||||
@Exclude()
|
||||
id!: number;
|
||||
|
||||
@Column({ name: 'user_id' })
|
||||
|
||||
@@ -1,12 +1,4 @@
|
||||
import {
|
||||
Controller,
|
||||
Get,
|
||||
Put,
|
||||
Param,
|
||||
UseGuards,
|
||||
ParseIntPipe,
|
||||
Query,
|
||||
} from '@nestjs/common';
|
||||
import { Controller, Get, Put, Param, UseGuards, Query } from '@nestjs/common';
|
||||
import { ApiTags, ApiOperation, ApiBearerAuth } from '@nestjs/swagger';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
@@ -17,6 +9,7 @@ import { JwtAuthGuard } from '../../common/guards/jwt-auth.guard';
|
||||
import { CurrentUser } from '../../common/decorators/current-user.decorator';
|
||||
import { User } from '../user/entities/user.entity';
|
||||
import { SearchNotificationDto } from './dto/search-notification.dto'; // ✅ Import
|
||||
import { ParseUuidPipe } from '../../common/pipes/parse-uuid.pipe';
|
||||
|
||||
@ApiTags('Notifications')
|
||||
@ApiBearerAuth()
|
||||
@@ -26,14 +19,14 @@ export class NotificationController {
|
||||
constructor(
|
||||
private readonly notificationService: NotificationService,
|
||||
@InjectRepository(Notification)
|
||||
private notificationRepo: Repository<Notification>,
|
||||
private notificationRepo: Repository<Notification>
|
||||
) {}
|
||||
|
||||
@Get()
|
||||
@ApiOperation({ summary: 'Get my notifications' })
|
||||
async getMyNotifications(
|
||||
@CurrentUser() user: User,
|
||||
@Query() searchDto: SearchNotificationDto, // ✅ ใช้ DTO แทน
|
||||
@Query() searchDto: SearchNotificationDto // ✅ ใช้ DTO แทน
|
||||
) {
|
||||
const { page = 1, limit = 20, isRead } = searchDto;
|
||||
|
||||
@@ -65,13 +58,13 @@ export class NotificationController {
|
||||
return { unreadCount: count };
|
||||
}
|
||||
|
||||
@Put(':id/read')
|
||||
@Put(':uuid/read')
|
||||
@ApiOperation({ summary: 'Mark notification as read' })
|
||||
async markAsRead(
|
||||
@Param('id', ParseIntPipe) id: number,
|
||||
@CurrentUser() user: User,
|
||||
@Param('uuid', ParseUuidPipe) uuid: string,
|
||||
@CurrentUser() user: User
|
||||
) {
|
||||
return this.notificationService.markAsRead(id, user.user_id);
|
||||
return this.notificationService.markAsReadByUuid(uuid, user.user_id);
|
||||
}
|
||||
|
||||
@Put('read-all')
|
||||
|
||||
@@ -39,7 +39,7 @@ export class NotificationService {
|
||||
@InjectRepository(User)
|
||||
private userRepo: Repository<User>,
|
||||
// ไม่ต้อง Inject UserPrefRepo แล้ว เพราะ Processor จะจัดการเอง
|
||||
private notificationGateway: NotificationGateway,
|
||||
private notificationGateway: NotificationGateway
|
||||
) {}
|
||||
|
||||
/**
|
||||
@@ -84,14 +84,14 @@ export class NotificationService {
|
||||
delay: 5000,
|
||||
},
|
||||
removeOnComplete: true,
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
this.logger.debug(`Dispatched notification job for user ${data.userId}`);
|
||||
} catch (error) {
|
||||
this.logger.error(
|
||||
`Failed to process notification for user ${data.userId}`,
|
||||
(error as Error).stack,
|
||||
(error as Error).stack
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -154,10 +154,25 @@ export class NotificationService {
|
||||
}
|
||||
}
|
||||
|
||||
async markAsReadByUuid(uuid: string, userId: number): Promise<void> {
|
||||
const notification = await this.notificationRepo.findOne({
|
||||
where: { uuid, userId },
|
||||
});
|
||||
|
||||
if (!notification) {
|
||||
throw new NotFoundException(`Notification UUID ${uuid} not found`);
|
||||
}
|
||||
|
||||
if (!notification.isRead) {
|
||||
notification.isRead = true;
|
||||
await this.notificationRepo.save(notification);
|
||||
}
|
||||
}
|
||||
|
||||
async markAllAsRead(userId: number): Promise<void> {
|
||||
await this.notificationRepo.update(
|
||||
{ userId, isRead: false },
|
||||
{ isRead: true },
|
||||
{ isRead: true }
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user