From bebde97bdceff9d8eb45c1b0b507b69b7da9a8ad Mon Sep 17 00:00:00 2001 From: Apoorv Mishra Date: Sat, 22 Apr 2023 20:10:46 +0530 Subject: [PATCH] fix: return both total and unseen separately --- .../api/notifications/notifications.test.ts | 38 ++++++++++++------- .../routes/api/notifications/notifications.ts | 16 +++++--- 2 files changed, 35 insertions(+), 19 deletions(-) diff --git a/server/routes/api/notifications/notifications.test.ts b/server/routes/api/notifications/notifications.test.ts index 547fcf93a8..aab763e88b 100644 --- a/server/routes/api/notifications/notifications.test.ts +++ b/server/routes/api/notifications/notifications.test.ts @@ -59,11 +59,15 @@ describe("#notifications.list", () => { const body = await res.json(); expect(res.status).toBe(200); - expect(body.data.length).toBe(3); - expect(body.pagination.total).toBe(2); - expect((randomElement(body.data) as any).actor.id).toBe(actor.id); - expect((randomElement(body.data) as any).userId).toBe(user.id); - const events = body.data.map((n: any) => n.event); + expect(body.data.notifications.length).toBe(3); + expect(body.pagination.total).toBe(3); + expect((randomElement(body.data.notifications) as any).actor.id).toBe( + actor.id + ); + expect((randomElement(body.data.notifications) as any).userId).toBe( + user.id + ); + const events = body.data.notifications.map((n: any) => n.event); expect(events).toContain(NotificationEventType.UpdateDocument); expect(events).toContain(NotificationEventType.CreateComment); expect(events).toContain(NotificationEventType.MentionedInComment); @@ -116,11 +120,15 @@ describe("#notifications.list", () => { const body = await res.json(); expect(res.status).toBe(200); - expect(body.data.length).toBe(1); + expect(body.data.notifications.length).toBe(1); expect(body.pagination.total).toBe(1); - expect((randomElement(body.data) as any).actor.id).toBe(actor.id); - expect((randomElement(body.data) as any).userId).toBe(user.id); - const events = body.data.map((n: any) => n.event); + expect((randomElement(body.data.notifications) as any).actor.id).toBe( + actor.id + ); + expect((randomElement(body.data.notifications) as any).userId).toBe( + user.id + ); + const events = body.data.notifications.map((n: any) => n.event); expect(events).toContain(NotificationEventType.MentionedInComment); }); @@ -173,11 +181,15 @@ describe("#notifications.list", () => { const body = await res.json(); expect(res.status).toBe(200); - expect(body.data.length).toBe(2); + expect(body.data.notifications.length).toBe(2); expect(body.pagination.total).toBe(2); - expect((randomElement(body.data) as any).actor.id).toBe(actor.id); - expect((randomElement(body.data) as any).userId).toBe(user.id); - const events = body.data.map((n: any) => n.event); + expect((randomElement(body.data.notifications) as any).actor.id).toBe( + actor.id + ); + expect((randomElement(body.data.notifications) as any).userId).toBe( + user.id + ); + const events = body.data.notifications.map((n: any) => n.event); expect(events).toContain(NotificationEventType.CreateComment); expect(events).toContain(NotificationEventType.UpdateDocument); }); diff --git a/server/routes/api/notifications/notifications.ts b/server/routes/api/notifications/notifications.ts index 0a5aece972..ef000ada9f 100644 --- a/server/routes/api/notifications/notifications.ts +++ b/server/routes/api/notifications/notifications.ts @@ -81,13 +81,16 @@ router.post( }, }; } - const [notifications, total] = await Promise.all([ + const [notifications, total, unseen] = await Promise.all([ Notification.scope(["withUser", "withActor"]).findAll({ where, order: [["createdAt", "DESC"]], offset: ctx.state.pagination.offset, limit: ctx.state.pagination.limit, }), + Notification.count({ + where, + }), Notification.count({ where: { ...where, @@ -98,13 +101,14 @@ router.post( }), ]); - const data = notifications.map((notification) => - presentNotification(notification) - ); - ctx.body = { pagination: { ...ctx.state.pagination, total }, - data, + data: { + notifications: notifications.map((notification) => + presentNotification(notification) + ), + unseen, + }, }; } );