|
1 | 1 | import { Injectable } from '@nestjs/common';
|
| 2 | +import { NotificationType } from 'src/graphql.types'; |
2 | 3 | import { PrismaService } from 'prisma/prisma.service';
|
3 | 4 | import { decode } from 'src/constants/decode';
|
| 5 | +import { NotificationOutput } from 'src/graphql.types'; |
| 6 | +import { HttpException, HttpStatus } from '@nestjs/common'; |
4 | 7 |
|
5 | 8 | @Injectable()
|
6 | 9 | export class NotificationService {
|
7 | 10 | constructor(private prisma: PrismaService) {}
|
8 | 11 |
|
9 | 12 | async sendNotification(
|
10 |
| - userId: string, |
| 13 | + toUser: string, |
11 | 14 | description: string,
|
12 |
| - type: 'FOLLOWING', |
13 |
| - ) { |
14 |
| - await this.prisma.user.update({ |
15 |
| - where: { id: userId }, |
16 |
| - data: { |
17 |
| - notifications: { |
18 |
| - create: { |
19 |
| - description, |
20 |
| - notificationType: type, |
| 15 | + type: NotificationType, |
| 16 | + fromUser?: string, |
| 17 | + ): Promise<void> { |
| 18 | + try { |
| 19 | + await this.prisma.notification.deleteMany({ |
| 20 | + where: { |
| 21 | + userId: toUser, |
| 22 | + notificationType: type, |
| 23 | + otherUserId: fromUser, |
| 24 | + }, |
| 25 | + }); |
| 26 | + } catch { |
| 27 | + throw new HttpException( |
| 28 | + 'Error occurred while deleting previous notifications', |
| 29 | + HttpStatus.INTERNAL_SERVER_ERROR, |
| 30 | + ); |
| 31 | + } |
| 32 | + try { |
| 33 | + await this.prisma.notification.create({ |
| 34 | + data: { |
| 35 | + description, |
| 36 | + notificationType: type, |
| 37 | + User: { |
| 38 | + connect: { |
| 39 | + id: toUser, |
| 40 | + }, |
| 41 | + }, |
| 42 | + OtherUser: { |
| 43 | + connect: { |
| 44 | + id: fromUser, |
| 45 | + }, |
21 | 46 | },
|
22 | 47 | },
|
23 |
| - }, |
24 |
| - }); |
| 48 | + }); |
| 49 | + } catch { |
| 50 | + throw new HttpException( |
| 51 | + 'Error occurred while sending notification', |
| 52 | + HttpStatus.INTERNAL_SERVER_ERROR, |
| 53 | + ); |
| 54 | + } |
25 | 55 | }
|
26 | 56 |
|
27 | 57 | async seeNotification(notificationId: string, token: string) {
|
28 | 58 | await decode(token, this.prisma);
|
29 |
| - const notification = await this.prisma.notification.update({ |
30 |
| - where: { |
31 |
| - id: notificationId, |
32 |
| - }, |
33 |
| - data: { |
34 |
| - seenStatus: true, |
35 |
| - seenAt: new Date(), |
36 |
| - }, |
37 |
| - }); |
38 |
| - return notification; |
| 59 | + try { |
| 60 | + const notification = await this.prisma.notification.update({ |
| 61 | + where: { |
| 62 | + id: notificationId, |
| 63 | + }, |
| 64 | + data: { |
| 65 | + seenStatus: true, |
| 66 | + seenAt: new Date(), |
| 67 | + }, |
| 68 | + }); |
| 69 | + return notification; |
| 70 | + } catch { |
| 71 | + throw new HttpException( |
| 72 | + 'Error occurred while marking notification as seen', |
| 73 | + HttpStatus.INTERNAL_SERVER_ERROR, |
| 74 | + ); |
| 75 | + } |
39 | 76 | }
|
40 | 77 |
|
41 | 78 | async seeNotifications(notificationIds: string[], token: string) {
|
42 | 79 | await decode(token, this.prisma);
|
43 |
| - const notifications = await this.prisma.notification.updateMany({ |
44 |
| - where: { |
45 |
| - id: { |
46 |
| - in: notificationIds, |
| 80 | + try { |
| 81 | + const notifications = await this.prisma.notification.updateMany({ |
| 82 | + where: { |
| 83 | + id: { |
| 84 | + in: notificationIds, |
| 85 | + }, |
| 86 | + }, |
| 87 | + data: { |
| 88 | + seenStatus: true, |
| 89 | + seenAt: new Date(), |
47 | 90 | },
|
48 |
| - }, |
49 |
| - data: { |
50 |
| - seenStatus: true, |
51 |
| - seenAt: new Date(), |
52 |
| - }, |
53 |
| - }); |
54 |
| - return notifications; |
| 91 | + }); |
| 92 | + return notifications; |
| 93 | + } catch { |
| 94 | + throw new HttpException( |
| 95 | + 'Error occurred while marking notifications as seen', |
| 96 | + HttpStatus.INTERNAL_SERVER_ERROR, |
| 97 | + ); |
| 98 | + } |
55 | 99 | }
|
56 | 100 |
|
57 |
| - async notifications(token: string) { |
| 101 | + async notifications(token: string): Promise<NotificationOutput> { |
58 | 102 | const user = await decode(token, this.prisma);
|
59 |
| - const notifs = await this.prisma.user.findUnique({ |
60 |
| - where: { |
61 |
| - id: user.id, |
62 |
| - }, |
63 |
| - select: { |
64 |
| - notifications: { |
65 |
| - orderBy: { |
66 |
| - createdAt: 'desc', |
67 |
| - }, |
| 103 | + this.deleteSeenPreviousNotifications(); |
| 104 | + try { |
| 105 | + const notifs = await this.prisma.user.findUnique({ |
| 106 | + where: { |
| 107 | + id: user.id, |
68 | 108 | },
|
69 |
| - }, |
70 |
| - }); |
71 |
| - const unseenNotifs = await this.prisma.user.count({ |
72 |
| - where: { |
73 |
| - id: user.id, |
74 |
| - notifications: { |
75 |
| - some: { |
76 |
| - seenStatus: false, |
| 109 | + select: { |
| 110 | + notifications: { |
| 111 | + orderBy: { |
| 112 | + createdAt: 'desc', |
| 113 | + }, |
| 114 | + select: { |
| 115 | + id: true, |
| 116 | + description: true, |
| 117 | + notificationType: true, |
| 118 | + seenStatus: true, |
| 119 | + createdAt: true, |
| 120 | + OtherUser: { |
| 121 | + select: { |
| 122 | + id: true, |
| 123 | + name: true, |
| 124 | + profilePicture: true, |
| 125 | + }, |
| 126 | + }, |
| 127 | + User: { |
| 128 | + select: { |
| 129 | + id: true, |
| 130 | + name: true, |
| 131 | + profilePicture: true, |
| 132 | + }, |
| 133 | + }, |
| 134 | + }, |
77 | 135 | },
|
78 | 136 | },
|
79 |
| - }, |
80 |
| - }); |
81 |
| - return { |
82 |
| - notifications: notifs?.notifications, |
83 |
| - unseenNotifications: unseenNotifs, |
84 |
| - }; |
| 137 | + }); |
| 138 | + let unseenNotifications = 0; |
| 139 | + for (const notif of notifs?.notifications) { |
| 140 | + if (notif.seenStatus === false) unseenNotifications++; |
| 141 | + } |
| 142 | + return { |
| 143 | + unseenNotifications: unseenNotifications, |
| 144 | + notifications: |
| 145 | + notifs?.notifications as NotificationOutput['notifications'], |
| 146 | + }; |
| 147 | + } catch { |
| 148 | + throw new HttpException( |
| 149 | + 'Error occurred while fetching notifications', |
| 150 | + HttpStatus.INTERNAL_SERVER_ERROR, |
| 151 | + ); |
| 152 | + } |
85 | 153 | }
|
86 | 154 |
|
87 |
| - /** |
88 |
| - * Delete all notifications that were seen 48 hours ago |
89 |
| - */ |
90 | 155 | async deleteSeenPreviousNotifications() {
|
91 |
| - await this.prisma.notification.deleteMany({ |
92 |
| - where: { |
93 |
| - seenStatus: true, |
94 |
| - seenAt: { |
95 |
| - lt: new Date(new Date().getTime() - 48 * 60 * 60 * 1000), |
| 156 | + try { |
| 157 | + await this.prisma.notification.deleteMany({ |
| 158 | + where: { |
| 159 | + seenStatus: true, |
| 160 | + seenAt: { |
| 161 | + lt: new Date(new Date().getTime() - 24 * 60 * 60 * 1000), |
| 162 | + }, |
96 | 163 | },
|
97 |
| - }, |
98 |
| - }); |
| 164 | + }); |
| 165 | + } catch (err) { |
| 166 | + throw new HttpException( |
| 167 | + 'Error occurred while deleting previous notifications', |
| 168 | + HttpStatus.INTERNAL_SERVER_ERROR, |
| 169 | + ); |
| 170 | + } |
99 | 171 | }
|
100 | 172 | }
|
0 commit comments