Split user
This commit is contained in:
@@ -4,61 +4,101 @@ const { body, validationResult } = require('express-validator')
|
||||
const { secretKey } = require('../authentication')
|
||||
const { _sr } = require('../plugins/serverResponses')
|
||||
const { checkValidations } = require('../plugins/controllersHelperFunctions')
|
||||
const User = require('../models/User')
|
||||
const UserGPS = require('../models/GPS.User')
|
||||
const { generateRandomDigits
|
||||
} = require('../plugins/controllersHelperFunctions')
|
||||
module.exports.login_with_pass = [
|
||||
[
|
||||
body('username')
|
||||
.isLength({ min: 4 })
|
||||
.withMessage(_sr.fa.min_char.min4)
|
||||
.bail()
|
||||
.if((value, { req }) => {
|
||||
return req.body.password && req.body.password.length >= 4
|
||||
})
|
||||
.custom((value, { req }) => {
|
||||
return UserGPS.findOne({
|
||||
$or: [{ national_code: value }, { mobile_number: value }]
|
||||
}).then(user => {
|
||||
if (!user) return Promise.reject(_sr.fa.not_found.password)
|
||||
else return true
|
||||
})
|
||||
}),
|
||||
body('password')
|
||||
.notEmpty()
|
||||
.withMessage(_sr.fa.required.password)
|
||||
.bail()
|
||||
.isLength({ min: 4 })
|
||||
.withMessage(_sr.fa.min_char.min4),
|
||||
body('remember_me')
|
||||
.exists()
|
||||
.withMessage(_sr.fa.required.remember_me)
|
||||
.bail()
|
||||
.isBoolean()
|
||||
.withMessage(_sr.fa.format.boolean)
|
||||
],
|
||||
checkValidations(validationResult),
|
||||
async (req, res) => {
|
||||
try {
|
||||
const { username, password } = req.body
|
||||
const user = await UserGPS.findOne({
|
||||
$or: [{ national_code: username }, { mobile_number: username }]
|
||||
})
|
||||
if (!user) throw new Error(_sr.fa.not_found.password)
|
||||
const passwordMatch = await bcrypt.compare(password, user.password)
|
||||
if (!passwordMatch) throw new Error(_sr.fa.not_found.password)
|
||||
if (!user.active) {
|
||||
throw new Error('حساب شما هنوز تاییده نشده است')
|
||||
}
|
||||
const token = await jwt.sign({ _id: user._id }, secretKey, {
|
||||
expiresIn: req.body.remember_me ? '30d' : '7d'
|
||||
})
|
||||
user.token = token
|
||||
await user.save()
|
||||
return res.status(200).json({ token })
|
||||
} catch (err) {
|
||||
return res.status(422).json({
|
||||
validation: { username: { msg: err.message } }
|
||||
})
|
||||
}
|
||||
}
|
||||
]
|
||||
|
||||
module.exports.login = [
|
||||
[
|
||||
body('username')
|
||||
.isLength({ min: 4 })
|
||||
.withMessage(_sr.fa.min_char.min4)
|
||||
.bail()
|
||||
.if((value, { req }) => {
|
||||
return req.body.password && req.body.password.length >= 4
|
||||
})
|
||||
.custom((value, { req }) => {
|
||||
return User.findOne({
|
||||
$or: [{ email: value.toLowerCase().trim() }, { national_code: value }, { username: value }]
|
||||
}).then(user => {
|
||||
if (!user) return Promise.reject(_sr.fa.not_found.password)
|
||||
else return true
|
||||
})
|
||||
}),
|
||||
body('password')
|
||||
.notEmpty()
|
||||
.withMessage(_sr.fa.required.password)
|
||||
.bail()
|
||||
.isLength({ min: 4 })
|
||||
.withMessage(_sr.fa.min_char.min4),
|
||||
body('remember_me')
|
||||
.exists()
|
||||
.withMessage(_sr.fa.required.remember_me)
|
||||
.bail()
|
||||
.isBoolean()
|
||||
.withMessage(_sr.fa.format.boolean)
|
||||
],
|
||||
checkValidations(validationResult),
|
||||
async (req, res) => {
|
||||
try {
|
||||
const { username, password } = req.body
|
||||
const user = await User.findOne({
|
||||
$or: [{ email: username.toLowerCase().trim() }, { national_code: username }, { username }]
|
||||
})
|
||||
if (!user) throw new Error(_sr.fa.not_found.password)
|
||||
const passwordMatch = await bcrypt.compare(password, user.password)
|
||||
if (!passwordMatch) throw new Error(_sr.fa.not_found.password)
|
||||
|
||||
module.exports.login_with_otp = [
|
||||
[
|
||||
body('mobile_number')
|
||||
.notEmpty()
|
||||
.withMessage(_sr.fa.required.phone_number),
|
||||
body('otp')
|
||||
.notEmpty()
|
||||
.withMessage(_sr.fa.required.phone_number)
|
||||
],
|
||||
checkValidations(validationResult),
|
||||
async (req, res) => {
|
||||
try {
|
||||
const { mobile_number, otp } = req.body
|
||||
const user = await UserGPS.findOne({ mobile_number })
|
||||
if (!user) throw new Error(_sr.fa.not_found.user_id)
|
||||
if (user.otp !== otp) throw new Error("کد اشتباه است")
|
||||
if (user.otp === otp) {
|
||||
if (!user.active) {
|
||||
throw new Error('حساب شما هنوز تاییده نشده است')
|
||||
}
|
||||
const token = await jwt.sign({ _id: user._id }, secretKey, {
|
||||
expiresIn: req.body.remember_me ? '30d' : '7d'
|
||||
})
|
||||
if(!user.scope.includes('gps')){
|
||||
throw new Error('شما به این بخش دسترسی ندارید')
|
||||
}
|
||||
user.token = token
|
||||
await user.save()
|
||||
user.otp = generateRandomDigits(6)
|
||||
user.save()
|
||||
return res.status(200).json({ token })
|
||||
} catch (err) {
|
||||
return res.status(422).json({
|
||||
validation: { username: { msg: err.message } }
|
||||
})
|
||||
|
||||
}
|
||||
} catch (err) {
|
||||
return res.status(422).json({
|
||||
validation: { otp: { msg: err.message } }
|
||||
})
|
||||
|
||||
}
|
||||
]
|
||||
|
||||
}
|
||||
]
|
||||
@@ -1,4 +1,4 @@
|
||||
const User = require('../models/User');
|
||||
const User = require('../models/GPS.User');
|
||||
const InstalledDevice = require('../models/GPS.InstalledDevice');
|
||||
const Withdraw = require('../models/GPS.Withdraw');
|
||||
|
||||
|
||||
@@ -1,147 +0,0 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
]
|
||||
@@ -0,0 +1,405 @@
|
||||
/* eslint-disable no-unused-vars */
|
||||
const bcrypt = require('bcryptjs')
|
||||
const { body, validationResult, param } = require('express-validator')
|
||||
const User = require('../models/GPS.User')
|
||||
const { _sr } = require('../plugins/serverResponses')
|
||||
const { SMS } = require('../plugins/SMS_Module')
|
||||
const {
|
||||
res500,
|
||||
checkValidations,
|
||||
removeWhiteSpaces,
|
||||
checkMobileNumber,
|
||||
normalizeMobileNumber,
|
||||
nameOptimizer,
|
||||
checkNationalCode,
|
||||
isPersian,
|
||||
generateRandomDigits
|
||||
} = require('../plugins/controllersHelperFunctions')
|
||||
const _faSr = _sr.fa
|
||||
|
||||
|
||||
|
||||
function sendConfirmationSMS(userId) {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
try {
|
||||
const user = await User.findById(userId)
|
||||
if (!user) throw new Error('invalid id')
|
||||
|
||||
const mobileKey = generateRandomDigits(6)
|
||||
user.otp = mobileKey
|
||||
await user.save()
|
||||
|
||||
const smsMsg = [
|
||||
'این شماره در وبسایت آسان سرویس ثبت گردیده.',
|
||||
' کد تایید شماره همراه: ',
|
||||
mobileKey
|
||||
]
|
||||
|
||||
await SMS([user.mobile_number], smsMsg.join(''))
|
||||
|
||||
resolve()
|
||||
} catch (err) {
|
||||
reject(err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
module.exports.register_user = [
|
||||
[
|
||||
body('first_name')
|
||||
.notEmpty()
|
||||
.withMessage(_faSr.required.first_name)
|
||||
.bail()
|
||||
.isLength({ min: 2 })
|
||||
.withMessage(_faSr.min_char.min2)
|
||||
.bail()
|
||||
.custom((value, { req }) => {
|
||||
if (removeWhiteSpaces(value).length < 2) return Promise.reject(_faSr.min_char.min2)
|
||||
if (!isPersian(value)) return Promise.reject(new Error('لطفا با حروف فارسی تایپ کنید'))
|
||||
else return true
|
||||
}),
|
||||
|
||||
body('last_name')
|
||||
.notEmpty()
|
||||
.withMessage(_faSr.required.first_name)
|
||||
.bail()
|
||||
.isLength({ min: 2 })
|
||||
.withMessage(_faSr.min_char.min2)
|
||||
.bail()
|
||||
.custom((value, { req }) => {
|
||||
if (removeWhiteSpaces(value).length < 2) return Promise.reject(_faSr.min_char.min2)
|
||||
if (!isPersian(value)) return Promise.reject(new Error('لطفا با حروف فارسی تایپ کنید'))
|
||||
else return true
|
||||
}),
|
||||
|
||||
body('national_code')
|
||||
.notEmpty()
|
||||
.withMessage(_faSr.required.national_code)
|
||||
.bail()
|
||||
.custom((value, { req }) => {
|
||||
if (checkNationalCode(value)) return true
|
||||
else return Promise.reject(_faSr.format.national_code)
|
||||
})
|
||||
.bail()
|
||||
.custom((value, { req }) => {
|
||||
const nationalCode = value.trim()
|
||||
return User.findOne({ national_code: nationalCode }).then(user => {
|
||||
if (user && user.national_code === nationalCode)
|
||||
return Promise.reject(new Error('کاربر با این کد ملی از قبل وجود دارد'))
|
||||
else return true
|
||||
})
|
||||
}),
|
||||
|
||||
body('province_name').notEmpty().withMessage(_faSr.required.province),
|
||||
|
||||
body('city_name').notEmpty().withMessage(_faSr.required.city),
|
||||
|
||||
body('shopName')
|
||||
.notEmpty()
|
||||
.withMessage(_faSr.required.address),
|
||||
|
||||
|
||||
body('mobile_number')
|
||||
.notEmpty()
|
||||
.withMessage(_faSr.required.phone_number)
|
||||
.bail()
|
||||
.custom((value, { req }) => {
|
||||
const mobileNStatus = checkMobileNumber(value)
|
||||
if (!mobileNStatus) return Promise.reject(_faSr.format.phone_number)
|
||||
else return Promise.resolve()
|
||||
})
|
||||
.bail()
|
||||
.custom((value, { req }) => {
|
||||
const mobileNumber = value.trim()
|
||||
return User.findOne({ mobile_number: mobileNumber }).then(user => {
|
||||
if (user && user.mobile_number === mobileNumber)
|
||||
return Promise.reject(new Error('کاربر با این شماره موبایل از قبل وجود دارد'))
|
||||
else return true
|
||||
})
|
||||
}),
|
||||
|
||||
body('password')
|
||||
.isLength({ min: 4 })
|
||||
.withMessage(_faSr.min_char.min4)
|
||||
.custom((value, { req }) => {
|
||||
if (value === req.body.password_confirmation) return true
|
||||
else return Promise.reject(_faSr.response.passwords_not_match)
|
||||
})
|
||||
],
|
||||
checkValidations(validationResult),
|
||||
async (req, res) => {
|
||||
try {
|
||||
const {
|
||||
first_name,
|
||||
last_name,
|
||||
national_code,
|
||||
province_name,
|
||||
city_name,
|
||||
mobile_number,
|
||||
password,
|
||||
shopName
|
||||
} = req.body
|
||||
|
||||
const data = {
|
||||
last_name,
|
||||
province_name,
|
||||
city_name,
|
||||
shopName
|
||||
}
|
||||
data.first_name = nameOptimizer(first_name)
|
||||
data.national_code = national_code.trim()
|
||||
data.mobile_number = normalizeMobileNumber(mobile_number)
|
||||
|
||||
|
||||
// hash user password and done
|
||||
const salt = await bcrypt.genSalt(10)
|
||||
const hash = await bcrypt.hash(password, salt)
|
||||
data.password = hash
|
||||
|
||||
const user = new User(data)
|
||||
await user.save()
|
||||
return res.json({ message: _faSr.response.success_save })
|
||||
} catch (err) {
|
||||
const registerFailurMsg = 'مشکلی در ثبت نام پیش آمده، لطفا مجددا تلاش کنید'
|
||||
res500(res, registerFailurMsg)
|
||||
}
|
||||
}
|
||||
]
|
||||
|
||||
|
||||
module.exports.update_user = [
|
||||
[
|
||||
body('first_name')
|
||||
.notEmpty()
|
||||
.withMessage(_faSr.required.first_name)
|
||||
.bail()
|
||||
.isLength({ min: 2 })
|
||||
.withMessage(_faSr.min_char.min2)
|
||||
.bail()
|
||||
.custom((value, { req }) => {
|
||||
if (removeWhiteSpaces(value).length < 2) return Promise.reject(_faSr.min_char.min2)
|
||||
if (!isPersian(value)) return Promise.reject(new Error('لطفا با حروف فارسی تایپ کنید'))
|
||||
else return true
|
||||
}),
|
||||
|
||||
body('last_name')
|
||||
.notEmpty()
|
||||
.withMessage(_faSr.required.first_name)
|
||||
.bail()
|
||||
.isLength({ min: 2 })
|
||||
.withMessage(_faSr.min_char.min2)
|
||||
.bail()
|
||||
.custom((value, { req }) => {
|
||||
if (removeWhiteSpaces(value).length < 2) return Promise.reject(_faSr.min_char.min2)
|
||||
if (!isPersian(value)) return Promise.reject(new Error('لطفا با حروف فارسی تایپ کنید'))
|
||||
else return true
|
||||
}),
|
||||
body('province_name').notEmpty().withMessage(_faSr.required.province),
|
||||
|
||||
body('city_name').notEmpty().withMessage(_faSr.required.city),
|
||||
|
||||
body('shopName')
|
||||
.notEmpty()
|
||||
.withMessage(_faSr.required.field),
|
||||
body('address')
|
||||
.notEmpty()
|
||||
.withMessage(_faSr.required.address),
|
||||
|
||||
body('cell_number')
|
||||
.notEmpty()
|
||||
.withMessage(_faSr.required.address),
|
||||
|
||||
body('birthDate')
|
||||
.notEmpty()
|
||||
.withMessage(_faSr.required.address),
|
||||
],
|
||||
checkValidations(validationResult),
|
||||
async (req, res) => {
|
||||
try {
|
||||
const {
|
||||
first_name,
|
||||
last_name,
|
||||
province_name,
|
||||
city_name,
|
||||
shopName,
|
||||
birthDate,
|
||||
address,
|
||||
cell_number
|
||||
} = req.body
|
||||
|
||||
const data = {
|
||||
last_name,
|
||||
province_name,
|
||||
city_name,
|
||||
shopName,
|
||||
birthDate,
|
||||
address,
|
||||
cell_number
|
||||
}
|
||||
data.first_name = nameOptimizer(first_name)
|
||||
await User.findByIdAndUpdate(
|
||||
req.params.id,
|
||||
data
|
||||
)
|
||||
return res.json({ message: _faSr.response.success_save })
|
||||
} catch (err) {
|
||||
const registerFailurMsg = 'مشکلی در به روز رسانی پیش آمده، لطفا مجددا تلاش کنید'
|
||||
res500(res, registerFailurMsg)
|
||||
}
|
||||
}
|
||||
]
|
||||
|
||||
module.exports.update_user_password = [
|
||||
[
|
||||
body('newPassword')
|
||||
.isLength({ min: 4 })
|
||||
.withMessage(_faSr.min_char.min4)
|
||||
.custom((value, { req }) => {
|
||||
if (value === req.body.password_confirmation) return true
|
||||
else return Promise.reject(_faSr.response.passwords_not_match)
|
||||
}),
|
||||
body('password')
|
||||
.isLength({ min: 4 })
|
||||
.withMessage(_faSr.min_char.min4)
|
||||
],
|
||||
checkValidations(validationResult),
|
||||
async (req, res) => {
|
||||
try {
|
||||
const { password, newPassword } = req.body
|
||||
const { user_id } = req
|
||||
const salt = await bcrypt.genSalt(10)
|
||||
const hash = await bcrypt.hash(newPassword, salt)
|
||||
const user = await User.findById(user_id)
|
||||
if (!user) throw new Error(_faSr.not_found.user_id)
|
||||
const passwordMatch = await bcrypt.compare(password, user.password)
|
||||
if (!passwordMatch) throw new Error('err')
|
||||
user.password = hash
|
||||
await user.save()
|
||||
return res.json({ message: _faSr.response.success_save })
|
||||
} catch (err) {
|
||||
if (err) return res500(res, err)
|
||||
}
|
||||
}
|
||||
]
|
||||
|
||||
module.exports.send_otp = [
|
||||
[
|
||||
body('mobile_number')
|
||||
.notEmpty()
|
||||
.withMessage(_faSr.required.phone_number)
|
||||
],
|
||||
checkValidations(validationResult),
|
||||
async (req, res) => {
|
||||
try {
|
||||
const { mobile_number } = req.body
|
||||
const user = await User.findOne({ mobile_number })
|
||||
if (!user) throw new Error(_sr.fa.not_found.user_id)
|
||||
await sendConfirmationSMS(user.id)
|
||||
res.status(200).json({msg:"ok"})
|
||||
} catch (err) {
|
||||
if (err) return res500(res, err)
|
||||
}
|
||||
}
|
||||
]
|
||||
|
||||
module.exports.check_otp = [
|
||||
[
|
||||
body('mobile_number')
|
||||
.notEmpty()
|
||||
.withMessage(_faSr.required.phone_number),
|
||||
body('otp')
|
||||
.notEmpty()
|
||||
.withMessage(_faSr.required.phone_number)
|
||||
],
|
||||
checkValidations(validationResult),
|
||||
async (req, res) => {
|
||||
try {
|
||||
const { mobile_number, otp } = req.body
|
||||
const user = await User.findOne({ mobile_number })
|
||||
if (!user) throw new Error(_sr.fa.not_found.user_id)
|
||||
if (user.otp !== otp) throw new Error("کد اشتباه است")
|
||||
if (user.otp === otp) res.status(200).json({ msg: "ok" })
|
||||
} catch (err) {
|
||||
if (err) return res500(res, err.message)
|
||||
}
|
||||
}
|
||||
]
|
||||
|
||||
module.exports.resetpass_otp = [
|
||||
[
|
||||
body('mobile_number')
|
||||
.notEmpty()
|
||||
.withMessage(_faSr.required.phone_number),
|
||||
body('otp')
|
||||
.notEmpty()
|
||||
.withMessage(_faSr.required.phone_number),
|
||||
body('password')
|
||||
.isLength({ min: 4 })
|
||||
.withMessage(_faSr.min_char.min4)
|
||||
.custom((value, { req }) => {
|
||||
if (value === req.body?.password_confirmation) return true
|
||||
else return Promise.reject(_faSr.response.passwords_not_match)
|
||||
})
|
||||
],
|
||||
checkValidations(validationResult),
|
||||
async (req, res) => {
|
||||
try {
|
||||
const { mobile_number, otp, password } = req.body
|
||||
const salt = await bcrypt.genSalt(10)
|
||||
const hash = await bcrypt.hash(password, salt)
|
||||
const user = await User.findOne({ mobile_number })
|
||||
if (!user) throw new Error(_sr.fa.not_found.user_id)
|
||||
if (user.otp !== otp) throw new Error("کد اشتباه است")
|
||||
if (user.otp === otp) {
|
||||
user.password = hash
|
||||
user.otp = generateRandomDigits(6)
|
||||
user.save()
|
||||
res.status(200).json({ msg: _sr.fa.response.success_save })
|
||||
}
|
||||
} catch (err) {
|
||||
if (err) return res500(res, err.message)
|
||||
}
|
||||
}
|
||||
]
|
||||
|
||||
module.exports.getCurrent = async (req, res) => {
|
||||
const user = await User.findById(req.user_id).select('-password -token -otp -seenByAdmin')
|
||||
if (!user || !user.active) return res.ststus(404).json({ msg: _sr.fa.not_found.user_id })
|
||||
else return res.status(200).json(user)
|
||||
}
|
||||
|
||||
module.exports.getUsersForAdmin = async (req, res) => {
|
||||
const user = await User.find({}).sort({ active: 1, created_at: -1 }).select('-password -token -otp -seenByAdmin')
|
||||
res.status(200).json(user)
|
||||
}
|
||||
|
||||
module.exports.getUserForAdmin = async (req, res) => {
|
||||
const user = await User.findById(req.params.id).select('-password -token -otp -seenByAdmin')
|
||||
if (!user) {
|
||||
return res.status(404).json({ msg: _sr.fa.not_found.user_id })
|
||||
}else {
|
||||
user.seenByAdmin = true
|
||||
user.save()
|
||||
res.status(200).json(user)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports.activeUser = [
|
||||
[
|
||||
param('status').notEmpty().isBoolean().withMessage('این استاتوس اشتباه است'),
|
||||
param('id').notEmpty().isMongoId().withMessage('این ایدی اشتباه است')
|
||||
],
|
||||
checkValidations(validationResult),
|
||||
async (req, res) => {
|
||||
const data = await User.findByIdAndUpdate(req.params.id,
|
||||
{
|
||||
active: req.params.status
|
||||
},
|
||||
{ new: true }).select('-password -token -otp -seenByAdmin')
|
||||
if (!data) return res.status(404).json({ msg: _sr.fa.not_found.user_id })
|
||||
|
||||
|
||||
res.status(200).json(data)
|
||||
|
||||
}
|
||||
]
|
||||
@@ -101,18 +101,16 @@ module.exports.checkSerial = [
|
||||
try {
|
||||
const { serial, type, g } = req.body
|
||||
const serialStatus = await serialChecker(serial, type, g)
|
||||
console.log("s");
|
||||
|
||||
console.log(serialStatus)
|
||||
let okMsg
|
||||
if (type === 'guaranteeSerial') okMsg = serialStatus
|
||||
else okMsg = serialStatus
|
||||
|
||||
|
||||
let errMsg
|
||||
if (type === 'guaranteeSerial') errMsg = 'شماره سریال گارانتی معتبر نیست.'
|
||||
else errMsg = 'شماره سریال کالا معتبر نیست.'
|
||||
|
||||
|
||||
if (serialStatus) return res.json({ message: okMsg })
|
||||
else return res404(res, errMsg)
|
||||
} catch (e) {
|
||||
|
||||
Reference in New Issue
Block a user