File size: 3,944 Bytes
639bb77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import {
  Controller,
  Get,
  Post,
  Delete,
  Body,
  Param,
  Query,
  UseGuards,
  Request,
  HttpCode,
  HttpStatus,
} from '@nestjs/common';
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
import { NotificationsService } from './notifications.service';
import { JwtAuthGuard } from '../../common/guards/jwt-auth.guard';
import { QueryNotificationsDto, MarkAsReadDto } from './dto';

interface RequestWithUser extends Request {
  user: {
    id: string;
    email: string;
  };
}

@ApiTags('Notifications')
@UseGuards(JwtAuthGuard)
@ApiBearerAuth()
@Controller('notifications')
export class NotificationsController {
  constructor(private readonly notificationsService: NotificationsService) {}

  /**
   * GET /notifications
   * Get user's notifications
   */
  @Get()
  @HttpCode(HttpStatus.OK)
  async getNotifications(
    @Query() query: QueryNotificationsDto,
    @Request() req: RequestWithUser,
  ): Promise<any> {
    const result = await this.notificationsService.getNotifications(req.user.id, query);
    return {
      success: true,
      data: result.notifications,
      meta: {
        total: result.total,
        unreadCount: result.unreadCount,
        page: query.page || 1,
        limit: query.limit || 20,
      },
    };
  }

  /**
   * GET /notifications/unread-count
   * Get unread notification count
   */
  @Get('unread-count')
  @HttpCode(HttpStatus.OK)
  async getUnreadCount(@Request() req: RequestWithUser): Promise<any> {
    const count = await this.notificationsService.getUnreadCount(req.user.id);
    return {
      success: true,
      data: { unreadCount: count },
    };
  }

  /**
   * GET /notifications/:notificationId
   * Get single notification
   */
  @Get(':notificationId')
  @HttpCode(HttpStatus.OK)
  async getNotification(
    @Param('notificationId') notificationId: string,
    @Request() req: RequestWithUser,
  ): Promise<any> {
    const notification = await this.notificationsService.getNotificationById(
      notificationId,
      req.user.id,
    );
    return {
      success: true,
      data: notification,
    };
  }

  /**
   * POST /notifications/read
   * Mark notifications as read
   */
  @Post('read')
  @HttpCode(HttpStatus.OK)
  async markAsRead(
    @Body() dto: MarkAsReadDto,
    @Request() req: RequestWithUser,
  ): Promise<any> {
    const result = await this.notificationsService.markAsRead(req.user.id, dto);
    return {
      success: true,
      message: `${result.markedCount} notification(s) marked as read`,
      data: { markedCount: result.markedCount },
    };
  }

  /**
   * POST /notifications/:notificationId/read
   * Mark single notification as read
   */
  @Post(':notificationId/read')
  @HttpCode(HttpStatus.OK)
  async markOneAsRead(
    @Param('notificationId') notificationId: string,
    @Request() req: RequestWithUser,
  ): Promise<any> {
    await this.notificationsService.markOneAsRead(req.user.id, notificationId);
    return {
      success: true,
      message: 'Notification marked as read',
    };
  }

  /**
   * DELETE /notifications/:notificationId
   * Delete a notification
   */
  @Delete(':notificationId')
  @HttpCode(HttpStatus.OK)
  async deleteNotification(
    @Param('notificationId') notificationId: string,
    @Request() req: RequestWithUser,
  ): Promise<any> {
    await this.notificationsService.deleteNotification(req.user.id, notificationId);
    return {
      success: true,
      message: 'Notification deleted successfully',
    };
  }

  /**
   * DELETE /notifications/read/all
   * Delete all read notifications
   */
  @Delete('read/all')
  @HttpCode(HttpStatus.OK)
  async deleteAllRead(@Request() req: RequestWithUser): Promise<any> {
    const result = await this.notificationsService.deleteAllRead(req.user.id);
    return {
      success: true,
      message: `${result.deletedCount} read notification(s) deleted`,
      data: { deletedCount: result.deletedCount },
    };
  }
}