You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

154 lines
4.7 KiB

4 years ago
const express = require('express');
const multer = require('multer')
const shortid = require('shortid')
const router = express.Router();
const userServeice = require('../services/user')
const fs = require('fs')
4 years ago
const path = require('path');
//------------------------------//
// multer config
//------------------------------//
var imageStorage = multer.diskStorage({
destination: function (req, file, cb) {
//TODO: change address*
cb(null, __dirname + '/../public/images')
},
filename: function (req, file, cb) {
var fileInfo = path.parse(file.originalname)
let fileName = shortid.generate() + Date.now().toString().substring(6) + fileInfo.ext
cb(null, fileName)
}
})
var imageFilter = function (req, file, callback) {
if (!file.originalname.match(/\.(jpg|jpeg|png|PNG|JPEG|)$/i)) {
return callback(new Error('Only image files are allowed!'), false)
}
callback(null, true)
}
var uploadImageConfig = multer({
storage: imageStorage,
fileFilter: imageFilter
})
var uploadImage = uploadImageConfig.single('image')
4 years ago
router.post('/add', uploadImage, function (req, res) {
4 years ago
let flag = false;
4 years ago
let emptyFieldsArray = []
4 years ago
const userInfo = {
4 years ago
firstName: req.body.firstName ? req.body.firstName.trim() : false,
lastName: req.body.lastName ? req.body.lastName.trim() : false,
fatherName: req.body.fatherName ? req.body.fatherName.trim() : false,
codeMelli: req.body.codeMelli ? req.body.codeMelli.trim() : false,
workUnit: req.body.workUnit ? req.body.workUnit.trim() : false,
workUnitCode: req.body.workUnitCode ? req.body.workUnitCode.trim() : false,
address: req.body.address ? req.body.address.trim() : false,
obligationNumber: req.body.obligationNumber ? req.body.obligationNumber.trim() : false,
obligationDate: req.body.obligationDate ? req.body.obligationDate.trim() : false,
requestNumber: req.body.requestNumber ? req.body.requestNumber.trim() : false,
requestDate: req.body.requestDate ? req.body.requestDate.trim() : false,
writtenOrderNumber: req.body.writtenOrderNumber ? req.body.writtenOrderNumber.trim() : false,
writtenOrderDate: req.body.writtenOrderDate ? req.body.writtenOrderDate.trim() : false,
deadline: req.body.deadline ? req.body.deadline.trim() : false,
deadlineUnit: req.body.deadlineUnit ? req.body.deadlineUnit.trim() : 'روز',
healthCenter: req.body.healthCenter ? req.body.healthCenter.trim() : false,
healthCenterCity: req.body.healthCenterCity ? req.body.healthCenterCity.trim() : false,
imageFileName: req.file ? req.file.filename.slice(0 , req.file.filename.indexOf('.')) : false
}
4 years ago
4 years ago
for (const key in userInfo) {
4 years ago
if ((!userInfo[key] || !userInfo[key].trim()) && key !== 'deadlineUnit') {
4 years ago
emptyFieldsArray.push(key)
flag = true
4 years ago
4 years ago
}
}
4 years ago
if (flag) {
if (req.file) {
try {
fs.unlinkSync(__dirname + '/../public/images/' + req.file.filename)
console.log('picture is deleted .....')
console.log('----------------------------')
} catch (err) {
console.log(err);
console.log('picture not deleted .....')
}
}
4 years ago
res.status(412).send({
4 years ago
success: false,
4 years ago
emptyFields: emptyFieldsArray,
4 years ago
error: `پر کردن همه فیلدها الزامی است`
4 years ago
})
4 years ago
} else {
userServeice.addUser(userInfo , req.file.filename)
4 years ago
.then((result) => {
res.status(200).send({
success: true,
user: result
4 years ago
})
4 years ago
})
.catch(err => {
if (err.eText) {
if (typeof err.eText !== 'string') {
err.eText = err.eText.toString()
}
res.status(err.eCode).send({
success: false,
error: err.eText
})
} else {
res.status(500).send({
success: false,
error: err.toString()
})
}
})
}
});
4 years ago
router.post('/complete/form', function (req, res) {
userServeice.completeForm(req.body.userId)
4 years ago
.then((result) => {
res.status(200).send({
success: true,
link: result
})
})
4 years ago
.catch(err => {
4 years ago
console.log(err);
4 years ago
if (err.eText) {
if (typeof err.eText !== 'string') {
err.eText = err.eText.toString()
}
res.status(err.eCode).send({
success: false,
error: err.eText
})
} else {
res.status(500).send({
success: false,
error: err.toString()
})
}
4 years ago
})
})
4 years ago
router.get('/download/:fileName', function (req, res) {
var file = fs.readFileSync(path.join(__dirname + `/../public/files/${req.params.fileName}`), 'binary');
res.setHeader('Content-Length', file.length);
res.write(file, 'binary');
res.end();
});
4 years ago
module.exports = router;