12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- import type { Request } from "express";
- import { TokenPayload } from "#modules/users/auth/types/token-playload-type.js";
- import { ApiError } from "#exceptions/api-error.js";
- import { z } from "zod";
- import { selPool } from "#db/db.js";
- import { sql } from "slonik";
- import { DbSchema } from "#db/db-schema.js";
- class SessionService {
- // getUserFromCookies(cookies: {
- // userData: {
- // username: string;
- // userId: string;
- // };
- // }) {
- // const userData = cookies.userData;
- // if (!userData) {
- // throw ApiError.UnauthorizedError();
- // }
- // return userData;
- // }
- //
- getUserFromReq(req: Request) {
- const userData = TokenPayload.safeParse(req.user);
- if (!userData.success) {
- throw ApiError.UnauthorizedError();
- }
- return userData.data;
- }
- getEventFromCookies(cookies: {
- userData: {
- username: string;
- userId: string;
- };
- }) {
- const userData = cookies.userData;
- if (!userData) {
- throw ApiError.UnauthorizedError();
- }
- return userData;
- }
- // TODO: Добавить ограничение в БД что только один ивент может быть актуальным
- async getCurrentEventFromReq(req: Request) {
- const eventCodeSafe = z.string().safeParse(req.eventCode);
- if (!eventCodeSafe.success) {
- throw ApiError.UnauthorizedError();
- }
- const eventData = await selPool.maybeOne(
- sql.type(
- z.object({
- eventInstId: DbSchema.ev.eventInst.eventInstId,
- eventId: DbSchema.ev.eventInst.eventId,
- code: DbSchema.ev.events.code,
- eventTypeCode: DbSchema.ev.eventTypes.code,
- privateName: DbSchema.ev.eventInst.privateName,
- publicName: DbSchema.ev.eventInst.publicName,
- timezone: DbSchema.ev.eventInst.timezone,
- }),
- )`
- select
- ei.event_inst_id as "eventInstId",
- ei.event_id as "eventId",
- e.code as "code",
- et.code as "eventTypeCode",
- ei.private_name as "privateName",
- ei.public_name as "publicName",
- ei.timezone as "timezone"
- from
- ev.events e
- join ev.event_inst ei on
- e.event_id = ei.event_id
- and ei.is_current = true
- join ev.event_types et on
- et.event_type_id = e.event_type_id
- where
- e.code = ${eventCodeSafe.data}
- `,
- );
- if (!eventData) {
- throw ApiError.BadRequest("noEvent", "Ивент не найден");
- }
- return eventData;
- }
- }
- export default new SessionService();
|