123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- // router
- import express from "express";
- const router = express.Router();
- export default router;
- // db
- import { db } from "#db";
- import { sql } from "slonik";
- // api
- // error
- // import { ApiError } from "#exceptions/api-error.ts";
- // dayjs
- import dayjs from "dayjs";
- import utc from "dayjs/plugin/utc.js";
- dayjs.extend(utc);
- import timezone from "dayjs/plugin/timezone.js";
- dayjs.extend(timezone);
- import { v7 as uuidv7 } from "uuid";
- import { UserUtils } from "#utils/user-utils.js";
- import { RouterUtils } from "#utils/router-utils.js";
- import { TasksManagementApi } from "#api";
- import { EventsService } from "#modules/events-management/events-service.js";
- dayjs.extend(utc);
- router.post("/task", async (req, res, next) => {
- try {
- // валидация запроса
- const {
- name,
- startDate,
- endDate,
- isTodo,
- taskBlockId,
- programPointId,
- executors,
- roomId,
- accountableId,
- } = TasksManagementApi.POST_Task.req.parse(req.body);
- const userId = UserUtils.getUserFromReq(req).userId;
- //
- // проверка прав
- const eventId = await EventsService.getEventIdByOther({ taskBlockId });
- await EventsService.checkEventAccess(userId, eventId);
- //
- const taskId = uuidv7();
- // create task
- await db.query(
- sql.unsafe`
- insert into ev.tasks
- (task_id, name, start_date, end_date, accountable_id, is_todo, task_block_id, program_point_id, room_id)
- values
- (${taskId}, ${name}, ${startDate}, ${endDate}, ${accountableId}, ${isTodo}, ${taskBlockId}, ${programPointId}, ${roomId})`,
- );
- // add executors
- for (const executorId of executors) {
- await db.query(
- sql.unsafe`
- insert into ev.task_executors
- (task_id, role)
- values
- (${taskId}, ${executorId})`,
- );
- }
- RouterUtils.validAndSendResponse(TasksManagementApi.POST_Task.res, res, {
- code: "success",
- });
- } catch (e) {
- next(e);
- }
- });
- router.patch("/task", async (req, res, next) => {
- try {
- // валидация запроса
- const {
- taskId,
- endDate,
- startDate,
- isTodo,
- name,
- programPointId,
- executors,
- roomId,
- accountableId,
- } = TasksManagementApi.PATCH_Task.req.parse(req.body);
- const userId = UserUtils.getUserFromReq(req).userId;
- // проверка прав
- const eventId = await EventsService.getEventIdByOther({ taskId });
- await EventsService.checkEventAccess(userId, eventId);
- // edit task
- await db.query(
- sql.unsafe`
- UPDATE ev.tasks
- SET
- ${sql.join(
- [
- name !== undefined
- ? sql.fragment`name = ${name}`
- : sql.fragment`name = name`,
- startDate !== undefined
- ? sql.fragment`start_date = ${startDate}`
- : sql.fragment`start_date = start_date`,
- endDate !== undefined
- ? sql.fragment`end_date = ${endDate}`
- : sql.fragment`end_date = end_date`,
- isTodo !== undefined
- ? sql.fragment`is_todo = ${isTodo}`
- : sql.fragment`is_todo = is_todo`,
- programPointId !== undefined
- ? sql.fragment`program_point_id = ${programPointId}`
- : sql.fragment`program_point_id = program_point_id`,
- roomId !== undefined
- ? sql.fragment`room_id = ${roomId}`
- : sql.fragment`room_id = room_id`,
- accountableId !== undefined
- ? sql.fragment`accountable_id = ${accountableId}`
- : sql.fragment`accountable_id = accountable_id`,
- ],
- sql.fragment`, `,
- )}
- WHERE task_id = ${taskId}
- `,
- );
- if (executors && executors.length) {
- // del executors
- await db.query(
- sql.unsafe`
- delete from ev.task_executors
- where
- task_id = ${taskId}`,
- );
- // add executors
- // TODO: вынести и слить с event-block
- for (const executorId of executors) {
- await db.query(
- sql.unsafe`
- insert into ev.task_executors
- (task_id, role_id)
- values
- (${taskId}, ${executorId})`,
- );
- }
- }
- RouterUtils.validAndSendResponse(TasksManagementApi.PATCH_Task.res, res, {
- code: "success",
- });
- } catch (e) {
- next(e);
- }
- });
|