-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgetRedirectFromChat.ts
More file actions
48 lines (43 loc) · 1.44 KB
/
getRedirectFromChat.ts
File metadata and controls
48 lines (43 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
"use server";
import { z } from "zod";
import { initContext } from "@/lib/chatHistory";
import { LangId, PageSlug } from "@/lib/docs";
import { chat, section } from "@/schema/chat";
import { and, eq } from "drizzle-orm";
import { setExtra, withServerActionInstrumentation } from "@sentry/nextjs";
import { headers } from "next/headers";
export async function getRedirectFromChat(chatId: string): Promise<string> {
return withServerActionInstrumentation(
"getRedirectFromChat", // Action name for Sentry
{
headers: await headers(), // Connect client and server traces
recordResponse: true, // Include response data
},
async () => {
setExtra("args", { chatId });
chatId = z.uuid().parse(chatId);
const { drizzle, userId } = await initContext();
if (!userId) {
throw new Error("Not authenticated");
}
const chatData = (await drizzle.query.chat.findFirst({
where: and(eq(chat.chatId, chatId), eq(chat.userId, userId)),
with: {
section: true,
},
})) as
| (typeof chat.$inferSelect & {
section: typeof section.$inferSelect;
})
| undefined;
if (!chatData?.section) {
throw new Error("Chat or section not found");
}
const [lang, page] = (chatData.section.pagePath.split("/") ?? []) as [
LangId,
PageSlug,
];
return `/${lang}/${page}#${chatData.sectionId}`;
}
);
}