started migration of user domain to ts

This commit is contained in:
canove
2020-09-14 12:42:20 -03:00
parent 32f7e48362
commit f18bab145f
28 changed files with 781 additions and 340 deletions

View File

@@ -0,0 +1,181 @@
// const Sequelize = require("sequelize");
// const Yup = require("yup");
// const { Op } = require("sequelize");
// const User = require("../models/User");
// const Setting = require("../models/Setting");
// 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." });
// }
// 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()}%` } },
// ],
// };
// 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 hasMore = count > offset + users.length;
// return res.status(200).json({ users, count, hasMore });
// };
export default async (req, res, next) => {
console.log(req.url);
const schema = Yup.object().shape({
name: Yup.string().required().min(2),
email: Yup.string()
.email()
.required()
.test(
"Check-email",
"An user with this email already exists",
async value => {
const userFound = await User.findOne({ where: { email: value } });
return !Boolean(userFound);
}
),
password: Yup.string().required().min(5)
});
if (req.url === "/signup") {
const { value: userCreation } = await Setting.findByPk("userCreation");
if (userCreation === "disabled") {
return res
.status(403)
.json({ error: "User creation is disabled by administrator." });
}
} else if (req.user.profile !== "admin") {
return res
.status(403)
.json({ error: "Only administrators can create users." });
}
try {
await schema.validate(req.body);
} catch (err) {
return res.status(400).json({ error: err.message });
}
const io = getIO();
const { name, id, email, profile } = await User.create(req.body);
io.emit("user", {
action: "create",
user: { name, id, email, profile }
});
return res.status(201).json({ message: "User created!", userId: id });
};
// exports.show = async (req, res) => {
// const { userId } = req.params;
// const user = await User.findByPk(userId, {
// attributes: ["id", "name", "email", "profile"],
// });
// if (!user) {
// res.status(400).json({ error: "No user found with this id." });
// }
// 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(),
// });
// if (req.user.profile !== "admin") {
// return res
// .status(403)
// .json({ error: "Only administrators can edit users." });
// }
// await schema.validate(req.body);
// const io = getIO();
// const { userId } = req.params;
// 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.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);
// io.emit("user", {
// action: "update",
// user: user,
// });
// return res.status(200).json(user);
// };
// exports.delete = async (req, res) => {
// const io = getIO();
// const { userId } = req.params;
// const user = await User.findByPk(userId);
// 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." });
// }
// await user.destroy();
// io.emit("user", {
// action: "delete",
// userId: userId,
// });
// return res.status(200).json({ message: "User deleted" });
// };

View File

@@ -1,181 +0,0 @@
const Sequelize = require("sequelize");
const Yup = require("yup");
const { Op } = require("sequelize");
const User = require("../models/User");
const Setting = require("../models/Setting");
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." });
}
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()}%` } },
],
};
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 hasMore = count > offset + users.length;
return res.status(200).json({ users, count, hasMore });
};
exports.store = async (req, res, next) => {
console.log(req.url);
const schema = Yup.object().shape({
name: Yup.string().required().min(2),
email: Yup.string()
.email()
.required()
.test(
"Check-email",
"An user with this email already exists",
async value => {
const userFound = await User.findOne({ where: { email: value } });
return !Boolean(userFound);
}
),
password: Yup.string().required().min(5),
});
if (req.url === "/signup") {
const { value: userCreation } = await Setting.findByPk("userCreation");
if (userCreation === "disabled") {
return res
.status(403)
.json({ error: "User creation is disabled by administrator." });
}
} else if (req.user.profile !== "admin") {
return res
.status(403)
.json({ error: "Only administrators can create users." });
}
try {
await schema.validate(req.body);
} catch (err) {
return res.status(400).json({ error: err.message });
}
const io = getIO();
const { name, id, email, profile } = await User.create(req.body);
io.emit("user", {
action: "create",
user: { name, id, email, profile },
});
return res.status(201).json({ message: "User created!", userId: id });
};
exports.show = async (req, res) => {
const { userId } = req.params;
const user = await User.findByPk(userId, {
attributes: ["id", "name", "email", "profile"],
});
if (!user) {
res.status(400).json({ error: "No user found with this id." });
}
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(),
});
if (req.user.profile !== "admin") {
return res
.status(403)
.json({ error: "Only administrators can edit users." });
}
await schema.validate(req.body);
const io = getIO();
const { userId } = req.params;
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.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);
io.emit("user", {
action: "update",
user: user,
});
return res.status(200).json(user);
};
exports.delete = async (req, res) => {
const io = getIO();
const { userId } = req.params;
const user = await User.findByPk(userId);
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." });
}
await user.destroy();
io.emit("user", {
action: "delete",
userId: userId,
});
return res.status(200).json({ message: "User deleted" });
};

View File

@@ -0,0 +1,69 @@
import { Request, Response } from "express";
// import CheckSettingsHelper from "../helpers/CheckSettingsHelper";
import AppError from "../errors/AppError";
import CreateUserService from "../services/CreateUserService";
// import UpdateUserService from "../services/UpdateUserService";
// import ListUsersService from "../services/ListUsersService";
// import FindUserService from "../services/FindUserService";
export const index = async (req: Request, res: Response): Promise<Response> => {
if (req.user.profile !== "admin") {
throw new AppError("Only administrators can access this route.", 403); // should be handled better.
}
const { searchParam, pageNumber } = req.query as any;
const { users, count, hasMore } = await ListUsersService({
searchParam,
pageNumber
});
return res.json({ users, count, hasMore });
};
export const store = async (req: Request, res: Response): Promise<Response> => {
const { email, password, name, profile } = req.body;
// if (
// req.url === "/signup" &&
// (await CheckSettingsHelper("userCreation")) === "disabled"
// ) {
// throw new AppError("User creation is disabled by administrator.", 403);
// } else if (req.user.profile !== "admin") {
// throw new AppError("Only administrators can create users.", 403);
// }
const user = await CreateUserService({
email,
password,
name,
profile
});
return res.status(200).json(user);
};
export const show = async (req: Request, res: Response): Promise<Response> => {
const { userId } = req.params;
const user = await FindUserService(userId);
return res.status(200).json(user);
};
export const update = async (
req: Request,
res: Response
): Promise<Response> => {
if (req.user.profile !== "admin") {
throw new AppError("Only administrators can edit users.", 403);
}
const { userId } = req.params;
const userData = req.body;
const user = await UpdateUserService({ userData, userId });
return res.status(200).json(user);
};