backend/src/graphql/notification/index.ts (view raw)
1import pMap from "p-map";
2
3export default [
4 ({ nexus, strapi }) => ({
5 types: [
6 nexus.mutationField("readNotifications", {
7 type: "NotificationEntityResponseCollection",
8 args: {
9 id: "ID",
10 },
11 }),
12 ],
13 resolvers: {
14 Mutation: {
15 readNotifications: {
16 async resolve(_root, args, context) {
17 const user = context.state.user;
18 const { id } = args;
19
20 const { toEntityResponseCollection } = strapi
21 .plugin("graphql")
22 .service("format").returnTypes;
23
24 let idFilter = {};
25 if (id) idFilter = { id };
26
27 const userNotifications = await strapi.entityService.findMany(
28 "api::notification.notification",
29 {
30 filters: {
31 user: { id: user.id },
32 read: false,
33 ...idFilter,
34 },
35 }
36 );
37 const updatedNotifications = await pMap(
38 userNotifications,
39 (notification: { id: "string" }) =>
40 strapi.entityService.update(
41 "api::notification.notification",
42 notification.id,
43 { data: { read: true } }
44 )
45 );
46 return toEntityResponseCollection(updatedNotifications, {
47 resourceUID: "api::notification.notification",
48 });
49 },
50 id: {
51 description:
52 "Notification ID to read. If no ID is provided, all user's notifications are set as read.",
53 },
54 },
55 },
56 },
57 resolversConfig: {
58 "Mutation.readNotifications": {
59 auth: true,
60 },
61 },
62 }),
63];