diff --git a/backend/src/app.js b/backend/src/app.js index ad92af2..273d65a 100644 --- a/backend/src/app.js +++ b/backend/src/app.js @@ -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 => { diff --git a/backend/src/controllers/UserController.js b/backend/src/controllers/UserController.js index d170845..fbbb94b 100644 --- a/backend/src/controllers/UserController.js +++ b/backend/src/controllers/UserController.js @@ -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" }); +}; diff --git a/backend/src/routes/auth.js b/backend/src/routes/auth.js index d845a20..7d0e52d 100644 --- a/backend/src/routes/auth.js +++ b/backend/src/routes/auth.js @@ -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) => { diff --git a/backend/src/routes/users.js b/backend/src/routes/users.js new file mode 100644 index 0000000..7e6d46d --- /dev/null +++ b/backend/src/routes/users.js @@ -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; diff --git a/frontend/src/components/_layout/MainListItems.js b/frontend/src/components/_layout/MainListItems.js index 41dd8c7..abe5727 100644 --- a/frontend/src/components/_layout/MainListItems.js +++ b/frontend/src/components/_layout/MainListItems.js @@ -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={} /> + + Administration + } + /> + } + /> ); }; diff --git a/frontend/src/pages/Signup/index.js b/frontend/src/pages/Signup/index.js index 97f42de..c02e995 100644 --- a/frontend/src/pages/Signup/index.js +++ b/frontend/src/pages/Signup/index.js @@ -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) { diff --git a/frontend/src/translate/languages/en.js b/frontend/src/translate/languages/en.js index f876199..a6feb5b 100644 --- a/frontend/src/translate/languages/en.js +++ b/frontend/src/translate/languages/en.js @@ -141,6 +141,9 @@ const messages = { connection: "Connection", tickets: "Tickets", contacts: "Contacts", + administration: "Administration", + users: "Users", + settings: "Settings", }, }, messagesList: {