This commit is contained in:
Ciro-pro
2022-09-14 18:30:44 -03:00
commit e23cf74cde
25 changed files with 4741 additions and 0 deletions

13
app/config/db.config.js Normal file
View File

@@ -0,0 +1,13 @@
module.exports = {
HOST: "localhost",
USER: "lockoutuser",
PASSWORD: "lockoutpassword",
DB: "lockout",
dialect: "mariadb",
pool: {
max: 5,
min: 0,
acquire: 30000,
idle: 10000,
},
};

View File

@@ -0,0 +1,149 @@
const db = require("../models");
const Bloqueo = db.bloqueos;
const Op = db.Sequelize.Op;
// Create and Save a new Bloqueo
exports.create = (req, res) => {
// Validate request
if (!req.body.name) {
console.log("body: ", req.body);
res.status(400).send({
message: "Content can not be empty!",
});
return;
}
// Create a Bloqueo
const bloqueo = {
name: req.body.name,
description: req.body.description,
location: req.body.location,
date: req.body.date,
done: req.body.done || false,
};
// Save Bloqueo in the database
Bloqueo.create(bloqueo)
.then((data) => {
res.send(data);
})
.catch((err) => {
res.status(500).send({
message:
err.message || "Some error occurred while creating the Bloqueo.",
});
});
};
// Retrieve all Bloqueos from the database.
exports.findAll = (req, res) => {
const title = req.query.title;
var condition = title ? { title: { [Op.like]: `%${title}%` } } : null;
Bloqueo.findAll({ where: condition })
.then((data) => {
res.send(data);
})
.catch((err) => {
res.status(500).send({
message:
err.message || "Some error occurred while retrieving bloqueos.",
});
});
};
// Find a single Bloqueo with an id
exports.findOne = (req, res) => {
const id = req.params.id;
Bloqueo.findByPk(id)
.then((data) => {
if (data) {
res.send(data);
} else {
res.status(404).send({
message: `Cannot find Bloqueo with id=${id}.`,
});
}
})
.catch((err) => {
res.status(500).send({
message: "Error retrieving Bloqueo with id=" + id,
});
});
};
// Update a Bloqueo by the id in the request
exports.update = (req, res) => {
const id = req.params.id;
Bloqueo.update(req.body, {
where: { id: id },
})
.then((num) => {
if (num == 1) {
res.send({
message: "Bloqueo was updated successfully.",
});
} else {
res.send({
message: `Cannot update Bloqueo with id=${id}. Maybe Bloqueo was not found or req.body is empty!`,
});
}
})
.catch((err) => {
res.status(500).send({
message: "Error updating Bloqueo with id=" + id,
});
});
};
// Delete a Bloqueo with the specified id in the request
exports.delete = (req, res) => {
const id = req.params.id;
Bloqueo.destroy({
where: { id: id },
})
.then((num) => {
if (num == 1) {
res.send({
message: "Bloqueo was deleted successfully!",
});
} else {
res.send({
message: `Cannot delete Bloqueo with id=${id}. Maybe Bloqueo was not found!`,
});
}
})
.catch((err) => {
res.status(500).send({
message: "Could not delete Bloqueo with id=" + id,
});
});
};
// Delete all Bloqueos from the database.
exports.deleteAll = (req, res) => {
Bloqueo.destroy({
where: {},
truncate: false,
})
.then((nums) => {
res.send({ message: `${nums} Bloqueos were deleted successfully!` });
})
.catch((err) => {
res.status(500).send({
message:
err.message || "Some error occurred while removing all Bloqueos.",
});
});
};
// Find all published Bloqueos
exports.findAllNotDone = (req, res) => {
Bloqueo.findAll({ where: { done: false } })
.then((data) => {
res.send(data);
})
.catch((err) => {
res.status(500).send({
message:
err.message || "Some error occurred while retrieving Bloqueos.",
});
});
};

View File

