This commit is contained in:
2021-06-22 20:45:25 -05:00
parent 14f9937d93
commit 600358d640
9 changed files with 628 additions and 83 deletions

114
routes/creditos.js Normal file
View File

@@ -0,0 +1,114 @@
const router = require("express").Router();
const Credito = require("../models/credito").Credito;
const itemCredito = require("../models/credito").itemCredito;
const { checkAuth } = require("../middlewares/authentication");
router.get("/credito", checkAuth, async (req, res) => {
var creditos;
let limite = req.get('limite');
creditos = await Credito.find({ user: req.userData._id }).sort({
date: "desc",
}).limit(parseInt(limite));
return res.send({
status: "ok",
data: creditos,
});
});
router.post("/credito", checkAuth, async (req, res) => {
const { nombreCredito,valor,tasa_interes } = req.body;
var creditos = await Credito.find({
user: req.userData._id,
nombreCredito: nombreCredito,
});
if (creditos.length == 0) {
const credito = new Credito({
nombreCredito: nombreCredito,
valor:valor,
tasa_interes:tasa_interes
});
credito.user = req.userData._id;
await credito.save();
return res.json({
status: "OK",
});
}
return res.status(500).json({
status: "FAIL",
});
});
router.delete("/credito", checkAuth, async (req, res) => {
try {
const userId = req.userData._id;
const id = req.query.id;
const resultado = await Credito.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 });
}
});
router.put("/credito", checkAuth, async (req, res) => {
const { _id, fecha, detalle, valor, tipo } = req.body;
const credito_edit = await Credito.findOne({
_id: _id,
user: req.userData._id,
});
const itemP = new itemCredito({ fecha,detalle, valor, tipo });
credito_edit.datos.push(itemP);
await credito_edit.save();
res.json({
status: "OK",
});
});
router.get("/credito_items", checkAuth, async (req, res) => {
const _id = req.query.credito_id;
const credito_edit = await Credito.findOne({
_id: _id,
user: req.userData._id,
});
return res.json({
status: "OK",
data: credito_edit.datos,
});
});
router.delete("/creditoitem", checkAuth, async (req, res) => {
try {
const userId = req.userData._id;
const iditem = req.query.iditem;
const credito_id = req.query.idCredito;
var credito_edit = await Credito.findOne({
user: userId,
_id: credito_id,
});
console.log(credito_edit)
console.log("---------------")
credito_edit.datos.id(iditem).remove();
await credito_edit.save();
return res.json({ status: "ok" });
} catch (error) {
console.log(error);
return res.status(500).json({ status: "fail", error: error });
}
});
module.exports = router;

View File

@@ -2,54 +2,72 @@ const express = require("express");
const router = express.Router();
const jwt = require("jsonwebtoken");
const bcrypt = require("bcrypt");
const User = require("../models/user")
const User = require("../models/user");
//import User from "../models/user.js";
//AUTH
router.post("/register", async (req, res) => {
const { name, email, password } = req.body;
const passEncrypted = bcrypt.hashSync(password, 10);
const newUser = new User({
name: name,
email: email,
password: password,
});
if (process.env.REGISTER == "true") {
router.post("/register", async (req, res) => {
const { name, email, password } = req.body;
const passEncrypted = bcrypt.hashSync(password, 10);
const emailUser = await User.findOne({ email: email });
if (emailUser) {
return res
.status(500)
.json({ status: "fail", error: "email already exists" });
}
try {
newUser.password = await newUser.encryptPassword(password);
await newUser.save();
res.json({
status: "ok",
msg: "Usuario creado",
const newUser = new User({
name: name,
email: email,
password: password,
});
} catch (error) {
const emailUser = await User.findOne({ email: email });
if (emailUser) {
return res
.status(500)
.json({ status: "fail", error: "email already exists" });
}
try {
newUser.password = await newUser.encryptPassword(password);
await newUser.save();
res.json({
status: "ok",
msg: "Usuario creado",
});
} catch (error) {
return res
.status(500)
.json({ status: "fail", error: `internal error:${error}` });
}
});
}
else{
router.post("/register", (req, res) => {
return res
.status(500)
.json({ status: "fail", error: `internal error:${error}` });
}
});
.status(500)
.json({ status: "faill", error: `No tiene permitido crear usuarios nuevos` });
})
}
router.post("/login", async (req, res) => {
const { email, password } = req.body;
var user = await User.findOne({ email: email });
if (!user) {
res.status(401).json({ status: "fail", error: "Invalid credentials email" });
res
.status(401)
.json({ status: "fail", error: "Invalid credentials email" });
return;
}
if (! await user.matchPassword(password)) {
return res.status(401).json({ status: "fail", error: "Invalid credentials pass" });
if (!(await user.matchPassword(password))) {
return res
.status(401)
.json({ status: "fail", error: "Invalid credentials pass" });
}
user.set("password", undefined, { strict: false });
const token = jwt.sign(