|
@@ -1,403 +0,0 @@
|
|
|
-// router
|
|
|
-import express from "express";
|
|
|
-const router = express.Router();
|
|
|
-export default router;
|
|
|
-
|
|
|
-// db
|
|
|
-import { selPool, updPool } from "#db";
|
|
|
-import { DbSchema } from "#db-schema";
|
|
|
-import { sql } from "slonik";
|
|
|
-
|
|
|
-// api
|
|
|
-import { EventsManagementApi } from "#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);
|
|
|
-
|
|
|
-// other
|
|
|
-import { z } from "zod";
|
|
|
-
|
|
|
-import { v7 as uuidv7 } from "uuid";
|
|
|
-// import { logger } from "#logger";
|
|
|
-import { UserUtils } from "#utils/user-utils.js";
|
|
|
-import { RouterUtils } from "#utils/router-utils.js";
|
|
|
-import { CompaniesService } from "#modules/companies-management/companies-service.js";
|
|
|
-import { EventsService } from "./events-service.js";
|
|
|
-
|
|
|
-dayjs.extend(utc);
|
|
|
-
|
|
|
-router.post("/event", async (req, res, next) => {
|
|
|
- try {
|
|
|
- // валидация запроса
|
|
|
- const { localName, dates, timezone, companyId } =
|
|
|
- EventsManagementApi.POST_Event.req.parse(req.body);
|
|
|
-
|
|
|
- const userId = UserUtils.getUserFromReq(req).userId;
|
|
|
-
|
|
|
- const company = await CompaniesService.checkCompanyAccess(
|
|
|
- userId,
|
|
|
- companyId,
|
|
|
- );
|
|
|
-
|
|
|
- // если тз не указана, то ставим тз компании
|
|
|
- let tz = timezone;
|
|
|
- if (!tz) {
|
|
|
- tz = company.timezone;
|
|
|
- }
|
|
|
- //
|
|
|
-
|
|
|
- const eventId = uuidv7();
|
|
|
-
|
|
|
- // создаём ивент
|
|
|
- await updPool.query(
|
|
|
- sql.unsafe`
|
|
|
- insert into ev.events
|
|
|
- (event_id, local_name, timezone, company_id)
|
|
|
- values
|
|
|
- (${eventId}, ${localName}, ${tz}, ${companyId})`,
|
|
|
- );
|
|
|
-
|
|
|
- // вставляем даты ивента
|
|
|
- for (const date of dates) {
|
|
|
- await updPool.query(
|
|
|
- sql.unsafe`
|
|
|
- insert into ev.event_dates
|
|
|
- (event_id, date)
|
|
|
- values
|
|
|
- (${eventId}, ${date})`,
|
|
|
- );
|
|
|
- }
|
|
|
-
|
|
|
- // менеджер
|
|
|
- await updPool.query(
|
|
|
- sql.unsafe`
|
|
|
- insert into ev.event_managers
|
|
|
- (event_id, user_id)
|
|
|
- values
|
|
|
- (${eventId}, ${userId})`,
|
|
|
- );
|
|
|
-
|
|
|
- RouterUtils.validAndSendResponse(EventsManagementApi.POST_Event.res, res, {
|
|
|
- code: "success",
|
|
|
- eventId: eventId,
|
|
|
- });
|
|
|
- } catch (e) {
|
|
|
- next(e);
|
|
|
- }
|
|
|
-});
|
|
|
-
|
|
|
-router.get("/event/:eventId", async (req, res, next) => {
|
|
|
- try {
|
|
|
- // валидация запроса
|
|
|
- const { eventId } = EventsManagementApi.GET_Event.req.parse(req.params);
|
|
|
- const userId = UserUtils.getUserFromReq(req).userId;
|
|
|
-
|
|
|
- await EventsService.checkEventAccess(userId, eventId);
|
|
|
-
|
|
|
- // event
|
|
|
- const event = await selPool.maybeOne(
|
|
|
- sql.type(
|
|
|
- z.object({
|
|
|
- eventId: DbSchema.ev.events.eventId,
|
|
|
- localName: DbSchema.ev.events.localName,
|
|
|
- timezone: DbSchema.ev.events.timezone,
|
|
|
- dates: z.array(DbSchema.ev.eventDates.date),
|
|
|
- }),
|
|
|
- )`
|
|
|
- select
|
|
|
- e.event_id as "eventId",
|
|
|
- e.local_name as "localName",
|
|
|
- e.timezone,
|
|
|
- COALESCE(ARRAY_REMOVE(ARRAY_AGG(ed."date"), NULL), '{}') AS dates
|
|
|
- from
|
|
|
- ev.events e
|
|
|
- left join ev.event_dates ed on
|
|
|
- e.event_id = ed.event_id
|
|
|
- where
|
|
|
- e.event_id = ${eventId}
|
|
|
- group by
|
|
|
- e.event_id,
|
|
|
- e.local_name,
|
|
|
- e.timezone;
|
|
|
- `,
|
|
|
- );
|
|
|
- if (!event) throw ApiError.BadRequest("EventNotFound", "Ивент не найден");
|
|
|
-
|
|
|
- // points
|
|
|
- const DbPointsType = DbSchema.ev.programPoints;
|
|
|
- const programPoints = await selPool.any(
|
|
|
- sql.type(
|
|
|
- z.object({
|
|
|
- programPointId: DbPointsType.programPointId,
|
|
|
- name: DbPointsType.name,
|
|
|
- startDate: DbPointsType.startDate,
|
|
|
- endDate: DbPointsType.endDate,
|
|
|
- roomId: DbPointsType.roomId,
|
|
|
- isInternal: DbPointsType.isInternal,
|
|
|
- }),
|
|
|
- )`
|
|
|
- select
|
|
|
- program_point_id as "programPointId",
|
|
|
- name,
|
|
|
- start_date as "startDate",
|
|
|
- end_date as "endDate",
|
|
|
- room_id as "roomId",
|
|
|
- is_internal as "isInternal"
|
|
|
- from
|
|
|
- ev.program_points
|
|
|
- where
|
|
|
- event_id = ${eventId}
|
|
|
- `,
|
|
|
- );
|
|
|
-
|
|
|
- // rooms
|
|
|
- const rooms = await selPool.any(
|
|
|
- sql.type(
|
|
|
- z.object({
|
|
|
- roomId: DbSchema.ev.rooms.roomId,
|
|
|
- name: DbSchema.ev.rooms.name,
|
|
|
- locationId: DbSchema.ev.rooms.locationId,
|
|
|
- }),
|
|
|
- )`
|
|
|
- select
|
|
|
- r.room_id as "roomId",
|
|
|
- r.name,
|
|
|
- r.location_id as "locationId"
|
|
|
- from
|
|
|
- ev.rooms r
|
|
|
- join ev.locations l on
|
|
|
- l.location_id = r.location_id
|
|
|
- where
|
|
|
- l.event_id = ${eventId}
|
|
|
- `,
|
|
|
- );
|
|
|
-
|
|
|
- // task-blocks
|
|
|
- // TODO: вынести
|
|
|
- const taskBlocks = await selPool.any(
|
|
|
- sql.type(
|
|
|
- z.object({
|
|
|
- taskBlockId: DbSchema.ev.taskBlocks.taskBlockId,
|
|
|
- name: DbSchema.ev.taskBlocks.name,
|
|
|
- }),
|
|
|
- )`
|
|
|
- select
|
|
|
- tb.task_block_id as "taskBlockId",
|
|
|
- tb.name
|
|
|
- from
|
|
|
- ev.task_blocks tb
|
|
|
- where
|
|
|
- tb.event_id = ${eventId}
|
|
|
- `,
|
|
|
- );
|
|
|
-
|
|
|
- const managers = await selPool.any(
|
|
|
- sql.type(
|
|
|
- z.object({
|
|
|
- userId: DbSchema.ev.eventManagers.userId,
|
|
|
- name: DbSchema.usr.users.name,
|
|
|
- email: DbSchema.usr.users.email,
|
|
|
- }),
|
|
|
- )`
|
|
|
- select
|
|
|
- em.user_id as "userId",
|
|
|
- u.name,
|
|
|
- u.email
|
|
|
- from
|
|
|
- ev.event_managers em
|
|
|
- join
|
|
|
- usr.users u on
|
|
|
- em.user_id = u.user_id
|
|
|
- where
|
|
|
- em.event_id = ${eventId}
|
|
|
- `,
|
|
|
- );
|
|
|
-
|
|
|
- const roles = await selPool.any(
|
|
|
- sql.type(
|
|
|
- z.object({
|
|
|
- roleId: DbSchema.ev.roles.roleId,
|
|
|
- name: DbSchema.ev.roles.name,
|
|
|
- }),
|
|
|
- )`
|
|
|
- select
|
|
|
- r.role_id as "roleId",
|
|
|
- r.name
|
|
|
- from
|
|
|
- ev.roles r
|
|
|
- where
|
|
|
- r.event_id = ${eventId}
|
|
|
- `,
|
|
|
- );
|
|
|
-
|
|
|
- // res
|
|
|
- RouterUtils.validAndSendResponse(EventsManagementApi.GET_Event.res, res, {
|
|
|
- code: "success",
|
|
|
- event: {
|
|
|
- ...event,
|
|
|
- programPoints: [...programPoints],
|
|
|
- rooms: [...rooms],
|
|
|
- taskBlocks: [...taskBlocks],
|
|
|
- managers: [...managers],
|
|
|
- roles: [...roles],
|
|
|
- },
|
|
|
- });
|
|
|
- } catch (e) {
|
|
|
- next(e);
|
|
|
- }
|
|
|
-});
|
|
|
-
|
|
|
-router.post("/program-point", async (req, res, next) => {
|
|
|
- try {
|
|
|
- // валидация запроса
|
|
|
- const { name, startDate, endDate, eventId, roomId, isInternal } =
|
|
|
- EventsManagementApi.POST_ProgramPoint.req.parse(req.body);
|
|
|
-
|
|
|
- const userId = UserUtils.getUserFromReq(req).userId;
|
|
|
-
|
|
|
- // проверка прав
|
|
|
- await EventsService.checkEventAccess(userId, eventId);
|
|
|
-
|
|
|
- const programPointId = uuidv7();
|
|
|
-
|
|
|
- await updPool.query(
|
|
|
- sql.unsafe`
|
|
|
- insert into ev.program_points
|
|
|
- (program_point_id, name, start_date, end_date, room_id, event_id, is_internal)
|
|
|
- values
|
|
|
- (${programPointId}, ${name}, ${startDate}, ${endDate}, ${roomId}, ${eventId}, ${isInternal})
|
|
|
- `,
|
|
|
- );
|
|
|
-
|
|
|
- RouterUtils.validAndSendResponse(
|
|
|
- EventsManagementApi.POST_ProgramPoint.res,
|
|
|
- res,
|
|
|
- { code: "success", programPointId },
|
|
|
- );
|
|
|
- } catch (e) {
|
|
|
- next(e);
|
|
|
- }
|
|
|
-});
|
|
|
-
|
|
|
-router.patch("/event", async (req, res, next) => {
|
|
|
- try {
|
|
|
- // валидация запроса
|
|
|
- const { eventId, localName, dates, timezone, programPoint, role } =
|
|
|
- EventsManagementApi.PATCH_Event.req.parse(req.body);
|
|
|
-
|
|
|
- const userId = UserUtils.getUserFromReq(req).userId;
|
|
|
-
|
|
|
- // проверка прав
|
|
|
- await EventsService.checkEventAccess(userId, eventId);
|
|
|
-
|
|
|
- // change localName, timezone
|
|
|
- await updPool.query(
|
|
|
- sql.unsafe`
|
|
|
- update ev.events
|
|
|
- set
|
|
|
- ${sql.join(
|
|
|
- [
|
|
|
- localName
|
|
|
- ? sql.unsafe`local_name = ${localName}`
|
|
|
- : sql.unsafe`local_name = local_name`,
|
|
|
-
|
|
|
- timezone
|
|
|
- ? sql.unsafe`timezone = ${timezone}`
|
|
|
- : sql.unsafe`timezone = timezone`,
|
|
|
- ],
|
|
|
- sql.fragment`, `,
|
|
|
- )}
|
|
|
- where
|
|
|
- event_id = ${eventId}`,
|
|
|
- );
|
|
|
-
|
|
|
- // change dates
|
|
|
- if (dates) {
|
|
|
- await updPool.query(
|
|
|
- sql.unsafe`
|
|
|
- delete from ev.event_dates
|
|
|
- where
|
|
|
- event_id = ${eventId}`,
|
|
|
- );
|
|
|
-
|
|
|
- for (const date of dates) {
|
|
|
- await updPool.query(
|
|
|
- sql.unsafe`
|
|
|
- insert into ev.event_dates
|
|
|
- (event_id, date)
|
|
|
- values
|
|
|
- (${eventId}, ${date})`,
|
|
|
- );
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- // TODO: преверить безопасность pp и roles
|
|
|
- if (programPoint) {
|
|
|
- await updPool.query(
|
|
|
- sql.unsafe`
|
|
|
- update ev.program_points
|
|
|
- set
|
|
|
- name = ${programPoint.name},
|
|
|
- start_date = ${programPoint.startDate},
|
|
|
- end_date = ${programPoint.endDate},
|
|
|
- room_id = ${programPoint.roomId}
|
|
|
- where
|
|
|
- program_point_id = ${programPoint.programPointId}`,
|
|
|
- );
|
|
|
- }
|
|
|
-
|
|
|
- if (role) {
|
|
|
- await updPool.query(
|
|
|
- sql.unsafe`
|
|
|
- update ev.roles
|
|
|
- set
|
|
|
- name = ${role.name}
|
|
|
- where
|
|
|
- role_id = ${role.roleId}`,
|
|
|
- );
|
|
|
- }
|
|
|
-
|
|
|
- RouterUtils.validAndSendResponse(EventsManagementApi.PATCH_Event.res, res, {
|
|
|
- code: "success",
|
|
|
- });
|
|
|
- } catch (e) {
|
|
|
- next(e);
|
|
|
- }
|
|
|
-});
|
|
|
-
|
|
|
-router.post("/role", async (req, res, next) => {
|
|
|
- try {
|
|
|
- // валидация запроса
|
|
|
- const { name, eventId } = EventsManagementApi.POST_Role.req.parse(req.body);
|
|
|
-
|
|
|
- const userId = UserUtils.getUserFromReq(req).userId;
|
|
|
-
|
|
|
- // проверка прав
|
|
|
- await EventsService.checkEventAccess(userId, eventId);
|
|
|
-
|
|
|
- const roleId = uuidv7();
|
|
|
-
|
|
|
- // create role
|
|
|
- await updPool.query(
|
|
|
- sql.unsafe`
|
|
|
- insert into ev.roles
|
|
|
- (role_id, name, event_id)
|
|
|
- values
|
|
|
- (${roleId}, ${name}, ${eventId})`,
|
|
|
- );
|
|
|
-
|
|
|
- RouterUtils.validAndSendResponse(EventsManagementApi.POST_Role.res, res, {
|
|
|
- code: "success",
|
|
|
- roleId,
|
|
|
- });
|
|
|
- } catch (e) {
|
|
|
- next(e);
|
|
|
- }
|
|
|
-});
|