back-end done

This commit is contained in:
Amir Mohamadi
2020-12-10 16:36:48 +03:30
parent 80a7abafb7
commit bc5e617778
92 changed files with 5217 additions and 3379 deletions
+365 -298
View File
@@ -1,168 +1,207 @@
const Product = require('../models/product/Product')
const CartItem = require('../models/user/Cart_item')
const Order = require('../models/user/Order')
const {body, validationResult} = require('express-validator')
const dateformat = require('dateformat')
const jimp = require('jimp')
const fs = require('fs')
const v_m = require('../validation_messages')
////////////////////////////////////////// products controllers
module.exports.createProduct = [
[
body('fa_name')
.isLength({min: 2}).withMessage('حداقل 2 کاراکتر')
.notEmpty().withMessage(v_m['fa'].required.title)
.bail()
.custom((value, {req}) => {
return Product.findOne({'product_details.fa.name': value})
.then(product => {
if (product) return Promise.reject('محصول با این نام از قبل وجود دارد.')
if (product) return Promise.reject(v_m['fa'].duplicated.name)
else return true
})
}),
body('en_name')
.if((value, {req}) => value !== '')
.notEmpty().withMessage(v_m['fa'].required.title)
.bail()
.custom((value, {req}) => {
return Product.findOne({'product_details.en.name': value})
.then(product => {
if (product) return Promise.reject('محصول با این نام از قبل وجود دارد.')
if (product) return Promise.reject(v_m['fa'].duplicated.name)
else return true
})
}),
body('de_name')
.if((value, {req}) => value !== '')
.custom((value, {req}) => {
return Product.findOne({'product_details.de.name': value})
.then(product => {
if (product) return Promise.reject('محصول با این نام از قبل وجود دارد.')
else return true
})
}),
body('tr_name')
.if((value, {req}) => value !== '')
.custom((value, {req}) => {
return Product.findOne({'product_details.tr.name': value})
.then(product => {
if (product) return Promise.reject('محصول با این نام از قبل وجود دارد.')
else return true
})
}),
body('it_name')
.if((value, {req}) => value !== '')
.custom((value, {req}) => {
return Product.findOne({'product_details.it.name': value})
.then(product => {
if (product) return Promise.reject('محصول با این نام از قبل وجود دارد.')
else return true
})
}),
body('fa_description')
.isLength({min: 2}).withMessage('حداقل 2 کاراکتر'),
body('fa_price')
.notEmpty().withMessage('قیمت را وارد کنید.')
body('fa_short_description')
.notEmpty().withMessage(v_m['fa'].required.caption)
.bail()
.isNumeric().withMessage('قیمت باید از جنس عدد باشد'),
.isLength({max: 100}).withMessage(v_m['fa'].max_char.max100),
body('en_price')
.notEmpty().withMessage('قیمت را وارد کنید.')
body('en_short_description')
.notEmpty().withMessage(v_m['fa'].required.caption)
.bail()
.isNumeric().withMessage('قیمت باید از جنس عدد باشد'),
.isLength({max: 100}).withMessage(v_m['fa'].max_char.max100),
body('de_price')
.notEmpty().withMessage('قیمت را وارد کنید.')
.bail()
.isNumeric().withMessage('قیمت باید از جنس عدد باشد'),
body('fa_page_title')
.notEmpty().withMessage(v_m['fa'].required.title),
body('tr_price')
.notEmpty().withMessage('قیمت را وارد کنید.')
.bail()
.isNumeric().withMessage('قیمت باید از جنس عدد باشد'),
body('en_page_title')
.notEmpty().withMessage(v_m['fa'].required.title),
body('it_price')
.notEmpty().withMessage('قیمت را وارد کنید.')
.bail()
.isNumeric().withMessage('قیمت باید از جنس عدد باشد'),
body('fa_page_description')
.notEmpty().withMessage(v_m['fa'].required.caption),
body('stock')
.notEmpty().withMessage('موجودی را وارد کنید.')
.bail()
.isNumeric().withMessage('موجودی باید از جنس عدد باشد'),
body('en_page_description')
.notEmpty().withMessage(v_m['fa'].required.caption),
body('en_page_description')
.notEmpty().withMessage(v_m['fa'].required.caption),
body('category')
.notEmpty().withMessage('دسته بندی را انتخاب کنید.')
.notEmpty().withMessage(v_m['fa'].required.category)
],
(req, res) => {
async (req, res) => {
const errors = validationResult(req)
if (!errors.isEmpty()) return res.status(422).json({validation: errors.mapped()})
if (!req.files || !req.files.cover) return res.status(422).json({validation: {cover: {msg: v_m['fa'].required.cover}}})
let image
let imageName
if (req.files && req.files.image) {
image = req.files.image
imageName = 'product_' + Date.now() + '.' + image.mimetype.split('/')[1]
} else {
return res.status(422).json({validation: {image: {msg: 'عکس اجباری است.'}}})
let cover = 'product_' + Date.now() + '.' + req.files.cover.mimetype.split('/')[1]
let more_section_image1 = null
let more_section_image2 = null
let render_image1 = null
let render_image2 = null
let chart_image = null
//// generate files names
if (req.files) {
if (req.files.more_section_image1) {
more_section_image1 = 'product_' + Date.now() + 1 + '.' + req.files.more_section_image1.mimetype.split('/')[1]
}
if (req.files.more_section_image2) {
more_section_image2 = 'product_' + Date.now() + 2 + '.' + req.files.more_section_image2.mimetype.split('/')[1]
}
if (req.files.render_image1) {
render_image1 = 'product_' + Date.now() + 3 + '.' + req.files.render_image1.mimetype.split('/')[1]
}
if (req.files.render_image2) {
render_image2 = 'product_' + Date.now() + 4 + '.' + req.files.render_image2.mimetype.split('/')[1]
}
if (req.files.chart_image) {
chart_image = 'product_' + Date.now() + 5 + '.' + req.files.chart_image.mimetype.split('/')[1]
}
}
const data = {
price: req.body.price,
stock: req.body.stock,
cover: cover,
category: req.body.category,
more_section: req.body.more_section,
download_section: req.body.download_section,
product_details: {
fa: {
name: req.body.fa_name,
description: req.body.fa_description,
price: req.body.fa_price
short_description: req.body.fa_short_description,
page_title: req.body.fa_page_title,
page_description: req.body.fa_page_description,
more_title1: req.body.fa_more_title1,
more_description1: req.body.fa_more_description1,
more_title2: req.body.fa_more_title2,
more_description2: req.body.fa_more_description2,
render_caption1: req.body.fa_render_caption1,
render_caption2: req.body.fa_render_caption2,
chart_title: req.body.fa_chart_title,
chart_description: req.body.fa_chart_description
},
en: {
name: req.body.en_name,
description: req.body.en_description,
price: req.body.en_price
},
de: {
name: req.body.de_name,
description: req.body.de_description,
price: req.body.de_price
},
it: {
name: req.body.it_name,
description: req.body.it_description,
price: req.body.it_price
},
tr: {
name: req.body.tr_name,
description: req.body.tr_description,
price: req.body.tr_price
short_description: req.body.en_short_description,
page_title: req.body.en_page_title,
page_description: req.body.en_page_description,
more_title1: req.body.en_more_title1,
more_description1: req.body.en_more_description1,
more_title2: req.body.en_more_title2,
more_description2: req.body.en_more_description2,
render_caption1: req.body.en_render_caption1,
render_caption2: req.body.en_render_caption2,
chart_title: req.body.en_chart_title,
chart_description: req.body.en_chart_description
}
},
created_at: dateformat(Date.now(), 'yyyy-mm-dd HH:MM:ss')
}
if (req.files.more_section_image1) data.more_section_image1 = more_section_image1
if (req.files.more_section_image2) data.more_section_image2 = more_section_image2
if (req.files.render_image1) data.render_image1 = render_image1
if (req.files.render_image2) data.render_image2 = render_image2
if (req.files.chart_image) data.chart_image = chart_image
jimp.read(image.data)
.then(img => {
img
.resize(250, jimp.AUTO)
.cover(250, 250)
.write(`./static/uploads/images/products/${imageName}`)
//// write to database
data.image = imageName
const product = new Product(data)
product.save((err, data) => {
if (err) console.log(err)
return res.json(data)
})
})
//// save images
if (req.files) {
if (req.files.cover) {
await jimp.read(req.files.cover.data)
.then(img => {
img
.resize(470, jimp.AUTO)
.cover(470, 390)
.quality(70)
.write(`./static/uploads/images/products/${cover}`)
})
}
if (req.files.more_section_image1) {
await jimp.read(req.files.more_section_image1.data)
.then(img => {
img
.resize(778, jimp.AUTO)
.cover(778, 395)
.quality(70)
.write(`./static/uploads/images/products/${more_section_image1}`)
})
}
if (req.files.more_section_image2) {
await jimp.read(req.files.more_section_image2.data)
.then(img => {
img
.resize(778, jimp.AUTO)
.cover(778, 395)
.quality(70)
.write(`./static/uploads/images/products/${more_section_image2}`)
})
}
if (req.files.render_image1) {
await jimp.read(req.files.render_image1.data)
.then(img => {
img
.resize(300, jimp.AUTO)
.cover(300, 300)
.quality(70)
.write(`./static/uploads/images/products/${render_image1}`)
})
}
if (req.files.render_image2) {
await jimp.read(req.files.render_image2.data)
.then(img => {
img
.resize(300, jimp.AUTO)
.cover(300, 300)
.quality(70)
.write(`./static/uploads/images/products/${render_image2}`)
})
}
if (req.files.chart_image) {
await jimp.read(req.files.chart_image.data)
.then(img => {
img
.quality(100)
.write(`./static/uploads/images/products/${chart_image}`)
})
}
}
//// write to database
const product = new Product(data)
product.save((err, data) => {
if (err) console.log(err)
return res.json(data)
})
}
]
@@ -179,7 +218,7 @@ module.exports.getAllProductsByCategory = [
module.exports.getAllProducts = [
(req, res) => {
Product.find({}).select('image stock category product_details').exec((err, products) => {
Product.find({}).select('cover category product_details').exec((err, products) => {
if (err) console.log(err)
return res.json(products)
})
@@ -198,181 +237,233 @@ module.exports.getOneProduct = [
module.exports.updateProduct = [
[
body('fa_name')
.isLength({min: 2}).withMessage('حداقل 2 کاراکتر')
.notEmpty().withMessage(v_m['fa'].required.title)
.bail()
.custom((value, {req}) => {
return Product.findOne({'product_details.fa.name': value})
.then(product => {
if (product && product._id.toString() !== req.params.id) return Promise.reject('محصول با این نام از قبل وجود دارد.')
if (product && product._id.toString() !== req.params.id) return Promise.reject(v_m['fa'].duplicated.name)
else return true
})
}),
body('en_name')
.if((value, {req}) => value !== '')
.notEmpty().withMessage(v_m['fa'].required.title)
.bail()
.custom((value, {req}) => {
return Product.findOne({'product_details.en.name': value})
.then(product => {
if (product && product._id.toString() !== req.params.id) return Promise.reject('محصول با این نام از قبل وجود دارد.')
if (product && product._id.toString() !== req.params.id) return Promise.reject(v_m['fa'].duplicated.name)
else return true
})
}),
body('de_name')
.if((value, {req}) => value !== '')
.custom((value, {req}) => {
return Product.findOne({'product_details.de.name': value})
.then(product => {
if (product && product._id.toString() !== req.params.id) return Promise.reject('محصول با این نام از قبل وجود دارد.')
else return true
})
}),
body('tr_name')
.if((value, {req}) => value !== '')
.custom((value, {req}) => {
return Product.findOne({'product_details.tr.name': value})
.then(product => {
if (product && product._id.toString() !== req.params.id) return Promise.reject('محصول با این نام از قبل وجود دارد.')
else return true
})
}),
body('it_name')
.if((value, {req}) => value !== '')
.custom((value, {req}) => {
return Product.findOne({'product_details.it.name': value})
.then(product => {
if (product && product._id.toString() !== req.params.id) return Promise.reject('محصول با این نام از قبل وجود دارد.')
else return true
})
}),
body('fa_description')
.isLength({min: 2}).withMessage('حداقل 2 کاراکتر'),
body('fa_price')
.notEmpty().withMessage('قیمت را وارد کنید.')
body('fa_short_description')
.notEmpty().withMessage(v_m['fa'].required.caption)
.bail()
.isNumeric().withMessage('قیمت باید از جنس عدد باشد'),
.isLength({max: 100}).withMessage(v_m['fa'].max_char.max100),
body('en_price')
.notEmpty().withMessage('قیمت را وارد کنید.')
body('en_short_description')
.notEmpty().withMessage(v_m['fa'].required.caption)
.bail()
.isNumeric().withMessage('قیمت باید از جنس عدد باشد'),
.isLength({max: 100}).withMessage(v_m['fa'].max_char.max100),
body('de_price')
.notEmpty().withMessage('قیمت را وارد کنید.')
.bail()
.isNumeric().withMessage('قیمت باید از جنس عدد باشد'),
body('fa_page_title')
.notEmpty().withMessage(v_m['fa'].required.title),
body('tr_price')
.notEmpty().withMessage('قیمت را وارد کنید.')
.bail()
.isNumeric().withMessage('قیمت باید از جنس عدد باشد'),
body('en_page_title')
.notEmpty().withMessage(v_m['fa'].required.title),
body('it_price')
.notEmpty().withMessage('قیمت را وارد کنید.')
.bail()
.isNumeric().withMessage('قیمت باید از جنس عدد باشد'),
body('fa_page_description')
.notEmpty().withMessage(v_m['fa'].required.caption),
body('stock')
.notEmpty().withMessage('موجودی را وارد کنید.')
.bail()
.isNumeric().withMessage('موجودی باید از جنس عدد باشد'),
body('en_page_description')
.notEmpty().withMessage(v_m['fa'].required.caption),
body('en_page_description')
.notEmpty().withMessage(v_m['fa'].required.caption),
body('category')
.notEmpty().withMessage('دسته بندی را انتخاب کنید.')
.notEmpty().withMessage(v_m['fa'].required.category)
],
(req, res) => {
async (req, res) => {
const errors = validationResult(req)
if (!errors.isEmpty()) return res.status(422).json({validation: errors.mapped()})
let cover = null
let more_section_image1 = null
let more_section_image2 = null
let render_image1 = null
let render_image2 = null
let chart_image = null
//// generate files names
if (req.files) {
if (req.files.cover) {
cover = 'product_' + Date.now() + '.' + req.files.cover.mimetype.split('/')[1]
}
if (req.files.more_section_image1) {
more_section_image1 = 'product_' + Date.now() + 1 + '.' + req.files.more_section_image1.mimetype.split('/')[1]
}
if (req.files.more_section_image2) {
more_section_image2 = 'product_' + Date.now() + 2 + '.' + req.files.more_section_image2.mimetype.split('/')[1]
}
if (req.files.render_image1) {
render_image1 = 'product_' + Date.now() + 3 + '.' + req.files.render_image1.mimetype.split('/')[1]
}
if (req.files.render_image2) {
render_image2 = 'product_' + Date.now() + 4 + '.' + req.files.render_image2.mimetype.split('/')[1]
}
if (req.files.chart_image) {
chart_image = 'product_' + Date.now() + 5 + '.' + req.files.chart_image.mimetype.split('/')[1]
}
}
const data = {
price: req.body.price,
stock: req.body.stock,
category: req.body.category,
more_section: req.body.more_section,
download_section: req.body.download_section,
product_details: {
fa: {
name: req.body.fa_name,
description: req.body.fa_description,
price: req.body.fa_price
short_description: req.body.fa_short_description,
page_title: req.body.fa_page_title,
page_description: req.body.fa_page_description,
more_title1: req.body.fa_more_title1,
more_description1: req.body.fa_more_description1,
more_title2: req.body.fa_more_title2,
more_description2: req.body.fa_more_description2,
render_caption1: req.body.fa_render_caption1,
render_caption2: req.body.fa_render_caption2,
chart_title: req.body.fa_chart_title,
chart_description: req.body.fa_chart_description
},
en: {
name: req.body.en_name,
description: req.body.en_description,
price: req.body.en_price
},
de: {
name: req.body.de_name,
description: req.body.de_description,
price: req.body.de_price
},
it: {
name: req.body.it_name,
description: req.body.it_description,
price: req.body.it_price
},
tr: {
name: req.body.tr_name,
description: req.body.tr_description,
price: req.body.tr_price
short_description: req.body.en_short_description,
page_title: req.body.en_page_title,
page_description: req.body.en_page_description,
more_title1: req.body.en_more_title1,
more_description1: req.body.en_more_description1,
more_title2: req.body.en_more_title2,
more_description2: req.body.en_more_description2,
render_caption1: req.body.en_render_caption1,
render_caption2: req.body.en_render_caption2,
chart_title: req.body.en_chart_title,
chart_description: req.body.en_chart_description
}
},
updated_at: dateformat(Date.now(), 'yyyy-mm-dd HH:MM:ss')
created_at: dateformat(Date.now(), 'yyyy-mm-dd HH:MM:ss')
}
if (req.files) {
if (req.files.cover) data.cover = cover
if (req.files.more_section_image1) data.more_section_image1 = more_section_image1
if (req.files.more_section_image2) data.more_section_image2 = more_section_image2
if (req.files.render_image1) data.render_image1 = render_image1
if (req.files.render_image2) data.render_image2 = render_image2
if (req.files.chart_image) data.chart_image = chart_image
}
let image
let imageName
if (req.files && req.files.image) {
image = req.files.image
imageName = 'product_' + Date.now() + '.' + image.mimetype.split('/')[1]
data.image = imageName
jimp.read(image.data)
.then(img => {
img
.resize(250, jimp.AUTO)
.cover(250, 250)
.write(`./static/uploads/images/products/${imageName}`)
Product.findByIdAndUpdate(req.params.id, data, (err, oldData) => {
if (err) console.log(err)
fs.unlink(`./static/${oldData.image}`, (err) => {
if (err) console.log(err)
})
return res.json({message: 'product updated.'})
//// save images
if (req.files) {
if (req.files.cover) {
await jimp.read(req.files.cover.data)
.then(img => {
img
.resize(470, jimp.AUTO)
.cover(470, 390)
.quality(70)
.write(`./static/uploads/images/products/${cover}`)
})
})
} else {
Product.findByIdAndUpdate(req.params.id, data, (err, oldData) => {
if (err) console.log(err)
return res.json({message: 'product updated.'})
})
}
if (req.files.more_section_image1) {
await jimp.read(req.files.more_section_image1.data)
.then(img => {
img
.resize(778, jimp.AUTO)
.cover(778, 395)
.quality(70)
.write(`./static/uploads/images/products/${more_section_image1}`)
})
}
if (req.files.more_section_image2) {
await jimp.read(req.files.more_section_image2.data)
.then(img => {
img
.resize(778, jimp.AUTO)
.cover(778, 395)
.quality(70)
.write(`./static/uploads/images/products/${more_section_image2}`)
})
}
if (req.files.render_image1) {
await jimp.read(req.files.render_image1.data)
.then(img => {
img
.resize(300, jimp.AUTO)
.cover(300, 300)
.quality(70)
.write(`./static/uploads/images/products/${render_image1}`)
})
}
if (req.files.render_image2) {
await jimp.read(req.files.render_image2.data)
.then(img => {
img
.resize(300, jimp.AUTO)
.cover(300, 300)
.quality(70)
.write(`./static/uploads/images/products/${render_image2}`)
})
}
if (req.files.chart_image) {
await jimp.read(req.files.chart_image.data)
.then(img => {
img
.quality(100)
.write(`./static/uploads/images/products/${chart_image}`)
})
}
}
//// write to database
Product.findByIdAndUpdate(req.params.id, data, (err, oldData) => {
if (err) console.log(err)
if (req.files) {
if (req.files.cover) fs.unlink(`./static/${oldData.cover}`, err => {
if (err) console.log(err)
})
if (req.files.more_section_image1 && oldData.more_section_image1) fs.unlink(`./static/${oldData.more_section_image1}`, err => {
if (err) console.log(err)
})
if (req.files.more_section_image2 && oldData.more_section_image2) fs.unlink(`./static/${oldData.more_section_image2}`, err => {
if (err) console.log(err)
})
if (req.files.render_image1 && oldData.render_image1) fs.unlink(`./static/${oldData.render_image1}`, err => {
if (err) console.log(err)
})
if (req.files.render_image2 && oldData.render_image2) fs.unlink(`./static/${oldData.render_image2}`, err => {
if (err) console.log(err)
})
if (req.files.chart_image && oldData.chart_image) fs.unlink(`./static/${oldData.chart_image}`, err => {
if (err) console.log(err)
})
}
Product.findById(oldData._id, (err, data) => {
if (err) console.log(err)
return res.json(data)
})
})
}
]
module.exports.deleteProduct = [
(req, res) => {
CartItem.findOne({product_id: req.params.id}, (err, item) => {
Product.findByIdAndDelete(req.params.id, (err, product) => {
if (err) console.log(err)
if (item) {
return res.status(401).json({message: 'این محصول درون سبد خرید ثبت شده و نمیتوان آن را حذف کرد.'})
} else {
Order.findOne({'order_items.product_id': req.params.id}, (err, order) => {
if (err) console.log(err)
if (order) {
return res.status(401).json({message: 'این محصول درون سفارش ثبت شده و نمیتوان آن را حذف کرد.'})
} else {
Product.findByIdAndDelete(req.params.id, (err, product) => {
if (err) console.log(err)
return res.json({message: 'محصول با موفقیت حذف شد.'})
})
}
})
}
return res.json({message: v_m['fa'].response.success_remove})
})
}
]
@@ -381,15 +472,13 @@ module.exports.deleteProduct = [
module.exports.createProductImage = [
[
body('product_id')
.notEmpty().withMessage('مشخصات محصول را وارد کنید.')
.bail()
.custom((value, {req}) => {
return Product.findById(value)
.then(product => {
return true
})
.catch(err => {
return Promise.reject('محصولی با این مشخصات وجود ندارد.')
return Promise.reject(v_m['fa'].not_found.item_id)
})
})
],
@@ -403,20 +492,17 @@ module.exports.createProductImage = [
image = req.files.image
imageName = 'productImages_' + Date.now() + '.' + image.mimetype.split('/')[1]
if (image.size / 1024 > 500) return res.status(422).json({validation: {images: {image: {msg: 'حجم عکس نباید بیشتر از 500 کیلوبایت باشد.'}}}})
jimp.read(image.data)
.then(img => {
img
.resize(250, jimp.AUTO)
.cover(250, 250)
.write(`./static/uploads/images/products/gallery/${imageName}`)
.resize(470, jimp.AUTO)
.cover(470, 390)
.write(`./static/uploads/images/products/${imageName}`)
//// write to database
Product.findById(req.body.product_id, (err, product) => {
if (err) console.log(err)
product.product_images.push({
product.images.push({
image: imageName,
created_at: dateformat(Date.now(), 'yyyy-mm-dd HH:MM:ss')
})
@@ -426,16 +512,16 @@ module.exports.createProductImage = [
})
} else {
return res.status(422).json({validation: {images: {image: {msg: 'عکس اجباری است.'}}}})
return res.status(422).json({validation: {images: {image: {msg: v_m['fa'].required.image}}}})
}
}
]
module.exports.deleteProductImage = [
(req, res) => {
Product.findOne({'product_images._id': req.params.imageID}, (err, product) => {
Product.findOne({'images._id': req.params.imageID}, (err, product) => {
if (err) return res.status(404).json(err)
const productImage = product.product_images.id(req.params.imageID)
const productImage = product.images.id(req.params.imageID)
fs.unlink(`./static/${productImage.image}`, err => {
if (err) console.log(err)
})
@@ -449,85 +535,66 @@ module.exports.deleteProductImage = [
]
////////////////////////////////////////// products features controllers
module.exports.createProductFeature = [
module.exports.createPDF = [
[
body('fa_name')
.notEmpty().withMessage('عنوان نباید خالی باشد'),
body('en_name')
.notEmpty().withMessage('عنوان نباید خالی باشد'),
body('de_name')
.notEmpty().withMessage('عنوان نباید خالی باشد'),
body('tr_name')
.notEmpty().withMessage('عنوان نباید خالی باشد'),
body('it_name')
.notEmpty().withMessage('عنوان نباید خالی باشد'),
body('fa_value')
.notEmpty().withMessage('توضیحات نباید خالی باشد'),
body('en_value')
.notEmpty().withMessage('توضیحات نباید خالی باشد'),
body('tr_value')
.notEmpty().withMessage('توضیحات نباید خالی باشد'),
body('de_value')
.notEmpty().withMessage('توضیحات نباید خالی باشد'),
body('it_value')
.notEmpty().withMessage('توضیحات نباید خالی باشد'),
body('fa_pdf')
.notEmpty().withMessage(v_m['fa'].required.title),
body('en_pdf')
.notEmpty().withMessage(v_m['fa'].required.title),
body('product_id')
.notEmpty().withMessage('مشخصات محصول را وارد کنید.')
.bail()
.custom((value, {req}) => {
return Product.findById(value)
.then(product => {
return true
})
.catch(err => {
return Promise.reject('محصولی با این مشخصات وجود ندارد.')
return Promise.reject(v_m['fa'].not_found.item_id)
})
})
],
(req, res) => {
const errors = validationResult(req)
if (!errors.isEmpty()) return res.status(422).json({validation: {features: errors.mapped()}})
if (!errors.isEmpty()) return res.status(422).json({validation: errors.mapped()})
if (!req.files || !req.files.pdf) return res.status(422).json({validation: {pdf: {msg: v_m['fa'].required.file}}})
let file = 'pdf_' + Date.now() + '.' + req.files.pdf.mimetype.split('/')[1]
req.files.pdf.mv(`./static/uploads/pdf/${file}`, err => {
if (err) console.log(err)
})
const data = {
fa: {
name: req.body.fa_name,
value: req.body.fa_value
},
en: {
name: req.body.en_name,
value: req.body.en_value
},
de: {
name: req.body.de_name,
value: req.body.de_value
},
tr: {
name: req.body.tr_name,
value: req.body.tr_value
},
it: {
name: req.body.it_name,
value: req.body.it_value
file: file,
pdf_details: {
fa: {
name: req.body.fa_pdf
},
en: {
name: req.body.en_pdf
}
},
created_at: dateformat(Date.now(), 'yyyy-mm-dd HH:MM:ss')
}
Product.findById(req.body.product_id, (err, product) => {
if (err) console.log(err)
product.product_features.push(data)
product.pdf_files.push(data)
product.save()
return res.json(product)
})
}
]
module.exports.deleteProductFeature = [
module.exports.deletePDF = [
(req, res) => {
Product.findOne({'product_features._id': req.params.featureID}, (err, product) => {
Product.findOne({'pdf_files._id': req.params.pdfID}, (err, product) => {
if (err) return res.status(500).json({message: err})
product.product_features.id(req.params.featureID).remove()
let pdf = product.pdf_files.id(req.params.pdfID)
pdf.remove(cb => {
fs.unlink(`./static/${pdf.file}`, err => {
if (err) console.log(err)
})
})
product.save()
return res.json(product)
})