|
|
@ -1,48 +1,93 @@ |
|
|
|
var express = require('express'); |
|
|
|
const express = require('express'); |
|
|
|
var router = express.Router(); |
|
|
|
const multer = require('multer') |
|
|
|
|
|
|
|
const shortid = require('shortid') |
|
|
|
|
|
|
|
const router = express.Router(); |
|
|
|
const userServeice = require('../services/user') |
|
|
|
const userServeice = require('../services/user') |
|
|
|
const fs = require('fs') |
|
|
|
const fs = require('fs') |
|
|
|
var path = require('path'); |
|
|
|
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') |
|
|
|
|
|
|
|
|
|
|
|
router.post('/add', function (req, res) { |
|
|
|
|
|
|
|
|
|
|
|
router.post('/add', uploadImage, function (req, res) { |
|
|
|
let flag = false; |
|
|
|
let flag = false; |
|
|
|
let emptyFieldsArray =[] |
|
|
|
let emptyFieldsArray = [] |
|
|
|
|
|
|
|
|
|
|
|
const userInfo = { |
|
|
|
const userInfo = { |
|
|
|
firstName: req.body.firstName.trim(), |
|
|
|
firstName: req.body.firstName ? req.body.firstName.trim() : false, |
|
|
|
lastName: req.body.lastName.trim(), |
|
|
|
lastName: req.body.lastName ? req.body.lastName.trim() : false, |
|
|
|
fatherName: req.body.fatherName.trim(), |
|
|
|
fatherName: req.body.fatherName ? req.body.fatherName.trim() : false, |
|
|
|
codeMelli: req.body.codeMelli.trim(), |
|
|
|
codeMelli: req.body.codeMelli ? req.body.codeMelli.trim() : false, |
|
|
|
workUnit: req.body.workUnit.trim(), |
|
|
|
workUnit: req.body.workUnit ? req.body.workUnit.trim() : false, |
|
|
|
workUnitCode: req.body.workUnitCode.trim(), |
|
|
|
workUnitCode: req.body.workUnitCode ? req.body.workUnitCode.trim() : false, |
|
|
|
address: req.body.address.trim(), |
|
|
|
address: req.body.address ? req.body.address.trim() : false, |
|
|
|
obligationNumber: req.body.obligationNumber.trim(), |
|
|
|
obligationNumber: req.body.obligationNumber ? req.body.obligationNumber.trim() : false, |
|
|
|
obligationDate: req.body.obligationDate.trim(), |
|
|
|
obligationDate: req.body.obligationDate ? req.body.obligationDate.trim() : false, |
|
|
|
requestNumber: req.body.requestNumber.trim(), |
|
|
|
requestNumber: req.body.requestNumber ? req.body.requestNumber.trim() : false, |
|
|
|
requestDate: req.body.requestDate.trim(), |
|
|
|
requestDate: req.body.requestDate ? req.body.requestDate.trim() : false, |
|
|
|
writtenOrderNumber: req.body.writtenOrderNumber.trim(), |
|
|
|
writtenOrderNumber: req.body.writtenOrderNumber ? req.body.writtenOrderNumber.trim() : false, |
|
|
|
writtenOrderDate: req.body.writtenOrderDate.trim(), |
|
|
|
writtenOrderDate: req.body.writtenOrderDate ? req.body.writtenOrderDate.trim() : false, |
|
|
|
deadline: req.body.deadline.trim(), |
|
|
|
deadline: req.body.deadline ? req.body.deadline.trim() : false, |
|
|
|
deadlineUnit: req.body.deadlineUnit.trim(), |
|
|
|
deadlineUnit: req.body.deadlineUnit ? req.body.deadlineUnit.trim() : 'روز', |
|
|
|
healthCenter: req.body.healthCenter.trim(), |
|
|
|
healthCenter: req.body.healthCenter ? req.body.healthCenter.trim() : false, |
|
|
|
healthCenterCity: req.body.healthCenterCity.trim() |
|
|
|
healthCenterCity: req.body.healthCenterCity ? req.body.healthCenterCity.trim() : false, |
|
|
|
|
|
|
|
imageFileName: req.file ? req.file.filename.slice(0 , req.file.filename.indexOf('.')) : false |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
for (const key in userInfo) { |
|
|
|
for (const key in userInfo) { |
|
|
|
if (!userInfo[key] || !userInfo[key].trim()) { |
|
|
|
if ((!userInfo[key] || !userInfo[key].trim()) && key !== 'deadlineUnit') { |
|
|
|
emptyFieldsArray.push(key) |
|
|
|
emptyFieldsArray.push(key) |
|
|
|
flag = true |
|
|
|
flag = true |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if(flag){ |
|
|
|
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 .....') |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
res.status(412).send({ |
|
|
|
res.status(412).send({ |
|
|
|
success: false, |
|
|
|
success: false, |
|
|
|
emptyFields : emptyFieldsArray, |
|
|
|
emptyFields: emptyFieldsArray, |
|
|
|
error: `پر کردن همه فیلدها الزامی است` |
|
|
|
error: `پر کردن همه فیلدها الزامی است` |
|
|
|
}) |
|
|
|
}) |
|
|
|
}else { |
|
|
|
|
|
|
|
userServeice.addUser(userInfo) |
|
|
|
} else { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
userServeice.addUser(userInfo , req.file.filename) |
|
|
|
.then((result) => { |
|
|
|
.then((result) => { |
|
|
|
res.status(200).send({ |
|
|
|
res.status(200).send({ |
|
|
|
success: true, |
|
|
|
success: true, |
|
|
|