-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroute.ts
More file actions
57 lines (50 loc) · 1.79 KB
/
route.ts
File metadata and controls
57 lines (50 loc) · 1.79 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
49
50
51
52
53
54
55
56
57
// GET /api/admin/stats/daily - 최근 14일간 일별 조회수
import { getServerSession } from 'next-auth';
import dbConnect from '@/app/lib/dbConnect';
import View from '@/app/models/View';
export const dynamic = 'force-dynamic';
export async function GET() {
try {
const session = await getServerSession();
if (!session) {
return Response.json({ success: false, error: 'Unauthorized' }, { status: 401 });
}
await dbConnect();
const now = new Date();
const start = new Date(now);
start.setDate(start.getDate() - 13);
start.setHours(0, 0, 0, 0);
const views = await View.aggregate([
{ $match: { createdAt: { $gte: start } } },
{
$group: {
_id: {
$dateToString: { format: '%Y-%m-%d', date: '$createdAt', timezone: '+09:00' },
},
count: { $sum: 1 },
},
},
{ $sort: { _id: 1 } },
]);
// 14일 전체 날짜 배열 생성 (데이터 없는 날은 0)
const daily: { date: string; count: number }[] = [];
for (let i = 13; i >= 0; i--) {
const d = new Date(now);
d.setDate(d.getDate() - i);
const dateStr = d.toLocaleDateString('ko-KR', {
timeZone: 'Asia/Seoul',
year: 'numeric',
month: '2-digit',
day: '2-digit',
}).replace(/\. /g, '-').replace('.', '');
// YYYY-MM-DD 형식으로 변환
const parts = d.toISOString().slice(0, 10);
const found = views.find((v) => v._id === parts);
daily.push({ date: parts, count: found ? found.count : 0 });
}
return Response.json({ success: true, daily }, { status: 200 });
} catch (error) {
console.error('Error fetching daily stats:', error);
return Response.json({ success: false, error: '일별 통계 불러오기 실패' }, { status: 500 });
}
}