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

router.post("/create", authorize(), (req, res, next) => 
    handoverCompletionService.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) => 
    handoverCompletionService.getAll(req.body)
    .then((r) => res.json(r))
    .catch(next)
);

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

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

router.get("/initial-data/:requestId", authorize(), (req, res, next) => 
    handoverCompletionService.getInitialDataFromRequest(req.params.requestId)
    .then((r) => res.json(r))
    .catch(next)
);

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

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

module.exports = router;
