feat(phase2): 일일 사용량 유틸(KST) + tarot_readings·ai_usage_log 마이그레이션
- kstDayStartISO: KST 자정을 UTC ISO로 변환 - getTodayUsage, recordUsage: AI 사용량 조회·기록 - DB: tarot_readings, ai_usage_log 테이블 생성 - saju service_settings 삭제 (숨김 해제) Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AAtcmKKtqDUe4NyVgy1aLQ
This commit is contained in:
28
lib/ai-usage.ts
Normal file
28
lib/ai-usage.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import type { SupabaseClient } from '@supabase/supabase-js';
|
||||
|
||||
export const SAJU_DAILY_LIMIT = 1;
|
||||
export const TAROT_DAILY_LIMIT = 3;
|
||||
export type AiService = 'saju' | 'tarot';
|
||||
|
||||
/** KST(UTC+9) 자정을 UTC ISO로. 오늘 사용량 집계 하한. */
|
||||
export function kstDayStartISO(now: Date): string {
|
||||
const kstMs = now.getTime() + 9 * 60 * 60 * 1000;
|
||||
const kst = new Date(kstMs);
|
||||
const kstMidnightUtcMs = Date.UTC(kst.getUTCFullYear(), kst.getUTCMonth(), kst.getUTCDate()) - 9 * 60 * 60 * 1000;
|
||||
return new Date(kstMidnightUtcMs).toISOString();
|
||||
}
|
||||
|
||||
export async function getTodayUsage(admin: SupabaseClient, userId: string, service: AiService): Promise<number> {
|
||||
const since = kstDayStartISO(new Date());
|
||||
const { count } = await admin
|
||||
.from('ai_usage_log')
|
||||
.select('id', { count: 'exact', head: true })
|
||||
.eq('user_id', userId)
|
||||
.eq('service', service)
|
||||
.gte('created_at', since);
|
||||
return count ?? 0;
|
||||
}
|
||||
|
||||
export async function recordUsage(admin: SupabaseClient, userId: string, service: AiService): Promise<void> {
|
||||
await admin.from('ai_usage_log').insert({ user_id: userId, service });
|
||||
}
|
||||
Reference in New Issue
Block a user