updateeeeeeeeeee
This commit is contained in:
+109
-124
@@ -1,144 +1,129 @@
|
|||||||
const { body, validationResult, param } = require('express-validator');
|
const { body, validationResult, param } = require('express-validator')
|
||||||
const { _sr } = require('../plugins/serverResponses');
|
const { _sr } = require('../plugins/serverResponses')
|
||||||
const { checkValidations } = require('../plugins/controllersHelperFunctions');
|
const { checkValidations } = require('../plugins/controllersHelperFunctions')
|
||||||
const Award = require('../models/GPS.Award');
|
const Award = require('../models/GPS.Award')
|
||||||
const AwardRequest = require('../models/GPS.AwardRequest');
|
// const Score = require('../models/GPS.Score')
|
||||||
|
const AwardRequest = require('../models/GPS.AwardRequest')
|
||||||
|
const GPSUser = require('../models/GPS.User')
|
||||||
|
|
||||||
module.exports.createAward = [
|
module.exports.createAward = [
|
||||||
[
|
[
|
||||||
body('score').notEmpty().isInt().withMessage(_sr.fa.required.field),
|
body('score').notEmpty().isInt().withMessage(_sr.fa.required.field),
|
||||||
body('title').notEmpty().isString().withMessage(_sr.fa.required.title),
|
body('title').notEmpty().isString().withMessage(_sr.fa.required.title),
|
||||||
body('desc').notEmpty().isString().withMessage(_sr.fa.required.description),
|
body('desc').notEmpty().isString().withMessage(_sr.fa.required.description),
|
||||||
body('expireDate').notEmpty().withMessage(_sr.fa.required.field),
|
body('expireDate').notEmpty().withMessage(_sr.fa.required.field)
|
||||||
],
|
],
|
||||||
checkValidations(validationResult),
|
checkValidations(validationResult),
|
||||||
async (req, res) => {
|
async (req, res) => {
|
||||||
const { score, title, desc, expireDate } = req.body;
|
const { score, title, desc, expireDate } = req.body
|
||||||
const fixDate = new Date(expireDate)
|
const fixDate = new Date(expireDate)
|
||||||
const award = await Award.create({ score, title, desc, expireDate:fixDate })
|
const award = await Award.create({ score, title, desc, expireDate: fixDate })
|
||||||
res.status(201).json({
|
res.status(201).json({
|
||||||
msg: _sr.fa.response.success_save,
|
msg: _sr.fa.response.success_save,
|
||||||
data: award
|
data: award
|
||||||
})
|
})
|
||||||
|
}
|
||||||
|
|
||||||
}
|
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
module.exports.findAllUser = async (req, res) => {
|
module.exports.findAllUser = async (req, res) => {
|
||||||
const toDay = new Date()
|
const toDay = new Date()
|
||||||
const awards = await Award.find({expireDate: {$gt: toDay}, isDeleted:false})
|
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 awardsReq = await AwardRequest.find({ userId: req.user_id }).populate('awardId', '-created_at -updated_at')
|
||||||
// eslint-disable-next-line array-callback-return
|
const score = await GPSUser.findById(req.user_id, { score: 1 })
|
||||||
const d = awards.map((award) => {
|
// 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
|
if (h.length) {
|
||||||
const h = awardsReq.filter(awardReq => awardReq.awardId.id == award.id);
|
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);
|
res.status(200).json({ awards: d, score })
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports.findAllAdmin = async (req, res) => {
|
module.exports.findAllAdmin = async (req, res) => {
|
||||||
const data = await Award.find({isDeleted:false})
|
const data = await Award.find({ isDeleted: false })
|
||||||
res.status(200).json(data)
|
res.status(200).json(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports.findOne = [
|
module.exports.findOne = [
|
||||||
[
|
[param('id').notEmpty().isMongoId().withMessage('ایدی را وارد یا تصحیح کنید')],
|
||||||
param('id').notEmpty().isMongoId().withMessage('ایدی را وارد یا تصحیح کنید')
|
checkValidations(validationResult),
|
||||||
],
|
async (req, res) => {
|
||||||
checkValidations(validationResult),
|
const data = await Award.findById(req.params.id)
|
||||||
async (req, res) => {
|
if (!data) {
|
||||||
const data = await Award.findById(req.params.id)
|
res.status(404).json({ msg: _sr.fa.not_found.item_id })
|
||||||
if (!data) {
|
} else {
|
||||||
res.status(404).json({ msg: _sr.fa.not_found.item_id })
|
res.status(200).json(data)
|
||||||
} else {
|
|
||||||
res.status(200).json(data)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
]
|
]
|
||||||
|
|
||||||
module.exports.update =[
|
module.exports.update = [
|
||||||
[
|
[
|
||||||
param('id').notEmpty().isMongoId().withMessage('ایدی را وارد یا تصحیح کنید'),
|
param('id').notEmpty().isMongoId().withMessage('ایدی را وارد یا تصحیح کنید'),
|
||||||
body('score').notEmpty().isInt().withMessage(_sr.fa.required.field),
|
body('score').notEmpty().isInt().withMessage(_sr.fa.required.field),
|
||||||
body('title').notEmpty().isString().withMessage(_sr.fa.required.title),
|
body('title').notEmpty().isString().withMessage(_sr.fa.required.title),
|
||||||
body('desc').notEmpty().isString().withMessage(_sr.fa.required.description),
|
body('desc').notEmpty().isString().withMessage(_sr.fa.required.description),
|
||||||
body('expireDate').notEmpty().withMessage(_sr.fa.required.field),
|
body('expireDate').notEmpty().withMessage(_sr.fa.required.field)
|
||||||
],
|
],
|
||||||
checkValidations(validationResult),
|
checkValidations(validationResult),
|
||||||
async (req, res) => {
|
async (req, res) => {
|
||||||
const { score, title, desc, expireDate } = req.body;
|
const { score, title, desc, expireDate } = req.body
|
||||||
const data = await Award.findById(req.params.id)
|
const data = await Award.findById(req.params.id)
|
||||||
if (!data) {
|
if (!data) {
|
||||||
res.status(404).json({ msg: _sr.fa.not_found.item_id })
|
res.status(404).json({ msg: _sr.fa.not_found.item_id })
|
||||||
} else {
|
} else {
|
||||||
data.score = score;
|
data.score = score
|
||||||
data.title = title;
|
data.title = title
|
||||||
data.desc = desc;
|
data.desc = desc
|
||||||
data.expireDate = new Date(expireDate)
|
data.expireDate = new Date(expireDate)
|
||||||
data.save()
|
data.save()
|
||||||
res.status(200).json({
|
res.status(200).json({
|
||||||
msg: _sr.fa.response.success_save,
|
msg: _sr.fa.response.success_save,
|
||||||
data
|
data
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
]
|
]
|
||||||
|
|
||||||
|
module.exports.delete = [
|
||||||
module.exports.delete=[
|
[param('id').notEmpty().isMongoId().withMessage('ایدی را وارد یا تصحیح کنید')],
|
||||||
[
|
checkValidations(validationResult),
|
||||||
param('id').notEmpty().isMongoId().withMessage('ایدی را وارد یا تصحیح کنید'),
|
async (req, res) => {
|
||||||
],
|
const data = await Award.findById(req.params.id)
|
||||||
checkValidations(validationResult),
|
if (!data) {
|
||||||
async (req, res) => {
|
res.status(404).json({ msg: _sr.fa.not_found.item_id })
|
||||||
const data = await Award.findById(req.params.id)
|
} else {
|
||||||
if (!data) {
|
data.isDeleted = true
|
||||||
res.status(404).json({ msg: _sr.fa.not_found.item_id })
|
data.expireDate = Date.now()
|
||||||
} else {
|
data.save()
|
||||||
data.isDeleted = true;
|
res.status(200).json({
|
||||||
data.expireDate = Date.now()
|
msg: _sr.fa.response.success_remove
|
||||||
data.save()
|
})
|
||||||
res.status(200).json({
|
|
||||||
msg: _sr.fa.response.success_remove,
|
|
||||||
|
|
||||||
})
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
]
|
}
|
||||||
|
]
|
||||||
|
|||||||
@@ -136,7 +136,7 @@ module.exports.updateInstalled = [
|
|||||||
if (!installedDevice) {
|
if (!installedDevice) {
|
||||||
throw res.status(404).json({ msg: 'این ایدی وجود ندارد' })
|
throw res.status(404).json({ msg: 'این ایدی وجود ندارد' })
|
||||||
} else {
|
} 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)
|
const device = await Device.findById(installedDevice.deviceId)
|
||||||
switch (req.params.type) {
|
switch (req.params.type) {
|
||||||
case 'accept': {
|
case 'accept': {
|
||||||
@@ -146,6 +146,7 @@ module.exports.updateInstalled = [
|
|||||||
|
|
||||||
user.walletBalance += device.profit
|
user.walletBalance += device.profit
|
||||||
// user.dollarBalance += device.dollarProfit
|
// user.dollarBalance += device.dollarProfit
|
||||||
|
user.totalScore += device.score
|
||||||
user.score += device.score
|
user.score += device.score
|
||||||
|
|
||||||
await Score.create({
|
await Score.create({
|
||||||
|
|||||||
@@ -4,57 +4,53 @@
|
|||||||
const Score = require('../models/GPS.Score')
|
const Score = require('../models/GPS.Score')
|
||||||
const User = require('../models/GPS.User')
|
const User = require('../models/GPS.User')
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
module.exports.getAll = async (req, res) => {
|
module.exports.getAll = async (req, res) => {
|
||||||
let page;
|
let page
|
||||||
let rows;
|
let rows
|
||||||
|
|
||||||
if (!req.query?.rows || req.query?.rows <= 5) {
|
if (!req.query?.rows || req.query?.rows <= 5) {
|
||||||
rows = 10;
|
rows = 10
|
||||||
} else {
|
} else {
|
||||||
rows = parseInt(req.query.rows);
|
rows = parseInt(req.query.rows)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!req.query?.page) {
|
if (!req.query?.page) {
|
||||||
page = 0;
|
page = 0
|
||||||
} else if (req.query.page <= 1) {
|
} else if (req.query.page <= 1) {
|
||||||
page = 0;
|
page = 0
|
||||||
} else {
|
} else {
|
||||||
page = req.query.page;
|
page = req.query.page
|
||||||
page = page * rows - rows;
|
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) {
|
if (req.query?.created_at) {
|
||||||
sort.created_at = parseInt(req.query.created_at);
|
sort.created_at = parseInt(req.query.created_at)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (req.query?.score) {
|
if (req.query?.score) {
|
||||||
sort.score = parseInt(req.query.score);
|
sort.score = parseInt(req.query.score)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Object.keys(sort).length === 0) {
|
if (Object.keys(sort).length === 0) {
|
||||||
sort = { created_at: -1 };
|
sort = { created_at: -1 }
|
||||||
}
|
}
|
||||||
|
|
||||||
const filter = {
|
const filter = {
|
||||||
userId: req.user_id,
|
userId: req.user_id
|
||||||
};
|
}
|
||||||
|
|
||||||
if (search) {
|
if (search) {
|
||||||
filter.$or = [
|
filter.$or = [{ title: new RegExp(search, 'i') }, ...(!isNaN(search) ? [{ score: Number(search) }] : [])]
|
||||||
{ title: new RegExp(search, 'i') },
|
}
|
||||||
...(!isNaN(search) ? [{ score: Number(search) }] : []),
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
const score = await Score.find(filter).skip(page).limit(rows).sort(sort);
|
const score = await Score.find(filter).skip(page).limit(rows).sort(sort)
|
||||||
const total = Math.ceil(await Score.countDocuments(filter));
|
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');
|
const user = await User.findById(req.user_id).select('score totalScore')
|
||||||
res.status(200).json({ data: score, score: user.score, total });
|
res.status(200).json({ data: score, score: user.score, total, totalScore: userScore.totalScore })
|
||||||
};
|
}
|
||||||
|
|||||||
@@ -130,8 +130,6 @@ module.exports.getAllForAdmin = async (req, res) => {
|
|||||||
|
|
||||||
const totalDocuments = await Withdraw.countDocuments(matchStage)
|
const totalDocuments = await Withdraw.countDocuments(matchStage)
|
||||||
|
|
||||||
console.log(data)
|
|
||||||
|
|
||||||
// const [data, totalDocuments] = await Promise.all([
|
// const [data, totalDocuments] = await Promise.all([
|
||||||
// Withdraw.aggregate(aggregationPipeline),
|
// Withdraw.aggregate(aggregationPipeline),
|
||||||
// Withdraw.countDocuments(matchStage)
|
// Withdraw.countDocuments(matchStage)
|
||||||
|
|||||||
@@ -42,6 +42,11 @@ const UserSchema = mongoose.Schema({
|
|||||||
min: 0,
|
min: 0,
|
||||||
default: 0
|
default: 0
|
||||||
},
|
},
|
||||||
|
totalScore: {
|
||||||
|
type: Number,
|
||||||
|
min: 0,
|
||||||
|
default: 0
|
||||||
|
},
|
||||||
cardBank: [BankSchema],
|
cardBank: [BankSchema],
|
||||||
active: { type: Boolean, default: false },
|
active: { type: Boolean, default: false },
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user