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

router.post("/create", authorize(), create);
router.post("/", authorize(), getAll);
router.post("/option", authorize(), option);
router.get("/count", authorize(), count);
router.put("/:id", authorize(), update);
router.put("/approve/:id", authorize(), approve);
router.put("/revise/:id", authorize(), revise);
router.put("/reject/:id", authorize(), reject);
router.delete("/:id", authorize(), _delete);

module.exports = router;

function create(req, res, next) {
	attendanceRequestService
		.create(req.body)
		.then((result) => res.json(result))
		.catch(next);
}

function getAll(req, res, next) {
	attendanceRequestService
		.getAll(req.body)
		.then((result) => res.json(result))
		.catch(next);
}

function option(req, res, next) {
	attendanceRequestService
		.option(req.body)
		.then((result) => res.json(result))
		.catch(next);
}

function count(req, res, next) {
	attendanceRequestService
		.count()
		.then((result) => res.json(result))
		.catch(next);
}

function update(req, res, next) {
	attendanceRequestService
		.update(req.params.id, req.body)
		.then((result) => res.json(result))
		.catch(next);
}

function approve(req, res, next) {
	attendanceRequestService
		.approve(req.params.id, req.body)
		.then((result) => res.json(result))
		.catch(next);
}

function revise(req, res, next) {
	attendanceRequestService
		.revise(req.params.id, req.body)
		.then((result) => res.json(result))
		.catch(next);
}

function reject(req, res, next) {
	attendanceRequestService
		.reject(req.params.id, req.body)
		.then((result) => res.json(result))
		.catch(next);
}

function _delete(req, res, next) {
	attendanceRequestService
		.delete(req.params.id)
		.then((result) => res.json(result))
		.catch(next);
}
