// packages/api-client/src/history.ts // history 테이블 도메인 함수 (개인 음성 입력 이력) import type { D3roSupabaseClient } from './client' import type { HistoryEntry, HistoryMode, HistoryStatus } from './types' export async function listHistory( client: D3roSupabaseClient, options?: { limit?: number; mode?: HistoryMode; status?: HistoryStatus } ): Promise { let query = client .from('history') .select('*') .order('created_at', { ascending: false }) if (options?.limit) query = query.limit(options.limit) if (options?.mode) query = query.eq('mode', options.mode) if (options?.status) query = query.eq('status', options.status) const { data, error } = await query if (error) throw new Error(`listHistory failed: ${error.message}`) return data ?? [] } export async function createHistoryEntry( client: D3roSupabaseClient, input: { userId: string originalText: string duration: number mode?: HistoryMode polishedText?: string | null detectedLanguage?: string | null } ): Promise { const { data, error } = await client .from('history') .insert({ user_id: input.userId, original_text: input.originalText, duration: input.duration, mode: input.mode ?? 'dictation', polished_text: input.polishedText ?? null, detected_language: input.detectedLanguage ?? null, word_count: input.originalText.split(/\s+/).filter(Boolean).length }) .select('*') .single() if (error) throw new Error(`createHistoryEntry failed: ${error.message}`) return data } export async function deleteHistoryEntry( client: D3roSupabaseClient, entryId: string ): Promise { const { error } = await client.from('history').delete().eq('id', entryId) if (error) throw new Error(`deleteHistoryEntry failed: ${error.message}`) }