diff --git a/server/controllers/GPS.Award.js b/server/controllers/GPS.Award.js index 27ce0bf..4816de2 100644 --- a/server/controllers/GPS.Award.js +++ b/server/controllers/GPS.Award.js @@ -1,144 +1,129 @@ -const { body, validationResult, param } = require('express-validator'); -const { _sr } = require('../plugins/serverResponses'); -const { checkValidations } = require('../plugins/controllersHelperFunctions'); -const Award = require('../models/GPS.Award'); -const AwardRequest = require('../models/GPS.AwardRequest'); - +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 - }) - - - } + [ + 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') - // eslint-disable-next-line array-callback-return - const d = awards.map((award) => { + 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 }) + // eslint-disable-next-line array-callback-return + const d = awards.map(award => { + // eslint-disable-next-line eqeqeq + const h = awardsReq.filter(awardReq => awardReq.awardId.id == award.id) - // eslint-disable-next-line eqeqeq - const h = awardsReq.filter(awardReq => awardReq.awardId.id == award.id); + if (h.length) { + return { + id: award.id, + score: award.score, + title: award.title, + desc: award.desc, + expireDate: award.expireDate, + req: { + desc: h[0].desc, + metaData: h[0].metaData, + status: h[0].status + }, + status: 1 + } + } else { + return { + id: award.id, + score: award.score, + title: award.title, + desc: award.desc, + expireDate: award.expireDate, + status: 0 + } + } + }) - console.log(award); - - if (h.length) { - return { - score:award.score, - title:award.title, - desc:award.desc, - expireDate:award.expireDate, - req: { - desc: h[0].desc, - metaData: h[0].metaData, - status: h[0].status, - }, - status: 1 - }; - } else { - return { - score:award.score, - title:award.title, - desc:award.desc, - expireDate:award.expireDate, - status: 0 - }; - } - }) - - res.status(200).json(d) + 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) + 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) - } - + [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.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, - - }) - - - } - +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 + }) } -] \ No newline at end of file + } +] diff --git a/server/controllers/GPS.InstalledDevice.js b/server/controllers/GPS.InstalledDevice.js index 7be2c13..09accb1 100644 --- a/server/controllers/GPS.InstalledDevice.js +++ b/server/controllers/GPS.InstalledDevice.js @@ -136,7 +136,7 @@ module.exports.updateInstalled = [ if (!installedDevice) { throw res.status(404).json({ msg: 'این ایدی وجود ندارد' }) } else { - const user = await User.findById(installedDevice.userId).select('walletBalance score') + const user = await User.findById(installedDevice.userId).select('walletBalance score totalScore') const device = await Device.findById(installedDevice.deviceId) switch (req.params.type) { case 'accept': { @@ -146,6 +146,7 @@ module.exports.updateInstalled = [ user.walletBalance += device.profit // user.dollarBalance += device.dollarProfit + user.totalScore += device.score user.score += device.score await Score.create({ diff --git a/server/controllers/GPS.Score.js b/server/controllers/GPS.Score.js index 2f37da7..6fa16c0 100644 --- a/server/controllers/GPS.Score.js +++ b/server/controllers/GPS.Score.js @@ -4,57 +4,53 @@ const Score = require('../models/GPS.Score') const User = require('../models/GPS.User') - - module.exports.getAll = async (req, res) => { - let page; - let rows; + let page + let rows - if (!req.query?.rows || req.query?.rows <= 5) { - rows = 10; - } else { - rows = parseInt(req.query.rows); - } + if (!req.query?.rows || req.query?.rows <= 5) { + rows = 10 + } else { + rows = parseInt(req.query.rows) + } - if (!req.query?.page) { - page = 0; - } else if (req.query.page <= 1) { - page = 0; - } else { - page = req.query.page; - page = page * rows - rows; - } + if (!req.query?.page) { + page = 0 + } else if (req.query.page <= 1) { + page = 0 + } else { + page = req.query.page + page = page * rows - rows + } - const search = req.query?.search ? req.query.search : null; + const search = req.query?.search ? req.query.search : null - let sort = {}; + let sort = {} - if (req.query?.created_at) { - sort.created_at = parseInt(req.query.created_at); - } + if (req.query?.created_at) { + sort.created_at = parseInt(req.query.created_at) + } - if (req.query?.score) { - sort.score = parseInt(req.query.score); - } + if (req.query?.score) { + sort.score = parseInt(req.query.score) + } - if (Object.keys(sort).length === 0) { - sort = { created_at: -1 }; - } + if (Object.keys(sort).length === 0) { + sort = { created_at: -1 } + } - const filter = { - userId: req.user_id, - }; + const filter = { + userId: req.user_id + } - if (search) { - filter.$or = [ - { title: new RegExp(search, 'i') }, - ...(!isNaN(search) ? [{ score: Number(search) }] : []), - ]; - } + if (search) { + filter.$or = [{ title: new RegExp(search, 'i') }, ...(!isNaN(search) ? [{ score: Number(search) }] : [])] + } - const score = await Score.find(filter).skip(page).limit(rows).sort(sort); - const total = Math.ceil(await Score.countDocuments(filter)); + const score = await Score.find(filter).skip(page).limit(rows).sort(sort) + const userScore = await User.findById(req.user_id).select('score totalScore') + const total = Math.ceil(await Score.countDocuments(filter)) - const user = await User.findById(req.user_id).select('score'); - res.status(200).json({ data: score, score: user.score, total }); -}; + const user = await User.findById(req.user_id).select('score totalScore') + res.status(200).json({ data: score, score: user.score, total, totalScore: userScore.totalScore }) +} diff --git a/server/controllers/GPS.Withdraw.js b/server/controllers/GPS.Withdraw.js index 3de3600..3e6a3b5 100644 --- a/server/controllers/GPS.Withdraw.js +++ b/server/controllers/GPS.Withdraw.js @@ -130,8 +130,6 @@ module.exports.getAllForAdmin = async (req, res) => { const totalDocuments = await Withdraw.countDocuments(matchStage) - console.log(data) - // const [data, totalDocuments] = await Promise.all([ // Withdraw.aggregate(aggregationPipeline), // Withdraw.countDocuments(matchStage) diff --git a/server/models/GPS.User.js b/server/models/GPS.User.js index d6fe946..a914563 100644 --- a/server/models/GPS.User.js +++ b/server/models/GPS.User.js @@ -42,6 +42,11 @@ const UserSchema = mongoose.Schema({ min: 0, default: 0 }, + totalScore: { + type: Number, + min: 0, + default: 0 + }, cardBank: [BankSchema], active: { type: Boolean, default: false },