HotTRDealsBackend/services/user.service.js
2026-01-25 17:50:56 +00:00

60 lines
1.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// services/user.service.js
const userDB = require("../db/user.db")
const dealDB = require("../db/deal.db")
const commentDB = require("../db/comment.db")
const dealService = require("./deal.service")
async function getUserProfileByUsername(userName) {
const username = String(userName).trim()
if (!username) throw new Error("username zorunlu")
const user = await userDB.findUser(
{ username },
{ select: { id: true, username: true, email: true, avatarUrl: true, createdAt: true } }
)
if (!user) {
const err = new Error("Kullanıcı bulunamadı.")
err.statusCode = 404
throw err
}
const [dealAgg, totalComments, comments] = await Promise.all([
dealDB.aggregateDeals({ userId: user.id }),
commentDB.countComments({ userId: user.id }),
commentDB.findComments(
{ userId: user.id },
{
orderBy: { createdAt: "desc" },
take: 20,
include: {
user: { select: { id: true, username: true, avatarUrl: true } },
deal: { select: { id: true, title: true } },
},
}
),
])
const userDeals = await dealService.getDeals({
preset: "USER_PUBLIC",
targetUserId: user.id,
viewer: null,
page: 1,
limit: 20,
})
const totalDeals = dealAgg?._count?._all ?? 0
const stats = {
totalLikes: dealAgg?._sum?.score ?? 0,
totalComments: totalComments ?? 0,
totalShares: totalDeals,
totalDeals,
}
return { user, stats, deals: userDeals.results, comments }
}
module.exports = { getUserProfileByUsername }