@@ -0,0 +1,162 @@
const db = require("../models");
const Candado = db.candados;
const Op = db.Sequelize.Op;
// Create and Save a new Candado
exports.create = (req, res) => {
// Validate request
if (!req.body.codigo) {
console.log("body: ", req.body);
res.status(400).send({
message: "Content can not be empty!",
});
return;
}
// Create a Candado
const candado = {
codigo: req.body.codigo,
rut: req.body.rut,
bloqueo_id: req.body.bloqueo_id || 0,
};
// Save Candado in the database
Candado.create(candado)
.then((data) => {
res.send(data);
})
.catch((err) => {
res.status(500).send({
message:
err.message || "Some error occurred while creating the Candado.",
});
});
};
// Retrieve all Candados from the database.
exports.findAll = (req, res) => {
const title = req.query.title;
var condition = title ? { title: { [Op.like]: `%${title}%` } } : null;
Candado.findAll({ where: condition })
.then((data) => {
res.send(data);
})
.catch((err) => {
res.status(500).send({
message:
err.message || "Some error occurred while retrieving Candados.",
});
});
};
// Find a single Candado with an id
exports.findOne = (req, res) => {
const id = req.params.id;
Candado.findByPk(id)
.then((data) => {
if (data) {
res.send(data);
} else {
res.status(404).send({
message: `Cannot find Candado with id=${id}.`,
});
}
})
.catch((err) => {
res.status(500).send({
message: "Error retrieving Candado with id=" + id,
});
});
};
// Update a Candado by the id in the request
exports.update = (req, res) => {
const id = req.params.id;
Candado.update(req.body, {
where: { id: id },
})
.then((num) => {
if (num == 1) {
res.send({
message: "Candado was updated successfully.",
});
} else {
res.send({
message: `Cannot update Candado with id=${id}. Maybe Candado was not found or req.body is empty!`,
});
}
})
.catch((err) => {
res.status(500).send({
message: "Error updating Candado with id=" + id,
});
});
};
// Delete a Candado with the specified id in the request
exports.delete = (req, res) => {
const id = req.params.id;
Candado.destroy({
where: { id: id },
})
.then((num) => {
if (num == 1) {
res.send({
message: "Candado was deleted successfully!",
});
} else {
res.send({
message: `Cannot delete Candado with id=${id}. Maybe Candado was not found!`,
});
}
})
.catch((err) => {
res.status(500).send({
message: "Could not delete Candado with id=" + id,
});
});
};
// Delete all Candados from the database.
exports.deleteAll = (req, res) => {
Candado.destroy({
where: {},
truncate: false,
})
.then((nums) => {
res.send({ message: `${nums} Candado were deleted successfully!` });
})
.catch((err) => {
res.status(500).send({
message:
err.message || "Some error occurred while removing all Candados.",
});
});
};
// Find all by ID Candados
exports.findAllByBloqueo = (req, res) => {
const bloqueo = req.params.bloqueo;
Candado.findAll({ where: { bloqueo_id: bloqueo } })
.then((data) => {
res.send(data);
})
.catch((err) => {
res.status(500).send({
message:
err.message || "Some error occurred while retrieving Candados.",
});
});
};
// Find all by ID Candados
exports.findAllByRut = (req, res) => {
const rut = req.params.rut;
Candado.findAll({ where: { rut } })
.then((data) => {
res.send(data);
})
.catch((err) => {
res.status(500).send({
message:
err.message || "Some error occurred while retrieving Candados.",
});
});
};

View File

