You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 lines
3.9 KiB

11 months ago
  1. const { EmbedChats } = require("../models/embedChats");
  2. const { EmbedConfig } = require("../models/embedConfig");
  3. const { EventLogs } = require("../models/eventLogs");
  4. const { reqBody, userFromSession } = require("../utils/http");
  5. const { validEmbedConfigId } = require("../utils/middleware/embedMiddleware");
  6. const {
  7. flexUserRoleValid,
  8. ROLES,
  9. } = require("../utils/middleware/multiUserProtected");
  10. const { validatedRequest } = require("../utils/middleware/validatedRequest");
  11. const {
  12. chatHistoryViewable,
  13. } = require("../utils/middleware/chatHistoryViewable");
  14. function embedManagementEndpoints(app) {
  15. if (!app) return;
  16. app.get(
  17. "/embeds",
  18. [validatedRequest, flexUserRoleValid([ROLES.admin])],
  19. async (_, response) => {
  20. try {
  21. const embeds = await EmbedConfig.whereWithWorkspace({}, null, {
  22. createdAt: "desc",
  23. });
  24. response.status(200).json({ embeds });
  25. } catch (e) {
  26. console.error(e);
  27. response.sendStatus(500).end();
  28. }
  29. }
  30. );
  31. app.post(
  32. "/embeds/new",
  33. [validatedRequest, flexUserRoleValid([ROLES.admin])],
  34. async (request, response) => {
  35. try {
  36. const user = await userFromSession(request, response);
  37. const data = reqBody(request);
  38. const { embed, message: error } = await EmbedConfig.new(data, user?.id);
  39. await EventLogs.logEvent(
  40. "embed_created",
  41. { embedId: embed.id },
  42. user?.id
  43. );
  44. response.status(200).json({ embed, error });
  45. } catch (e) {
  46. console.error(e);
  47. response.sendStatus(500).end();
  48. }
  49. }
  50. );
  51. app.post(
  52. "/embed/update/:embedId",
  53. [validatedRequest, flexUserRoleValid([ROLES.admin]), validEmbedConfigId],
  54. async (request, response) => {
  55. try {
  56. const user = await userFromSession(request, response);
  57. const { embedId } = request.params;
  58. const updates = reqBody(request);
  59. const { success, error } = await EmbedConfig.update(embedId, updates);
  60. await EventLogs.logEvent("embed_updated", { embedId }, user?.id);
  61. response.status(200).json({ success, error });
  62. } catch (e) {
  63. console.error(e);
  64. response.sendStatus(500).end();
  65. }
  66. }
  67. );
  68. app.delete(
  69. "/embed/:embedId",
  70. [validatedRequest, flexUserRoleValid([ROLES.admin]), validEmbedConfigId],
  71. async (request, response) => {
  72. try {
  73. const { embedId } = request.params;
  74. await EmbedConfig.delete({ id: Number(embedId) });
  75. await EventLogs.logEvent(
  76. "embed_deleted",
  77. { embedId },
  78. response?.locals?.user?.id
  79. );
  80. response.status(200).json({ success: true, error: null });
  81. } catch (e) {
  82. console.error(e);
  83. response.sendStatus(500).end();
  84. }
  85. }
  86. );
  87. app.post(
  88. "/embed/chats",
  89. [chatHistoryViewable, validatedRequest, flexUserRoleValid([ROLES.admin])],
  90. async (request, response) => {
  91. try {
  92. const { offset = 0, limit = 20 } = reqBody(request);
  93. const embedChats = await EmbedChats.whereWithEmbedAndWorkspace(
  94. {},
  95. limit,
  96. { id: "desc" },
  97. offset * limit
  98. );
  99. const totalChats = await EmbedChats.count();
  100. const hasPages = totalChats > (offset + 1) * limit;
  101. response.status(200).json({ chats: embedChats, hasPages, totalChats });
  102. } catch (e) {
  103. console.error(e);
  104. response.sendStatus(500).end();
  105. }
  106. }
  107. );
  108. app.delete(
  109. "/embed/chats/:chatId",
  110. [validatedRequest, flexUserRoleValid([ROLES.admin])],
  111. async (request, response) => {
  112. try {
  113. const { chatId } = request.params;
  114. await EmbedChats.delete({ id: Number(chatId) });
  115. response.status(200).json({ success: true, error: null });
  116. } catch (e) {
  117. console.error(e);
  118. response.sendStatus(500).end();
  119. }
  120. }
  121. );
  122. }
  123. module.exports = { embedManagementEndpoints };