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

router.post("/create", authorize(), create);
router.get("/", authorize(), getAll);
router.get("/filter", authorize(), filter);
router.get("/filter-by-year", authorize(), filterByYear);
router.get("/option", authorize(), option);
router.get("/count", authorize(), count);
router.get("/get-latest", authorize(), getLatestDeliveryNumber);
router.get("/used-delivery-note-names", authorize(), getUsedDeliveryNoteNames);
router.put("/:id", authorize(), update);
router.put("/enabled/:id", authorize(), enabled);
router.delete("/:id", authorize(), _delete);

module.exports = router;

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

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

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

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

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

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

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

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

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

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