@@ -0,0 +1,147 @@
const db = require("../models");
const Equipo = db.equipos;
const Op = db.Sequelize.Op;
// Create and Save a new Equipo
exports.create = (req, res) => {
// Validate request
console.log("body: ", req.body);
if (!req.body.tag) {
res.status(400).send({
message: "Content can not be empty!",
});
return;
}
// Create a Equipo
const equipo = {
tag: req.body.tag,
descripcion: req.body.descripcion,
ubicacion: req.body.ubicacion,
bloqueo_id: req.body.bloqueo_id || 0,
};
// Save Equipo in the database
Equipo.create(equipo)
.then((data) => {
res.send(data);
})
.catch((err) => {
res.status(500).send({
message:
err.message || "Some error occurred while creating the Equipo.",
});
});
};
// Retrieve all Equipos from the database.
exports.findAll = (req, res) => {
const title = req.query.title;
var condition = title ? { title: { [Op.like]: `%${title}%` } } : null;
Equipo.findAll({ where: condition })
.then((data) => {
res.send(data);
})
.catch((err) => {
res.status(500).send({
message: err.message || "Some error occurred while retrieving Equipos.",
});
});
};
// Find a single Equipo with an id
exports.findOne = (req, res) => {
const id = req.params.id;
Equipo.findByPk(id)
.then((data) => {
if (data) {
res.send(data);
} else {
res.status(404).send({
message: `Cannot find Equipo with id=${id}.`,
});
}
})
.catch((err) => {
res.status(500).send({
message: "Error retrieving Equipo with id=" + id,
});
});
};
// Update a Equipo by the id in the request
exports.update = (req, res) => {
const id = req.params.id;
Equipo.update(req.body, {
where: { id: id },
})
.then((num) => {
if (num == 1) {
res.send({
message: "Equipo was updated successfully.",
});
} else {
res.send({
message: `Cannot update Equipo with id=${id}. Maybe Equipo was not found or req.body is empty!`,
});
}
})
.catch((err) => {
res.status(500).send({
message: "Error updating Equipo with id=" + id,
});
});
};
// Delete a Equipo with the specified id in the request
exports.delete = (req, res) => {
const id = req.params.id;
Equipo.destroy({
where: { id: id },
})
.then((num) => {
if (num == 1) {
res.send({
message: "Equipo was deleted successfully!",
});
} else {
res.send({
message: `Cannot delete Equipo with id=${id}. Maybe Equipo was not found!`,
});
}
})
.catch((err) => {
res.status(500).send({
message: "Could not delete Equipo with id=" + id,
});
});
};
// Delete all Equipos from the database.
exports.deleteAll = (req, res) => {
Equipo.destroy({
where: {},
truncate: false,
})
.then((nums) => {
res.send({ message: `${nums} Equipo were deleted successfully!` });
})
.catch((err) => {
res.status(500).send({
message:
err.message || "Some error occurred while removing all Equipos.",
});
});
};
// Find all by ID Equipos
exports.findAllByBloqueo = (req, res) => {
const bloqueo = req.params.bloqueo;
Equipo.findAll({ where: { bloqueo_id: bloqueo } })
.then((data) => {
res.send(data);
})
.catch((err) => {
res.status(500).send({
message: err.message || "Some error occurred while retrieving Equipos.",
});
});
};

18
app/controllers/proxis.js Normal file
View File

@@ -0,0 +1,18 @@
const jwt = require("jsonwebtoken");
require("dotenv").config();
exports.authenticateUser = (req, res, next) => {
console.log(req.headers);
const authHeader = req.headers["authorization"];
const token = authHeader && authHeader.split(" ")[1];
if (token == null) return res.sendStatus(401);
jwt.verify(token, process.env.ACCESS_TOKEN_SECRET, (err, user) => {
if (err) {
console.log(err.message);
return res.sendStatus(403);
}
req.user = user;
next();
});
};

View File

@@ -0,0 +1,151 @@
const db = require("../models");
const Trabajador = db.trabajadores;
const Op = db.Sequelize.Op;
// Create and Save a new Bloqueo
exports.create = (req, res) => {
// Validate request
if (!req.body.rut) {
console.log("body: ", req.body);
res.status(400).send({
message: "Content can not be empty!",
});
return;
}
// Create a Bloqueo
const trabajador = {
rut: req.body.rut,
nombre: req.body.nombre,
empresa: req.body.empresa,
fdn: req.body.fdn,
bloqueo_id: req.body.bloqueo_id || 0,
};
// Save Bloqueo in the database
Trabajador.create(trabajador)
.then((data) => {
res.send(data);
})
.catch((err) => {
res.status(500).send({
message:
err.message || "Some error occurred while creating the Trabajador.",
});
});
};
// Retrieve all Bloqueos from the database.
exports.findAll = (req, res) => {
const title = req.query.title;
var condition = title ? { title: { [Op.like]: `%${title}%` } } : null;
Trabajador.findAll({ where: condition })
.then((data) => {
res.send(data);
})
.catch((err) => {
res.status(500).send({
message:
err.message || "Some error occurred while retrieving Trabajadores.",
});
});
};
// Find a single Bloqueo with an id
exports.findOne = (req, res) => {
const id = req.params.id;
Trabajador.findByPk(id)
.then((data) => {
if (data) {
res.send(data);
} else {
res.status(404).send({
message: `Cannot find Trabajador with id=${id}.`,
});
}
})
.catch((err) => {
res.status(500).send({
message: "Error retrieving Trabajador with id=" + id,
});
});
};
// Update a Bloqueo by the id in the request
exports.update = (req, res) => {
const id = req.params.id;
Trabajador.update(req.body, {
where: { id: id },
})
.then((num) => {
if (num == 1) {
res.send({
message: "Trabajador was updated successfully.",
});
} else {
res.send({
message: `Cannot update Trabajador with id=${id}. Maybe Trabajador was not found or req.body is empty!`,
});
}
})
.catch((err) => {
res.status(500).send({
message: "Error updating Trabajador with id=" + id,
});
});
};
// Delete a Bloqueo with the specified id in the request
exports.delete = (req, res) => {
const id = req.params.id;
Trabajador.destroy({
where: { id: id },
})
.then((num) => {
if (num == 1) {
res.send({
message: "Trabajador was deleted successfully!",
});
} else {
res.send({
message: `Cannot delete Trabajador with id=${id}. Maybe Trabajador was not found!`,
});
}
})
.catch((err) => {
res.status(500).send({
message: "Could not delete Trabajador with id=" + id,
});
});
};
// Delete all Bloqueos from the database.
exports.deleteAll = (req, res) => {
Trabajador.destroy({
where: {},
truncate: false,
})
.then((nums) => {
res.send({ message: `${nums} Trabajador were deleted successfully!` });
})
.catch((err) => {
res.status(500).send({
message:
err.message || "Some error occurred while removing all Trabajadores.",
});
});
};
// Find all published Bloqueos
exports.findAllByBloqueo = (req, res) => {
const bloqueo = req.params.bloqueo;
Trabajador.findAll({ where: { bloqueo_id: bloqueo } })
.then((data) => {
console.log(data);
res.send(data);
})
.catch((err) => {
res.status(500).send({
message:
err.message || "Some error occurred while retrieving Trabajadores.",
});
});
};

