TagController.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import { Request, Response } from "express";
  2. import { getIO } from "../libs/socket";
  3. import AppError from "../errors/AppError";
  4. import CreateService from "../services/TagServices/CreateService";
  5. import ListService from "../services/TagServices/ListService";
  6. import UpdateService from "../services/TagServices/UpdateService";
  7. import ShowService from "../services/TagServices/ShowService";
  8. import DeleteService from "../services/TagServices/DeleteService";
  9. import SimpleListService from "../services/TagServices/SimpleListService";
  10. import SyncTagService from "../services/TagServices/SyncTagsService";
  11. import KanbanListService from "../services/TagServices/KanbanListService";
  12. type IndexQuery = {
  13. searchParam?: string;
  14. pageNumber?: string | number;
  15. kanban?: number;
  16. };
  17. export const index = async (req: Request, res: Response): Promise<Response> => {
  18. const { pageNumber, searchParam } = req.query as IndexQuery;
  19. const { companyId } = req.user;
  20. const { tags, count, hasMore } = await ListService({
  21. searchParam,
  22. pageNumber,
  23. companyId
  24. });
  25. return res.json({ tags, count, hasMore });
  26. };
  27. export const store = async (req: Request, res: Response): Promise<Response> => {
  28. const { name, color, kanban } = req.body;
  29. const { companyId } = req.user;
  30. const tag = await CreateService({
  31. name,
  32. color,
  33. companyId,
  34. kanban
  35. });
  36. const io = getIO();
  37. io.to(`company-${companyId}-mainchannel`).emit("tag", {
  38. action: "create",
  39. tag
  40. });
  41. return res.status(200).json(tag);
  42. };
  43. export const kanban = async (req: Request, res: Response): Promise<Response> => {
  44. const { companyId } = req.user;
  45. const tags = await KanbanListService({ companyId });
  46. return res.json({lista:tags});
  47. };
  48. export const show = async (req: Request, res: Response): Promise<Response> => {
  49. const { tagId } = req.params;
  50. const tag = await ShowService(tagId);
  51. return res.status(200).json(tag);
  52. };
  53. export const update = async (
  54. req: Request,
  55. res: Response
  56. ): Promise<Response> => {
  57. if (req.user.profile !== "admin") {
  58. throw new AppError("ERR_NO_PERMISSION", 403);
  59. }
  60. const { tagId } = req.params;
  61. const tagData = req.body;
  62. const tag = await UpdateService({ tagData, id: tagId });
  63. const io = getIO();
  64. io.to(`company-${req.user.companyId}-mainchannel`).emit("tag", {
  65. action: "update",
  66. tag
  67. });
  68. return res.status(200).json(tag);
  69. };
  70. export const remove = async (
  71. req: Request,
  72. res: Response
  73. ): Promise<Response> => {
  74. const { tagId } = req.params;
  75. await DeleteService(tagId);
  76. const io = getIO();
  77. io.to(`company-${req.user.companyId}-mainchannel`).emit("tag", {
  78. action: "delete",
  79. tagId
  80. });
  81. return res.status(200).json({ message: "Tag deleted" });
  82. };
  83. export const list = async (req: Request, res: Response): Promise<Response> => {
  84. const { searchParam } = req.query as IndexQuery;
  85. const { companyId } = req.user;
  86. const tags = await SimpleListService({ searchParam, companyId });
  87. return res.json(tags);
  88. };
  89. export const syncTags = async (
  90. req: Request,
  91. res: Response
  92. ): Promise<Response> => {
  93. const data = req.body;
  94. const { companyId } = req.user;
  95. const tags = await SyncTagService({ ...data, companyId });
  96. return res.json(tags);
  97. };