full app
This commit is contained in:
@@ -0,0 +1,102 @@
|
||||
const mongoose = require('mongoose')
|
||||
const ShareableLinkModel = require('../../../models/ShareableLinkModel')
|
||||
const UserModel = require('../../../models/UserModel')
|
||||
const { resolveLinkedEntity } = require('../../../utils/resolveLinkedEntity')
|
||||
const { resolveShareableLinkDisplay } = require('../../../utils/shareableLinkDisplay')
|
||||
|
||||
const KINDS = ['pkg', 'project', 'billboard', 'shop', 'shopproduct', 'booking']
|
||||
|
||||
const getMyShareableLinks = async (req, res, next) => {
|
||||
try {
|
||||
const rows = await ShareableLinkModel.find({ user_id: req.user._id })
|
||||
.select('kind ref_id')
|
||||
.lean()
|
||||
return res.json({ links: rows.map((r) => ({ kind: r.kind, ref_id: r.ref_id })) })
|
||||
} catch (error) {
|
||||
next(error)
|
||||
}
|
||||
}
|
||||
|
||||
const toggleShareableLink = async (req, res, next) => {
|
||||
try {
|
||||
const { kind, ref_id: refId, shared } = req.body
|
||||
if (!KINDS.includes(kind) || !refId) {
|
||||
return res.status(422).json({ error: true, message: 'ورودی نامعتبر است' })
|
||||
}
|
||||
|
||||
if (shared) {
|
||||
// صفحه اشتراکگذاری لینک: فقط مالکیت کافی است (نه الزام وضعیت عمومی)
|
||||
const resolvedId = await resolveLinkedEntity(kind, refId, req.user._id, {
|
||||
requirePublic: false,
|
||||
})
|
||||
if (!resolvedId) {
|
||||
return res.status(403).json({
|
||||
error: true,
|
||||
message: 'شما مالک این محتوا نیستید یا آیتم یافت نشد',
|
||||
kind,
|
||||
ref_id: String(refId),
|
||||
})
|
||||
}
|
||||
await ShareableLinkModel.updateOne(
|
||||
{ user_id: req.user._id, kind, ref_id: String(resolvedId) },
|
||||
{ $setOnInsert: { user_id: req.user._id, kind, ref_id: String(resolvedId) } },
|
||||
{ upsert: true }
|
||||
)
|
||||
} else {
|
||||
await ShareableLinkModel.deleteOne({ user_id: req.user._id, kind, ref_id: String(refId) })
|
||||
}
|
||||
|
||||
return res.json({ message: 'بهروزرسانی شد' })
|
||||
} catch (error) {
|
||||
next(error)
|
||||
}
|
||||
}
|
||||
|
||||
const getShareableLinksForUsers = async (req, res, next) => {
|
||||
try {
|
||||
const raw = req.query.userIds
|
||||
const userIds = (typeof raw === 'string' ? raw.split(',') : Array.isArray(raw) ? raw : [])
|
||||
.map((id) => String(id).trim())
|
||||
.filter((id) => mongoose.Types.ObjectId.isValid(id))
|
||||
.slice(0, 50)
|
||||
|
||||
if (!userIds.length) return res.json({ users: [] })
|
||||
|
||||
const [rows, users] = await Promise.all([
|
||||
ShareableLinkModel.find({ user_id: { $in: userIds } }).select('user_id kind ref_id').lean(),
|
||||
UserModel.find({ _id: { $in: userIds } }).select('user_name').lean()
|
||||
])
|
||||
|
||||
const usernameById = Object.fromEntries(users.map((u) => [String(u._id), u.user_name]))
|
||||
|
||||
const byUser = {}
|
||||
for (const row of rows) {
|
||||
const uid = String(row.user_id)
|
||||
if (!byUser[uid]) byUser[uid] = []
|
||||
byUser[uid].push(row)
|
||||
}
|
||||
|
||||
const result = []
|
||||
for (const uid of Object.keys(byUser)) {
|
||||
const links = []
|
||||
for (const row of byUser[uid]) {
|
||||
const display = await resolveShareableLinkDisplay(row.kind, row.ref_id)
|
||||
if (!display) continue
|
||||
links.push({ kind: row.kind, ref_id: row.ref_id, title: display.title, image: display.image })
|
||||
}
|
||||
if (links.length) {
|
||||
result.push({ userId: uid, username: usernameById[uid] || '', links })
|
||||
}
|
||||
}
|
||||
|
||||
return res.json({ users: result })
|
||||
} catch (error) {
|
||||
next(error)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getMyShareableLinks,
|
||||
toggleShareableLink,
|
||||
getShareableLinksForUsers
|
||||
}
|
||||
Reference in New Issue
Block a user