This commit is contained in:
payacom
2026-08-31 20:45:41 +03:30
parent fa9f23c61d
commit 3f416a7a60
367 changed files with 621895 additions and 6443 deletions

View File

@@ -0,0 +1,68 @@
const jwt = require('jsonwebtoken')
const jMoment = require('moment-jalaali')
const UserSuggestionModel = require('../../../models/UserSuggestionModel')
const getUserSuggestions = async (req, res) => {
try {
const token = req.header('Authorization').split(' ')[1]
if (!token) return res.status(401).send('Access Denied')
const decodedToken = jwt.verify(token, process.env.APP_SECRET)
const userId = decodedToken.id
const { page = 1, limit = 20 } = req.query
const options = {
page: parseInt(page, 10) || 1,
limit: parseInt(limit, 10) || 20,
sort: { createdAt: -1 }
}
const suggestions = await UserSuggestionModel.paginate({ user: userId }, options)
const list = suggestions.docs.map((item) => ({
...item._doc,
status: item.status || 'new',
createdAt: jMoment(item.createdAt).format('jYYYY/jMM/jDD HH:mm')
}))
res.json({
suggestions: list,
totalPages: suggestions.totalPages,
totalItems: suggestions.totalDocs
})
} catch (error) {
res.status(500).json({ error: 'Internal Server Error' })
}
}
const createSuggestion = async (req, res) => {
try {
const token = req.header('Authorization').split(' ')[1]
if (!token) return res.status(401).send('Access Denied')
const decodedToken = jwt.verify(token, process.env.APP_SECRET)
const userId = decodedToken.id
const text = String(req.body.text || '').trim()
if (text.length < 3) {
return res.status(422).json({
error: true,
message: 'متن پیشنهاد باید حداقل ۳ کاراکتر باشد'
})
}
const suggestion = new UserSuggestionModel({ user: userId, text })
await suggestion.save()
res.status(201).json({
message: 'پیشنهاد شما ثبت شد',
suggestion: {
...suggestion._doc,
createdAt: jMoment(suggestion.createdAt).format('jYYYY/jMM/jDD HH:mm')
}
})
} catch (error) {
res.status(500).json({ error: 'Internal Server Error' })
}
}
module.exports = { getUserSuggestions, createSuggestion }