full
This commit is contained in:
76
controllers/panel/licenses/licensesController.js
Normal file
76
controllers/panel/licenses/licensesController.js
Normal file
@@ -0,0 +1,76 @@
|
||||
const mongoose = require('mongoose')
|
||||
const jMoment = require('moment-jalaali')
|
||||
const LicenseModel = require('../../../models/license')
|
||||
const UserModel = require('../../../models/UserModel')
|
||||
const { syncVerifyBadge } = require('../../../utils/verifyBadge')
|
||||
|
||||
const getAllLicensesPanel = async (req, res, next) => {
|
||||
try {
|
||||
const { page = 1, limit = 20 } = req.query
|
||||
|
||||
const options = {
|
||||
page: parseInt(page),
|
||||
limit: parseInt(limit),
|
||||
sort: { createdAt: -1 }
|
||||
}
|
||||
|
||||
const result = await LicenseModel.paginate({}, options)
|
||||
const userIds = result.docs.map((doc) => doc.userId).filter(Boolean)
|
||||
const users = await UserModel.find({ _id: { $in: userIds } })
|
||||
.select('user_name first_name last_name is_verified profile_image')
|
||||
.lean()
|
||||
const userMap = new Map(users.map((u) => [String(u._id), u]))
|
||||
|
||||
const licenses = result.docs.map((doc) => ({
|
||||
_id: doc._id,
|
||||
userId: doc.userId,
|
||||
Confirmation: doc.Confirmation,
|
||||
licenseImg: doc.licenseImg,
|
||||
user: userMap.get(String(doc.userId)) || null,
|
||||
createdAt: jMoment(doc.createdAt).format('jYYYY-jMM-jDD HH:mm'),
|
||||
updatedAt: jMoment(doc.updatedAt).format('jYYYY-jMM-jDD HH:mm')
|
||||
}))
|
||||
|
||||
res.status(200).json({
|
||||
licenses,
|
||||
totalPages: result.totalPages,
|
||||
totalItems: result.totalDocs
|
||||
})
|
||||
} catch (error) {
|
||||
next(error)
|
||||
}
|
||||
}
|
||||
|
||||
const updateLicenseConfirmationPanel = async (req, res, next) => {
|
||||
try {
|
||||
const { userId } = req.params
|
||||
const { Confirmation } = req.body
|
||||
|
||||
if (!mongoose.Types.ObjectId.isValid(userId)) {
|
||||
return res.status(400).json({ message: 'شناسه کاربر معتبر نیست' })
|
||||
}
|
||||
|
||||
const license = await LicenseModel.findOneAndUpdate(
|
||||
{ userId },
|
||||
{ Confirmation: Boolean(Confirmation) },
|
||||
{ new: true }
|
||||
)
|
||||
if (!license) return res.status(404).json({ message: 'مجوز یافت نشد' })
|
||||
|
||||
const user = await UserModel.findById(userId)
|
||||
if (user) {
|
||||
user.license_verified = Boolean(Confirmation)
|
||||
syncVerifyBadge(user)
|
||||
await user.save()
|
||||
}
|
||||
|
||||
return res.status(200).json({ message: 'وضعیت مجوز با موفقیت تغییر کرد', license })
|
||||
} catch (error) {
|
||||
next(error)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getAllLicensesPanel,
|
||||
updateLicenseConfirmationPanel
|
||||
}
|
||||
Reference in New Issue
Block a user