diff --git a/routes/users.js b/routes/users.js index 1596941..0c69320 100644 --- a/routes/users.js +++ b/routes/users.js @@ -6,46 +6,68 @@ var path = require('path'); router.post('/add', function (req, res) { + let flag = false; + let emptyFieldsArray =[] const userInfo = { - firstName: req.body.firstName, - lastName: req.body.lastName, - fatherName: req.body.fatherName, - codeMelli: req.body.codeMelli, - workUnit: req.body.workUnit, - workUnitCode: req.body.workUnitCode, - address: req.body.address, - obligationNumber: req.body.obligationNumber, - obligationDate: req.body.obligationDate, - requestNumber: req.body.requestNumber, - requestDate: req.body.requestDate, - writtenOrderNumber: req.body.writtenOrderNumber, - writtenOrderDate: req.body.writtenOrderDate, - deadline: req.body.deadline + 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() } - userServeice.addUser(userInfo) - .then((result) => { - res.status(200).send({ - success: true, - user: result - }) + 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: `پر کردن همه فیلدها الزامی است` }) - .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 { + userServeice.addUser(userInfo) + .then((result) => { + res.status(200).send({ + success: true, + user: result }) - } else { - res.status(500).send({ - success: false, - error: err.toString() - }) - } + }) + .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) { @@ -57,6 +79,7 @@ router.post('/complete/form', function (req, res) { }) }) .catch(err => { + console.log(err); if (err.eText) { if (typeof err.eText !== 'string') { err.eText = err.eText.toString() diff --git a/services/user.js b/services/user.js index 18a4c34..e63df25 100644 --- a/services/user.js +++ b/services/user.js @@ -1,5 +1,6 @@ const models = require('../models') const jalali = require("jalali-moment"); +const persianjs = require('persianjs') const docxTemp = require('../tools/docxtemp') @@ -56,11 +57,26 @@ methods.completeForm =(userId)=>{ .then(user=>{ let userInfo = user + userInfo.fullName = user.firstName + ' ' + user.lastName - userInfo.deadlineAndUnit = user.deadline + ' ' + user.deadlineUnit - userInfo.obligationDate = jalali( user.obligationDate, 'YYYY/MM/DD').locale('fa').format('YYYY/MM/DD') - userInfo.requestDate = jalali( user.requestDate, 'YYYY/MM/DD').locale('fa').format('YYYY/MM/DD') - userInfo.writtenOrderDate = jalali( user.writtenOrderDate, 'YYYY/MM/DD').locale('fa').format('YYYY/MM/DD') + + userInfo.codeMelli = persianjs(user.codeMelli).englishNumber().toString(); + userInfo.workUnitCode = persianJs(user.workUnitCode).englishNumber().toString(); + userInfo.obligationNumber = persianJs(user.obligationNumber).englishNumber().toString(); + userInfo.requestNumber = persianJs(user.requestNumber).englishNumber().toString(); + userInfo.writtenOrderNumber = persianJs(user.writtenOrderNumber).englishNumber().toString(); + userInfo.deadline = persianJs(user.deadline).englishNumber().toString(); + + userInfo.deadlineAndUnit = userInfo.deadline + ' ' + userInfo.deadlineUnit + + userInfo.obligationDate = jalali( user.obligationDate, 'YYYY/MM/DD').locale('fa').format('DD/MM/YYYY') + userInfo.requestDate = jalali( user.requestDate, 'YYYY/MM/DD').locale('fa').format('DD/MM/YYYY') + userInfo.writtenOrderDate = jalali( user.writtenOrderDate, 'YYYY/MM/DD').locale('fa').format('DD/MM/YYYY') + userInfo.obligationDate = persianJs(user.obligationDate).englishNumber().toString(); + userInfo.requestDate = persianJs(user.requestDate).englishNumber().toString(); + userInfo.writtenOrderDate = persianJs(user.writtenOrderDate).englishNumber().toString(); + + return docxTemp(userInfo, userInfo.fileName) }).then(link=>{ diff --git a/template/Hform4.docx b/template/Hform4.docx index 05b3012..e312d07 100644 Binary files a/template/Hform4.docx and b/template/Hform4.docx differ diff --git a/tools/docxtemp.js b/tools/docxtemp.js index aa0e810..b502750 100644 --- a/tools/docxtemp.js +++ b/tools/docxtemp.js @@ -30,8 +30,16 @@ function errorHandler(error) { throw error; } -module.exports = function (userInfo, fileName) { +module.exports = function (templateInfo, fileName) { return new Promise((resolve, reject) => { + for (const key in templateInfo) { + if (!templateInfo[key]) { + throw({ + eCode: 406, + eText: 'empty fields' + }) + } + } let content = fs.readFileSync(path.resolve(__dirname, `../template/${fileName}`), 'binary'); let zip = new PizZip(content); let doc; @@ -39,10 +47,11 @@ module.exports = function (userInfo, fileName) { try { doc = new Docxtemplater(zip); } catch (error) { + errorHandler(error); } - doc.setData(userInfo); + doc.setData(templateInfo); try { doc.render() @@ -53,8 +62,8 @@ module.exports = function (userInfo, fileName) { let buf = doc.getZip().generate({ type: 'nodebuffer' }); - fs.writeFileSync(path.resolve(__dirname, `../public/files/${userInfo.firstName}-${userInfo.lastName}-${fileName}`), buf); - resolve(`http://localhost:3000/users/download/${userInfo.firstName}-${userInfo.lastName}-${fileName}`) + fs.writeFileSync(path.resolve(__dirname, `../public/files/${templateInfo.firstName}-${templateInfo.lastName}-${fileName}`), buf); + resolve(`http://localhost:3000/users/download/${templateInfo.firstName}-${templateInfo.lastName}-${fileName}`) .catch(err => { reject({