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

router.get('/', authorize(), getAll);
router.get('/filter', authorize(), filter);
router.get('/by-territory-period', authorize(), getByTerritoryIdPeriodId);
router.post('/outlet-client', authorize(), getClientOutletByTerritories);
router.post('/create', authorize(), create);
router.delete('/:id', authorize(), _delete);

module.exports = router;

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

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

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

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

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

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