148 lines
5.6 KiB
JavaScript
148 lines
5.6 KiB
JavaScript
const { body, param, validationResult } = require('express-validator')
|
|
const { _sr } = require('../plugins/serverResponses')
|
|
const { checkValidations, res400 } = require('../plugins/controllersHelperFunctions')
|
|
const User = require('../models/User')
|
|
const RequestGPS = require('../models/GPS.Request')
|
|
|
|
module.exports.gpsRequest = [
|
|
[
|
|
body('shopName').notEmpty().isString().withMessage(_sr.fa.required.name),
|
|
body('shopNumber').notEmpty().isString().withMessage(_sr.fa.required.store_number),
|
|
body('shopAddress').notEmpty().isString().withMessage(_sr.fa.required.address),
|
|
body('birthDate').notEmpty().isString().withMessage(_sr.fa.required.date),
|
|
],
|
|
checkValidations(validationResult),
|
|
async (req, res) => {
|
|
const { shopName, shopNumber, shopAddress, birthDate, profilePic } = req.body;
|
|
const repeated = await RequestGPS.findOne({ userId: req.user_id })
|
|
if (repeated) {
|
|
res400(res, "شما قبلا درخواست ثبت کرده اید")
|
|
} else {
|
|
const request = await RequestGPS.create({
|
|
shopName,
|
|
shopNumber,
|
|
shopAddress,
|
|
birthDate,
|
|
profilePic,
|
|
userId: req.user_id,
|
|
})
|
|
// res.status(201).json({ msg: 'درخواست شما با موفقیت ثبت شد', date: request })
|
|
res.status(201).json(request)
|
|
|
|
}
|
|
|
|
}
|
|
]
|
|
|
|
|
|
module.exports.getAllRequest = [
|
|
async (req, res) => {
|
|
let page;
|
|
let rows;
|
|
if (!req.query?.rows) {
|
|
rows = 10
|
|
} else {
|
|
rows = req.query.rows
|
|
}
|
|
if (!req.query?.page) {
|
|
page = 0
|
|
// eslint-disable-next-line eqeqeq
|
|
} else if (!req.query.page == 1) {
|
|
page = 0
|
|
} else {
|
|
page = req.query.page
|
|
page = page * rows - rows
|
|
}
|
|
const data = await RequestGPS.find({}).sort({ status: 1 }).skip(page).limit(rows)
|
|
.populate('userId', 'shopName shopNumber shopAddress birthDate score first_name last_name profilePic')
|
|
const total = Math.ceil((await RequestGPS.estimatedDocumentCount()))
|
|
res.status(200).json({ data, total })
|
|
}
|
|
]
|
|
|
|
module.exports.getOneRequest = [
|
|
[
|
|
param('id').notEmpty().isMongoId().withMessage('این ایدی اشتباه است')
|
|
],
|
|
checkValidations(validationResult),
|
|
async (req, res) => {
|
|
const data = await RequestGPS.findById(req.params.id).populate('userId', 'shopName shopNumber shopAddress birthDate score first_name last_name profilePic')
|
|
res.status(200).json(data)
|
|
}
|
|
]
|
|
|
|
|
|
module.exports.changeStatus = [
|
|
[
|
|
param('status').notEmpty().isString().withMessage('این استاتوس اشتباه است'),
|
|
param('id').notEmpty().isMongoId().withMessage('این ایدی اشتباه است')
|
|
],
|
|
checkValidations(validationResult),
|
|
async (req, res) => {
|
|
switch (req.params.status) {
|
|
case "accept": {
|
|
const data = await RequestGPS.findByIdAndUpdate(req.params.id,
|
|
{
|
|
status: 1
|
|
},
|
|
{ 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 })
|
|
res.status(200).json(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 })
|
|
res.status(200).json(userData)
|
|
|
|
|
|
}
|
|
break;
|
|
}
|
|
case "reject": {
|
|
const data = await RequestGPS.findByIdAndUpdate(req.params.id,
|
|
{
|
|
status: 2
|
|
},
|
|
{ new: true })
|
|
|
|
// res.status(200).json({ msg: 'با موفقیت به روز شد', data })
|
|
res.status(200).json(data)
|
|
|
|
break;
|
|
}
|
|
default:
|
|
res.status(400).json({ msg: 'این استاتوس اشتباه است' })
|
|
break;
|
|
}
|
|
}
|
|
]
|