aboutsummaryrefslogtreecommitdiff
path: root/apps/web/app/api/getCount/route.ts
blob: 7cd2a2d3f246e9d6d3c41f9a019f6fe0efb71884 (plain) (blame)
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
import { db } from "@/server/db";
import { and, eq, ne, sql } from "drizzle-orm";
import { sessions, storedContent, users } from "@/server/db/schema";
import { type NextRequest, NextResponse } from "next/server";
import { ensureAuth } from "../ensureAuth";

export const runtime = "edge";

export async function GET(req: NextRequest) {
  const session = await ensureAuth(req);

  if (!session) {
    return new Response("Unauthorized", { status: 401 });
  }

  const tweetsCount = await db
    .select({
      count: sql<number>`count(*)`.mapWith(Number),
    })
    .from(storedContent)
    .where(
      and(
        eq(storedContent.userId, session.user.id),
        eq(storedContent.type, "twitter-bookmark"),
      ),
    );

  const pageCount = await db
    .select({
      count: sql<number>`count(*)`.mapWith(Number),
    })
    .from(storedContent)
    .where(
      and(
        eq(storedContent.userId, session.user.id),
        ne(storedContent.type, "twitter-bookmark"),
      ),
    );

  return NextResponse.json({
    tweetsCount: tweetsCount[0]!.count,
    tweetsLimit: 1000,
    pageCount: pageCount[0]!.count,
    pageLimit: 100,
    user: session.user.email,
  });
}