Atendimentos.Controller.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. import { NotFoundError } from "../shared/errors/index.js";
  2. import {
  3. fetchAtendimentoJson,
  4. salvarAtendimento,
  5. sincronizarProtocolosAbertos,
  6. sincronizarTodosProtocolosAbertos,
  7. sincronizarAtendimentoPorId,
  8. listarAtendimentos,
  9. obterAtendimento
  10. } from "../services/atendimentosService.js";
  11. import {
  12. avaliarAtendimento,
  13. avaliarPendentes,
  14. listarAvaliacoes,
  15. estatisticasAvaliacoes,
  16. listarSetores
  17. } from "../services/atendimentoAvaliacaoService.js";
  18. import { gerarRelatorioNaoResolvidosPdf } from "../services/relatorioNaoResolvidosPdfService.js";
  19. import { listarGoldenSet, salvarGoldenLabel } from "../services/atendimentoGoldenLabelService.js";
  20. import { estruturarMensagem, montarUrlMidia } from "../utils/atendimentoFormat.js";
  21. import { parseFiltrosAvaliacoes } from "../utils/atendimentoAvaliacaoFiltros.js";
  22. import { config } from "../config/index.js";
  23. export const AtendimentosController = {
  24. Ingerir: async function (req, res, next) {
  25. try {
  26. const { atendimento } = req.body;
  27. const result = await salvarAtendimento({ payload: atendimento });
  28. res.json({ ok: true, ...result });
  29. } catch (err) {
  30. next(err);
  31. }
  32. },
  33. IngerirUrl: async function (req, res, next) {
  34. try {
  35. const { url } = req.body;
  36. const payload = await fetchAtendimentoJson(url);
  37. const result = await salvarAtendimento({ payload, sourceUrl: url });
  38. res.json({ ok: true, ...result });
  39. } catch (err) {
  40. next(err);
  41. }
  42. },
  43. Sincronizar: async function (req, res, next) {
  44. try {
  45. const result = await sincronizarProtocolosAbertos(req.body);
  46. res.json({ ok: true, ...result });
  47. } catch (err) {
  48. next(err);
  49. }
  50. },
  51. SincronizarTudo: async function (req, res, next) {
  52. try {
  53. const result = await sincronizarTodosProtocolosAbertos(req.body);
  54. res.json({ ok: true, ...result });
  55. } catch (err) {
  56. next(err);
  57. }
  58. },
  59. Listar: async function (req, res, next) {
  60. try {
  61. const page = Math.max(1, Number(req.query.page) || 1);
  62. const pageSize = Math.min(100, Math.max(1, Number(req.query.pageSize) || 20));
  63. const setor = typeof req.query.setor === "string" && req.query.setor.trim() ? req.query.setor.trim() : undefined;
  64. const status = req.query.status !== undefined ? Number(req.query.status) : undefined;
  65. const result = await listarAtendimentos({
  66. page,
  67. pageSize,
  68. setor,
  69. status: Number.isInteger(status) ? status : undefined
  70. });
  71. res.json(result);
  72. } catch (err) {
  73. next(err);
  74. }
  75. },
  76. Obter: async function (req, res, next) {
  77. try {
  78. const atendimento = await obterAtendimento(req.params.id);
  79. if (!atendimento) throw new NotFoundError("atendimento_not_found");
  80. const mensagens = (atendimento.mensagens ?? [])
  81. .map((m) => {
  82. const { papel, autor, texto } = estruturarMensagem(m);
  83. if (!texto) return null;
  84. const transcricao = typeof m.Transcricao === "string" && m.Transcricao.trim() ? m.Transcricao.trim() : null;
  85. return {
  86. Id: m.Id,
  87. Papel: papel,
  88. Autor: autor,
  89. Texto: texto,
  90. Transcricao: transcricao,
  91. Timestamp: m.Timestamp,
  92. Tipodemidia: m.Tipodemidia,
  93. Midia: m.Midia,
  94. MidiaUrl: montarUrlMidia(m, config.ifbot.mediaBaseUrl)
  95. };
  96. })
  97. .filter(Boolean);
  98. res.json({ ...atendimento.toJSON(), mensagens });
  99. } catch (err) {
  100. next(err);
  101. }
  102. },
  103. AvaliarPendentes: async function (req, res, next) {
  104. try {
  105. const result = await avaliarPendentes(req.body);
  106. res.json({ ok: true, ...result });
  107. } catch (err) {
  108. next(err);
  109. }
  110. },
  111. AvaliarUm: async function (req, res, next) {
  112. try {
  113. const result = await avaliarAtendimento(req.params.id);
  114. res.json({ ok: true, ...result });
  115. } catch (err) {
  116. next(err);
  117. }
  118. },
  119. SincronizarUm: async function (req, res, next) {
  120. try {
  121. const result = await sincronizarAtendimentoPorId(req.params.id);
  122. res.json({ ok: true, ...result });
  123. } catch (err) {
  124. next(err);
  125. }
  126. },
  127. ListarAvaliacoes: async function (req, res, next) {
  128. try {
  129. const page = Math.max(1, Number(req.query.page) || 1);
  130. const pageSize = Math.min(100, Math.max(1, Number(req.query.pageSize) || 20));
  131. const filtros = parseFiltrosAvaliacoes(req.query);
  132. const result = await listarAvaliacoes({ page, pageSize, ...filtros });
  133. res.json(result);
  134. } catch (err) {
  135. next(err);
  136. }
  137. },
  138. EstatisticasAvaliacoes: async function (req, res, next) {
  139. try {
  140. const filtros = parseFiltrosAvaliacoes(req.query);
  141. const result = await estatisticasAvaliacoes(filtros);
  142. res.json(result);
  143. } catch (err) {
  144. next(err);
  145. }
  146. },
  147. ListarSetoresAvaliacoes: async function (req, res, next) {
  148. try {
  149. const results = await listarSetores();
  150. res.json({ results });
  151. } catch (err) {
  152. next(err);
  153. }
  154. },
  155. GerarRelatorioNaoResolvidos: async function (req, res, next) {
  156. try {
  157. const filtros = parseFiltrosAvaliacoes(req.query);
  158. const { buffer } = await gerarRelatorioNaoResolvidosPdf({ ...filtros, resolvido: "nao" });
  159. const dataArquivo = new Date().toISOString().slice(0, 10);
  160. res.setHeader("Content-Type", "application/pdf");
  161. res.setHeader("Content-Disposition", `attachment; filename="atendimentos-nao-resolvidos-${dataArquivo}.pdf"`);
  162. res.setHeader("Content-Length", buffer.length);
  163. res.send(buffer);
  164. } catch (err) {
  165. next(err);
  166. }
  167. },
  168. ListarGoldenSet: async function (req, res, next) {
  169. try {
  170. const status = typeof req.query.status === "string" && req.query.status.trim() ? req.query.status.trim() : undefined;
  171. const setor = typeof req.query.setor === "string" && req.query.setor.trim() ? req.query.setor.trim() : undefined;
  172. const results = await listarGoldenSet({ status, setor });
  173. res.json({ results });
  174. } catch (err) {
  175. next(err);
  176. }
  177. },
  178. SalvarGoldenLabel: async function (req, res, next) {
  179. try {
  180. const result = await salvarGoldenLabel(req.params.id, req.body);
  181. res.json(result);
  182. } catch (err) {
  183. next(err);
  184. }
  185. }
  186. };
  187. export default AtendimentosController;