eletrotupi / tcc/ api/src/controllers/careActions.tsca6234de503f574e5e6cd3a52e58f2d5b6c253f2
2.8 KB | 99 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 {
6
  getCareActionsByUserId,
7
  getCareActionById,
8
  createCareAction,
9
  destroyCareActionById,
10
  patchCareActionById,
11
} from '@app/services/careAction.service';
12

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

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

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

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

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

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

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

54
      if (!parsed.success) {
55
        return res.status(400).json({ errors: parsed.error!.issues });
56
      }
57

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

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

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

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

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

80
      if (!parsed.success) {
81
        return res.status(400).json({ errors: parsed.error.issues });
82
      }
83

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

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

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