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.

157 lines
4.5 KiB

  1. const { DeptDocument } = require("../models/deptDocument");
  2. const { validatedRequest } = require("../utils/middleware/validatedRequest");
  3. const {
  4. strictMultiUserRoleValid,
  5. ROLES,
  6. } = require("../utils/middleware/multiUserProtected");
  7. function deptDocumentEndpoints(app) {
  8. if (!app) return;
  9. // 获取部门文档列表
  10. app.get(
  11. "/deptDocument/list",
  12. [validatedRequest, strictMultiUserRoleValid([ROLES.admin, ROLES.manager])],
  13. async (_request, response) => {
  14. try {
  15. const deptDocuments = await DeptDocument.where();
  16. response.status(200).json({ deptDocuments });
  17. } catch (e) {
  18. console.error(e);
  19. response.sendStatus(500).end();
  20. }
  21. }
  22. );
  23. // 添加部门文档
  24. app.post(
  25. "/deptDocument/add",
  26. [validatedRequest, strictMultiUserRoleValid([ROLES.admin, ROLES.manager])],
  27. async (request, response) => {
  28. try {
  29. const documentData = request.body; // 获取请求体中的文档数据
  30. // 检查文档路径是否唯一
  31. const isDocpathUnique = await DeptDocument.checkDocpathUnique(
  32. documentData.docpath
  33. );
  34. if (!isDocpathUnique) {
  35. return response.status(400).json({
  36. success: false,
  37. message: `文档路径 '${documentData.docpath}' 已存在`,
  38. });
  39. }
  40. // 插入文档数据
  41. const { deptDocument, error } = await DeptDocument.create(documentData);
  42. if (error) {
  43. return response.status(500).json({
  44. success: false,
  45. message: "添加部门文档失败",
  46. error: error,
  47. });
  48. }
  49. // 返回成功响应
  50. response.status(200).json({
  51. success: true,
  52. data: deptDocument,
  53. });
  54. } catch (error) {
  55. console.error("添加部门文档失败:", error);
  56. response.status(500).json({
  57. success: false,
  58. message: "添加部门文档失败,服务器内部错误",
  59. });
  60. }
  61. }
  62. );
  63. // 编辑部门文档
  64. app.post(
  65. "/deptDocument/edit",
  66. [validatedRequest, strictMultiUserRoleValid([ROLES.admin, ROLES.manager])],
  67. async (request, response) => {
  68. try {
  69. const documentData = request.body; // 获取请求体中的文档数据
  70. // 检查文档是否存在
  71. const existingDocument = await DeptDocument.get({ id: documentData.id });
  72. if (!existingDocument) {
  73. return response.status(404).json({
  74. success: false,
  75. message: "文档不存在",
  76. });
  77. }
  78. // 更新文档数据
  79. const { success, error, deptDocument } = await DeptDocument.update(
  80. documentData.id,
  81. documentData
  82. );
  83. if (!success) {
  84. return response.status(500).json({
  85. success: false,
  86. message: "编辑部门文档失败",
  87. error: error,
  88. });
  89. }
  90. // 返回成功响应
  91. response.status(200).json({
  92. success: true,
  93. data: deptDocument,
  94. });
  95. } catch (error) {
  96. console.error("编辑部门文档失败:", error);
  97. response.status(500).json({
  98. success: false,
  99. message: "编辑部门文档失败,服务器内部错误",
  100. });
  101. }
  102. }
  103. );
  104. // 删除部门文档
  105. app.delete(
  106. "/deptDocument/:id",
  107. [validatedRequest, strictMultiUserRoleValid([ROLES.admin, ROLES.manager])],
  108. async (request, response) => {
  109. try {
  110. const documentId = parseInt(request.params.id); // 获取文档 ID
  111. // 检查文档是否存在
  112. const existingDocument = await DeptDocument.get({ id: documentId });
  113. if (!existingDocument) {
  114. return response.status(404).json({
  115. success: false,
  116. message: "文档不存在",
  117. });
  118. }
  119. // 删除文档
  120. const success = await DeptDocument.delete({ id: documentId });
  121. if (!success) {
  122. return response.status(500).json({
  123. success: false,
  124. message: "删除部门文档失败",
  125. });
  126. }
  127. // 返回成功响应
  128. response.status(200).json({
  129. success: true,
  130. message: "文档删除成功",
  131. });
  132. } catch (error) {
  133. console.error("删除部门文档失败:", error);
  134. response.status(500).json({
  135. success: false,
  136. message: "删除部门文档失败,服务器内部错误",
  137. });
  138. }
  139. }
  140. );
  141. }
  142. module.exports = { deptDocumentEndpoints };