aboutsummaryrefslogtreecommitdiff
path: root/apps/web/app/api/getJobCount/route.ts
blob: 75c04c6b2d60fa672c39c56c15345a252b6d03b4 (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
import { db } from "@/server/db";
import { and, eq, sql } from "drizzle-orm";
import { jobs } from "@repo/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 });
	}

	// Calculate the timestamp for one hour ago
	const oneHourAgoTimestamp = Math.floor(Date.now() / 1000) - 3600;

	const jobCount = await db
		.select({ count: sql<number>`count(*)`.mapWith(Number) })
		.from(jobs)
		.where(
			and(
				eq(jobs.userId, session.user.id),
				eq(jobs.status, "Processing"),
				sql`${jobs.updatedAt} > ${oneHourAgoTimestamp}`,
			),
		);

	return NextResponse.json({
		pendingJobs: jobCount[0]?.count ?? 0,
	});
}