eletrotupi / tcc/ api/src/controllers/notifications.ts1b8d5d79f03d61a20e683b0da33bbd8abe43bf86
918 B | 32 lines Raw
1
import { Response, NextFunction } from "express";
2
import { AuthenticatedRequest } from "@app/middleware/auth";
3
import { getNotificationsByUserId } from "@app/services/notification.service";
4
import { NotificationsQuerySchema } from "@app/schemas";
5
import { formatValidationError } from "@app/lib/errors/validationError";
6

7
export const NotificationsController = {
8
  index: async (
9
    req: AuthenticatedRequest,
10
    res: Response,
11
    next: NextFunction,
12
  ) => {
13
    try {
14
      const parsedQuery = NotificationsQuerySchema.safeParse(req.query);
15

16
      if (!parsedQuery.success) {
17
        return res.status(400).json(formatValidationError(parsedQuery.error));
18
      }
19

20
      const { limit, cursor } = parsedQuery.data;
21

22
      const page = await getNotificationsByUserId(req.userId!, {
23
        limit,
24
        cursor,
25
      });
26

27
      return res.status(200).json(page);
28
    } catch (err) {
29
      next(err);
30
    }
31
  },
32
};