-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathroute.ts
More file actions
45 lines (36 loc) · 1.57 KB
/
Copy pathroute.ts
File metadata and controls
45 lines (36 loc) · 1.57 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
import { NextRequest, NextResponse } from "next/server";
import dbConnect from "@/lib/db";
import User from "@/models/User";
import Team from "@/models/Team";
import Analytics from "@/models/Analytics";
export const dynamic = 'force-dynamic';
export async function GET(request: NextRequest) {
try {
const authHeader = request.headers.get('authorization');
if (authHeader !== `Bearer ${process.env.CRON_SECRET}`) {
return new NextResponse('Unauthorized', { status: 401 });
}
await dbConnect();
// Fetch real stats
const [totalUsers, totalTeams, totalSubmissions, totalEvaluated] = await Promise.all([
User.countDocuments({ role: 'user' }),
Team.countDocuments({}),
Team.countDocuments({ teamStatus: { $in: ['submitted', 'shortlisted', 'rsvped', 'rsvp_declined'] } }),
Team.countDocuments({ teamStatus: { $in: ['shortlisted', 'rsvped', 'rsvp_declined'] } }),
]);
const registered = Math.max(0, totalTeams - (totalSubmissions + totalEvaluated));
const stats = {
totalUsers,
totalTeams: registered + totalSubmissions + totalEvaluated,
totalSubmissions,
totalEvaluated,
date: new Date(),
};
// Store in DB
await Analytics.create(stats);
return NextResponse.json({ success: true, data: stats });
} catch (error: any) {
console.error("Cron job error:", error);
return NextResponse.json({ success: false, error: error.message }, { status: 500 });
}
}