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

router.post("/create", authorize(), create);
router.get("/duplicate-data", authorize(), getDuplicateData);
router.get("/", authorize(), getAll);
router.get("/filter", authorize(), filter);
router.get("/sales-by-period-and-territory", authorize(), getSalesByPeriodAndTerritory);
router.get("/option", authorize(), option);
router.get("/count", authorize(), count);
router.put("/:id", authorize(), update);
router.put("/enabled/:id", authorize(), enabled);
router.delete("/:id", authorize(), _delete);

module.exports = router;

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

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

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

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

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

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

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

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

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

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