reels
This commit is contained in:
@@ -1,8 +1,6 @@
|
||||
/* 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 {
|
||||
@@ -10,13 +8,6 @@ const setServices = async (req, res, next) => {
|
||||
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({
|
||||
@@ -25,38 +16,37 @@ const setServices = async (req, res, next) => {
|
||||
})
|
||||
}
|
||||
|
||||
// مدیریت تصاویر برای خدمات
|
||||
const servicesWithImages = []
|
||||
// مدیریت تصاویر برای خدمات — multer (routes/application/verify/index.js، با
|
||||
// .any() چون اسمِ فیلد به ازای هر خدمت پویاست: serviceImages[id]) فایلها رو
|
||||
// بهصورتِ آرایه میده، با fieldname مشخص میکنیم مالِ کدوم خدمته
|
||||
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 })
|
||||
const uploadedFiles = Array.isArray(req.files) ? req.files : []
|
||||
const servicesWithImages = (parsedServices || []).map((service) => {
|
||||
const uploaded = uploadedFiles.find(
|
||||
(f) => f.fieldname === `serviceImages[${service.id}]`
|
||||
)
|
||||
if (uploaded) {
|
||||
const imageUrl = `/services/${uploaded.filename}`
|
||||
return { ...service, image: imageUrl, images: [imageUrl] }
|
||||
}
|
||||
}
|
||||
// بدون عکسِ تازه — هر مسیرِ عکسِ قبلی که خودِ کلاینت توی همین آیتم فرستاده
|
||||
// (حالتِ ویرایش، نگاه کن به EditServicesScreen.tsx/web) دستنخورده میمونه
|
||||
return { ...service, images: service.image ? [service.image] : [] }
|
||||
})
|
||||
|
||||
user.services = servicesWithImages
|
||||
await user.save()
|
||||
// updateOne اتمیک بهجای findById+user.save() — یه enum نامعتبرِ قدیمی روی یه
|
||||
// فیلدِ کاملاً بیربطِ همین کاربر با save() کلِ سند رو re-validate میکرد و همین
|
||||
// آپدیتِ بیربط رو میشکست (نگاه کن به همین فیکس توی setProfileImageController.js)
|
||||
const result = await UserModel.updateOne(
|
||||
{ _id: decodedToken.id },
|
||||
{ services: servicesWithImages }
|
||||
)
|
||||
if (result.matchedCount === 0) {
|
||||
return res.status(422).json({
|
||||
error: true,
|
||||
message: 'شما دسترسی به این بخش ندارید'
|
||||
})
|
||||
}
|
||||
|
||||
return res.json({
|
||||
message: 'خدمات با موفقیت ثبت شد'
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
/* eslint-disable camelcase */
|
||||
const fs = require('fs-extra')
|
||||
const path = require('path')
|
||||
const UserModel = require('../../../models/UserModel')
|
||||
const jwt = require('jsonwebtoken')
|
||||
|
||||
@@ -8,16 +6,8 @@ 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 national_card_image = req.file
|
||||
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({
|
||||
@@ -26,25 +16,27 @@ const setNationalCardImage = async (req, res, next) => {
|
||||
})
|
||||
}
|
||||
|
||||
// ذخیره فایل عکس پروفایل
|
||||
const uploadDir = path.join(__dirname, '../../../../storage/carts')
|
||||
if (!fs.existsSync(uploadDir)) {
|
||||
fs.mkdirSync(uploadDir, { recursive: true })
|
||||
// multer (routes/application/verify/index.js) فایل رو مستقیم با اسمِ یکتا توی
|
||||
// storage/carts ذخیره کرده — نیازی به move جداگانه نیست
|
||||
const imagePath = `/carts/${national_card_image.filename}`
|
||||
|
||||
// updateOne اتمیک بهجای findById+user.save() — یه enum نامعتبرِ قدیمی روی یه
|
||||
// فیلدِ بیربطِ همین کاربر با save() کلِ سند رو re-validate و همین آپدیت رو
|
||||
// میشکست (نگاه کن به همین فیکس توی setProfileImageController.js)
|
||||
const result = await UserModel.updateOne(
|
||||
{ _id: decodedToken.id },
|
||||
{ national_card_image: imagePath, is_Register: true, is_verified: 'pending' }
|
||||
)
|
||||
if (result.matchedCount === 0) {
|
||||
return res.status(404).json({
|
||||
error: true,
|
||||
message: 'کاربری با این شماره موبایل یافت نشد'
|
||||
})
|
||||
}
|
||||
|
||||
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_Register = true
|
||||
user.is_verified = 'pending'
|
||||
await user.save()
|
||||
|
||||
return res.json({
|
||||
message: 'عکس کارت ملی با موفقیت ذخیره شد',
|
||||
national_card_image: user.national_card_image
|
||||
national_card_image: imagePath
|
||||
})
|
||||
} catch (error) {
|
||||
next(error)
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
/* eslint-disable camelcase */
|
||||
const fs = require('fs-extra')
|
||||
const path = require('path')
|
||||
const UserModel = require('../../../models/UserModel')
|
||||
const jwt = require('jsonwebtoken')
|
||||
const { optimizeImageFile } = require('../../../utils/imageOptimize')
|
||||
@@ -13,15 +11,8 @@ const setProfileImage = async (req, res, next) => {
|
||||
try {
|
||||
const token = req.header('Authorization').split(' ')[1]
|
||||
if (!token) return res.status(401).send('Access Denied')
|
||||
const { profile_image } = req.files
|
||||
const profile_image = req.file
|
||||
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({
|
||||
@@ -30,23 +21,28 @@ const setProfileImage = async (req, res, next) => {
|
||||
})
|
||||
}
|
||||
|
||||
// ذخیره فایل عکس پروفایل
|
||||
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)
|
||||
// multer (routes/application/verify/index.js) فایل رو مستقیم با اسمِ یکتا توی
|
||||
// storage/profiles ذخیره کرده — نیازی به move جداگانه نیست
|
||||
await optimizeImageFile(profile_image.path, { maxDimension: AVATAR_MAX_DIMENSION })
|
||||
const imagePath = `/profiles/${profile_image.filename}`
|
||||
|
||||
await fs.move(profile_image.path, filePath)
|
||||
await optimizeImageFile(filePath, { maxDimension: AVATAR_MAX_DIMENSION })
|
||||
// ذخیره مسیر فایل در دیتابیس
|
||||
user.profile_image = `/profiles/${uniqueFileName}`
|
||||
await user.save()
|
||||
// updateOne اتمیک بهجای findById+user.save() — یه enum نامعتبرِ قدیمی روی یه
|
||||
// فیلدِ کاملاً بیربطِ همین کاربر (مثلاً personal_style، از دادههای قدیمی) با
|
||||
// save() کلِ سند رو re-validate میکرد و همین آپدیتِ بیربط رو میشکست
|
||||
const result = await UserModel.updateOne(
|
||||
{ _id: decodedToken.id },
|
||||
{ profile_image: imagePath }
|
||||
)
|
||||
if (result.matchedCount === 0) {
|
||||
return res.status(404).json({
|
||||
error: true,
|
||||
message: 'کاربری با این شماره موبایل یافت نشد'
|
||||
})
|
||||
}
|
||||
|
||||
return res.json({
|
||||
message: 'عکس پروفایل با موفقیت ذخیره شد',
|
||||
profile_image: user.profile_image
|
||||
profile_image: imagePath
|
||||
})
|
||||
} catch (error) {
|
||||
next(error)
|
||||
@@ -59,7 +55,7 @@ const updateProfileImage = async (req, res, next) => {
|
||||
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
|
||||
const profile_image = req.file
|
||||
|
||||
if (!profile_image) {
|
||||
return res.status(422).json({
|
||||
@@ -68,33 +64,23 @@ const updateProfileImage = async (req, res, next) => {
|
||||
})
|
||||
}
|
||||
|
||||
const user = await UserModel.findById(userId)
|
||||
// multer (routes/application/verify/index.js) فایل رو مستقیم با اسمِ یکتا توی
|
||||
// storage/profiles ذخیره کرده — نیازی به move جداگانه نیست
|
||||
await optimizeImageFile(profile_image.path, { maxDimension: AVATAR_MAX_DIMENSION })
|
||||
const imagePath = `/profiles/${profile_image.filename}`
|
||||
|
||||
if (!user) {
|
||||
// updateOne اتمیک بهجای findById+user.save() — همون دلیلِ setProfileImage بالا
|
||||
const result = await UserModel.updateOne({ _id: userId }, { profile_image: imagePath })
|
||||
if (result.matchedCount === 0) {
|
||||
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)
|
||||
await optimizeImageFile(filePath, { maxDimension: AVATAR_MAX_DIMENSION })
|
||||
|
||||
user.profile_image = `/profiles/${uniqueFileName}`
|
||||
await user.save()
|
||||
|
||||
return res.json({
|
||||
message: 'عکس پروفایل با موفقیت بهروزرسانی شد',
|
||||
profile_image: user.profile_image
|
||||
profile_image: imagePath
|
||||
})
|
||||
} catch (error) {
|
||||
next(error)
|
||||
|
||||
Reference in New Issue
Block a user