import * as express from "express";

const router = express.Router();


router.use((req, res, next) => {
  if (!req.cookies || !req.cookies.uid) {
    res.status(404);
    res.send();
  } else {
    next();
  }
});

router.get("/personal", (req, res) => {
  const ageStr = req.param("age", null);
  const age = parseInt(ageStr, 10);
  const sex = req.param("sex", null);
  const result: {age?: number, sex?: string} = {};
  if (age > 15 && age < 99) {
    result.age = age;
  } else if (ageStr === "" || !isNaN(age)) {
    result.age = -1;
  }
  if (["na", "m", "w", "o"].indexOf(sex) > -1) {
    result.sex = sex;
  }
  console.log(result);
  if (!!result.age || !!result.sex) {
    req.app.db.get("users")
      .find({id: req.cookies.uid})
      .assign(result)
      .write();
  }
  res.status(204);
  res.send();
});

export = router;
