diff options
| author | Dhravya <[email protected]> | 2024-04-12 22:38:12 -0700 |
|---|---|---|
| committer | Dhravya <[email protected]> | 2024-04-12 22:38:12 -0700 |
| commit | 0d6cd7c447ccd4af05031d9897786abfee80b6b8 (patch) | |
| tree | abded39a0c9acc02399a69a1dd5c251d8e03ec83 /apps/web/src/app/api/getCount | |
| parent | Merge branch 'main' of https://github.com/Dhravya/supermemory (diff) | |
| download | supermemory-0d6cd7c447ccd4af05031d9897786abfee80b6b8.tar.xz supermemory-0d6cd7c447ccd4af05031d9897786abfee80b6b8.zip | |
added logic for importing all tweets
Diffstat (limited to 'apps/web/src/app/api/getCount')
| -rw-r--r-- | apps/web/src/app/api/getCount/route.ts | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/apps/web/src/app/api/getCount/route.ts b/apps/web/src/app/api/getCount/route.ts new file mode 100644 index 00000000..05a3aa30 --- /dev/null +++ b/apps/web/src/app/api/getCount/route.ts @@ -0,0 +1,66 @@ +import { db } from "@/server/db"; +import { and, eq, sql } from "drizzle-orm"; +import { sessions, storedContent, users } from "@/server/db/schema"; +import { type NextRequest, NextResponse } from "next/server"; + +export const runtime = "edge"; + +export async function GET(req: NextRequest) { + const token = + req.cookies.get("next-auth.session-token")?.value ?? + req.cookies.get("__Secure-authjs.session-token")?.value ?? + req.cookies.get("authjs.session-token")?.value ?? + req.headers.get("Authorization")?.replace("Bearer ", ""); + + if (!token) { + return new Response( + JSON.stringify({ message: "Invalid Key, session not found." }), + { status: 404 }, + ); + } + + const sessionData = await db + .select() + .from(sessions) + .where(eq(sessions.sessionToken, token!)); + + if (!sessionData || sessionData.length === 0) { + return new Response( + JSON.stringify({ message: "Invalid Key, session not found." }), + { status: 404 }, + ); + } + + const user = await db + .select() + .from(users) + .where(eq(users.id, sessionData[0].userId)) + .limit(1); + + if (!user || user.length === 0) { + return NextResponse.json( + { message: "Invalid Key, session not found." }, + { status: 404 }, + ); + } + + const session = { session: sessionData[0], user: user[0] }; + + const count = await db + .select({ + count: sql<number>`count(*)`.mapWith(Number), + }) + .from(storedContent) + .where( + and( + eq(storedContent.user, session.user.id), + eq(storedContent.type, "twitter-bookmark"), + ), + ); + + return NextResponse.json({ + tweetsCount: count[0].count, + limit: 1000, + user: session.user.email, + }); +} |