UserController.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import { Request, Response } from "express";
  2. import { getIO } from "../libs/socket";
  3. import CheckSettingsHelper from "../helpers/CheckSettings";
  4. import AppError from "../errors/AppError";
  5. import CreateUserService from "../services/UserServices/CreateUserService";
  6. import ListUsersService from "../services/UserServices/ListUsersService";
  7. import UpdateUserService from "../services/UserServices/UpdateUserService";
  8. import ShowUserService from "../services/UserServices/ShowUserService";
  9. import DeleteUserService from "../services/UserServices/DeleteUserService";
  10. import SimpleListService from "../services/UserServices/SimpleListService";
  11. import User from "../models/User";
  12. import SetLanguageCompanyService from "../services/UserServices/SetLanguageCompanyService";
  13. type IndexQuery = {
  14. searchParam: string;
  15. pageNumber: string;
  16. };
  17. type ListQueryParams = {
  18. companyId: string;
  19. };
  20. export const index = async (req: Request, res: Response): Promise<Response> => {
  21. const { searchParam, pageNumber } = req.query as IndexQuery;
  22. const { companyId, profile } = req.user;
  23. const { users, count, hasMore } = await ListUsersService({
  24. searchParam,
  25. pageNumber,
  26. companyId,
  27. profile
  28. });
  29. return res.json({ users, count, hasMore });
  30. };
  31. export const store = async (req: Request, res: Response): Promise<Response> => {
  32. const {
  33. email,
  34. password,
  35. name,
  36. profile,
  37. companyId: bodyCompanyId,
  38. queueIds,
  39. whatsappId,
  40. allTicket
  41. } = req.body;
  42. let userCompanyId: number | null = null;
  43. let requestUser: User = null;
  44. if (req.user !== undefined) {
  45. const { companyId: cId } = req.user;
  46. userCompanyId = cId;
  47. requestUser = await User.findByPk(req.user.id);
  48. }
  49. const newUserCompanyId = bodyCompanyId || userCompanyId;
  50. if (req.url === "/signup") {
  51. if (await CheckSettingsHelper("userCreation") === "disabled") {
  52. throw new AppError("ERR_USER_CREATION_DISABLED", 403);
  53. }
  54. } else if (req.user?.profile !== "admin") {
  55. throw new AppError("ERR_NO_PERMISSION", 403);
  56. } else if (newUserCompanyId !== req.user?.companyId && !requestUser?.super) {
  57. throw new AppError("ERR_NO_SUPER", 403);
  58. }
  59. const user = await CreateUserService({
  60. email,
  61. password,
  62. name,
  63. profile,
  64. companyId: newUserCompanyId,
  65. queueIds,
  66. whatsappId,
  67. allTicket
  68. });
  69. const io = getIO();
  70. io.to(`company-${userCompanyId}-mainchannel`).emit(`company-${userCompanyId}-user`, {
  71. action: "create",
  72. user
  73. });
  74. return res.status(200).json(user);
  75. };
  76. export const show = async (req: Request, res: Response): Promise<Response> => {
  77. const { userId } = req.params;
  78. const user = await ShowUserService(userId);
  79. return res.status(200).json(user);
  80. };
  81. export const update = async (
  82. req: Request,
  83. res: Response
  84. ): Promise<Response> => {
  85. if (req.user.profile !== "admin") {
  86. throw new AppError("ERR_NO_PERMISSION", 403);
  87. }
  88. const { id: requestUserId, companyId } = req.user;
  89. const { userId } = req.params;
  90. const userData = req.body;
  91. const user = await UpdateUserService({
  92. userData,
  93. userId,
  94. companyId,
  95. requestUserId: +requestUserId
  96. });
  97. const io = getIO();
  98. io.to(`company-${companyId}-mainchannel`).emit(`company-${companyId}-user`, {
  99. action: "update",
  100. user
  101. });
  102. return res.status(200).json(user);
  103. };
  104. export const remove = async (
  105. req: Request,
  106. res: Response
  107. ): Promise<Response> => {
  108. const { userId } = req.params;
  109. const { companyId } = req.user;
  110. if (req.user.profile !== "admin") {
  111. throw new AppError("ERR_NO_PERMISSION", 403);
  112. }
  113. await DeleteUserService(userId, companyId);
  114. const io = getIO();
  115. io.to(`company-${companyId}-mainchannel`).emit(`company-${companyId}-user`, {
  116. action: "delete",
  117. userId
  118. });
  119. return res.status(200).json({ message: "User deleted" });
  120. };
  121. export const list = async (req: Request, res: Response): Promise<Response> => {
  122. const { companyId } = req.query;
  123. const { companyId: userCompanyId } = req.user;
  124. const users = await SimpleListService({
  125. companyId: companyId ? +companyId : userCompanyId
  126. });
  127. return res.status(200).json(users);
  128. };
  129. export const setLanguage = async (req: Request, res: Response): Promise<Response> => {
  130. const { companyId } = req.user;
  131. const {newLanguage} = req.params;
  132. if( newLanguage !== "pt" && newLanguage !== "en" && newLanguage !== "es" )
  133. throw new AppError("ERR_INTERNAL_SERVER_ERROR", 500);
  134. await SetLanguageCompanyService( companyId, newLanguage );
  135. return res.status(200).json({message: "Language updated successfully"});
  136. }