const prisma = require("./client") function getDb(db) { return db || prisma } async function findDeals(where = {}, options = {}) { return prisma.deal.findMany({ where, include: options.include || undefined, select: options.select || undefined, orderBy: options.orderBy || { createdAt: "desc" }, skip: options.skip || 0, take: options.take || undefined, }) } async function findDeal(where, options = {}, db) { const p = getDb(db) return p.deal.findUnique({ where, include: options.include || undefined, select: options.select || undefined, }) } async function createDeal(data, options = {}) { return prisma.deal.create({ data, include: options.include || undefined, select: options.select || undefined, }) } async function updateDeal(where, data, options = {}, db) { const p = getDb(db) return p.deal.update({ where, data, include: options.include || undefined, select: options.select || undefined, }) } async function countDeals(where = {}) { return prisma.deal.count({ where }) } async function findVotes(where = {}, options = {}) { return prisma.dealVote.findMany({ where, include: options.include || undefined, select: options.select || undefined, }) } async function createVote(data, options = {}) { return prisma.dealVote.create({ data, include: options.include || undefined, select: options.select || undefined, }) } async function updateVote(where, data, options = {}) { return prisma.dealVote.update({ where, data, include: options.include || undefined, select: options.select || undefined, }) } async function countVotes(where = {}) { return prisma.dealVote.count({ where }) } async function getDealWithImages(dealId) { return prisma.deal.findUnique({ where: { id: dealId }, include: { images: { orderBy: { order: "asc" } } }, }); } async function aggregateDeals(where = {}, db) { const p = getDb(db) return p.deal.aggregate({ where, _count: { _all: true }, _sum: { score: true }, }) } module.exports = { findDeals, aggregateDeals, getDealWithImages, findDeal, createDeal, updateDeal, countDeals, findVotes, createVote, updateVote, countVotes, }