View File

@@ -0,0 +1,147 @@
const db = require("../models");
const Tutorial = db.bloqueos;
const Op = db.Sequelize.Op;
// Create and Save a new Tutorial
exports.create = (req, res) => {
// Validate request
if (!req.body.title) {
console.log("body: ", req.body);
res.status(400).send({
message: "Content can not be empty!",
});
return;
}
// Create a Tutorial
const tutorial = {
title: req.body.title,
description: req.body.description,
published: req.body.published ? req.body.published : false,
};
// Save Tutorial in the database
Tutorial.create(tutorial)
.then((data) => {
res.send(data);
})
.catch((err) => {
res.status(500).send({
message:
err.message || "Some error occurred while creating the Tutorial.",
});
});
};
// Retrieve all Tutorials from the database.
exports.findAll = (req, res) => {
const title = req.query.title;
var condition = title ? { title: { [Op.like]: `%${title}%` } } : null;
Tutorial.findAll({ where: condition })
.then((data) => {
res.send(data);
})
.catch((err) => {
res.status(500).send({
message:
err.message || "Some error occurred while retrieving tutorials.",
});
});
};
// Find a single Tutorial with an id
exports.findOne = (req, res) => {
const id = req.params.id;
Tutorial.findByPk(id)
.then((data) => {
if (data) {
res.send(data);
} else {
res.status(404).send({
message: `Cannot find Tutorial with id=${id}.`,
});
}
})
.catch((err) => {
res.status(500).send({
message: "Error retrieving Tutorial with id=" + id,
});
});
};
// Update a Tutorial by the id in the request
exports.update = (req, res) => {
const id = req.params.id;
Tutorial.update(req.body, {
where: { id: id },
})
.then((num) => {
if (num == 1) {
res.send({
message: "Tutorial was updated successfully.",
});
} else {
res.send({
message: `Cannot update Tutorial with id=${id}. Maybe Tutorial was not found or req.body is empty!`,
});
}
})
.catch((err) => {
res.status(500).send({
message: "Error updating Tutorial with id=" + id,
});
});
};
// Delete a Tutorial with the specified id in the request
exports.delete = (req, res) => {
const id = req.params.id;
Tutorial.destroy({
where: { id: id },
})
.then((num) => {
if (num == 1) {
res.send({
message: "Tutorial was deleted successfully!",
});
} else {
res.send({
message: `Cannot delete Tutorial with id=${id}. Maybe Tutorial was not found!`,
});
}
})
.catch((err) => {
res.status(500).send({
message: "Could not delete Tutorial with id=" + id,
});
});
};
// Delete all Tutorials from the database.
exports.deleteAll = (req, res) => {
Tutorial.destroy({
where: {},
truncate: false,
})
.then((nums) => {
res.send({ message: `${nums} Tutorials were deleted successfully!` });
})
.catch((err) => {
res.status(500).send({
message:
err.message || "Some error occurred while removing all tutorials.",
});
});
};
// Find all published Tutorials
exports.findAllPublished = (req, res) => {
Tutorial.findAll({ where: { published: true } })
.then((data) => {
res.send(data);
})
.catch((err) => {
res.status(500).send({
message:
err.message || "Some error occurred while retrieving tutorials.",
});
});
};

View File

