import { database } from '../database.js'; import type { FeedbackReply, FeedbackType } from '../../shared/types.js'; export type FeedbackTarget = { note_id: number; version_number: number; author_name: string; author_role: 'client' | 'operator'; withdrawn_at: string | null; }; export async function findFeedbackTarget(workId: number, type: FeedbackType, feedbackId: number): Promise { if (type === 'image_annotation') { return database.one(`SELECT i.note_id,i.version_number,a.author_name,a.author_role,a.withdrawn_at FROM annotations a JOIN images i ON i.id=a.image_id WHERE a.id=? AND i.note_id=?`, [feedbackId, workId]); } const table = type === 'text_annotation' ? 'text_annotations' : 'work_comments'; return database.one(`SELECT note_id,version_number,author_name,author_role,withdrawn_at FROM ${table} WHERE id=? AND note_id=?`, [feedbackId, workId]); } export async function addFeedbackReply(target: FeedbackTarget, type: FeedbackType, feedbackId: number, content: string, authorName: string, authorRole: 'client' | 'operator'): Promise { const id = await database.insertId(`INSERT INTO feedback_replies (note_id,version_number,feedback_type,feedback_id,content,author_name,author_role) VALUES (?,?,?,?,?,?,?)`, [target.note_id, target.version_number, type, feedbackId, content, authorName, authorRole]); return (await database.one('SELECT * FROM feedback_replies WHERE id=?', [id]))!; } export async function withdrawFeedback(type: FeedbackType, feedbackId: number): Promise { const table = type === 'image_annotation' ? 'annotations' : type === 'text_annotation' ? 'text_annotations' : 'work_comments'; await database.execute(`UPDATE ${table} SET withdrawn_at=CURRENT_TIMESTAMP WHERE id=? AND withdrawn_at IS NULL`, [feedbackId]); }