| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- import { Request, Response } from "express";
- import { getIO } from "../libs/socket";
- import CheckSettingsHelper from "../helpers/CheckSettings";
- import AppError from "../errors/AppError";
- import CreateUserService from "../services/UserServices/CreateUserService";
- import ListUsersService from "../services/UserServices/ListUsersService";
- import UpdateUserService from "../services/UserServices/UpdateUserService";
- import ShowUserService from "../services/UserServices/ShowUserService";
- import DeleteUserService from "../services/UserServices/DeleteUserService";
- import SimpleListService from "../services/UserServices/SimpleListService";
- import User from "../models/User";
- import SetLanguageCompanyService from "../services/UserServices/SetLanguageCompanyService";
- type IndexQuery = {
- searchParam: string;
- pageNumber: string;
- };
- type ListQueryParams = {
- companyId: string;
- };
- export const index = async (req: Request, res: Response): Promise<Response> => {
- const { searchParam, pageNumber } = req.query as IndexQuery;
- const { companyId, profile } = req.user;
- const { users, count, hasMore } = await ListUsersService({
- searchParam,
- pageNumber,
- companyId,
- profile
- });
- return res.json({ users, count, hasMore });
- };
- export const store = async (req: Request, res: Response): Promise<Response> => {
- const {
- email,
- password,
- name,
- profile,
- companyId: bodyCompanyId,
- queueIds,
- whatsappId,
- allTicket
- } = req.body;
- let userCompanyId: number | null = null;
- let requestUser: User = null;
- if (req.user !== undefined) {
- const { companyId: cId } = req.user;
- userCompanyId = cId;
- requestUser = await User.findByPk(req.user.id);
- }
- const newUserCompanyId = bodyCompanyId || userCompanyId;
- if (req.url === "/signup") {
- if (await CheckSettingsHelper("userCreation") === "disabled") {
- throw new AppError("ERR_USER_CREATION_DISABLED", 403);
- }
- } else if (req.user?.profile !== "admin") {
- throw new AppError("ERR_NO_PERMISSION", 403);
- } else if (newUserCompanyId !== req.user?.companyId && !requestUser?.super) {
- throw new AppError("ERR_NO_SUPER", 403);
- }
- const user = await CreateUserService({
- email,
- password,
- name,
- profile,
- companyId: newUserCompanyId,
- queueIds,
- whatsappId,
- allTicket
- });
- const io = getIO();
- io.to(`company-${userCompanyId}-mainchannel`).emit(`company-${userCompanyId}-user`, {
- action: "create",
- user
- });
- return res.status(200).json(user);
- };
- export const show = async (req: Request, res: Response): Promise<Response> => {
- const { userId } = req.params;
- const user = await ShowUserService(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("ERR_NO_PERMISSION", 403);
- }
- const { id: requestUserId, companyId } = req.user;
- const { userId } = req.params;
- const userData = req.body;
- const user = await UpdateUserService({
- userData,
- userId,
- companyId,
- requestUserId: +requestUserId
- });
- const io = getIO();
- io.to(`company-${companyId}-mainchannel`).emit(`company-${companyId}-user`, {
- action: "update",
- user
- });
- return res.status(200).json(user);
- };
- export const remove = async (
- req: Request,
- res: Response
- ): Promise<Response> => {
- const { userId } = req.params;
- const { companyId } = req.user;
- if (req.user.profile !== "admin") {
- throw new AppError("ERR_NO_PERMISSION", 403);
- }
- await DeleteUserService(userId, companyId);
- const io = getIO();
- io.to(`company-${companyId}-mainchannel`).emit(`company-${companyId}-user`, {
- action: "delete",
- userId
- });
- return res.status(200).json({ message: "User deleted" });
- };
- export const list = async (req: Request, res: Response): Promise<Response> => {
- const { companyId } = req.query;
- const { companyId: userCompanyId } = req.user;
- const users = await SimpleListService({
- companyId: companyId ? +companyId : userCompanyId
- });
- return res.status(200).json(users);
- };
- export const setLanguage = async (req: Request, res: Response): Promise<Response> => {
- const { companyId } = req.user;
- const {newLanguage} = req.params;
- if( newLanguage !== "pt" && newLanguage !== "en" && newLanguage !== "es" )
- throw new AppError("ERR_INTERNAL_SERVER_ERROR", 500);
- await SetLanguageCompanyService( companyId, newLanguage );
- return res.status(200).json({message: "Language updated successfully"});
- }
|