@@ -0,0 +1,200 @@
require("dotenv").config();
const db = require("../models");
const Usuario = db.usuarios;
const Op = db.Sequelize.Op;
const bcrypt = require("bcrypt");
const jwt = require("jsonwebtoken");
// Create and Save a new Usuario
exports.create = async (req, res) => {
// Validate request
if (!req.body.email) {
console.log("body: ", req.body);
res.status(400).send({
message: "Content can not be empty!",
});
return;
}
// Create a Usuario
try {
const hashedPassword = await bcrypt.hash(req.body.password, 10);
const usuario = {
email: req.body.email,
password: hashedPassword,
level: req.body.level || 1,
};
console.log(usuario);
// Save Usuario in the database
Usuario.create(usuario)
.then((data) => {
console.log(data);
res.status(201).send(data);
})
.catch((err) => {
res.status(500).send({
message:
err.message || "Some error occurred while creating the Usuario.",
});
});
} catch {
res.status(500).send();
}
};
// Login a Usuario
exports.login = async (req, res) => {
const email = req.body.email;
Usuario.findOne({ where: { email } })
.then((data) => {
if (data !== null) {
//Se encontro un registro
console.log(data.toJSON());
let jsonData = data.toJSON();
bcrypt
.compare(req.body.password, jsonData.password)
.then((result) => {
if (result) {
const accessToken = jwt.sign(
{ email: jsonData.email },
process.env.ACCESS_TOKEN_SECRET
);
res.send({
message: "VALID",
user: { email, level: jsonData.level, token: accessToken },
}); //Password OK
} else {
res.send({ message: "REJECT" });
}
})
.catch((err) => {
res.status(500).send({ message: err.message });
});
} else {
// No se encontro registro asociado al email
res.send({ message: "NOTFOUND" });
}
})
.catch((err) => {
console.log(err);
res.status(500).send({
message: err.message || "Some error occurred while retrieving Usuario.",
});
});
};
// Retrieve all Usuarios from the database.
exports.findAll = (req, res) => {
const title = req.query.title;
var condition = title ? { title: { [Op.like]: `%${title}%` } } : null;
Usuario.findAll({ where: condition })
.then((data) => {
res.send(data);
})
.catch((err) => {
res.status(500).send({
message:
err.message || "Some error occurred while retrieving Usuarios.",
});
});
};
// Find a single Usuario with an id
exports.findOne = (req, res) => {
const id = req.params.id;
Usuario.findByPk(id)
.then((data) => {
if (data) {
res.send(data);
} else {
res.status(404).send({
message: `Cannot find Usuario with id=${id}.`,
});
}
})
.catch((err) => {
res.status(500).send({
message: "Error retrieving Usuario with id=" + id,
});
});
};
// Update a Usuario by the id in the request
exports.update = (req, res) => {
const id = req.params.id;
Usuario.update(req.body, {
where: { id: id },
})
.then((num) => {
if (num == 1) {
res.send({
message: "Usuario was updated successfully.",
});
} else {
res.send({
message: `Cannot update Usuario with id=${id}. Maybe Usuario was not found or req.body is empty!`,
});
}
})
.catch((err) => {
res.status(500).send({
message: "Error updating Usuario with id=" + id,
});
});
};
// Delete a Usuario with the specified id in the request
exports.delete = (req, res) => {
const id = req.params.id;
Usuario.destroy({
where: { id: id },
})
.then((num) => {
if (num == 1) {
res.send({
message: "Usuario was deleted successfully!",
});
} else {
res.send({
message: `Cannot delete Usuario with id=${id}. Maybe Usuario was not found!`,
});
}
})
.catch((err) => {
res.status(500).send({
message: "Could not delete Usuario with id=" + id,
});
});
};
// Delete all Usuarios from the database.
exports.deleteAll = (req, res) => {
Usuario.destroy({
where: {},
truncate: false,
})
.then((nums) => {
res.send({ message: `${nums} Usuario were deleted successfully!` });
})
.catch((err) => {
res.status(500).send({
message:
err.message || "Some error occurred while removing all Usuarios.",
});
});
};
// Find all by ID Usuarios
exports.findAllByBloqueo = (req, res) => {
const bloqueo = req.params.bloqueo;
Usuario.findAll({ where: { bloqueo_id: bloqueo } })
.then((data) => {
res.send(data);
})
.catch((err) => {
res.status(500).send({
message:
err.message || "Some error occurred while retrieving Usuarios.",
});
});
};

View File

