enabled socketio on users controller

This commit is contained in:
canove
2020-09-20 12:04:21 -03:00
parent 95c6dcb32f
commit 256262f353
2 changed files with 120 additions and 101 deletions

View File

@@ -1,4 +1,5 @@
import { Request, Response } from "express"; import { Request, Response } from "express";
import { getIO } from "../libs/socket";
import CheckSettingsHelper from "../helpers/CheckSettings"; import CheckSettingsHelper from "../helpers/CheckSettings";
import AppError from "../errors/AppError"; import AppError from "../errors/AppError";
@@ -47,6 +48,12 @@ export const store = async (req: Request, res: Response): Promise<Response> => {
profile profile
}); });
const io = getIO();
io.emit("user", {
action: "create",
user
});
return res.status(200).json(user); return res.status(200).json(user);
}; };
@@ -71,6 +78,12 @@ export const update = async (
const user = await UpdateUserService({ userData, userId }); const user = await UpdateUserService({ userData, userId });
const io = getIO();
io.emit("user", {
action: "update",
user
});
return res.status(200).json(user); return res.status(200).json(user);
}; };
@@ -86,5 +99,11 @@ export const remove = async (
await DeleteUserService(userId); await DeleteUserService(userId);
const io = getIO();
io.emit("user", {
action: "delete",
userId
});
return res.status(200).json({ message: "User deleted" }); return res.status(200).json({ message: "User deleted" });
}; };

View File

@@ -1,49 +1,49 @@
// const Sequelize = require("sequelize"); const Sequelize = require("sequelize");
// const Yup = require("yup"); const Yup = require("yup");
// const { Op } = require("sequelize"); const { Op } = require("sequelize");
// const User = require("../models/User"); const User = require("../models/User");
// const Setting = require("../models/Setting"); const Setting = require("../models/Setting");
// const { getIO } = require("../libs/socket"); const { getIO } = require("../libs/socket");
// exports.index = async (req, res) => { exports.index = async (req, res) => {
// if (req.user.profile !== "admin") { if (req.user.profile !== "admin") {
// return res return res
// .status(403) .status(403)
// .json({ error: "Only administrators can access this route." }); .json({ error: "Only administrators can access this route." });
// } }
// const { searchParam = "", pageNumber = 1 } = req.query; const { searchParam = "", pageNumber = 1 } = req.query;
// const whereCondition = { const whereCondition = {
// [Op.or]: [ [Op.or]: [
// { {
// name: Sequelize.where( name: Sequelize.where(
// Sequelize.fn("LOWER", Sequelize.col("name")), Sequelize.fn("LOWER", Sequelize.col("name")),
// "LIKE", "LIKE",
// "%" + searchParam.toLowerCase() + "%" "%" + searchParam.toLowerCase() + "%"
// ), )
// }, },
// { email: { [Op.like]: `%${searchParam.toLowerCase()}%` } }, { email: { [Op.like]: `%${searchParam.toLowerCase()}%` } }
// ], ]
// }; };
// let limit = 20; let limit = 20;
// let offset = limit * (pageNumber - 1); let offset = limit * (pageNumber - 1);
// const { count, rows: users } = await User.findAndCountAll({ const { count, rows: users } = await User.findAndCountAll({
// attributes: ["name", "id", "email", "profile"], attributes: ["name", "id", "email", "profile"],
// where: whereCondition, where: whereCondition,
// limit, limit,
// offset, offset,
// order: [["createdAt", "DESC"]], 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) => { export default async (req, res, next) => {
console.log(req.url); console.log(req.url);
@@ -95,87 +95,87 @@ export default async (req, res, next) => {
return res.status(201).json({ message: "User created!", userId: id }); return res.status(201).json({ message: "User created!", userId: id });
}; };
// exports.show = async (req, res) => { exports.show = async (req, res) => {
// const { userId } = req.params; const { userId } = req.params;
// const user = await User.findByPk(userId, { const user = await User.findByPk(userId, {
// attributes: ["id", "name", "email", "profile"], attributes: ["id", "name", "email", "profile"]
// }); });
// if (!user) { if (!user) {
// res.status(400).json({ error: "No user found with this id." }); 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) => { exports.update = async (req, res) => {
// const schema = Yup.object().shape({ const schema = Yup.object().shape({
// name: Yup.string().min(2), name: Yup.string().min(2),
// email: Yup.string().email(), email: Yup.string().email(),
// password: Yup.string(), password: Yup.string()
// }); });
// if (req.user.profile !== "admin") { if (req.user.profile !== "admin") {
// return res return res
// .status(403) .status(403)
// .json({ error: "Only administrators can edit users." }); .json({ error: "Only administrators can edit users." });
// } }
// await schema.validate(req.body); await schema.validate(req.body);
// const io = getIO(); const io = getIO();
// const { userId } = req.params; const { userId } = req.params;
// const user = await User.findByPk(userId, { const user = await User.findByPk(userId, {
// attributes: ["name", "id", "email", "profile"], attributes: ["name", "id", "email", "profile"]
// }); });
// if (!user) { if (!user) {
// res.status(404).json({ error: "No user found with this id." }); res.status(404).json({ error: "No user found with this id." });
// } }
// if (user.profile === "admin" && req.body.profile === "user") { if (user.profile === "admin" && req.body.profile === "user") {
// const adminUsers = await User.count({ where: { profile: "admin" } }); const adminUsers = await User.count({ where: { profile: "admin" } });
// if (adminUsers <= 1) { if (adminUsers <= 1) {
// return res return res
// .status(403) .status(403)
// .json({ error: "There must be at leat one admin user." }); .json({ error: "There must be at leat one admin user." });
// } }
// } }
// await user.update(req.body); await user.update(req.body);
// io.emit("user", { io.emit("user", {
// action: "update", action: "update",
// user: user, user: user
// }); });
// return res.status(200).json(user); return res.status(200).json(user);
// }; };
// exports.delete = async (req, res) => { exports.delete = async (req, res) => {
// const io = getIO(); const io = getIO();
// const { userId } = req.params; const { userId } = req.params;
// const user = await User.findByPk(userId); const user = await User.findByPk(userId);
// if (!user) { if (!user) {
// res.status(400).json({ error: "No user found with this id." }); res.status(400).json({ error: "No user found with this id." });
// } }
// if (req.user.profile !== "admin") { if (req.user.profile !== "admin") {
// return res return res
// .status(403) .status(403)
// .json({ error: "Only administrators can edit users." }); .json({ error: "Only administrators can edit users." });
// } }
// await user.destroy(); await user.destroy();
// io.emit("user", { io.emit("user", {
// action: "delete", action: "delete",
// userId: userId, userId: userId
// }); });
// return res.status(200).json({ message: "User deleted" }); return res.status(200).json({ message: "User deleted" });
// }; };