text: const express = require("express");
  const router = express.Router();
  const Customer = require("../models/Customer"),
  Stall = require("../models/Stall"),
  Order = require("../models/Order"),
  OrderItem = require("../models/OrderItem"),
  FoodCourt = require("../models/FoodCourt");
  const expressJwt = require("express-jwt");
  const { Op } = require("sequelize");
  
  router.use(expressJwt({
      secret: process.env.auth_jwt,
      algorithms: ["HS256"]
  }), (err, req, res, next) => {
      console.log(req.path);
      if(/\/search/.test(req.path) || /avail/.test(req.path)) {
          // ignore query
          next();
          return;
      }
      // check if person sending request is the same as specified in params
      if(req.params.id != null && req.params.id != req.user.id) return res.sendStatus(401);
      if(err.name == "UnauthorizedError") return res.sendStatus(401);
      next();
  });
  
  router.get("/hpavail", (req, res) => {
      Customer.findOne({where: {phone: req.query.hp}})
      .then(cust => {
          if(cust == null) {
              // if hp not taken, return 200 OK
              res.sendStatus(200);
          } else res.sendStatus(409);
      });
  });
  
  router.get("/emailavail", (req, res) => {
      Customer.findOne({where: {email: req.query.email}})
      .then(cust => {
          if(cust == null) res.sendStatus(200);
          else res.sendStatus(409);
      });
  });
  
  router.get("/:type/search", (req, res) => {
      if(req.params.type == "customer") {
          Customer.findOne({where: {email: req.query.email}})
          .then(cust => {
              if(cust != null) res.json(cust);
              else res.sendStatus(404);
          });
      } else {
          Stall.findOne({where: {email: req.query.email}})
          .then(stall => {
              if(stall != null) res.json(stall);
              else res.sendStatus(404);
          });
      }
  });
  
  router.put("/profile/:id", (req, res) => {
      Customer.findByPk(req.params.id)
      .then(user => {
          user.update({
              name: req.body.name ? req.body.name : user.name,
              email: req.body.email ? req.body.email : user.email,
              phone: req.body.phone ? req.body.phone : user.phone
          }).then(user => {
              res.sendStatus(200);
          });
      });
  });
  
  router.put("/school/:id", (req, res) => {
      Customer.findByPk(req.params.id)
      .then(user => {
          user.update({
              fc: req.body.fc
          }).then(_ => {
              res.sendStatus(200);
          });
      });
  });
  
  router.get("/:id/history", (req, res) => {
      Order.findAll({where: {
          cust_id: req.params.id
      }, include: [{model: Stall, attributes: ["name"], include: [{model: FoodCourt, attributes: ["name"]}]},
      {model: OrderItem, as: "items"},
      {model: Customer, attributes: ["name"]}],
      order: [
          ["datetime", "DESC"]
      ]
      })
      .then(orders => {
          var result = [];
          for(var order of orders) {
              order = order.toJSON();
              order.total = order.items.reduce((acc, curr) => acc + curr.amount, 0);
              if(order.status == 4 || order.status == 5) result.push(order);
          }
          res.json(result);
      });
  });
  
  router.get("/:id/current", (req, res) => {
      // display today's orders regardless of status
      Order.findAll({where: {
          datetime: {
              [Op.gte]: new Date().setHours(0, 0, 0, 0)
          },
          cust_id: req.params.id,
      }, include: [{model: Stall, attributes: ["name"], include: [{model: FoodCourt, attributes: ["name"]}]},
      {model: OrderItem, as: "items"},
      {model: Customer, attributes: ["name"]}],
      order: [
          ["datetime", "DESC"]
      ]
      })
      .then(orders => {
          var result = [];
          for(var order of orders) {
              order = order.toJSON();
              order.total = order.items.reduce((acc, curr) => acc + curr.amount, 0);
              if(order.status == 0 || order.status == 1 || order.status == 3) result.push(order);
          }
          res.json(result);
      });
  });
  
  router.get("/:id", (req, res) => {
      Customer.findByPk(req.params.id)
      .then(user => {
          if(user) res.json(user);
      });
  });
  
  module.exports = router;