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

router.post("/create", authorize(), create);
router.post("/", authorize(), getAll);
router.post('/unassigned', authorize(), getUnassignedDocTypesToRole);
router.post("/option", authorize(), option);
router.get("/count", authorize(), count);
router.put("/update", authorize(), update);
router.delete("/:id", authorize(), _delete);

module.exports = router;

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

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

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

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

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

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

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