text: const router = require("express").Router();
  let billing = require("../models/Billing.model");
  const checkMail = (mail) =>{
    if (/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/.test(myForm.emailAddr.value))
     {
       return (true)
     }
       return (false)
   }
  router.route("/find").get((req, res)=>{
    if(!req.query.id) res.json({error: "Id is missing!!"})
    billing.findById(req.query.id).then((bill)=> res.json({
      data: {bill}
    })).catch(err => res.json({
      error: {err}
    }))
  })
  router.route("/findAll").get((req, res)=>{
    
    billing.find().then((bill)=> res.json({
      data: {bill}
    })).catch(err => res.json({
      error: {err}
    }))
  })
  router.route("/create").post((req, res) => {
    if(! req.query.Creator_mail) res.json({error: "E-mail is missing!!"});
    if(! req.query.Creator_project) res.json({error: "Project name is missing!!"});
    if(!checkMail(req.query.Creator_mail)) res.json({error: "Please insert a valid e-mail"})
    const Bill = new billing({
      users: [req.query.Creator_mail],
      projects:[req.query.Creator_project],
      paymentInfo : "",
      credit: 0
    })
    Bill.save().then(() => res.json({data: "Bill Created!!"})).catch(err => res.json({error: err}))
  })
  
  router.route("/addProject").post((req, res) => {
    if(! req.query.id) res.json({error: "ID is missing!!"});
    if(! req.query.project) res.json({error: "Project name is missing!!"});
    billing.findById(req.query.id).then(bill=>{
      bill.projects.push(req.query.project)
      bill.save().then(() => res.json("ok")).catch(err => res.json(err));
    }).catch(err => res.json(err));
  });
  
  router.route("/addUser").post((req, res) => {
    if(! req.query.id) res.json({error: "ID is missing!!"});
    if(! req.query.user) res.json({error: "User is missing!!"});
    if(!checkMail(req.query.user)) res.json({error: "Please insert a valid e-mail"})
  
    billing.findById(req.query.id).then(bill=>{
      bill.users.push(req.query.user)
      bill.save().then(() => res.json("ok")).catch(err => res.json("err"));
    }).catch(err => res.json(err));
  });
  
  router.route("/delete").delete((req, res)=>{
    if(! req.query.id) res.json({error: "ID is missing!!"});
    billing.findByIdAndDelete(req.query.id).then(()=> res.json("deleted successfully")).catch(err => res.json(err))
  })
   router.route("/removeUser").post((req, res) => {
    if(! req.query.id) res.json({error: "ID is missing!!"});
    if(! req.query.user) res.json({error: "User is missing!!"});
    if(!checkMail(req.query.user)) res.json({error: "Please insert a valid e-mail"})
  
     billing.findById(req.query.id).then(bill =>{
       const index = bill.users.indexOf(req.query.user);
       if(index<0) res.json("user is not on this billing list")
       else{
        bill.users.splice(index, 1);
        bill.save().then(()=> res.json("done")).catch(err => res.json(err));
      }
     }).catch(err=> res.json(err));
   })
   router.route("/removeProject").post((req, res) => {
    if(! req.query.id) res.json({error: "ID is missing!!"});
    if(! req.query.project) res.json({error: "User is missing!!"});
    billing.findById(req.query.id).then(bill =>{
      const index = bill.projects.indexOf(req.query.project);
      if(index<0) res.json("project is not on this billing list")
      else{
        bill.projects.splice(index, 1);
        console.log(bill)
        bill.save().then(()=> res.json("done")).catch(err => console.log(err))}
    }).catch(err=> res.json(err));
  })
  router.route("/removeBill").delete((req, res)=>{
    if(! req.query.id) res.json({error: "ID is missing!!"});
  
    billing.findByIdAndDelete(req.query.id).then(() => res.json("Bill deleted")).catch(err => res.json("failed to delete"))
  })
  module.exports = router;
  