const express = require("express");
const router = express.Router();
const authorize = require("../../../../middlewares/authorize-sapps");
const leaveAllocationService = require("./leaveAllocation.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.delete("/:id", authorize(), _delete);
router.post("/preview-import", authorize(), previewImport);
router.post("/apply-import", authorize(), applyImport);

module.exports = router;

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

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

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

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

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

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

function previewImport(req, res, next) {
	leaveAllocationService
		.previewImportLeaveAllocations(req.body)
		.then((result) => res.json(result))
		.catch(next);
}

function applyImport(req, res, next) {
	leaveAllocationService
		.applyImportLeaveAllocations(req.body)
		.then((result) => res.json(result))
		.catch(next);
}
