tasks-router.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // router
  2. import express from "express";
  3. const router = express.Router();
  4. export default router;
  5. // db
  6. import { updPool } from "#db";
  7. import { sql } from "slonik";
  8. // api
  9. // error
  10. // import { ApiError } from "#exceptions/api-error.ts";
  11. // dayjs
  12. import dayjs from "dayjs";
  13. import utc from "dayjs/plugin/utc.js";
  14. dayjs.extend(utc);
  15. import timezone from "dayjs/plugin/timezone.js";
  16. dayjs.extend(timezone);
  17. import { v7 as uuidv7 } from "uuid";
  18. import { UserUtils } from "#utils/user-utils.js";
  19. import { RouterUtils } from "#utils/router-utils.js";
  20. import { TasksManagementApi } from "#api";
  21. import { EventsService } from "#modules/events-management/events-service.js";
  22. dayjs.extend(utc);
  23. router.post("/task", async (req, res, next) => {
  24. try {
  25. // валидация запроса
  26. const {
  27. name,
  28. startDate,
  29. endDate,
  30. isTodo,
  31. taskBlockId,
  32. programPointId,
  33. executors,
  34. roomId,
  35. accountableId,
  36. } = TasksManagementApi.POST_Task.req.parse(req.body);
  37. const userId = UserUtils.getUserFromReq(req).userId;
  38. //
  39. // проверка прав
  40. const eventId = await EventsService.getEventIdByOther({ taskBlockId });
  41. await EventsService.checkEventAccess(userId, eventId);
  42. //
  43. const taskId = uuidv7();
  44. // create task
  45. await updPool.query(
  46. sql.unsafe`
  47. insert into ev.tasks
  48. (task_id, name, start_date, end_date, accountable_id, is_todo, task_block_id, program_point_id, room_id)
  49. values
  50. (${taskId}, ${name}, ${startDate}, ${endDate}, ${accountableId}, ${isTodo}, ${taskBlockId}, ${programPointId}, ${roomId})`,
  51. );
  52. // add executors
  53. for (const executorId of executors) {
  54. await updPool.query(
  55. sql.unsafe`
  56. insert into ev.task_executors
  57. (task_id, role)
  58. values
  59. (${taskId}, ${executorId})`,
  60. );
  61. }
  62. RouterUtils.validAndSendResponse(TasksManagementApi.POST_Task.res, res, {
  63. code: "success",
  64. });
  65. } catch (e) {
  66. next(e);
  67. }
  68. });
  69. router.patch("/task", async (req, res, next) => {
  70. try {
  71. // валидация запроса
  72. const {
  73. taskId,
  74. endDate,
  75. startDate,
  76. isTodo,
  77. name,
  78. programPointId,
  79. executors,
  80. roomId,
  81. accountableId,
  82. } = TasksManagementApi.PATCH_Task.req.parse(req.body);
  83. const userId = UserUtils.getUserFromReq(req).userId;
  84. // проверка прав
  85. const eventId = await EventsService.getEventIdByOther({ taskId });
  86. await EventsService.checkEventAccess(userId, eventId);
  87. // edit task
  88. await updPool.query(
  89. sql.unsafe`
  90. UPDATE ev.tasks
  91. SET
  92. ${sql.join(
  93. [
  94. name !== undefined
  95. ? sql.fragment`name = ${name}`
  96. : sql.fragment`name = name`,
  97. startDate !== undefined
  98. ? sql.fragment`start_date = ${startDate}`
  99. : sql.fragment`start_date = start_date`,
  100. endDate !== undefined
  101. ? sql.fragment`end_date = ${endDate}`
  102. : sql.fragment`end_date = end_date`,
  103. isTodo !== undefined
  104. ? sql.fragment`is_todo = ${isTodo}`
  105. : sql.fragment`is_todo = is_todo`,
  106. programPointId !== undefined
  107. ? sql.fragment`program_point_id = ${programPointId}`
  108. : sql.fragment`program_point_id = program_point_id`,
  109. roomId !== undefined
  110. ? sql.fragment`room_id = ${roomId}`
  111. : sql.fragment`room_id = room_id`,
  112. accountableId !== undefined
  113. ? sql.fragment`accountable_id = ${accountableId}`
  114. : sql.fragment`accountable_id = accountable_id`,
  115. ],
  116. sql.fragment`, `,
  117. )}
  118. WHERE task_id = ${taskId}
  119. `,
  120. );
  121. if (executors && executors.length) {
  122. // del executors
  123. await updPool.query(
  124. sql.unsafe`
  125. delete from ev.task_executors
  126. where
  127. task_id = ${taskId}`,
  128. );
  129. // add executors
  130. // TODO: вынести и слить с event-block
  131. for (const executorId of executors) {
  132. await updPool.query(
  133. sql.unsafe`
  134. insert into ev.task_executors
  135. (task_id, role_id)
  136. values
  137. (${taskId}, ${executorId})`,
  138. );
  139. }
  140. }
  141. RouterUtils.validAndSendResponse(TasksManagementApi.PATCH_Task.res, res, {
  142. code: "success",
  143. });
  144. } catch (e) {
  145. next(e);
  146. }
  147. });