요청한 날짜로 저장하는 버그 해결결

This commit is contained in:
2025-10-15 08:02:42 +00:00
parent 76db85a22f
commit ddb70018b9
3 changed files with 29 additions and 11 deletions

View File

@@ -90,6 +90,10 @@ export async function GET(request: Request) {
const isoDate = parseKoreanDateToISO(dateStr) || parseYYMMDD(dateStr) || new Date().toISOString();
const prisma = new PrismaClient();
// 날짜를 하루 단위(UTC 자정)로 고정해 동일 날짜는 같은 값으로 취급
const dayStart = new Date(isoDate);
dayStart.setUTCHours(0, 0, 0, 0);
const nextDay = new Date(dayStart.getTime() + 24 * 60 * 60 * 1000);
try {
// handle 연결은 비워둠 (요청에 따라 핸들 매핑 생략)
@@ -123,19 +127,31 @@ export async function GET(request: Request) {
const premiumViews = toInt(row['YouTube Premium 조회수']);
const watchTime = toInt(row['시청 시간(단위: 시간)']);
// contentId + date 복합 유니크로 upsert
await prisma.contentDayView.upsert({
where: { contentId_date: { contentId: upsertedContent.id, date: new Date(isoDate) } },
update: { views, validViews, premiumViews, watchTime },
create: {
// 같은 날짜(하루 단위)에 이미 존재하면 update, 없으면 create
const existing = await prisma.contentDayView.findFirst({
where: {
contentId: upsertedContent.id,
date: new Date(isoDate),
views,
validViews,
premiumViews,
watchTime,
date: { gte: dayStart, lt: nextDay },
},
select: { id: true },
});
if (existing) {
await prisma.contentDayView.update({
where: { id: existing.id },
data: { views, validViews, premiumViews, watchTime },
});
} else {
await prisma.contentDayView.create({
data: {
contentId: upsertedContent.id,
date: dayStart,
views,
validViews,
premiumViews,
watchTime,
},
});
}
upserted += 1;
}

File diff suppressed because one or more lines are too long

View File

@@ -277,7 +277,8 @@ const server = http.createServer((req, res) => {
const result = await runFreshExport(ymd);
const payload = {
...result,
date: ymd === 'latest' ? result.endDate : (ymd || result.endDate),
// 요청된 ymd 대신 실제 적용된 기간의 종료일(=단일일자)로 반환
date: result.endDate,
};
sendJson(res, 200, payload);
} catch (err) {