const express = require('express');
const router = express.Router();
const authorize = require('../../../../middlewares/authorize-sapps');
const employeeTerritoryService = require('./employeeTerritory.service');

router.get('/', authorize(), getAll);
router.get('/filter', authorize(), filter);
router.post('/create', authorize(), create);
router.delete('/:id', authorize(), _delete);

module.exports = router;

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

function filter(req, res, next) {
    employeeTerritoryService.filter(req.query)
        .then(result => res.json(result))
        .catch(next);
}

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

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