const dealDB = require("../../db/deal.db") const commentDB = require("../../db/comment.db") function assertPositiveInt(v, name = "id") { const n = Number(v) if (!Number.isInteger(n) || n <= 0) throw new Error(`Geçersiz ${name}.`) return n } async function getCommentsByDealId(dealId) { const id = assertPositiveInt(dealId, "dealId") const deal = await dealDB.findDeal({ id }) if (!deal) throw new Error("Deal bulunamadı.") const include = { user: { select: { username: true, avatarUrl: true } } } return commentDB.findComments({ dealId: id }, { include }) } async function createComment({ dealId, userId, text }) { const dId = assertPositiveInt(dealId, "dealId") const uId = assertPositiveInt(userId, "userId") if (!text || typeof text !== "string" || !text.trim()) throw new Error("Yorum boş olamaz.") const deal = await dealDB.findDeal({ id: dId }) if (!deal) throw new Error("Deal bulunamadı.") const include = { user: { select: { username: true, avatarUrl: true } } } const data = { text: text.trim(), userId: uId, dealId: dId, } return commentDB.createComment(data, { include }) } async function deleteComment(commentId, userId) { const cId = assertPositiveInt(commentId, "commentId") const uId = assertPositiveInt(userId, "userId") const comments = await commentDB.findComments( { id: cId }, { select: { userId: true } } ) if (!comments || comments.length === 0) throw new Error("Yorum bulunamadı.") if (comments[0].userId !== uId) throw new Error("Bu yorumu silme yetkin yok.") await commentDB.deleteComment({ id: cId }) return { message: "Yorum silindi." } } module.exports = { getCommentsByDealId, createComment, deleteComment, }