const router = require("express").Router(); const Ingreso = require("../models/ingresos"); const { checkAuth } = require("../middlewares/authentication"); router.get("/ingreso", checkAuth, async (req, res) => { var Ingresos; Ingresos = await Ingreso.find({ user: req.userData._id }).sort({ fecha: "desc", }); return res.send( { status:"ok", data:Ingresos } ) }); router.post("/ingreso", checkAuth, async (req, res) => { const { fecha, detalle, valor} = req.body; const newIngreso = new Ingreso({ fecha, detalle, valor }); console.log(newIngreso) newIngreso.user = req.userData._id; await newIngreso.save(); res.json({ status:"OK" }) }); router.put("/ingreso", checkAuth, async (req, res) => { const { _id, fecha, detalle, valor } = req.body; const Ingreso_edit = await Ingreso.findOne({ _id: _id }); await Ingreso_edit.updateOne({ fecha, detalle, valor}); res.json({ status:"OK" }) }); router.delete("/ingreso", checkAuth, async (req, res) => { try{ const userId = req.userData._id; const id = req.query.id; const resultado = await Ingreso.deleteOne({user:userId,_id:id}); return res.json({status:"ok",data: resultado}) } catch(error){ console.log(error); return res.status(500).json({status:"fail",error:error}) } }); module.exports = router;