mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-17 19:37:02 +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;
|
||||
@@ -4,9 +4,13 @@ import { Link as RouterLink } from "react-router-dom";
|
||||
import ListItem from "@material-ui/core/ListItem";
|
||||
import ListItemIcon from "@material-ui/core/ListItemIcon";
|
||||
import ListItemText from "@material-ui/core/ListItemText";
|
||||
import ListSubheader from "@material-ui/core/ListSubheader";
|
||||
import Divider from "@material-ui/core/Divider";
|
||||
import DashboardIcon from "@material-ui/icons/Dashboard";
|
||||
import WhatsAppIcon from "@material-ui/icons/WhatsApp";
|
||||
import SyncAltIcon from "@material-ui/icons/SyncAlt";
|
||||
import SettingsIcon from "@material-ui/icons/Settings";
|
||||
import GroupIcon from "@material-ui/icons/Group";
|
||||
|
||||
import ContactPhoneIcon from "@material-ui/icons/ContactPhone";
|
||||
|
||||
@@ -53,6 +57,18 @@ const MainListItems = () => {
|
||||
primary={i18n.t("mainDrawer.listItems.contacts")}
|
||||
icon={<ContactPhoneIcon />}
|
||||
/>
|
||||
<Divider />
|
||||
<ListSubheader inset>Administration</ListSubheader>
|
||||
<ListItemLink
|
||||
to="/chat"
|
||||
primary={i18n.t("mainDrawer.listItems.users")}
|
||||
icon={<GroupIcon />}
|
||||
/>
|
||||
<ListItemLink
|
||||
to="/chat"
|
||||
primary={i18n.t("mainDrawer.listItems.settings")}
|
||||
icon={<SettingsIcon />}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
@@ -66,7 +66,7 @@ const SignUp = () => {
|
||||
const handleSignUp = async e => {
|
||||
e.preventDefault();
|
||||
try {
|
||||
await api.post("/auth/signup", user);
|
||||
await api.post("/users", user);
|
||||
toast.success(i18n.t("signup.toasts.success"));
|
||||
history.push("/login");
|
||||
} catch (err) {
|
||||
|
||||
@@ -141,6 +141,9 @@ const messages = {
|
||||
connection: "Connection",
|
||||
tickets: "Tickets",
|
||||
contacts: "Contacts",
|
||||
administration: "Administration",
|
||||
users: "Users",
|
||||
settings: "Settings",
|
||||
},
|
||||
},
|
||||
messagesList: {
|
||||
|
||||
Reference in New Issue
Block a user