138 lines
4.2 KiB
JavaScript
138 lines
4.2 KiB
JavaScript
const { body, validationResult, param } = require('express-validator')
|
|
const { _sr } = require('../plugins/serverResponses')
|
|
const { checkValidations } = require('../plugins/controllersHelperFunctions')
|
|
const Award = require('../models/GPS.Award')
|
|
// const Score = require('../models/GPS.Score')
|
|
const AwardRequest = require('../models/GPS.AwardRequest')
|
|
const GPSUser = require('../models/GPS.User')
|
|
|
|
module.exports.createAward = [
|
|
[
|
|
body('score').notEmpty().isInt().withMessage(_sr.fa.required.field),
|
|
body('title').notEmpty().isString().withMessage(_sr.fa.required.title),
|
|
body('desc').notEmpty().isString().withMessage(_sr.fa.required.description),
|
|
body('expireDate').notEmpty().withMessage(_sr.fa.required.field)
|
|
],
|
|
checkValidations(validationResult),
|
|
async (req, res) => {
|
|
const { score, title, desc, expireDate } = req.body
|
|
const fixDate = new Date(expireDate)
|
|
const award = await Award.create({ score, title, desc, expireDate: fixDate })
|
|
res.status(201).json({
|
|
msg: _sr.fa.response.success_save,
|
|
data: award
|
|
})
|
|
}
|
|
]
|
|
|
|
module.exports.findAllUser = async (req, res) => {
|
|
const toDay = new Date()
|
|
const awards = await Award.find({
|
|
// expireDate: { $gt: toDay },
|
|
isDeleted: false
|
|
})
|
|
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 d = []
|
|
|
|
awards.forEach(award => {
|
|
// eslint-disable-next-line eqeqeq
|
|
const h = awardsReq.find(awardReq => awardReq.awardId && awardReq.awardId.id == award.id)
|
|
const isExpired = new Date(award.expireDate) < toDay
|
|
|
|
if (h) {
|
|
// Award has a request
|
|
d.push({
|
|
id: award.id,
|
|
score: award.score,
|
|
title: award.title,
|
|
desc: award.desc,
|
|
expireDate: award.expireDate,
|
|
req: {
|
|
desc: h.desc,
|
|
metaData: h.metaData,
|
|
status: h.status
|
|
},
|
|
status: 1
|
|
})
|
|
} else if (!isExpired) {
|
|
// Award is expired and has no request
|
|
d.push({
|
|
id: award.id,
|
|
score: award.score,
|
|
title: award.title,
|
|
desc: award.desc,
|
|
expireDate: award.expireDate,
|
|
req: null,
|
|
status: 0
|
|
})
|
|
}
|
|
})
|
|
|
|
res.status(200).json({ awards: d, score })
|
|
}
|
|
|
|
module.exports.findAllAdmin = async (req, res) => {
|
|
const data = await Award.find({ isDeleted: false })
|
|
res.status(200).json(data)
|
|
}
|
|
|
|
module.exports.findOne = [
|
|
[param('id').notEmpty().isMongoId().withMessage('ایدی را وارد یا تصحیح کنید')],
|
|
checkValidations(validationResult),
|
|
async (req, res) => {
|
|
const data = await Award.findById(req.params.id)
|
|
if (!data) {
|
|
res.status(404).json({ msg: _sr.fa.not_found.item_id })
|
|
} else {
|
|
res.status(200).json(data)
|
|
}
|
|
}
|
|
]
|
|
|
|
module.exports.update = [
|
|
[
|
|
param('id').notEmpty().isMongoId().withMessage('ایدی را وارد یا تصحیح کنید'),
|
|
body('score').notEmpty().isInt().withMessage(_sr.fa.required.field),
|
|
body('title').notEmpty().isString().withMessage(_sr.fa.required.title),
|
|
body('desc').notEmpty().isString().withMessage(_sr.fa.required.description),
|
|
body('expireDate').notEmpty().withMessage(_sr.fa.required.field)
|
|
],
|
|
checkValidations(validationResult),
|
|
async (req, res) => {
|
|
const { score, title, desc, expireDate } = req.body
|
|
const data = await Award.findById(req.params.id)
|
|
if (!data) {
|
|
res.status(404).json({ msg: _sr.fa.not_found.item_id })
|
|
} else {
|
|
data.score = score
|
|
data.title = title
|
|
data.desc = desc
|
|
data.expireDate = new Date(expireDate)
|
|
data.save()
|
|
res.status(200).json({
|
|
msg: _sr.fa.response.success_save,
|
|
data
|
|
})
|
|
}
|
|
}
|
|
]
|
|
|
|
module.exports.delete = [
|
|
[param('id').notEmpty().isMongoId().withMessage('ایدی را وارد یا تصحیح کنید')],
|
|
checkValidations(validationResult),
|
|
async (req, res) => {
|
|
const data = await Award.findById(req.params.id)
|
|
if (!data) {
|
|
res.status(404).json({ msg: _sr.fa.not_found.item_id })
|
|
} else {
|
|
data.isDeleted = true
|
|
data.expireDate = Date.now()
|
|
data.save()
|
|
res.status(200).json({
|
|
msg: _sr.fa.response.success_remove
|
|
})
|
|
}
|
|
}
|
|
]
|