Skip to content

Commit 70597ed

Browse files
authored
Merge pull request #160 from cuappdev/Kai/mark-notification-read
Kai/mark notification read
2 parents dec5312 + a5d83c2 commit 70597ed

3 files changed

Lines changed: 100 additions & 58 deletions

File tree

src/api/controllers/NotifController.ts

Lines changed: 41 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -17,56 +17,54 @@ import { UserModel } from "../../models/UserModel";
1717

1818
@JsonController("notif/")
1919
export class NotifController {
20-
private notifService: NotifService;
20+
private notifService: NotifService;
2121

22-
constructor(notifService: NotifService) {
23-
this.notifService = notifService;
24-
}
22+
constructor(notifService: NotifService) {
23+
this.notifService = notifService;
24+
}
2525

26-
@Get("recent")
27-
async getRecentNotifications(@CurrentUser() user: UserModel) {
28-
return this.notifService.getRecentNotifications(user.firebaseUid);
29-
}
26+
@Get('recent')
27+
async getRecentNotifications(@CurrentUser() user: UserModel) {
28+
return this.notifService.getRecentNotifications(user.firebaseUid);
29+
}
3030

31-
@Get("new")
32-
async getUnread(@CurrentUser() user: UserModel) {
33-
return this.notifService.getUnreadNotifications(user.firebaseUid);
34-
}
31+
@Get('new')
32+
async getUnread(@CurrentUser() user: UserModel) {
33+
return this.notifService.getUnreadNotifications(user.firebaseUid);
34+
}
3535

36-
@Get("last7days")
37-
getLast7Days(@CurrentUser() user: UserModel) {
38-
return this.notifService.getNotificationsLast7Days(user.firebaseUid);
39-
}
36+
@Get('last7days')
37+
getLast7Days(@CurrentUser() user: UserModel) {
38+
return this.notifService.getNotificationsLast7Days(user.firebaseUid);
39+
}
4040

41-
@Get("last30days")
42-
getLast30Days(@CurrentUser() user: UserModel) {
43-
return this.notifService.getNotificationsLast30Days(user.firebaseUid);
44-
}
41+
@Get('last30days')
42+
getLast30Days(@CurrentUser() user: UserModel) {
43+
return this.notifService.getNotificationsLast30Days(user.firebaseUid);
44+
}
4545

46-
@Post()
47-
async sendNotif(@Body() findTokensRequest: FindTokensRequest) {
48-
return this.notifService.sendNotifs(findTokensRequest);
49-
}
46+
@Post()
47+
async sendNotif(@Body() findTokensRequest: FindTokensRequest) {
48+
return this.notifService.sendNotifs(findTokensRequest);
49+
}
5050

51-
@Post("discount")
52-
async sendDiscountNotif(
53-
@Body() discountRequest: DiscountNotificationRequest,
54-
) {
55-
return this.notifService.sendDiscountNotification(discountRequest);
56-
}
51+
@Post('discount')
52+
async sendDiscountNotif(@Body() discountRequest: DiscountNotificationRequest) {
53+
return this.notifService.sendDiscountNotification(discountRequest);
54+
}
5755

58-
@Post("request-match")
59-
async sendRequestMatchNotif(
60-
@Body() matchRequest: RequestMatchNotificationRequest,
61-
) {
62-
return this.notifService.sendRequestMatchNotification(matchRequest);
63-
}
56+
@Post('request-match')
57+
async sendRequestMatchNotif(@Body() matchRequest: RequestMatchNotificationRequest) {
58+
return this.notifService.sendRequestMatchNotification(matchRequest);
59+
}
6460

65-
@Delete("id/:id")
66-
async deleteNotification(
67-
@CurrentUser() user: UserModel,
68-
@Params() params: { id: string },
69-
) {
70-
return this.notifService.deleteNotification(user.firebaseUid, params.id);
71-
}
61+
@Post('markAsRead/id/:id')
62+
async markAsRead(@CurrentUser() user: UserModel, @Params() params: { id: string }) {
63+
return this.notifService.markAsRead(user.firebaseUid, params.id);
64+
}
65+
66+
@Delete('id/:id')
67+
async deleteNotification(@CurrentUser() user: UserModel, @Params() params: { id: string }) {
68+
return this.notifService.deleteNotification(user.firebaseUid, params.id);
69+
}
7270
}

src/services/NotifService.ts

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -291,19 +291,31 @@ export class NotifService {
291291
});
292292
}
293293

294-
async deleteNotification(userId: string, notifId: string) {
295-
return this.transactions.readWrite(async (transactionalEntityManager) => {
296-
const notifRepository = Repositories.notification(
297-
transactionalEntityManager,
298-
);
299-
const notif = await notifRepository.findOne({ where: { id: notifId } });
300-
if (!notif) {
301-
throw new NotFoundError("Notification not found");
302-
}
303-
if (notif.userId !== userId) {
304-
throw new NotFoundError("Notification not found");
305-
}
306-
return await notifRepository.deleteNotification(notif);
307-
});
308-
}
294+
async markAsRead(userId: string, notifId: string) {
295+
return this.transactions.readWrite(async (transactionalEntityManager) => {
296+
const notifRepository = Repositories.notification(transactionalEntityManager);
297+
const notif = await notifRepository.findOne({ where: { id: notifId } });
298+
if (!notif) {
299+
throw new NotFoundError("Notification not found");
300+
}
301+
if (notif.userId !== userId) {
302+
throw new NotFoundError("Notification not found");
303+
}
304+
return await notifRepository.markAsRead(notifId);
305+
});
306+
}
307+
308+
async deleteNotification(userId: string, notifId: string) {
309+
return this.transactions.readWrite(async (transactionalEntityManager) => {
310+
const notifRepository = Repositories.notification(transactionalEntityManager);
311+
const notif = await notifRepository.findOne({ where: { id: notifId } });
312+
if (!notif) {
313+
throw new NotFoundError("Notification not found");
314+
}
315+
if (notif.userId !== userId) {
316+
throw new NotFoundError("Notification not found");
317+
}
318+
return await notifRepository.deleteNotification(notif);
319+
});
320+
}
309321
}

swagger.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1474,6 +1474,38 @@
14741474
}
14751475
}
14761476
},
1477+
"/notif/markAsRead/id/{id}": {
1478+
"post": {
1479+
"summary": "Mark notification as read",
1480+
"tags": ["Notification"],
1481+
"security": [{ "bearerAuth": [] }],
1482+
"parameters": [
1483+
{
1484+
"in": "path",
1485+
"name": "id",
1486+
"required": true,
1487+
"schema": {
1488+
"type": "string",
1489+
"format": "uuid"
1490+
},
1491+
"description": "Notification ID"
1492+
}
1493+
],
1494+
"responses": {
1495+
"200": {
1496+
"description": "Notification marked as read successfully",
1497+
"content": {
1498+
"application/json": {
1499+
"schema": { "$ref": "#/components/schemas/Notification" }
1500+
}
1501+
}
1502+
},
1503+
"404": {
1504+
"description": "Notification not found"
1505+
}
1506+
}
1507+
}
1508+
},
14771509
"/request": {
14781510
"get": {
14791511
"tags": ["Request"],

0 commit comments

Comments
 (0)