var express = require('express'); var router = express.Router(); const userServeice = require('../services/user') const fs = require('fs') var path = require('path'); router.post('/add', function (req, res) { let flag = false; let emptyFieldsArray =[] const userInfo = { firstName: req.body.firstName.trim(), lastName: req.body.lastName.trim(), fatherName: req.body.fatherName.trim(), codeMelli: req.body.codeMelli.trim(), workUnit: req.body.workUnit.trim(), workUnitCode: req.body.workUnitCode.trim(), address: req.body.address.trim(), obligationNumber: req.body.obligationNumber.trim(), obligationDate: req.body.obligationDate.trim(), requestNumber: req.body.requestNumber.trim(), requestDate: req.body.requestDate.trim(), writtenOrderNumber: req.body.writtenOrderNumber.trim(), writtenOrderDate: req.body.writtenOrderDate.trim(), deadline: req.body.deadline.trim(), deadlineUnit: req.body.deadlineUnit.trim(), healthCenter: req.body.healthCenter.trim(), healthCenterCity: req.body.healthCenterCity.trim() } for (const key in userInfo) { if (!userInfo[key] || !userInfo[key].trim()) { emptyFieldsArray.push(key) flag = true } } if(flag){ res.status(406).send({ success: false, emptyFields : emptyFieldsArray, error: `پر کردن همه فیلدها الزامی است` }) }else { userServeice.addUser(userInfo) .then((result) => { res.status(200).send({ success: true, user: result }) }) .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() }) } }) } }); router.post('/complete/form', function (req, res) { userServeice.completeForm(req.body.userId) .then((result) => { res.status(200).send({ success: true, link: result }) }) .catch(err => { console.log(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() }) } }) }) 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(); }); module.exports = router;