/**
 * Shipment routes
 */

const express = require('express');
const { requireAuth } = require('../middleware/auth.middleware');
const { create, getAll, getOne, update, deleteOne } = require('../controllers/shipment.controller');
const {
  shipmentCreateSchema,
  shipmentUpdateSchema,
  shipmentNumberSchema,
  validate
} = require('../utils/validators');

const router = express.Router();

router.use(requireAuth);

router.post('/', shipmentCreateSchema, (req, res, next) => {
  try {
    validate(req);
    return create(req, res).catch(next);
  } catch (e) {
    return next(e);
  }
});

router.get('/', getAll);

router.get('/:shipmentNumber', shipmentNumberSchema, (req, res, next) => {
  try {
    validate(req);
    return getOne(req, res).catch(next);
  } catch (e) {
    return next(e);
  }
});

router.put('/:shipmentNumber', shipmentNumberSchema, shipmentUpdateSchema, (req, res, next) => {
  try {
    validate(req);
    return update(req, res).catch(next);
  } catch (e) {
    return next(e);
  }
});

router.delete('/:shipmentNumber', shipmentNumberSchema, (req, res, next) => {
  try {
    validate(req);
    return deleteOne(req, res).catch(next);
  } catch (e) {
    return next(e);
  }
});

module.exports = router;

