eletrotupi / tcc/ api/src/controllers/insights.tsb3cae39fbd7a790d651c1950d4bc42dc3bc9e9aa
927 B | 33 lines Raw
1
import { Response, NextFunction } from "express";
2
import { AuthenticatedRequest } from "@app/middleware/auth";
3
import { getInsightsByUserId } from "@app/services/insight.service";
4
import { InsightsQuerySchema } from "@app/schemas";
5

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

15
      if (!parsedQuery.success) {
16
        return res.status(400).json({
17
          message: "Invalid query parameters",
18
          errors: parsedQuery.error.flatten(),
19
        });
20
      }
21

22
      const insights = await getInsightsByUserId(req.userId!, {
23
        type: parsedQuery.data.type,
24
        period: parsedQuery.data.period,
25
        limit: parsedQuery.data.limit,
26
      });
27

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