const express = require("express");
const router = express.Router();
const authorize = require("../../../../middlewares/authorize-sapps");
const handoverRequestService = require("./handoverRequest.service");

router.post("/create", authorize(), (req, res, next) => 
    handoverRequestService.create({ ...req.body, owner: req.auth.id, modified_by: req.auth.id })
    .then((r) => res.json(r))
    .catch(next)
);

router.post("/", authorize(), (req, res, next) => 
    handoverRequestService.getAll(req.body)
    .then((r) => res.json(r))
    .catch(next)
);

router.get("/:id", authorize(), (req, res, next) => 
    handoverRequestService.getById(req.params.id)
    .then((r) => res.json(r))
    .catch(next)
);

router.put("/:id", authorize(), (req, res, next) => 
    handoverRequestService.update(req.params.id, { ...req.body, modified_by: req.auth.id })
    .then((r) => res.json(r))
    .catch(next)
);

router.post("/approve/:id", authorize(), (req, res, next) => 
    handoverRequestService.approve(req.params.id, req.auth.id)
    .then((r) => res.json(r))
    .catch(next)
);

router.delete("/:id", authorize(), (req, res, next) => 
    handoverRequestService.remove(req.params.id)
    .then((r) => res.json(r))
    .catch(next)
);

router.post("/enabled/:id", authorize(), (req, res, next) => 
    handoverRequestService.enabled(req.params.id, { ...req.body, modified_by: req.auth.id })
    .then((r) => res.json(r))
    .catch(next)
);

module.exports = router;
