This commit is contained in:
morteza-mortezai
2025-12-04 10:34:12 +03:30
parent 8c6aa4595c
commit f488b7aa8e
+16 -24
View File
@@ -29,17 +29,20 @@ module.exports.findAllUser = async (req, res) => {
const toDay = new Date() const toDay = new Date()
const awards = await Award.find({ const awards = await Award.find({
// expireDate: { $gt: toDay }, // expireDate: { $gt: toDay },
isDeleted: false }) isDeleted: false
const awardsReq = await AwardRequest.find({ userId: req.user_id }) })
.populate('awardId', '-created_at -updated_at') const awardsReq = await AwardRequest.find({ userId: req.user_id }).populate('awardId', '-created_at -updated_at')
const score = await GPSUser.findById(req.user_id, { score: 1 }) const score = await GPSUser.findById(req.user_id, { score: 1 })
// eslint-disable-next-line array-callback-return const d = []
const d = awards.map(award => {
awards.forEach(award => {
// eslint-disable-next-line eqeqeq // eslint-disable-next-line eqeqeq
const h = awardsReq.find(awardReq => awardReq.awardId.id == award.id) const h = awardsReq.find(awardReq => awardReq.awardId && awardReq.awardId.id == award.id)
const isExpired = new Date(award.expireDate) < toDay
if (h) { if (h) {
return { // Award has a request
d.push({
id: award.id, id: award.id,
score: award.score, score: award.score,
title: award.title, title: award.title,
@@ -51,9 +54,10 @@ module.exports.findAllUser = async (req, res) => {
status: h.status status: h.status
}, },
status: 1 status: 1
} })
} else if (new Date(award.expireDate) < toDay) { } else if (!isExpired) {
return { // Award is expired and has no request
d.push({
id: award.id, id: award.id,
score: award.score, score: award.score,
title: award.title, title: award.title,
@@ -61,21 +65,9 @@ module.exports.findAllUser = async (req, res) => {
expireDate: award.expireDate, expireDate: award.expireDate,
req: null, req: null,
status: 0 status: 0
} })
} }
// else { })
// // Award is not expired and has no request
// return {
// id: award.id,
// score: award.score,
// title: award.title,
// desc: award.desc,
// expireDate: award.expireDate,
// req: null,
// status: 2
// }
// }
}).filter(Boolean) // Remove any undefined values
res.status(200).json({ awards: d, score }) res.status(200).json({ awards: d, score })
} }