updateeeeeeeeeee
This commit is contained in:
@@ -1,46 +1,43 @@
|
||||
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),
|
||||
body('expireDate').notEmpty().withMessage(_sr.fa.required.field)
|
||||
],
|
||||
checkValidations(validationResult),
|
||||
async (req, res) => {
|
||||
const { score, title, desc, expireDate } = req.body;
|
||||
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 })
|
||||
// eslint-disable-next-line array-callback-return
|
||||
const d = awards.map((award) => {
|
||||
|
||||
const d = awards.map(award => {
|
||||
// eslint-disable-next-line eqeqeq
|
||||
const h = awardsReq.filter(awardReq => awardReq.awardId.id == award.id);
|
||||
|
||||
console.log(award);
|
||||
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,
|
||||
@@ -48,22 +45,23 @@ module.exports.findAllUser = async (req, res) => {
|
||||
req: {
|
||||
desc: h[0].desc,
|
||||
metaData: h[0].metaData,
|
||||
status: h[0].status,
|
||||
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
|
||||
};
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
res.status(200).json(d)
|
||||
res.status(200).json({ awards: d, score })
|
||||
}
|
||||
|
||||
module.exports.findAllAdmin = async (req, res) => {
|
||||
@@ -72,9 +70,7 @@ module.exports.findAllAdmin = async (req, res) => {
|
||||
}
|
||||
|
||||
module.exports.findOne = [
|
||||
[
|
||||
param('id').notEmpty().isMongoId().withMessage('ایدی را وارد یا تصحیح کنید')
|
||||
],
|
||||
[param('id').notEmpty().isMongoId().withMessage('ایدی را وارد یا تصحیح کنید')],
|
||||
checkValidations(validationResult),
|
||||
async (req, res) => {
|
||||
const data = await Award.findById(req.params.id)
|
||||
@@ -83,7 +79,6 @@ module.exports.findOne = [
|
||||
} else {
|
||||
res.status(200).json(data)
|
||||
}
|
||||
|
||||
}
|
||||
]
|
||||
|
||||
@@ -93,52 +88,42 @@ module.exports.update =[
|
||||
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),
|
||||
body('expireDate').notEmpty().withMessage(_sr.fa.required.field)
|
||||
],
|
||||
checkValidations(validationResult),
|
||||
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)
|
||||
if (!data) {
|
||||
res.status(404).json({ msg: _sr.fa.not_found.item_id })
|
||||
} else {
|
||||
data.score = score;
|
||||
data.title = title;
|
||||
data.desc = desc;
|
||||
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('ایدی را وارد یا تصحیح کنید'),
|
||||
],
|
||||
[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.isDeleted = true
|
||||
data.expireDate = Date.now()
|
||||
data.save()
|
||||
res.status(200).json({
|
||||
msg: _sr.fa.response.success_remove,
|
||||
|
||||
msg: _sr.fa.response.success_remove
|
||||
})
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
]
|
||||
@@ -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({
|
||||
|
||||
@@ -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;
|
||||
rows = 10
|
||||
} else {
|
||||
rows = parseInt(req.query.rows);
|
||||
rows = parseInt(req.query.rows)
|
||||
}
|
||||
|
||||
if (!req.query?.page) {
|
||||
page = 0;
|
||||
page = 0
|
||||
} else if (req.query.page <= 1) {
|
||||
page = 0;
|
||||
page = 0
|
||||
} else {
|
||||
page = req.query.page;
|
||||
page = page * rows - rows;
|
||||
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);
|
||||
sort.created_at = parseInt(req.query.created_at)
|
||||
}
|
||||
|
||||
if (req.query?.score) {
|
||||
sort.score = parseInt(req.query.score);
|
||||
sort.score = parseInt(req.query.score)
|
||||
}
|
||||
|
||||
if (Object.keys(sort).length === 0) {
|
||||
sort = { created_at: -1 };
|
||||
sort = { created_at: -1 }
|
||||
}
|
||||
|
||||
const filter = {
|
||||
userId: req.user_id,
|
||||
};
|
||||
|
||||
if (search) {
|
||||
filter.$or = [
|
||||
{ title: new RegExp(search, 'i') },
|
||||
...(!isNaN(search) ? [{ score: Number(search) }] : []),
|
||||
];
|
||||
userId: req.user_id
|
||||
}
|
||||
|
||||
const score = await Score.find(filter).skip(page).limit(rows).sort(sort);
|
||||
const total = Math.ceil(await Score.countDocuments(filter));
|
||||
if (search) {
|
||||
filter.$or = [{ title: new RegExp(search, 'i') }, ...(!isNaN(search) ? [{ score: Number(search) }] : [])]
|
||||
}
|
||||
|
||||
const user = await User.findById(req.user_id).select('score');
|
||||
res.status(200).json({ data: score, score: user.score, total });
|
||||
};
|
||||
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 totalScore')
|
||||
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)
|
||||
|
||||
console.log(data)
|
||||
|
||||
// const [data, totalDocuments] = await Promise.all([
|
||||
// Withdraw.aggregate(aggregationPipeline),
|
||||
// Withdraw.countDocuments(matchStage)
|
||||
|
||||
@@ -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 },
|
||||
|
||||
|
||||
Reference in New Issue
Block a user