@@ -0,0 +1,22 @@
module.exports = (sequelize, Sequelize) => {
const Bloqueo = sequelize.define("bloqueo", {
name: {
type: Sequelize.STRING,
allowNull: false,
},
date: {
type: Sequelize.DATE,
allowNull: false,
},
location: {
type: Sequelize.STRING,
allowNull: true,
},
done: {
type: Sequelize.BOOLEAN,
allowNull: false,
defaultValue: false,
},
});
return Bloqueo;
};

View File

@@ -0,0 +1,19 @@
module.exports = (sequelize, Sequelize) => {
const Candado = sequelize.define("candado", {
codigo: {
type: Sequelize.STRING,
allowNull: false,
unique: true,
},
rut: {
type: Sequelize.STRING,
allowNull: false,
},
bloqueo_id: {
type: Sequelize.INTEGER,
allowNull: false,
defaultValue: 0,
},
});
return Candado;
};

View File

@@ -0,0 +1,23 @@
module.exports = (sequelize, Sequelize) => {
const Equipo = sequelize.define("equipo", {
tag: {
type: Sequelize.STRING,
allowNull: false,
unique: true,
},
descripcion: {
type: Sequelize.STRING,
allowNull: false,
},
ubicacion: {
type: Sequelize.STRING,
allowNull: true,
},
bloqueo_id: {
type: Sequelize.INTEGER,
allowNull: false,
defaultValue: 0,
},
});
return Equipo;
};

23
app/models/index.js Normal file
View File

@@ -0,0 +1,23 @@
const dbConfig = require("../config/db.config.js");
const Sequelize = require("sequelize");
const sequelize = new Sequelize(dbConfig.DB, dbConfig.USER, dbConfig.PASSWORD, {
host: dbConfig.HOST,
dialect: dbConfig.dialect,
operatorsAliases: false,
pool: {
max: dbConfig.pool.max,
min: dbConfig.pool.min,
acquire: dbConfig.pool.acquire,
idle: dbConfig.pool.idle,
},
});
const db = {};
db.Sequelize = Sequelize;
db.sequelize = sequelize;
db.tutorials = require("./tutorial.model.js")(sequelize, Sequelize);
db.bloqueos = require("./bloqueo.model.js")(sequelize, Sequelize);
db.trabajadores = require("./trabajador.model.js")(sequelize, Sequelize);
db.candados = require("./candado.model.js")(sequelize, Sequelize);
db.equipos = require("./equipo.model.js")(sequelize, Sequelize);
db.usuarios = require("./usuario.model.js")(sequelize, Sequelize);
module.exports = db;

View File

@@ -0,0 +1,26 @@
module.exports = (sequelize, Sequelize) => {
const Trabajador = sequelize.define("trabajador", {
rut: {
type: Sequelize.STRING,
allowNull: false,
unique: true,
},
nombre: {
type: Sequelize.STRING,
allowNull: false,
},
empresa: {
type: Sequelize.STRING,
},
fdn: {
type: Sequelize.DATEONLY,
allowNull: false,
},
bloqueo_id: {
type: Sequelize.INTEGER,
allowNull: false,
defaultValue: 0,
},
});
return Trabajador;
};

View File

@@ -0,0 +1,14 @@
module.exports = (sequelize, Sequelize) => {
const Tutorial = sequelize.define("tutorial", {
title: {
type: Sequelize.STRING,
},
description: {
type: Sequelize.STRING,
},
published: {
type: Sequelize.BOOLEAN,
},
});
return Tutorial;
};

View File

@@ -0,0 +1,19 @@
module.exports = (sequelize, Sequelize) => {
const Usuario = sequelize.define("usuario", {
email: {
type: Sequelize.STRING,
allowNull: false,
unique: true,
},
password: {
type: Sequelize.STRING,
allowNull: false,
},
level: {
type: Sequelize.INTEGER,
allowNull: false,
defaultValue: 1,
},
});
return Usuario;
};

View File

@@ -0,0 +1,19 @@
module.exports = (app) => {
const bloqueos = require("../controllers/bloqueo.controller.js");
var router = require("express").Router();
// Create a new bloqueo
router.post("/", bloqueos.create);
// Retrieve all bloqueos
router.get("/", bloqueos.findAll);
// Retrieve all published bloqueos
router.get("/notDone", bloqueos.findAllNotDone);
// Retrieve a single bloqueo with id
router.get("/:id", bloqueos.findOne);
// Update a bloqueo with id
router.put("/:id", bloqueos.update);
// Delete a bloqueo with id
router.delete("/:id", bloqueos.delete);
// Delete all bloqueos
router.delete("/", bloqueos.deleteAll);
app.use("/api/bloqueos", router);
};

