text: const express = require("express");
  const router = express.Router();
  const Joi = require("joi");
  const validateRequest = require("_middleware/validate-request");
  const authorize = require("_middleware/authorize");
  const Role = require("_helpers/role");
  const categoryService = require("../services/category.service");
  Joi.objectId = require("joi-objectid")(Joi);
  
  //routes
  router.get("/", authorize(), getAll);
  router.post("/", authorize(), validateSchema, create);
  router.get("/:id", authorize(Role.Admin), getById);
  router.put("/:id", authorize(Role.Admin), update);
  router.delete("/:id", authorize(Role.Admin), _delete);
  
  module.exports = router;
  
  function validateSchema(req, res, next) {
    const schema = Joi.object({
      name: Joi.string().required(),
      description: Joi.string(),
      active: Joi.boolean(),
    });
    validateRequest(req, next, schema);
  }
  
  function getAll(req, res, next) {
    categoryService
      .getAll()
      .then((categories) => res.json({ data: { categories: categories } }))
      .catch(next);
  }
  
  function create(req, res, next) {
    const request = req.body;
    const data = {
      name: request.name,
      description: request.description,
      active: request.active,
    };
    categoryService
      .create(data)
      .then(() =>
        res.json({ data: { message: "Wash Category saved successfully" } })
      )
      .catch(next);
  }
  
  function getById(req, res, next) {
    categoryService
      .getById(req.params.id)
      .then((category) => {
        res.json({ data: { category: category } });
      })
      .catch(next);
  }
  
  function update(req, res, next) {
    const request = req.body;
  
    categoryService
      .update(req.params.id, request)
      .then(() =>
        res.json({ data: { message: "Wash Category updated successfully." } })
      )
      .catch(next);
  }
  
  function _delete(req, res, next) {
    // only admins can delete categories
    if (req.params.id !== req.user.id && req.user.role !== Role.Admin) {
      return res.status(401).json({ message: "Unauthorized" });
    }
  
    categoryService
      .delete(req.params.id)
      .then(() =>
        res.json({ data: { message: "Wash Category deleted successfully" } })
      )
      .catch(next);
  }
  