add new endpoint for add user by admin
This commit is contained in:
@@ -42,7 +42,13 @@ export default {
|
||||
title: 'آسان سرویس | مدیریت سایت'
|
||||
}
|
||||
},
|
||||
|
||||
async mounted() {
|
||||
try {
|
||||
await this.$axios.get('/api/admin/permissions')
|
||||
} catch (error) {
|
||||
this.$router.push('/auth/login-register')
|
||||
}
|
||||
/// /////////////////////////////////////////////////// get products list | provinces | cities
|
||||
try {
|
||||
// verity request info
|
||||
@@ -79,6 +85,8 @@ export default {
|
||||
} catch (e) {
|
||||
this.$arpaError()
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
@@ -0,0 +1,210 @@
|
||||
<template>
|
||||
<div>
|
||||
<CustomSubHeader>
|
||||
<CBreadcrumb class="border-0 mb-0">{{ title }}</CBreadcrumb>
|
||||
<CButton size="sm" color="primary" class="mr-auto" :to="{ name: 'admin-gps-users' }">برگشت به صفحه قبل</CButton>
|
||||
<CButton v-if="$route.params.user === 'new'" size="sm" color="success" style="margin-right: 10px" @click="add"
|
||||
>افزودن</CButton
|
||||
>
|
||||
</CustomSubHeader>
|
||||
<CRow class="form">
|
||||
<CCol xl="12">
|
||||
<CCard>
|
||||
<CCardBody>
|
||||
<CForm>
|
||||
<CInput
|
||||
v-model="user.first_name"
|
||||
label="نام"
|
||||
horizontal
|
||||
:description="validation.first_name ? validation.first_name.msg : null"
|
||||
:class="validation.first_name ? 'err' : null"
|
||||
/>
|
||||
<CInput
|
||||
v-model="user.last_name"
|
||||
label="نام خانوادگی"
|
||||
horizontal
|
||||
:description="validation.last_name ? validation.last_name.msg : null"
|
||||
:class="validation.last_name ? 'err' : null"
|
||||
/>
|
||||
<CInput
|
||||
v-model="user.national_code"
|
||||
label="کد ملی"
|
||||
horizontal
|
||||
:description="validation.personalCode ? validation.personalCode.msg : null"
|
||||
:class="validation.personalCode ? 'err' : null"
|
||||
/>
|
||||
<CInput
|
||||
v-model="user.mobile_number"
|
||||
label="شماره همراه"
|
||||
horizontal
|
||||
:description="validation.personalCode ? validation.personalCode.msg : null"
|
||||
:class="validation.personalCode ? 'err' : null"
|
||||
/>
|
||||
<CInput
|
||||
v-model="user.shopName"
|
||||
label="نام فروشگاه"
|
||||
horizontal
|
||||
:description="validation.personalCode ? validation.personalCode.msg : null"
|
||||
:class="validation.personalCode ? 'err' : null"
|
||||
/>
|
||||
|
||||
<div style="display: flex; align-content: center; align-items: center; justify-content: space-between; margin-bottom: 15px;">
|
||||
<label>استان</label>
|
||||
<el-select
|
||||
v-model="province_name"
|
||||
filterable
|
||||
placeholder="استان را انتخاب کنید"
|
||||
style="width: 75%; "
|
||||
>
|
||||
<el-option v-for="(item, index) in iranProvinces" :key="index" :label="item.name" :value="item.name">
|
||||
</el-option>
|
||||
</el-select>
|
||||
<p v-if="validation.province_name">{{ validation.province_name.msg }}</p>
|
||||
</div>
|
||||
|
||||
<div style="display: flex; align-content: center; align-items: center; justify-content: space-between; margin-bottom: 15px;">
|
||||
<label style="padding-left: 10px">شهر </label>
|
||||
<el-select
|
||||
v-model="user.city_name"
|
||||
filterable
|
||||
placeholder="شهر را انتخاب کنید"
|
||||
style="width: 75%; "
|
||||
>
|
||||
<el-option
|
||||
v-for="(item, index) in user.province_name?.cities"
|
||||
:key="index"
|
||||
:label="item"
|
||||
:value="item"
|
||||
>
|
||||
</el-option>
|
||||
</el-select>
|
||||
<p v-if="validation.city_name">{{ validation.city_name.msg }}</p>
|
||||
</div>
|
||||
|
||||
<template v-if="$route.params.user === 'new'">
|
||||
<CInput
|
||||
v-model="user.password"
|
||||
label="کلمه عبور"
|
||||
horizontal
|
||||
type="password"
|
||||
:description="validation.password ? validation.password.msg : null"
|
||||
:class="validation.password ? 'err' : null"
|
||||
/>
|
||||
<CInput
|
||||
v-model="user.password_confirmation"
|
||||
label="تکرار کلمه عبور"
|
||||
horizontal
|
||||
type="password"
|
||||
:description="validation.password_confirmation ? validation.password_confirmation.msg : null"
|
||||
:class="validation.password_confirmation ? 'err' : null"
|
||||
/>
|
||||
</template>
|
||||
</CForm>
|
||||
</CCardBody>
|
||||
</CCard>
|
||||
</CCol>
|
||||
</CRow>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
export default {
|
||||
name: 'AdminGpsUserDetails',
|
||||
layout: 'admin',
|
||||
asyncData({ $axios, params, error }) {
|
||||
try {
|
||||
const data = {}
|
||||
|
||||
return data
|
||||
} catch (e) {
|
||||
error({ status: 404, message: 'Page not found' })
|
||||
}
|
||||
},
|
||||
data() {
|
||||
return {
|
||||
title: 'افزودن کاربر',
|
||||
user: {
|
||||
first_name: '',
|
||||
last_name: '',
|
||||
national_code: '',
|
||||
province_name: '',
|
||||
city_name: '',
|
||||
mobile_number: '',
|
||||
password: '',
|
||||
shopName: '',
|
||||
password_confirmation: ''
|
||||
},
|
||||
iranProvinces: [],
|
||||
province_name: '',
|
||||
city_name: '',
|
||||
validation: {
|
||||
first_name: '',
|
||||
last_name: '',
|
||||
national_code: '',
|
||||
province_name: '',
|
||||
city_name: '',
|
||||
mobile_number: '',
|
||||
password: '',
|
||||
shopName: '',
|
||||
password_confirmation: ''
|
||||
}
|
||||
}
|
||||
},
|
||||
head() {
|
||||
return {
|
||||
title: this.title
|
||||
}
|
||||
},
|
||||
watch: {
|
||||
province_name(newVal, oldVal) {
|
||||
this.user.province_name = this.iranProvinces.filter(item => item.name === newVal)[0]
|
||||
}
|
||||
},
|
||||
mounted() {
|
||||
this.$axios
|
||||
.get('/api/public/iranCities')
|
||||
.then(res => {
|
||||
this.iranProvinces = res.data
|
||||
})
|
||||
.catch(err => {
|
||||
this.$arpaError()
|
||||
console.log('provinces list fetch error -- ', err)
|
||||
})
|
||||
},
|
||||
methods: {
|
||||
setProvice(d) {
|
||||
this.user.province_name = d
|
||||
console.log(d)
|
||||
},
|
||||
add() {
|
||||
this.validation = {}
|
||||
const data = this.user
|
||||
data.province_name = this.province_name
|
||||
this.$axios
|
||||
.post(`/api/admin/gps/users`, data)
|
||||
.then(response => {
|
||||
if (response.data) {
|
||||
this.$message({
|
||||
message: 'کاربر با موفقیت ثبت شد.',
|
||||
type: 'success'
|
||||
})
|
||||
this.$router.push({ name: 'admin-gps-users' })
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
if (error.response.status === 422) {
|
||||
this.validation = error.response.data.validation
|
||||
this.$message({
|
||||
type: 'error',
|
||||
message: 'اطلاعات رو بررسی و دوباره تلاش کنید'
|
||||
})
|
||||
} else {
|
||||
this.$alert(error.response.data.message, 'خطا', {
|
||||
confirmButtonText: 'باشه'
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
@@ -5,8 +5,8 @@
|
||||
|
||||
<el-descriptions-item label="نام و نام خانودگی">{{ user.first_name +" "+ user.last_name }}</el-descriptions-item>
|
||||
<el-descriptions-item label="شماره همراه">{{ user.mobile_number }}</el-descriptions-item>
|
||||
<el-descriptions-item label="استان" >{{ user.province_name }}</el-descriptions-item>
|
||||
<el-descriptions-item label="شهر">{{ user.city_name }}</el-descriptions-item>
|
||||
<el-descriptions-item label="استان" >{{ user.province_name?.provinceName }}</el-descriptions-item>
|
||||
<el-descriptions-item label="شهر">{{ user.city_name?.cityName }}</el-descriptions-item>
|
||||
<el-descriptions-item label="کد ملی ">{{ user.national_code }}</el-descriptions-item>
|
||||
<el-descriptions-item label="نام فروشگاه">{{ user.shopName }}</el-descriptions-item>
|
||||
<el-descriptions-item label="شماره ثابت">{{ user.cell_number }}</el-descriptions-item>
|
||||
@@ -32,6 +32,9 @@
|
||||
|
||||
<CustomSubHeader>
|
||||
<CBreadcrumb class="border-0 mb-0"> کاربران جی پی اس </CBreadcrumb>
|
||||
<CButton size="sm" color="success" :to="{ name: 'admin-gps-users-user', params: { user: 'new' } }" class="mr-auto"
|
||||
>افزودن</CButton
|
||||
>
|
||||
</CustomSubHeader>
|
||||
|
||||
<CCard>
|
||||
|
||||
@@ -429,4 +429,132 @@ module.exports.activeUser = [
|
||||
res.status(200).json(data)
|
||||
|
||||
}
|
||||
]
|
||||
|
||||
module.exports.addUserByAdmin = [
|
||||
[
|
||||
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 provinceId = uid.v4()
|
||||
const cityId = uid.v4()
|
||||
|
||||
const {
|
||||
first_name,
|
||||
last_name,
|
||||
national_code,
|
||||
province_name,
|
||||
city_name,
|
||||
mobile_number,
|
||||
password,
|
||||
shopName
|
||||
} = req.body
|
||||
|
||||
const data = {
|
||||
last_name,
|
||||
province_name:{id:provinceId,provinceName:province_name},
|
||||
city_name:{id:cityId,cityName:city_name},
|
||||
shopName,
|
||||
active:true
|
||||
}
|
||||
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.status(200).json({ message: _faSr.response.success_save })
|
||||
} catch (err) {
|
||||
const registerFailurMsg = 'مشکلی در ثبت نام پیش آمده، لطفا مجددا تلاش کنید'
|
||||
res500(res, registerFailurMsg)
|
||||
}
|
||||
}
|
||||
]
|
||||
@@ -45,6 +45,8 @@ router.get('/gps/users',hasPermission('gps'), userGPS.getUsersForAdmin)
|
||||
router.get('/gps/users/pending',hasPermission('gps'), userGPS.getPendingUsersForAdmin)
|
||||
router.get('/gps/users/:id',hasPermission('gps'), userGPS.getUserForAdmin)
|
||||
|
||||
router.post('/gps/users',hasPermission('gps'), userGPS.addUserByAdmin)
|
||||
|
||||
router.patch('/gps/users/:id/:status',hasPermission('gps'), userGPS.activeUser)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user