View File

@@ -0,0 +1,27 @@
const proxy = require("../controllers/proxis");
module.exports = (app) => {
const candados = require("../controllers/candado.controller.js");
var router = require("express").Router();
// Create a new bloqueo
router.post("/", proxy.authenticateUser, candados.create);
// Retrieve all bloqueos
router.get("/", proxy.authenticateUser, candados.findAll);
// Retrieve all published bloqueos
router.get(
"/byBloqueo/:bloqueo",
proxy.authenticateUser,
candados.findAllByBloqueo
);
// Retrieve all published bloqueos
router.get("/byRut/:rut", proxy.authenticateUser, candados.findAllByRut);
// Retrieve a single bloqueo with id
router.get("/:id", proxy.authenticateUser, candados.findOne);
// Update a bloqueo with id
router.put("/:id", proxy.authenticateUser, candados.update);
// Delete a bloqueo with id
router.delete("/:id", proxy.authenticateUser, candados.delete);
// Delete all bloqueos
router.delete("/", proxy.authenticateUser, candados.deleteAll);
app.use("/api/candados", router);
};

View File

@@ -0,0 +1,25 @@
const proxy = require("../controllers/proxis");
module.exports = (app) => {
const equipos = require("../controllers/equipo.controller.js");
var router = require("express").Router();
// Create a new bloqueo
router.post("/", proxy.authenticateUser, equipos.create);
// Retrieve all bloqueos
router.get("/", proxy.authenticateUser, equipos.findAll);
// Retrieve all published bloqueos
router.get(
"/byBloqueo/:bloqueo",
proxy.authenticateUser,
equipos.findAllByBloqueo
);
// Retrieve a single bloqueo with id
router.get("/:id", proxy.authenticateUser, equipos.findOne);
// Update a bloqueo with id
router.put("/:id", proxy.authenticateUser, equipos.update);
// Delete a bloqueo with id
router.delete("/:id", proxy.authenticateUser, equipos.delete);
// Delete all bloqueos
router.delete("/", proxy.authenticateUser, equipos.deleteAll);
app.use("/api/equipos", router);
};

View File

@@ -0,0 +1,25 @@
const proxy = require("../controllers/proxis");
module.exports = (app) => {
const trabajadores = require("../controllers/trabajador.controller.js");
var router = require("express").Router();
// Create a new bloqueo
router.post("/", proxy.authenticateUser, trabajadores.create);
// Retrieve all bloqueos
router.get("/", proxy.authenticateUser, trabajadores.findAll);
// Retrieve all published bloqueos
router.get(
"/byBloqueo/:bloqueo",
proxy.authenticateUser,
trabajadores.findAllByBloqueo
);
// Retrieve a single bloqueo with id
router.get("/:id", proxy.authenticateUser, trabajadores.findOne);
// Update a bloqueo with id
router.put("/:id", proxy.authenticateUser, trabajadores.update);
// Delete a bloqueo with id
router.delete("/:id", proxy.authenticateUser, trabajadores.delete);
// Delete all bloqueos
router.delete("/", proxy.authenticateUser, trabajadores.deleteAll);
app.use("/api/trabajadores", router);
};

View File

@@ -0,0 +1,19 @@
module.exports = (app) => {
const tutorials = require("../controllers/tutorial.controller.js");
var router = require("express").Router();
// Create a new Tutorial
router.post("/", tutorials.create);
// Retrieve all Tutorials
router.get("/", tutorials.findAll);
// Retrieve all published Tutorials
router.get("/published", tutorials.findAllPublished);
// Retrieve a single Tutorial with id
router.get("/:id", tutorials.findOne);
// Update a Tutorial with id
router.put("/:id", tutorials.update);
// Delete a Tutorial with id
router.delete("/:id", tutorials.delete);
// Delete all Tutorials
router.delete("/", tutorials.deleteAll);
app.use("/api/tutorials", router);
};

View File

@@ -0,0 +1,21 @@
const proxy = require("../controllers/proxis");
module.exports = (app) => {
const usuario = require("../controllers/usuario.controller.js");
var router = require("express").Router();
// Create a new bloqueo
router.post("/", usuario.create);
// Retrieve all bloqueos
router.get("/", usuario.findAll);
// Retrieve all published bloqueos
router.post("/login", usuario.login);
// Retrieve a single bloqueo with id
router.get("/:id", proxy.authenticateUser, usuario.findOne);
// Update a bloqueo with id
router.put("/:id", usuario.update);
// Delete a bloqueo with id
router.delete("/:id", usuario.delete);
// Delete all bloqueos
router.delete("/", usuario.deleteAll);
app.use("/api/usuarios", router);
};

