mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-19 04:09:26 +00:00
feat: create user endpoint
This commit is contained in:
@@ -16,6 +16,7 @@ const ContactsRoutes = require("./routes/contacts");
|
||||
const AuthRoutes = require("./routes/auth");
|
||||
const TicketsRoutes = require("./routes/tickets");
|
||||
const WhatsRoutes = require("./routes/whatsapp");
|
||||
const UsersRoutes = require("./routes/users");
|
||||
|
||||
const app = express();
|
||||
|
||||
@@ -45,6 +46,7 @@ app.use(ContactsRoutes);
|
||||
app.use(TicketsRoutes);
|
||||
app.use(MessagesRoutes);
|
||||
app.use(WhatsRoutes);
|
||||
app.use(UsersRoutes);
|
||||
|
||||
const io = require("./libs/socket").init(server);
|
||||
io.on("connection", socket => {
|
||||
|
||||
@@ -2,6 +2,14 @@ const { validationResult } = require("express-validator");
|
||||
|
||||
const User = require("../models/User");
|
||||
|
||||
exports.index = async (req, res) => {
|
||||
// const { searchParam = "", pageNumber = 1 } = req.query;
|
||||
|
||||
const users = await User.findAll({ attributes: ["name", "id", "email"] });
|
||||
|
||||
return res.status(200).json(users);
|
||||
};
|
||||
|
||||
exports.store = async (req, res, next) => {
|
||||
const errors = validationResult(req);
|
||||
|
||||
@@ -12,6 +20,38 @@ exports.store = async (req, res, next) => {
|
||||
}
|
||||
|
||||
const { name, id, email } = await User.create(req.body);
|
||||
// const result = await user.save();
|
||||
|
||||
res.status(201).json({ message: "User created!", userId: id });
|
||||
};
|
||||
|
||||
exports.update = async (req, res) => {
|
||||
const { userId } = req.params;
|
||||
|
||||
const user = await User.findByPk(userId, {
|
||||
attributes: ["name", "id", "email"],
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
res.status(400).json({ error: "No user found with this id." });
|
||||
}
|
||||
|
||||
await user.update(req.body);
|
||||
|
||||
//todo, send socket IO to users channel.
|
||||
|
||||
res.status(200).json(user);
|
||||
};
|
||||
|
||||
exports.delete = async (req, res) => {
|
||||
const { userId } = req.params;
|
||||
|
||||
const user = await User.findByPk(userId);
|
||||
|
||||
if (!user) {
|
||||
res.status(400).json({ error: "No user found with this id." });
|
||||
}
|
||||
|
||||
await user.destroy();
|
||||
|
||||
res.status(200).json({ message: "User deleted" });
|
||||
};
|
||||
|
||||
@@ -1,32 +1,9 @@
|
||||
const express = require("express");
|
||||
const { body } = require("express-validator");
|
||||
const User = require("../models/User");
|
||||
const SessionController = require("../controllers/SessionController");
|
||||
const UserController = require("../controllers/UserController");
|
||||
const isAuth = require("../middleware/is-auth");
|
||||
|
||||
const routes = express.Router();
|
||||
|
||||
routes.post(
|
||||
"/signup",
|
||||
[
|
||||
body("email")
|
||||
.isEmail()
|
||||
.withMessage("Email inválido")
|
||||
.custom((value, { req }) => {
|
||||
return User.findOne({ where: { email: value } }).then(user => {
|
||||
if (user) {
|
||||
return Promise.reject("An user with this email already exists!");
|
||||
}
|
||||
});
|
||||
})
|
||||
.normalizeEmail(),
|
||||
body("password").trim().isLength({ min: 5 }),
|
||||
body("name").trim().not().isEmpty(),
|
||||
],
|
||||
UserController.store
|
||||
);
|
||||
|
||||
routes.post("/login", SessionController.store);
|
||||
|
||||
routes.get("/check", isAuth, (req, res) => {
|
||||
|
||||
36
backend/src/routes/users.js
Normal file
36
backend/src/routes/users.js
Normal file
@@ -0,0 +1,36 @@
|
||||
const express = require("express");
|
||||
const { body } = require("express-validator");
|
||||
const User = require("../models/User");
|
||||
|
||||
const isAuth = require("../middleware/is-auth");
|
||||
const UserController = require("../controllers/UserController");
|
||||
|
||||
const routes = express.Router();
|
||||
|
||||
routes.get("/users", isAuth, UserController.index);
|
||||
|
||||
routes.post(
|
||||
"/users",
|
||||
[
|
||||
body("email")
|
||||
.isEmail()
|
||||
.withMessage("Email inválido")
|
||||
.custom((value, { req }) => {
|
||||
return User.findOne({ where: { email: value } }).then(user => {
|
||||
if (user) {
|
||||
return Promise.reject("An user with this email already exists!");
|
||||
}
|
||||
});
|
||||
})
|
||||
.normalizeEmail(),
|
||||
body("password").trim().isLength({ min: 5 }),
|
||||
body("name").trim().not().isEmpty(),
|
||||
],
|
||||
UserController.store
|
||||
);
|
||||
|
||||
routes.put("/users/:userId", isAuth, UserController.update);
|
||||
|
||||
routes.delete("/users/:userId", isAuth, UserController.delete);
|
||||
|
||||
module.exports = routes;
|
||||
Reference in New Issue
Block a user