eletrotupi / tcc/ api/src/controllers/careActions.tsc121404b00d29de6d91827ce3a3fc0ede1a0c5ab
2.9 KB | 100 lines Raw
1
import { Response, NextFunction } from 'express';
2
import { AuthenticatedRequest } from '@app/middleware/auth';
3
import { CareActionType } from '@prisma/client';
4
import { CreateCareActionSchema, PatchCareActionSchema } from '@app/schemas';
5
import { formatValidationError } from '@app/lib/errors/validationError';
6
import {
7
  getCareActionsByUserId,
8
  getCareActionById,
9
  createCareAction,
10
  destroyCareActionById,
11
  patchCareActionById,
12
} from '@app/services/careAction.service';
13

14
export const CareActionsController = {
15
  index: async (req: AuthenticatedRequest, res: Response, next: NextFunction) => {
16
    try {
17
      const result = await getCareActionsByUserId(Number(req.userId), {
18
        type: req.query.type as CareActionType | undefined,
19
        from: req.query.from as string | undefined,
20
        to: req.query.to as string | undefined,
21
        limit: req.query.limit ? Number(req.query.limit) : undefined,
22
        page: req.query.page ? Number(req.query.page) : undefined,
23
      });
24

25
      return res.status(200).json(result);
26
    } catch (err) {
27
      next(err);
28
    }
29
  },
30

31
  show: async (req: AuthenticatedRequest, res: Response, next: NextFunction) => {
32
    try {
33
      const careAction = await getCareActionById(
34
        Number(req.userId),
35
        Number(req.params.id),
36
      );
37

38
      if (!careAction) {
39
        return res.status(404).json({ errors: 'Care action was not found' });
40
      }
41

42
      return res.status(200).json(careAction);
43
    } catch (err) {
44
      next(err);
45
    }
46
  },
47

48
  create: async (req: AuthenticatedRequest, res: Response, next: NextFunction) => {
49
    try {
50
      const parsed = CreateCareActionSchema.safeParse(req.body.careAction);
51

52
      req.log.info("Params: %s", req.body.careAction)
53
      req.log.info("Care action: %s", parsed.error)
54

55
      if (!parsed.success) {
56
        return res.status(400).json(formatValidationError(parsed.error!));
57
      }
58

59
      const careAction = await createCareAction(req.userId!, parsed.data);
60

61
      return res.status(201).json({ careAction });
62
    } catch (err) {
63
      next(err);
64
    }
65
  },
66

67
  destroy: async (req: AuthenticatedRequest, res: Response, next: NextFunction) => {
68
    try {
69
      await destroyCareActionById(req.userId!, Number(req.params.id));
70

71
      return res.status(200).json({});
72
    } catch (err) {
73
      next(err);
74
    }
75
  },
76

77
  patch: async (req: AuthenticatedRequest, res: Response, next: NextFunction) => {
78
    try {
79
      const parsed = PatchCareActionSchema.safeParse(req.body.careAction);
80

81
      if (!parsed.success) {
82
        return res.status(400).json(formatValidationError(parsed.error!));
83
      }
84

85
      const careAction = await patchCareActionById(
86
        req.userId!,
87
        Number(req.params.id),
88
        parsed.data,
89
      );
90

91
      if (!careAction) {
92
        return res.status(404).json({ errors: 'Care action was not found' });
93
      }
94

95
      return res.status(200).json(careAction);
96
    } catch (err) {
97
      next(err);
98
    }
99
  },
100
};