Add full project files
This commit is contained in:
112
controllers/application/verify/addressController.js
Normal file
112
controllers/application/verify/addressController.js
Normal file
@@ -0,0 +1,112 @@
|
||||
/* eslint-disable camelcase */
|
||||
const { ProvinceModel, CityModel } = require('../../../models/StateCity')
|
||||
const UserModel = require('../../../models/UserModel')
|
||||
const jwt = require('jsonwebtoken')
|
||||
|
||||
const setAddress = async (req, res, next) => {
|
||||
try {
|
||||
const token = req.header('Authorization').split(' ')[1]
|
||||
if (!token) return res.status(401).send('Access Denied')
|
||||
const { province_id, city_id, address, lat, lng, show_location } = req.body
|
||||
// اعتبارسنجی دادهها
|
||||
if (!province_id || !city_id || !address || !lat || !lng || show_location === 'undefined') {
|
||||
return res.status(422).json({
|
||||
error: true,
|
||||
message: 'اطلاعات ارسالی ناقص است'
|
||||
})
|
||||
}
|
||||
|
||||
// یافتن کاربر با شماره موبایل
|
||||
const decodedToken = jwt.verify(token, process.env.APP_SECRET)
|
||||
const user = await UserModel.findById(decodedToken.id)
|
||||
if (!user) {
|
||||
return res.status(404).json({
|
||||
error: true,
|
||||
message: 'کاربری با این شماره موبایل یافت نشد'
|
||||
})
|
||||
}
|
||||
|
||||
// یافتن استان و شهر
|
||||
const province = await ProvinceModel.findOne({ id: province_id })
|
||||
const city = await CityModel.findOne({ id: city_id })
|
||||
if (!province || !city) {
|
||||
return res.status(404).json({
|
||||
error: true,
|
||||
message: 'استان یا شهر موردنظر یافت نشد'
|
||||
})
|
||||
}
|
||||
|
||||
user.province = province
|
||||
user.city = city
|
||||
user.address = address
|
||||
user.lat = lat
|
||||
user.lng = lng
|
||||
user.show_location = show_location
|
||||
|
||||
await user.save()
|
||||
|
||||
return res.json({
|
||||
message: 'آدرس با موفقیت بهروزرسانی شد'
|
||||
})
|
||||
} catch (error) {
|
||||
next(error)
|
||||
}
|
||||
}
|
||||
const updateAddress = async (req, res, next) => {
|
||||
try {
|
||||
const token = req.header('Authorization').split(' ')[1]
|
||||
if (!token) return res.status(401).send('Access Denied')
|
||||
|
||||
const { province_id, city_id, address, lat, lng, show_location } = req.body
|
||||
|
||||
// اعتبارسنجی دادهها
|
||||
if (!province_id || !city_id || !address || !lat || !lng || show_location === undefined) {
|
||||
return res.status(422).json({
|
||||
error: true,
|
||||
message: 'اطلاعات ارسالی ناقص است'
|
||||
})
|
||||
}
|
||||
|
||||
const decodedToken = jwt.verify(token, process.env.APP_SECRET)
|
||||
const userId = decodedToken.id
|
||||
|
||||
// یافتن کاربر با استفاده از شناسه کاربر
|
||||
const user = await UserModel.findById(userId)
|
||||
if (!user) {
|
||||
return res.status(404).json({
|
||||
error: true,
|
||||
message: 'کاربری با این شناسه یافت نشد'
|
||||
})
|
||||
}
|
||||
|
||||
// یافتن استان و شهر با استفاده از شناسه استان و شهر
|
||||
const province = await ProvinceModel.findOne({ id: province_id })
|
||||
const city = await CityModel.findOne({ id: city_id })
|
||||
if (!province || !city) {
|
||||
return res.status(404).json({
|
||||
error: true,
|
||||
message: 'استان یا شهر مورد نظر یافت نشد'
|
||||
})
|
||||
}
|
||||
|
||||
// بهروزرسانی اطلاعات آدرس کاربر
|
||||
user.province = province
|
||||
user.city = city
|
||||
user.address = address
|
||||
user.lat = lat
|
||||
user.lng = lng
|
||||
user.show_location = show_location
|
||||
|
||||
await user.save()
|
||||
|
||||
return res.json({
|
||||
message: 'آدرس با موفقیت بهروزرسانی شد'
|
||||
})
|
||||
} catch (error) {
|
||||
next(error)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
setAddress, updateAddress
|
||||
}
|
||||
108
controllers/application/verify/authController.js
Normal file
108
controllers/application/verify/authController.js
Normal file
@@ -0,0 +1,108 @@
|
||||
/* eslint-disable camelcase */
|
||||
const UserModel = require('../../../models/UserModel')
|
||||
const { check, validationResult } = require('express-validator')
|
||||
const jwt = require('jsonwebtoken')
|
||||
|
||||
const saveAuthValidationRules = () => {
|
||||
return [
|
||||
check('shaba').notEmpty().withMessage('شماره شبا نمیتواند خالی باشد')
|
||||
.matches(/^(?=.{24}$)[0-9]*$/).withMessage('فرمت شماره شبا صحیح نیست'),
|
||||
check('birthday').notEmpty().withMessage('تاریخ تولد نمیتواند خالی باشد'),
|
||||
check('national_code').notEmpty().withMessage('کد ملی نمیتواند خالی باشد')
|
||||
.isLength({ min: 10, max: 10 }).withMessage('کد ملی باید 10 رقم باشد')
|
||||
]
|
||||
}
|
||||
|
||||
const saveAuth = async (req, res, next) => {
|
||||
try {
|
||||
const token = req.header('Authorization').split(' ')[1]
|
||||
if (!token) return res.status(401).send('Access Denied')
|
||||
const errors = validationResult(req)
|
||||
if (!errors.isEmpty()) {
|
||||
return res.status(422).json({ errors: errors.array() })
|
||||
}
|
||||
const { shaba, birthday, national_code } = req.body
|
||||
if (!shaba || !birthday || !national_code) {
|
||||
return res.status(422).json({
|
||||
error: true,
|
||||
message: 'اطلاعات ارسالی اشتباه است'
|
||||
})
|
||||
}
|
||||
|
||||
// جستجوی کاربر با شماره موبایل ارسالی
|
||||
const decodedToken = jwt.verify(token, process.env.APP_SECRET)
|
||||
const user = await UserModel.findById(decodedToken.id)
|
||||
if (!user) {
|
||||
return res.status(404).json({
|
||||
error: true,
|
||||
message: 'کاربری با این شماره موبایل یافت نشد'
|
||||
})
|
||||
}
|
||||
|
||||
// بررسی تکراری بودن کد ملی
|
||||
const existingUserWithNationalCode = await UserModel.findOne({ national_code })
|
||||
if (existingUserWithNationalCode && existingUserWithNationalCode._id.toString() !== user._id.toString()) {
|
||||
return res.status(409).json({
|
||||
error: true,
|
||||
message: 'کد ملی تکراری است'
|
||||
})
|
||||
}
|
||||
user.shaba = shaba
|
||||
user.birthday = birthday
|
||||
user.national_code = national_code
|
||||
await user.save()
|
||||
|
||||
return res.json({
|
||||
message: 'اطلاعات با موفقیت بهروزرسانی شد'
|
||||
})
|
||||
} catch (error) {
|
||||
next(error)
|
||||
}
|
||||
}
|
||||
const updateShaba = async (req, res, next) => {
|
||||
try {
|
||||
const token = req.header('Authorization').split(' ')[1]
|
||||
if (!token) return res.status(401).send('Access Denied')
|
||||
|
||||
const errors = validationResult(req)
|
||||
if (!errors.isEmpty()) {
|
||||
return res.status(422).json({ errors: errors.array() })
|
||||
}
|
||||
|
||||
const { shaba } = req.body
|
||||
|
||||
if (!shaba) {
|
||||
return res.status(422).json({
|
||||
error: true,
|
||||
message: 'شبا ارسال نشده است'
|
||||
})
|
||||
}
|
||||
|
||||
const decodedToken = jwt.verify(token, process.env.APP_SECRET)
|
||||
const userId = decodedToken.id
|
||||
|
||||
// جستجوی کاربر با استفاده از شناسه کاربر
|
||||
const user = await UserModel.findById(userId)
|
||||
|
||||
if (!user) {
|
||||
return res.status(404).json({
|
||||
error: true,
|
||||
message: 'کاربری با این شناسه یافت نشد'
|
||||
})
|
||||
}
|
||||
|
||||
// فقط شبا را بهروزرسانی کنید
|
||||
user.shaba = shaba
|
||||
await user.save()
|
||||
|
||||
return res.json({
|
||||
message: 'شبا با موفقیت بهروزرسانی شد'
|
||||
})
|
||||
} catch (error) {
|
||||
next(error)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
saveAuth, saveAuthValidationRules, updateShaba
|
||||
}
|
||||
80
controllers/application/verify/colorsController.js
Normal file
80
controllers/application/verify/colorsController.js
Normal file
@@ -0,0 +1,80 @@
|
||||
/* eslint-disable camelcase */
|
||||
const UserModel = require('../../../models/UserModel')
|
||||
const jwt = require('jsonwebtoken')
|
||||
|
||||
const setColors = async (req, res, next) => {
|
||||
try {
|
||||
const token = req.header('Authorization').split(' ')[1]
|
||||
if (!token) return res.status(401).send('Access Denied')
|
||||
const { eye_color, hair_color } = req.body
|
||||
if (!eye_color || !hair_color) {
|
||||
return res.status(422).json({
|
||||
error: true,
|
||||
message: 'اطلاعات ارسالی اشتباه است'
|
||||
})
|
||||
}
|
||||
|
||||
// جستجوی کاربر با شماره موبایل ارسالی
|
||||
const decodedToken = jwt.verify(token, process.env.APP_SECRET)
|
||||
const user = await UserModel.findById(decodedToken.id)
|
||||
if (!user) {
|
||||
return res.status(404).json({
|
||||
error: true,
|
||||
message: 'کاربری با این شماره موبایل یافت نشد'
|
||||
})
|
||||
}
|
||||
|
||||
user.eye_color = eye_color
|
||||
user.hair_color = hair_color
|
||||
await user.save()
|
||||
|
||||
return res.json({
|
||||
message: 'رنگ ها با موفقیت بهروزرسانی شد'
|
||||
})
|
||||
} catch (error) {
|
||||
next(error)
|
||||
}
|
||||
}
|
||||
const updateColors = async (req, res, next) => {
|
||||
try {
|
||||
const token = req.header('Authorization').split(' ')[1]
|
||||
if (!token) return res.status(401).send('Access Denied')
|
||||
|
||||
const { eye_color, hair_color } = req.body
|
||||
|
||||
// اعتبارسنجی دادهها
|
||||
if (!eye_color || !hair_color) {
|
||||
return res.status(422).json({
|
||||
error: true,
|
||||
message: 'اطلاعات ارسالی ناقص است'
|
||||
})
|
||||
}
|
||||
|
||||
const decodedToken = jwt.verify(token, process.env.APP_SECRET)
|
||||
const userId = decodedToken.id
|
||||
|
||||
// یافتن کاربر با استفاده از شناسه کاربر
|
||||
const user = await UserModel.findById(userId)
|
||||
if (!user) {
|
||||
return res.status(404).json({
|
||||
error: true,
|
||||
message: 'کاربری با این شناسه یافت نشد'
|
||||
})
|
||||
}
|
||||
|
||||
// بهروزرسانی اطلاعات رنگها
|
||||
user.eye_color = eye_color
|
||||
user.hair_color = hair_color
|
||||
await user.save()
|
||||
|
||||
return res.json({
|
||||
message: 'رنگها با موفقیت بهروزرسانی شد'
|
||||
})
|
||||
} catch (error) {
|
||||
next(error)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
setColors, updateColors
|
||||
}
|
||||
74
controllers/application/verify/conversationController.js
Normal file
74
controllers/application/verify/conversationController.js
Normal file
@@ -0,0 +1,74 @@
|
||||
/* eslint-disable camelcase */
|
||||
const UserModel = require('../../../models/UserModel')
|
||||
const jwt = require('jsonwebtoken')
|
||||
|
||||
const setConversation = async (req, res, next) => {
|
||||
try {
|
||||
const token = req.header('Authorization').split(' ')[1]
|
||||
if (!token) return res.status(401).send('Access Denied')
|
||||
const { bio, conversation_projects } = req.body
|
||||
if (!bio || conversation_projects === undefined) {
|
||||
return res.status(422).json({
|
||||
error: true,
|
||||
message: 'اطلاعات ارسالی اشتباه است'
|
||||
})
|
||||
}
|
||||
// جستجوی کاربر با شماره موبایل ارسالی
|
||||
const decodedToken = jwt.verify(token, process.env.APP_SECRET)
|
||||
const user = await UserModel.findById(decodedToken.id)
|
||||
if (!user) {
|
||||
return res.status(404).json({
|
||||
error: true,
|
||||
message: 'کاربری با این شماره موبایل یافت نشد'
|
||||
})
|
||||
}
|
||||
|
||||
user.bio = bio
|
||||
user.conversation_projects = conversation_projects
|
||||
await user.save()
|
||||
|
||||
return res.json({
|
||||
message: 'روابط عمومی با موفقیت بهروزرسانی شد'
|
||||
})
|
||||
} catch (error) {
|
||||
next(error)
|
||||
}
|
||||
}
|
||||
const updateConversation = async (req, res, next) => {
|
||||
try {
|
||||
const token = req.header('Authorization').split(' ')[1]
|
||||
if (!token) return res.status(401).send('Access Denied')
|
||||
|
||||
const { bio, conversation_projects } = req.body
|
||||
|
||||
if (!bio || conversation_projects === undefined) {
|
||||
return res.status(422).json({
|
||||
error: true,
|
||||
message: 'اطلاعات ارسالی اشتباه است'
|
||||
})
|
||||
}
|
||||
|
||||
const decodedToken = jwt.verify(token, process.env.APP_SECRET)
|
||||
const userId = decodedToken.id
|
||||
// جستجوی کاربر با استفاده از شناسه کاربر
|
||||
const user = await UserModel.findById(userId)
|
||||
if (!user) {
|
||||
return res.status(404).json({
|
||||
error: true,
|
||||
message: 'کاربری با این شناسه یافت نشد'
|
||||
})
|
||||
}
|
||||
user.bio = bio
|
||||
user.conversation_projects = conversation_projects
|
||||
await user.save()
|
||||
|
||||
return res.json({
|
||||
message: 'روابط عمومی با موفقیت بهروزرسانی شد'
|
||||
})
|
||||
} catch (error) {
|
||||
next(error)
|
||||
}
|
||||
}
|
||||
module.exports = {
|
||||
setConversation, updateConversation
|
||||
}
|
||||
93
controllers/application/verify/cooperationTypeController.js
Normal file
93
controllers/application/verify/cooperationTypeController.js
Normal file
@@ -0,0 +1,93 @@
|
||||
/* eslint-disable camelcase */
|
||||
const UserModel = require('../../../models/UserModel')
|
||||
const jwt = require('jsonwebtoken')
|
||||
|
||||
const setCooperationType = async (req, res, next) => {
|
||||
try {
|
||||
const token = req.header('Authorization').split(' ')[1]
|
||||
if (!token) return res.status(401).send('Access Denied')
|
||||
const { cooperation_type, cooperation_abroad } = req.body
|
||||
if (!cooperation_type || cooperation_abroad === undefined) {
|
||||
return res.status(422).json({
|
||||
error: true,
|
||||
message: 'اطلاعات ارسالی اشتباه است'
|
||||
})
|
||||
}
|
||||
if (!['all', 'verified'].includes(cooperation_type)) {
|
||||
return res.status(422).json({
|
||||
error: true,
|
||||
message: 'نوع همکاری نیست'
|
||||
})
|
||||
}
|
||||
|
||||
// جستجوی کاربر با شماره موبایل ارسالی
|
||||
const decodedToken = jwt.verify(token, process.env.APP_SECRET)
|
||||
const user = await UserModel.findById(decodedToken.id)
|
||||
if (!user) {
|
||||
return res.status(404).json({
|
||||
error: true,
|
||||
message: 'کاربری با این شماره موبایل یافت نشد'
|
||||
})
|
||||
}
|
||||
|
||||
user.cooperation_type = cooperation_type
|
||||
user.cooperation_abroad = cooperation_abroad
|
||||
await user.save()
|
||||
|
||||
return res.json({
|
||||
message: 'نوع همکاری با موفقیت بهروزرسانی شد'
|
||||
})
|
||||
} catch (error) {
|
||||
next(error)
|
||||
}
|
||||
}
|
||||
const updateCooperationType = async (req, res, next) => {
|
||||
try {
|
||||
const token = req.header('Authorization').split(' ')[1]
|
||||
if (!token) return res.status(401).send('Access Denied')
|
||||
|
||||
const { cooperation_type, cooperation_abroad } = req.body
|
||||
|
||||
if (!cooperation_type || cooperation_abroad === undefined) {
|
||||
return res.status(422).json({
|
||||
error: true,
|
||||
message: 'اطلاعات ارسالی اشتباه است'
|
||||
})
|
||||
}
|
||||
|
||||
if (!['all', 'verified'].includes(cooperation_type)) {
|
||||
return res.status(422).json({
|
||||
error: true,
|
||||
message: 'نوع همکاری نامعتبر است'
|
||||
})
|
||||
}
|
||||
|
||||
const decodedToken = jwt.verify(token, process.env.APP_SECRET)
|
||||
const userId = decodedToken.id
|
||||
|
||||
// جستجوی کاربر با استفاده از شناسه کاربر
|
||||
const user = await UserModel.findById(userId)
|
||||
|
||||
if (!user) {
|
||||
return res.status(404).json({
|
||||
error: true,
|
||||
message: 'کاربری با این شناسه یافت نشد'
|
||||
})
|
||||
}
|
||||
|
||||
user.cooperation_type = cooperation_type
|
||||
user.cooperation_abroad = cooperation_abroad
|
||||
await user.save()
|
||||
|
||||
return res.json({
|
||||
message: 'نوع همکاری با موفقیت بهروزرسانی شد'
|
||||
})
|
||||
} catch (error) {
|
||||
next(error)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
setCooperationType,
|
||||
updateCooperationType
|
||||
}
|
||||
81
controllers/application/verify/expertiseController.js
Normal file
81
controllers/application/verify/expertiseController.js
Normal file
@@ -0,0 +1,81 @@
|
||||
/* eslint-disable camelcase */
|
||||
const UserModel = require('../../../models/UserModel')
|
||||
const jwt = require('jsonwebtoken')
|
||||
|
||||
const setExpertise = async (req, res, next) => {
|
||||
try {
|
||||
const token = req.header('Authorization').split(' ')[1]
|
||||
if (!token) return res.status(401).send('Access Denied')
|
||||
const { expertise, sub_expertise } = req.body
|
||||
if (!expertise || !sub_expertise) {
|
||||
return res.status(422).json({
|
||||
error: true,
|
||||
message: 'اطلاعات ارسالی اشتباه است'
|
||||
})
|
||||
}
|
||||
|
||||
// جستجوی کاربر با شماره موبایل ارسالی
|
||||
const decodedToken = jwt.verify(token, process.env.APP_SECRET)
|
||||
const user = await UserModel.findById(decodedToken.id)
|
||||
if (!user) {
|
||||
return res.status(404).json({
|
||||
error: true,
|
||||
message: 'کاربری با این شماره موبایل یافت نشد'
|
||||
})
|
||||
}
|
||||
|
||||
// آپدیت نام کاربری به صورت lowercase
|
||||
user.expertise = expertise
|
||||
user.sub_expertise = sub_expertise
|
||||
await user.save()
|
||||
|
||||
return res.json({
|
||||
message: 'تخصص با موفقیت بهروزرسانی شد'
|
||||
})
|
||||
} catch (error) {
|
||||
next(error)
|
||||
}
|
||||
}
|
||||
|
||||
const updateExpertise = async (req, res, next) => {
|
||||
try {
|
||||
const token = req.header('Authorization').split(' ')[1]
|
||||
if (!token) return res.status(401).send('Access Denied')
|
||||
|
||||
const { expertise, sub_expertise } = req.body
|
||||
|
||||
// اعتبارسنجی دادهها
|
||||
if (!expertise || !sub_expertise) {
|
||||
return res.status(422).json({
|
||||
error: true,
|
||||
message: 'اطلاعات ارسالی ناقص است'
|
||||
})
|
||||
}
|
||||
|
||||
const decodedToken = jwt.verify(token, process.env.APP_SECRET)
|
||||
const userId = decodedToken.id
|
||||
|
||||
// یافتن کاربر با استفاده از شناسه کاربر
|
||||
const user = await UserModel.findById(userId)
|
||||
if (!user) {
|
||||
return res.status(404).json({
|
||||
error: true,
|
||||
message: 'کاربری با این شناسه یافت نشد'
|
||||
})
|
||||
}
|
||||
|
||||
// بهروزرسانی اطلاعات تخصص و زیرتخصص
|
||||
user.expertise = expertise
|
||||
user.sub_expertise = sub_expertise
|
||||
await user.save()
|
||||
|
||||
return res.json({
|
||||
message: 'تخصص با موفقیت بهروزرسانی شد'
|
||||
})
|
||||
} catch (error) {
|
||||
next(error)
|
||||
}
|
||||
}
|
||||
module.exports = {
|
||||
setExpertise, updateExpertise
|
||||
}
|
||||
46
controllers/application/verify/genderController.js
Normal file
46
controllers/application/verify/genderController.js
Normal file
@@ -0,0 +1,46 @@
|
||||
/* eslint-disable camelcase */
|
||||
const UserModel = require('../../../models/UserModel')
|
||||
const jwt = require('jsonwebtoken')
|
||||
|
||||
const setGender = async (req, res, next) => {
|
||||
try {
|
||||
const token = req.header('Authorization').split(' ')[1]
|
||||
if (!token) return res.status(401).send('Access Denied')
|
||||
const { gender } = req.body
|
||||
if (!gender) {
|
||||
return res.status(422).json({
|
||||
error: true,
|
||||
message: 'اطلاعات ورودی اشتباه است'
|
||||
})
|
||||
}
|
||||
if (!['male', 'female'].includes(gender)) {
|
||||
return res.status(422).json({
|
||||
error: true,
|
||||
message: 'جنسیت معتبر نیست'
|
||||
})
|
||||
}
|
||||
// جستجوی کاربر با شماره موبایل ارسالی
|
||||
const decodedToken = jwt.verify(token, process.env.APP_SECRET)
|
||||
const user = await UserModel.findById(decodedToken.id)
|
||||
if (!user) {
|
||||
return res.status(404).json({
|
||||
error: true,
|
||||
message: 'کاربری با این شماره موبایل یافت نشد'
|
||||
})
|
||||
}
|
||||
|
||||
// آپدیت نوع یوزر
|
||||
user.gender = gender.toLowerCase()
|
||||
await user.save()
|
||||
|
||||
return res.json({
|
||||
message: 'جنسیت با موفقیت بهروزرسانی شد'
|
||||
})
|
||||
} catch (error) {
|
||||
next(error)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
setGender
|
||||
}
|
||||
107
controllers/application/verify/servicesController.js
Normal file
107
controllers/application/verify/servicesController.js
Normal file
@@ -0,0 +1,107 @@
|
||||
/* eslint-disable camelcase */
|
||||
const UserModel = require('../../../models/UserModel')
|
||||
const jwt = require('jsonwebtoken')
|
||||
const fs = require('fs-extra')
|
||||
const path = require('path')
|
||||
|
||||
const setServices = async (req, res, next) => {
|
||||
try {
|
||||
const token = req.header('Authorization').split(' ')[1]
|
||||
if (!token) return res.status(401).send('Access Denied')
|
||||
|
||||
const decodedToken = jwt.verify(token, process.env.APP_SECRET)
|
||||
const user = await UserModel.findById(decodedToken.id)
|
||||
if (!user) {
|
||||
return res.status(422).json({
|
||||
error: true,
|
||||
message: 'شما دسترسی به این بخش ندارید'
|
||||
})
|
||||
}
|
||||
const { services } = req.body
|
||||
if (!services) {
|
||||
return res.status(422).json({
|
||||
error: true,
|
||||
message: 'اطلاعات ارسالی اشتباه است'
|
||||
})
|
||||
}
|
||||
|
||||
// مدیریت تصاویر برای خدمات
|
||||
const servicesWithImages = []
|
||||
const parsedServices = JSON.parse(services) // پارس کردن رشته JSON به آرایه جاوا اسکریپت
|
||||
if (parsedServices && parsedServices.length > 0) {
|
||||
for (const service of parsedServices) {
|
||||
const serviceImages = []
|
||||
const serviceId = service.id
|
||||
if (req.files && req.files.serviceImages && req.files.serviceImages[serviceId]) {
|
||||
const imageFiles = Array.isArray(req.files.serviceImages[serviceId]) ? req.files.serviceImages[serviceId] : [req.files.serviceImages[serviceId]]
|
||||
const uploadDir = path.join(__dirname, '../../../../storage/services')
|
||||
if (!fs.existsSync(uploadDir)) {
|
||||
fs.mkdirSync(uploadDir, { recursive: true })
|
||||
}
|
||||
for (const imageFile of imageFiles) {
|
||||
const uniqueFileName = `${user.user_name}-${Date.now()}${Math.random().toString().slice(2, 11)}${path.extname(imageFile.name)}`
|
||||
const filePath = path.join(uploadDir, uniqueFileName)
|
||||
// await fs.promises.rename(imageFile.path, filePath)
|
||||
await fs.move(imageFile.path, filePath)
|
||||
const imageUrl = `/services/${uniqueFileName}`
|
||||
serviceImages.push(imageUrl)
|
||||
// بهروزرسانی مسیر تصویر در آبجکت service
|
||||
service.image = imageUrl
|
||||
}
|
||||
} else {
|
||||
console.log('sss')
|
||||
}
|
||||
servicesWithImages.push({ ...service, images: serviceImages })
|
||||
}
|
||||
}
|
||||
|
||||
user.services = servicesWithImages
|
||||
await user.save()
|
||||
|
||||
return res.json({
|
||||
message: 'خدمات با موفقیت ثبت شد'
|
||||
})
|
||||
} catch (error) {
|
||||
next(error)
|
||||
}
|
||||
}
|
||||
|
||||
const updateServices = async (req, res, next) => {
|
||||
try {
|
||||
const { height, weight, size } = req.body
|
||||
|
||||
if (!height || !weight || !size) {
|
||||
return res.status(422).json({
|
||||
error: true,
|
||||
message: 'اطلاعات ارسالی اشتباه است'
|
||||
})
|
||||
}
|
||||
const token = req.header('Authorization').split(' ')[1]
|
||||
if (!token) return res.status(401).send('Access Denied')
|
||||
const decodedToken = jwt.verify(token, process.env.APP_SECRET)
|
||||
const userId = decodedToken.id
|
||||
// جستجوی کاربر
|
||||
const user = await UserModel.findById(userId)
|
||||
|
||||
if (!user) {
|
||||
return res.status(404).json({
|
||||
error: true,
|
||||
message: 'کاربر یافت نشد'
|
||||
})
|
||||
}
|
||||
|
||||
user.height = height
|
||||
user.weight = weight
|
||||
user.size = size
|
||||
await user.save()
|
||||
|
||||
return res.json({
|
||||
message: 'سایز با موفقیت بهروزرسانی شد'
|
||||
})
|
||||
} catch (error) {
|
||||
next(error)
|
||||
}
|
||||
}
|
||||
module.exports = {
|
||||
setServices, updateServices
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
/* eslint-disable camelcase */
|
||||
const fs = require('fs-extra')
|
||||
const path = require('path')
|
||||
const UserModel = require('../../../models/UserModel')
|
||||
const jwt = require('jsonwebtoken')
|
||||
|
||||
const setNationalCardImage = async (req, res, next) => {
|
||||
try {
|
||||
const token = req.header('Authorization').split(' ')[1]
|
||||
if (!token) return res.status(401).send('Access Denied')
|
||||
const { national_card_image } = req.files
|
||||
// جستجوی کاربر با شماره موبایل ارسالی
|
||||
const decodedToken = jwt.verify(token, process.env.APP_SECRET)
|
||||
const user = await UserModel.findById(decodedToken.id)
|
||||
if (!user) {
|
||||
return res.status(404).json({
|
||||
error: true,
|
||||
message: 'کاربری با این شماره موبایل یافت نشد'
|
||||
})
|
||||
}
|
||||
// بررسی آیا فایل آپلود شده است
|
||||
if (!national_card_image) {
|
||||
return res.status(400).json({
|
||||
error: true,
|
||||
message: 'لطفاً عکس کارت ملی را انتخاب کنید'
|
||||
})
|
||||
}
|
||||
|
||||
// ذخیره فایل عکس پروفایل
|
||||
const uploadDir = path.join(__dirname, '../../../../storage/carts')
|
||||
if (!fs.existsSync(uploadDir)) {
|
||||
fs.mkdirSync(uploadDir, { recursive: true })
|
||||
}
|
||||
|
||||
const uniqueFileName = `${user.user_name}-${Date.now()}${path.extname(national_card_image.name)}`
|
||||
const filePath = path.join(uploadDir, uniqueFileName)
|
||||
await fs.move(national_card_image.path, filePath)
|
||||
|
||||
// ذخیره مسیر فایل در دیتابیس
|
||||
user.national_card_image = `/carts/${uniqueFileName}`
|
||||
user.is_verified = 'pending'
|
||||
await user.save()
|
||||
|
||||
return res.json({
|
||||
message: 'عکس کارت ملی با موفقیت ذخیره شد',
|
||||
national_card_image: user.national_card_image
|
||||
})
|
||||
} catch (error) {
|
||||
next(error)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
setNationalCardImage
|
||||
}
|
||||
105
controllers/application/verify/setProfileImageController.js
Normal file
105
controllers/application/verify/setProfileImageController.js
Normal file
@@ -0,0 +1,105 @@
|
||||
/* eslint-disable camelcase */
|
||||
const fs = require('fs-extra')
|
||||
const path = require('path')
|
||||
const UserModel = require('../../../models/UserModel')
|
||||
const jwt = require('jsonwebtoken')
|
||||
|
||||
const setProfileImage = async (req, res, next) => {
|
||||
try {
|
||||
const token = req.header('Authorization').split(' ')[1]
|
||||
if (!token) return res.status(401).send('Access Denied')
|
||||
const { mobile } = req.body
|
||||
const { profile_image } = req.files
|
||||
if (!mobile) {
|
||||
return res.status(422).json({
|
||||
error: true,
|
||||
message: 'اطلاعات ارسالی اشتباه است'
|
||||
})
|
||||
}
|
||||
const decodedToken = jwt.verify(token, process.env.APP_SECRET)
|
||||
const user = await UserModel.findById(decodedToken.id)
|
||||
if (!user) {
|
||||
return res.status(404).json({
|
||||
error: true,
|
||||
message: 'کاربری با این شماره موبایل یافت نشد'
|
||||
})
|
||||
}
|
||||
// بررسی آیا فایل آپلود شده است
|
||||
if (!profile_image) {
|
||||
return res.status(400).json({
|
||||
error: true,
|
||||
message: 'لطفاً عکس پروفایل را انتخاب کنید'
|
||||
})
|
||||
}
|
||||
|
||||
// ذخیره فایل عکس پروفایل
|
||||
const uploadDir = path.join(__dirname, '../../../../storage/profiles')
|
||||
if (!fs.existsSync(uploadDir)) {
|
||||
fs.mkdirSync(uploadDir, { recursive: true })
|
||||
}
|
||||
const uniqueFileName = `${user.user_name}-${Date.now()}${path.extname(profile_image.name)}`
|
||||
const filePath = path.join(uploadDir, uniqueFileName)
|
||||
|
||||
await fs.move(profile_image.path, filePath)
|
||||
// ذخیره مسیر فایل در دیتابیس
|
||||
user.profile_image = `/profiles/${uniqueFileName}`
|
||||
await user.save()
|
||||
|
||||
return res.json({
|
||||
message: 'عکس پروفایل با موفقیت ذخیره شد',
|
||||
profile_image: user.profile_image
|
||||
})
|
||||
} catch (error) {
|
||||
next(error)
|
||||
}
|
||||
}
|
||||
|
||||
const updateProfileImage = async (req, res, next) => {
|
||||
try {
|
||||
const token = req.header('Authorization').split(' ')[1]
|
||||
if (!token) return res.status(401).send('Access Denied')
|
||||
const decodedToken = jwt.verify(token, process.env.APP_SECRET)
|
||||
const userId = decodedToken.id
|
||||
const { profile_image } = req.files
|
||||
|
||||
if (!profile_image) {
|
||||
return res.status(422).json({
|
||||
error: true,
|
||||
message: 'اطلاعات ارسالی اشتباه است'
|
||||
})
|
||||
}
|
||||
|
||||
const user = await UserModel.findById(userId)
|
||||
|
||||
if (!user) {
|
||||
return res.status(404).json({
|
||||
error: true,
|
||||
message: 'کاربری با این شماره موبایل یافت نشد'
|
||||
})
|
||||
}
|
||||
|
||||
const uploadDir = path.join(__dirname, '../../../../storage/profiles')
|
||||
|
||||
if (!fs.existsSync(uploadDir)) {
|
||||
fs.mkdirSync(uploadDir, { recursive: true })
|
||||
}
|
||||
|
||||
const uniqueFileName = `${user.user_name}-${Date.now()}${path.extname(profile_image.name)}`
|
||||
const filePath = path.join(uploadDir, uniqueFileName)
|
||||
|
||||
await fs.move(profile_image.path, filePath)
|
||||
|
||||
user.profile_image = `/profiles/${uniqueFileName}`
|
||||
await user.save()
|
||||
|
||||
return res.json({
|
||||
message: 'عکس پروفایل با موفقیت بهروزرسانی شد',
|
||||
profile_image: user.profile_image
|
||||
})
|
||||
} catch (error) {
|
||||
next(error)
|
||||
}
|
||||
}
|
||||
module.exports = {
|
||||
setProfileImage, updateProfileImage
|
||||
}
|
||||
78
controllers/application/verify/sizesController.js
Normal file
78
controllers/application/verify/sizesController.js
Normal file
@@ -0,0 +1,78 @@
|
||||
/* eslint-disable camelcase */
|
||||
const UserModel = require('../../../models/UserModel')
|
||||
const jwt = require('jsonwebtoken')
|
||||
|
||||
const setSizes = async (req, res, next) => {
|
||||
try {
|
||||
const token = req.header('Authorization').split(' ')[1]
|
||||
if (!token) return res.status(401).send('Access Denied')
|
||||
const { height, weight, size } = req.body
|
||||
if (!height || !weight || !size) {
|
||||
return res.status(422).json({
|
||||
error: true,
|
||||
message: 'اطلاعات ارسالی اشتباه است'
|
||||
})
|
||||
}
|
||||
|
||||
// جستجوی کاربر با شماره موبایل ارسالی
|
||||
const decodedToken = jwt.verify(token, process.env.APP_SECRET)
|
||||
const user = await UserModel.findById(decodedToken.id)
|
||||
if (!user) {
|
||||
return res.status(404).json({
|
||||
error: true,
|
||||
message: 'کاربری با این شماره موبایل یافت نشد'
|
||||
})
|
||||
}
|
||||
|
||||
user.height = height
|
||||
user.weight = weight
|
||||
user.size = size
|
||||
await user.save()
|
||||
|
||||
return res.json({
|
||||
message: 'سایز با موفقیت بهروزرسانی شد'
|
||||
})
|
||||
} catch (error) {
|
||||
next(error)
|
||||
}
|
||||
}
|
||||
|
||||
const updateSizes = async (req, res, next) => {
|
||||
try {
|
||||
const { height, weight, size } = req.body
|
||||
|
||||
if (!height || !weight || !size) {
|
||||
return res.status(422).json({
|
||||
error: true,
|
||||
message: 'اطلاعات ارسالی اشتباه است'
|
||||
})
|
||||
}
|
||||
const token = req.header('Authorization').split(' ')[1]
|
||||
if (!token) return res.status(401).send('Access Denied')
|
||||
const decodedToken = jwt.verify(token, process.env.APP_SECRET)
|
||||
const userId = decodedToken.id
|
||||
// جستجوی کاربر
|
||||
const user = await UserModel.findById(userId)
|
||||
|
||||
if (!user) {
|
||||
return res.status(404).json({
|
||||
error: true,
|
||||
message: 'کاربر یافت نشد'
|
||||
})
|
||||
}
|
||||
|
||||
user.height = height
|
||||
user.weight = weight
|
||||
user.size = size
|
||||
await user.save()
|
||||
|
||||
return res.json({
|
||||
message: 'سایز با موفقیت بهروزرسانی شد'
|
||||
})
|
||||
} catch (error) {
|
||||
next(error)
|
||||
}
|
||||
}
|
||||
module.exports = {
|
||||
setSizes, updateSizes
|
||||
}
|
||||
Reference in New Issue
Block a user