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.

146 lines
4.2 KiB

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