瀏覽代碼

добавлен getCurrentEventFromReq, перенесена функция

Vadim 3 月之前
父節點
當前提交
2451b96281
共有 2 個文件被更改,包括 77 次插入23 次删除
  1. 77 5
      src/modules/users/auth/services/session-service.ts
  2. 0 18
      src/utils/user-utils.ts

+ 77 - 5
src/modules/users/auth/services/session-service.ts

@@ -1,11 +1,36 @@
-// база данных
-
+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: {
+  // 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;
@@ -17,6 +42,53 @@ class SessionService {
     }
     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();

+ 0 - 18
src/utils/user-utils.ts

@@ -1,18 +0,0 @@
-import type { Request } from "express";
-// types
-import { TokenPayload } from "#modules/users/auth/types/token-playload-type.js";
-
-import { ApiError } from "#exceptions/api-error.js";
-
-class userUtils {
-  public getUserFromReq(req: Request) {
-    const userData = TokenPayload.safeParse(req.user);
-
-    if (!userData.success) {
-      throw ApiError.UnauthorizedError();
-    }
-    return userData.data;
-  }
-}
-
-export const UserUtils = new userUtils();