This commit is contained in:
Mr Swift
2024-08-11 16:11:32 +03:30
parent 21a6907d9f
commit 180cb8b5cf
18 changed files with 192 additions and 100 deletions
+4 -4
View File
@@ -43,21 +43,21 @@ module.exports.login = [
const user = await User.findOne({
$or: [{ email: username.toLowerCase().trim() }, { national_code: username }, { username }]
})
if (!user) throw new Error('err')
if (!user) throw new Error(_sr.fa.not_found.password)
const passwordMatch = await bcrypt.compare(password, user.password)
if (!passwordMatch) throw new Error('err')
if (!passwordMatch) throw new Error(_sr.fa.not_found.password)
const token = await jwt.sign({ _id: user._id }, secretKey, {
expiresIn: req.body.remember_me ? '30d' : '7d'
})
if(!user.scope.includes('gps')){
throw new Error('err')
throw new Error('شما به این بخش دسترسی ندارید')
}
user.token = token
await user.save()
return res.status(200).json({ token })
} catch (err) {
return res.status(422).json({
validation: { username: { msg: _sr.fa.not_found.password } }
validation: { username: { msg: err.message } }
})
}
}
+8 -7
View File
@@ -9,12 +9,13 @@ module.exports.createAward = [
body('score').notEmpty().isInt().withMessage(_sr.fa.required.field),
body('title').notEmpty().isString().withMessage(_sr.fa.required.field),
body('desc').notEmpty().isString().withMessage(_sr.fa.required.field),
body('expireDate').notEmpty().isDate().withMessage(_sr.fa.required.field),
body('expireDate').notEmpty().withMessage(_sr.fa.required.field),
],
checkValidations(validationResult),
async (req, res) => {
const { score, title, desc, expireData } = req.body;
const award = await Award.create({ score, title, desc, expireData })
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
@@ -26,7 +27,7 @@ module.exports.createAward = [
module.exports.findAllUser = async (req, res) => {
const toDay = new Date()
const data = await Award.find({$lt: {expireDate: toDay}})
const data = await Award.find({expireDate: {$gt: toDay}})
res.status(200).json(data)
}
@@ -57,11 +58,11 @@ module.exports.update =[
body('score').notEmpty().isInt().withMessage(_sr.fa.required.field),
body('title').notEmpty().isString().withMessage(_sr.fa.required.field),
body('desc').notEmpty().isString().withMessage(_sr.fa.required.field),
body('expireDate').notEmpty().isDate().withMessage(_sr.fa.required.field),
body('expireDate').notEmpty().withMessage(_sr.fa.required.field),
],
checkValidations(validationResult),
async (req, res) => {
const { score, title, desc, expireData } = 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 })
@@ -69,7 +70,7 @@ module.exports.update =[
data.score = score;
data.title = title;
data.desc = desc;
data.expireData = expireData;
data.expireDate = new Date(expireDate)
data.save()
res.status(200).json({
msg: _sr.fa.response.success_save,
+2
View File
@@ -85,6 +85,8 @@ module.exports.update = [
metaData
}
)
res.status(200).json({ msg: _sr.fa.response.success_save })
}
}
+25 -19
View File
@@ -11,24 +11,29 @@ module.exports.addCard = [
],
checkValidations(validationResult),
async (req, res) => {
const { holderName, PAN, IBAN } = req.body;
const userData = await User.findById(req.user_id).select('id cardBank')
if (userData?.cardBank && Array.isArray(userData?.cardBank)) {
userData.cardBank.push({
holderName,
PAN,
IBAN
})
userData.save()
res.status(201).json({ msg: 'با موفقیت ثبت شد ' })
} else {
userData.cardBank = [{
holderName,
PAN,
IBAN
}];
userData.save()
res.status(201).json({ msg: 'با موفقیت ثبت شد ' })
try {
const { holderName, PAN, IBAN } = req.body;
const userData = await User.findById(req.user_id).select('id cardBank')
if (userData?.cardBank && Array.isArray(userData?.cardBank)) {
userData.cardBank.push({
holderName,
PAN,
IBAN
})
userData.save()
res.status(201).json({ msg: 'با موفقیت ثبت شد ' })
} else {
userData.cardBank = [{
holderName,
PAN,
IBAN
}];
userData.save()
res.status(201).json({ msg: 'با موفقیت ثبت شد ' })
}
} catch (error) {
res.status(500).json({ msg: error })
}
}
]
@@ -57,7 +62,8 @@ module.exports.deleteCard = [
$pull: {
cardBank: { _id: req.params.id }
}
}
},
{ new:true }
).select('id cardBank')
res.status(200).json({ msg: "با موفقیت حذف شد ", data: userData })
}
+17
View File
@@ -89,6 +89,23 @@ module.exports.getOne = [
}
]
module.exports.getOneByIMEI = [
[
param('imei').notEmpty().isString().withMessage('ایدی اشتباه است')
],
checkValidations(validationResult),
async (req, res) => {
const data = await Device.findOne({IMEI:req.params.imei}).sort({ created_at: -1 })
if (!data) {
res.status(404).json(_sr.fa.not_found.item_id)
} else {
res.status(200).json(data)
}
}
]
module.exports.updateDevice = [
[
+24 -16
View File
@@ -12,7 +12,7 @@ module.exports.deviceRegistration = [
],
checkValidations(validationResult),
async (req, res) => {
const device = await Device.find({ IMEI: req.body.IMEI });
const device = await Device.findOne({ IMEI: req.body.IMEI });
if (!device) {
throw res.status(404).json({ msg: 'این IMEI وجود ندارد' })
// eslint-disable-next-line eqeqeq
@@ -38,21 +38,23 @@ module.exports.deviceRegistration = [
module.exports.getAllForUser = async (req, res) => {
let page;
let rows;
if (!req.query?.rows) {
if (!req.query?.rows || req.query?.rows <= 5) {
rows = 10
} else {
rows = req.query.rows
rows = parseInt(req.query.rows)
}
if (!req.query?.page) {
page = 0
// eslint-disable-next-line eqeqeq
} else if (!req.query.page == 1) {
} else if (req.query.page <= 1) {
page = 0
} else {
page = req.query.page
page = page * rows - rows
}
const data = await InstalledDevice.find({ userId: req.user_id }).sort({ created_at: -1 }).populate('deviceId').skip(page).limit(rows);
const data = await InstalledDevice.find({ userId: req.user_id }).skip(page).limit(rows).sort({ created_at: -1 }).populate('deviceId')
const total = Math.ceil((await InstalledDevice.countDocuments({ userId: req.user_id })))
res.status(200).json({ data, total })
@@ -61,15 +63,16 @@ module.exports.getAllForUser = async (req, res) => {
module.exports.getAllForAdmin = async (req, res) => {
let page;
let rows;
if (!req.query?.rows) {
if (!req.query?.rows || req.query?.rows <= 5) {
rows = 10
} else {
rows = req.query.rows
rows = parseInt(req.query.rows)
}
if (!req.query?.page) {
page = 0
// eslint-disable-next-line eqeqeq
} else if (!req.query.page == 1) {
} else if (req.query.page <= 1) {
page = 0
} else {
page = req.query.page
@@ -95,24 +98,29 @@ module.exports.updateInstalled = [
if (!installedDevice) {
throw res.status(404).json({ msg: 'این ایدی وجود ندارد' })
} else {
const user = await User.findById(installedDevice.userID)
const user = await User.findById(installedDevice.userId).select('walletBalance dollarBalance score')
const device = await Device.findById(installedDevice.deviceId)
switch (req.params.type) {
case "accept": {
installedDevice.status = 1;
installedDevice.installationDate = new Date();
installedDevice.save()
device.status = 1;
device.save()
user.walletBalance += device.profit
user.dollarBalance += device.dollarProfit
user.score += device.score
user.save()
user.walletBalance = device.profit
user.dollarBalance = device.dollarProfit
user.score = device.score
await Score.create({
deviceId: device.id,
score: device.score,
userId: installedDevice.userID
userId: installedDevice.userId
})
user.save()
device.save()
installedDevice.save()
res.status(201).json({ msg: _sr.fa.response.success_save, data: installedDevice })
break;
}
+34 -15
View File
@@ -84,22 +84,41 @@ module.exports.changeStatus = [
status: 1
},
{ new: true })
const userPreData = await User.findById(data.userId).select('walletBalance dollarBalance score')
const userData = await User.findByIdAndUpdate(
data.userId,
{
shopName: data.shopName,
shopNumber: data.shopNumber,
shopAddress: data.shopAddress,
birthDate: data.birthDate,
walletBalance: userPreData?.walletBalance | 0,
dollarBalance: userPreData?.dollarBalance | 0,
score: userPreData?.score | 0,
$push:{scope:'gps'}
},
{ new: true }
)
const userPreData = await User.findById(data.userId).select('walletBalance dollarBalance score scope')
if(userPreData.scope.includes('gps')){
const userData = await User.findByIdAndUpdate(
data.userId,
{
shopName: data.shopName,
shopNumber: data.shopNumber,
shopAddress: data.shopAddress,
birthDate: data.birthDate,
walletBalance: userPreData?.walletBalance | 0,
dollarBalance: userPreData?.dollarBalance | 0,
score: userPreData?.score | 0,
$push:{scope:'gps'}
},
{ new: true }
).select('first_name last_name')
res.status(200).json({ msg: 'با موفقیت به روز شد', data: userData })
}else{
const userData = await User.findByIdAndUpdate(
data.userId,
{
shopName: data.shopName,
shopNumber: data.shopNumber,
shopAddress: data.shopAddress,
birthDate: data.birthDate,
walletBalance: userPreData?.walletBalance | 0,
dollarBalance: userPreData?.dollarBalance | 0,
score: userPreData?.score | 0,
},
{ new: true }
).select('first_name last_name')
res.status(200).json({ msg: 'با موفقیت به روز شد', data: userData })
}
break;
}
case "reject": {
+5 -4
View File
@@ -9,15 +9,16 @@ const User = require('../models/User')
module.exports.getAll = async (req, res) => {
let page;
let rows;
if (!req.query?.rows) {
if (!req.query?.rows || req.query?.rows <= 5) {
rows = 10
} else {
rows = req.query.rows
rows = parseInt(req.query.rows)
}
if (!req.query?.page) {
page = 0
// eslint-disable-next-line eqeqeq
} else if (!req.query.page == 1) {
} else if (req.query.page <= 1) {
page = 0
} else {
page = req.query.page
@@ -25,7 +26,7 @@ module.exports.getAll = async (req, res) => {
}
const score = await Score.find({ userId: req.user_id }).skip(page).limit(rows);
const total = Math.ceil((await score.estimatedDocumentCount()))
const total = Math.ceil((await Score.countDocuments({ userId: req.user_id })))
const user = await User.findById(req.user_id).select('score')
res.status(200).json({ data: score, score: user.score, total })
+28 -20
View File
@@ -11,20 +11,25 @@ module.exports.createRequest = [
],
checkValidations(validationResult),
async (req, res) => {
const user = await User.findById(req.user_id)
const data = {
card: req.body.card,
userId: req.user_id,
amount: user.walletBalance,
dollarAmount: user.dollarBalance,
status: 0
try {
const user = await User.findById(req.user_id)
const data = {
card: req.body.card,
userId: req.user_id,
amount: user.walletBalance,
dollarAmount: user.dollarBalance,
status: 0
}
const request = await Withdraw.create(data)
user.dollarBalance = 0;
user.walletBalance = 0;
user.save()
res.status(201).json({ msg: _sr.fa.response.success_save, data: request })
} catch (error) {
res.status(500).json({ msg: error })
}
user.dollarBalance = 0;
user.walletBalance = 0;
user.save()
const request = await Withdraw.create(data)
res.status(201).json({ msg: _sr.fa.response.success_save, data: request })
}
]
@@ -32,21 +37,23 @@ module.exports.createRequest = [
module.exports.getAllForUser = async (req, res) => {
let page;
let rows;
if (!req.query?.rows) {
if (!req.query?.rows || req.query?.rows <= 5) {
rows = 10
} else {
rows = req.query.rows
rows = parseInt(req.query.rows)
}
if (!req.query?.page) {
page = 0
// eslint-disable-next-line eqeqeq
} else if (!req.query.page == 1) {
} else if (req.query.page <= 1) {
page = 0
} else {
page = req.query.page
page = page * rows - rows
}
const data = await Withdraw.find({ userId: req.user_id }).sort({ created_at: -1 }).skip(page).limit(rows);
const data = await Withdraw.find({ userId: req.user_id }).skip(page).limit(rows).sort({ created_at: -1 });
const total = Math.ceil((await Withdraw.countDocuments({ userId: req.user_id })))
res.status(201).json({ data, total })
}
@@ -54,15 +61,16 @@ module.exports.getAllForUser = async (req, res) => {
module.exports.getAllForAdmin = async (req, res) => {
let page;
let rows;
if (!req.query?.rows) {
if (!req.query?.rows || req.query?.rows <= 5) {
rows = 10
} else {
rows = req.query.rows
rows = parseInt(req.query.rows)
}
if (!req.query?.page) {
page = 0
// eslint-disable-next-line eqeqeq
} else if (!req.query.page == 1) {
} else if (req.query.page <= 1) {
page = 0
} else {
page = req.query.page