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

router.post("/", authorize(), getAll);
router.post("/unassigned", authorize(), getUnassignedCompaniesToUser);
router.post("/create", authorize(), create);
router.delete("/:id", authorize(), _delete);

module.exports = router;

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

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

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

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