From 256262f353bd3306adc287625c7f119fa13e74a6 Mon Sep 17 00:00:00 2001 From: canove Date: Sun, 20 Sep 2020 12:04:21 -0300 Subject: [PATCH] enabled socketio on users controller --- backend/src/controllers/UserController.ts | 19 ++ backend/src/controllersOld/UserController.js | 202 +++++++++---------- 2 files changed, 120 insertions(+), 101 deletions(-) diff --git a/backend/src/controllers/UserController.ts b/backend/src/controllers/UserController.ts index 5adf7b0..d0a5503 100644 --- a/backend/src/controllers/UserController.ts +++ b/backend/src/controllers/UserController.ts @@ -1,4 +1,5 @@ import { Request, Response } from "express"; +import { getIO } from "../libs/socket"; import CheckSettingsHelper from "../helpers/CheckSettings"; import AppError from "../errors/AppError"; @@ -47,6 +48,12 @@ export const store = async (req: Request, res: Response): Promise => { profile }); + const io = getIO(); + io.emit("user", { + action: "create", + user + }); + return res.status(200).json(user); }; @@ -71,6 +78,12 @@ export const update = async ( const user = await UpdateUserService({ userData, userId }); + const io = getIO(); + io.emit("user", { + action: "update", + user + }); + return res.status(200).json(user); }; @@ -86,5 +99,11 @@ export const remove = async ( await DeleteUserService(userId); + const io = getIO(); + io.emit("user", { + action: "delete", + userId + }); + return res.status(200).json({ message: "User deleted" }); }; diff --git a/backend/src/controllersOld/UserController.js b/backend/src/controllersOld/UserController.js index 13f8186..06fd1a1 100644 --- a/backend/src/controllersOld/UserController.js +++ b/backend/src/controllersOld/UserController.js @@ -1,49 +1,49 @@ -// const Sequelize = require("sequelize"); -// const Yup = require("yup"); -// const { Op } = require("sequelize"); +const Sequelize = require("sequelize"); +const Yup = require("yup"); +const { Op } = require("sequelize"); -// const User = require("../models/User"); -// const Setting = require("../models/Setting"); +const User = require("../models/User"); +const Setting = require("../models/Setting"); -// const { getIO } = require("../libs/socket"); +const { getIO } = require("../libs/socket"); -// exports.index = async (req, res) => { -// if (req.user.profile !== "admin") { -// return res -// .status(403) -// .json({ error: "Only administrators can access this route." }); -// } +exports.index = async (req, res) => { + if (req.user.profile !== "admin") { + return res + .status(403) + .json({ error: "Only administrators can access this route." }); + } -// const { searchParam = "", pageNumber = 1 } = req.query; + const { searchParam = "", pageNumber = 1 } = req.query; -// const whereCondition = { -// [Op.or]: [ -// { -// name: Sequelize.where( -// Sequelize.fn("LOWER", Sequelize.col("name")), -// "LIKE", -// "%" + searchParam.toLowerCase() + "%" -// ), -// }, -// { email: { [Op.like]: `%${searchParam.toLowerCase()}%` } }, -// ], -// }; + const whereCondition = { + [Op.or]: [ + { + name: Sequelize.where( + Sequelize.fn("LOWER", Sequelize.col("name")), + "LIKE", + "%" + searchParam.toLowerCase() + "%" + ) + }, + { email: { [Op.like]: `%${searchParam.toLowerCase()}%` } } + ] + }; -// let limit = 20; -// let offset = limit * (pageNumber - 1); + let limit = 20; + let offset = limit * (pageNumber - 1); -// const { count, rows: users } = await User.findAndCountAll({ -// attributes: ["name", "id", "email", "profile"], -// where: whereCondition, -// limit, -// offset, -// order: [["createdAt", "DESC"]], -// }); + const { count, rows: users } = await User.findAndCountAll({ + attributes: ["name", "id", "email", "profile"], + where: whereCondition, + limit, + offset, + order: [["createdAt", "DESC"]] + }); -// const hasMore = count > offset + users.length; + const hasMore = count > offset + users.length; -// return res.status(200).json({ users, count, hasMore }); -// }; + return res.status(200).json({ users, count, hasMore }); +}; export default async (req, res, next) => { console.log(req.url); @@ -95,87 +95,87 @@ export default async (req, res, next) => { return res.status(201).json({ message: "User created!", userId: id }); }; -// exports.show = async (req, res) => { -// const { userId } = req.params; +exports.show = async (req, res) => { + const { userId } = req.params; -// const user = await User.findByPk(userId, { -// attributes: ["id", "name", "email", "profile"], -// }); + const user = await User.findByPk(userId, { + attributes: ["id", "name", "email", "profile"] + }); -// if (!user) { -// res.status(400).json({ error: "No user found with this id." }); -// } + if (!user) { + res.status(400).json({ error: "No user found with this id." }); + } -// return res.status(200).json(user); -// }; + return res.status(200).json(user); +}; -// exports.update = async (req, res) => { -// const schema = Yup.object().shape({ -// name: Yup.string().min(2), -// email: Yup.string().email(), -// password: Yup.string(), -// }); +exports.update = async (req, res) => { + const schema = Yup.object().shape({ + name: Yup.string().min(2), + email: Yup.string().email(), + password: Yup.string() + }); -// if (req.user.profile !== "admin") { -// return res -// .status(403) -// .json({ error: "Only administrators can edit users." }); -// } + if (req.user.profile !== "admin") { + return res + .status(403) + .json({ error: "Only administrators can edit users." }); + } -// await schema.validate(req.body); + await schema.validate(req.body); -// const io = getIO(); -// const { userId } = req.params; + const io = getIO(); + const { userId } = req.params; -// const user = await User.findByPk(userId, { -// attributes: ["name", "id", "email", "profile"], -// }); + const user = await User.findByPk(userId, { + attributes: ["name", "id", "email", "profile"] + }); -// if (!user) { -// res.status(404).json({ error: "No user found with this id." }); -// } + if (!user) { + res.status(404).json({ error: "No user found with this id." }); + } -// if (user.profile === "admin" && req.body.profile === "user") { -// const adminUsers = await User.count({ where: { profile: "admin" } }); -// if (adminUsers <= 1) { -// return res -// .status(403) -// .json({ error: "There must be at leat one admin user." }); -// } -// } + if (user.profile === "admin" && req.body.profile === "user") { + const adminUsers = await User.count({ where: { profile: "admin" } }); + if (adminUsers <= 1) { + return res + .status(403) + .json({ error: "There must be at leat one admin user." }); + } + } -// await user.update(req.body); + await user.update(req.body); -// io.emit("user", { -// action: "update", -// user: user, -// }); + io.emit("user", { + action: "update", + user: user + }); -// return res.status(200).json(user); -// }; + return res.status(200).json(user); +}; -// exports.delete = async (req, res) => { -// const io = getIO(); -// const { userId } = req.params; +exports.delete = async (req, res) => { + const io = getIO(); + const { userId } = req.params; -// const user = await User.findByPk(userId); + const user = await User.findByPk(userId); -// if (!user) { -// res.status(400).json({ error: "No user found with this id." }); -// } + if (!user) { + res.status(400).json({ error: "No user found with this id." }); + } -// if (req.user.profile !== "admin") { -// return res -// .status(403) -// .json({ error: "Only administrators can edit users." }); -// } + if (req.user.profile !== "admin") { + return res + .status(403) + .json({ error: "Only administrators can edit users." }); + } -// await user.destroy(); + await user.destroy(); -// io.emit("user", { -// action: "delete", -// userId: userId, -// }); + io.emit("user", { + action: "delete", + userId: userId + }); -// return res.status(200).json({ message: "User deleted" }); -// }; + return res.status(200).json({ message: "User deleted" }); +};