3355
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

28
package.json Normal file
View File

@@ -0,0 +1,28 @@
{
"name": "ls-api",
"version": "1.0.0",
"description": "Lockout Success API",
"main": "server.js",
"scripts": {
"dev": "nodemon server.js"
},
"keywords": [
"node",
"express",
"sequelize",
"mariaDB"
],
"author": "Ciro Bahamonde - Lockout Success",
"license": "ISC",
"dependencies": {
"bcrypt": "^5.0.1",
"cors": "^2.8.5",
"dotenv": "^16.0.1",
"express": "^4.18.1",
"jsonwebtoken": "^8.5.1",
"mariadb": "^3.0.1",
"mysql2": "^2.3.3",
"nodemon": "^2.0.19",
"sequelize": "^6.21.4"
}
}

49
request.rest Normal file
View File

@@ -0,0 +1,49 @@
GET http://localhost:8000/api/usuarios
###
GET http://localhost:8000/api/usuarios/8
Authorization: BEARER eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6Imp1YW5AdGVzdC5jb20iLCJpYXQiOjE2NjE4MDk3NDJ9.ZqxyRnPXF426zr-NvBQdrvLZhu-MSaBXsAo_gmsZ6s0
###
POST http://localhost:8000/api/usuarios
Content-Type: application/json
{
"email": "john@test.com",
"password": "mypass"
}
###
POST http://localhost:8000/api/usuarios/login
Content-Type: application/json
{
"email": "juan@test.com",
"password": "mypass"
}
## TRABAJADORES
###
GET http://localhost:8000/api/trabajadores
Authorization: BEARER eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6Imp1YW5AdGVzdC5jb20iLCJpYXQiOjE2NjE4MTMyOTN9._xo3eCA6NDsKKjCJ1n6YBO6xM0RyHxXuLfP6HK604q4
###
DELETE http://localhost:8000/api/trabajadores/5
Authorization: BEARER eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6Imp1YW5AdGVzdC5jb20iLCJpYXQiOjE2NjE4MTMyOTN9._xo3eCA6NDsKKjCJ1n6YBO6xM0RyHxXuLfP6HK604q4
###
PUT http://localhost:8000/api/trabajadores/12
Authorization: BEARER eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6Imp1YW5AdGVzdC5jb20iLCJpYXQiOjE2NjE4MTMyOTN9._xo3eCA6NDsKKjCJ1n6YBO6xM0RyHxXuLfP6HK604q4
Content-Type: application/json
{
"id": 12,
"rut": "11432448-5",
"empresa": "Coretec SpA"
}
## EQUIPOS
###
GET http://localhost:8000/api/equipos
Authorization: BEARER eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6Imp1YW5AdGVzdC5jb20iLCJpYXQiOjE2NjE4MTMyOTN9._xo3eCA6NDsKKjCJ1n6YBO6xM0RyHxXuLfP6HK604q4

40
server.js Normal file
View File

@@ -0,0 +1,40 @@
require("dotenv").config();
const express = require("express");
const cors = require("cors");
const app = express();
var corsOptions = {
origin: ["http://localhost:3000", "http://localhost:4000"],
};
app.use(cors(corsOptions));
// parse requests of content-type - application/json
app.use(express.json());
// parse requests of content-type - application/x-www-form-urlencoded
app.use(express.urlencoded({ extended: true }));
// simple route
app.get("/", (req, res) => {
res.json({ message: "Welcome to Lockout Success application." });
});
// set port, listen for requests
//require("./app/routes/tutorial.routes")(app);
require("./app/routes/bloqueo.routes")(app);
require("./app/routes/trabajador.routes")(app);
require("./app/routes/candado.routes")(app);
require("./app/routes/equipo.routes")(app);
require("./app/routes/usuario.routes")(app);
const PORT = process.env.PORT || 8000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}.`);
});
const db = require("./app/models");
const { JsonWebTokenError } = require("jsonwebtoken");
db.sequelize
.sync({ alter: true }) //Eliminar alter en ambiente productivo.
.then(() => {
console.log("Synced DB");
})
.catch((err) => {
console.log("Failed to sync DB ", err.message);
});