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

router.post("/", authorize(), (req, res, next) =>
	assetLoanService.getAll(req.body).then((r) => res.json(r)).catch(next)
);
router.post("/create", authorize(), (req, res, next) =>
	assetLoanService.create({ ...req.body, owner: req.auth?.id, modified_by: req.auth?.id }).then((r) => res.json(r)).catch(next)
);
router.post("/option", authorize(), (req, res, next) =>
	assetLoanService.option(req.body).then((r) => res.json(r)).catch(next)
);
router.get("/:id", authorize(), (req, res, next) =>
	assetLoanService.getById(req.params.id).then((r) => res.json(r)).catch(next)
);
router.put("/:id", authorize(), (req, res, next) =>
	assetLoanService.update(req.params.id, req.body).then((r) => res.json(r)).catch(next)
);
router.post("/approve/:id", authorize(), (req, res, next) =>
	assetLoanService.approve(req.params.id, req.auth?.id).then((r) => res.json(r)).catch(next)
);
router.post("/return/:id", authorize(), (req, res, next) =>
	assetLoanService.return(req.params.id, req.auth?.id).then((r) => res.json(r)).catch(next)
);
router.post("/cancel/:id", authorize(), (req, res, next) =>
	assetLoanService.cancel(req.params.id, req.auth?.id).then((r) => res.json(r)).catch(next)
);
router.post("/submit/:id", authorize(), (req, res, next) =>
	assetLoanService.submit(req.params.id, req.auth?.id).then((r) => res.json(r)).catch(next)
);
router.post("/revise/:id", authorize(), (req, res, next) =>
	assetLoanService.revise(req.params.id, req.body).then((r) => res.json(r)).catch(next)
);
router.post("/reject/:id", authorize(), (req, res, next) =>
	assetLoanService.reject(req.params.id, req.body).then((r) => res.json(r)).catch(next)
);
router.delete("/:id", authorize(), (req, res, next) =>
	assetLoanService.delete(req.params.id).then((r) => res.json(r)).catch(next)
);

module.exports = router;
