Skip to content

Commit dd98b70

Browse files
committed
✨ Заменен метод получения пользователя с getContext на getAuthUser для улучшения обработки данных пользователя в действиях генерации размера и обработчиках API.
1 parent 0b1eccc commit dd98b70

4 files changed

Lines changed: 11 additions & 5 deletions

File tree

src/features/cock-size/api/handler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getContext } from "@/shared/context";
1+
import { getAuthUser } from "@/shared/context";
22
import { di } from "@/shared/injection";
33
import { success } from "@/shared/net/response";
44
import type { Handler } from "@/shared/net/types";
@@ -9,6 +9,6 @@ type GenerateSizeAction = (params: GetSizeParams) => Promise<CockSizeResponse>;
99
export const createSizeHandler =
1010
(generateSizeAction: GenerateSizeAction): Handler =>
1111
async () =>
12-
success(await generateSizeAction({ user_id: getContext()?.user?.id as number }));
12+
success(await generateSizeAction({ user_id: getAuthUser().id }));
1313

1414
createSizeHandler.inject = [di.generateSizeAction] as const;

src/features/cock-size/generate.action.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { RedisClient } from "bun";
22
import type { CockDal } from "@/entities/cock";
3-
import { getContext } from "@/shared/context";
3+
import { getAuthUser } from "@/shared/context";
44
import { di } from "@/shared/injection";
55
import { computeHash } from "@/shared/lib/crypto";
66
import { getMoscowDate, getTtlToMoscowMidnight } from "@/shared/lib/datetime";
@@ -29,7 +29,7 @@ export const createGenerateSizeAction =
2929
}
3030

3131
const size = await random(0, 61);
32-
const nickname = getContext()?.user?.username ?? "";
32+
const nickname = getAuthUser().username ?? "";
3333

3434
await cockDal.create({
3535
_id: crypto.randomUUID(),

src/shared/context/context.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,9 @@ export const withContext = async <T>(context: RequestContext, fn: () => T | Prom
1818
export const getContext = (): RequestContext | null => {
1919
return requestStorage.getStore() ?? null;
2020
};
21+
22+
export const getAuthUser = (): UserProfile => {
23+
const ctx = requestStorage.getStore();
24+
if (!ctx?.user) throw new Error("Unauthorized: user not found in context");
25+
return ctx.user;
26+
};

src/shared/context/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export { getContext, withContext } from "./context";
1+
export { getAuthUser, getContext, withContext } from "./context";

0 commit comments

Comments
 (0)