diff --git a/app.js b/app.js index 62dff0d..bfcde2c 100644 --- a/app.js +++ b/app.js @@ -1,13 +1,12 @@ -var createError = require('http-errors'); -var express = require('express'); -var path = require('path'); -var cookieParser = require('cookie-parser'); -var logger = require('morgan'); +const createError = require('http-errors'); +const express = require('express'); +const path = require('path'); +const cookieParser = require('cookie-parser'); +const logger = require('morgan'); -var indexRouter = require('./routes/index'); -var usersRouter = require('./routes/users'); +const usersRouter = require('./routes/users'); -var app = express(); +const app = express(); // view engine setup app.set('views', path.join(__dirname, 'views')); @@ -19,7 +18,6 @@ app.use(express.urlencoded({ extended: false })); app.use(cookieParser()); app.use(express.static(path.join(__dirname, 'public'))); -app.use('/', indexRouter); app.use('/users', usersRouter); // catch 404 and forward to error handler diff --git a/routes/index.js b/routes/index.js deleted file mode 100644 index ecca96a..0000000 --- a/routes/index.js +++ /dev/null @@ -1,9 +0,0 @@ -var express = require('express'); -var router = express.Router(); - -/* GET home page. */ -router.get('/', function(req, res, next) { - res.render('index', { title: 'Express' }); -}); - -module.exports = router; diff --git a/routes/users.js b/routes/users.js index adfcfb8..1596941 100644 --- a/routes/users.js +++ b/routes/users.js @@ -5,79 +5,77 @@ const fs = require('fs') var path = require('path'); -router.post('/add', function(req, res) { - 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 +router.post('/add', function (req, res) { + 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 } 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() - }) - } + 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) { +router.post('/complete/form', function (req, res) { userServeice.completeForm(req.body.userId) - .then((result) => { - console.log('sssssssssssssssssssss'); - console.log(result); - res.status(200).send({ - success: true, - user: result + .then((result) => { + res.status(200).send({ + success: true, + link: result + }) }) - }) - .catch(err => { - if (err.eText) { - if (typeof err.eText !== 'string') { - err.eText = err.eText.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() + }) } - 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){ +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); @@ -85,4 +83,4 @@ router.get('/download/:fileName', function(req, res){ res.end(); }); -module.exports = router; +module.exports = router; \ No newline at end of file diff --git a/services/user.js b/services/user.js index 9d1b7b9..ce6528f 100644 --- a/services/user.js +++ b/services/user.js @@ -7,11 +7,15 @@ let methods = {} let privates = { findUser : (userId)=>{ return new Promise((resolve, reject) => { + models.User.findByPk(userId) + .then((user) => { if(user){ + resolve(user.dataValues) - }else{ + + }else{ reject({ eCode: 404, eText: 'کاربر یافت نشد' @@ -33,7 +37,7 @@ let privates = { methods.addUser = (userInfo) => { return new Promise((resolve, reject) => { models.User.create(userInfo).then((user) => { - console.log(user); + resolve(user) }).catch(err => { @@ -50,6 +54,7 @@ methods.completeForm =(userId)=>{ return new Promise((resolve, reject) => { privates.findUser(userId) .then(user=>{ + let userInfo = user userInfo.fullName = user.firstName + ' ' + user.lastName userInfo.obligationDate = jalali( user.obligationDate, 'YYYY/MM/DD').locale('fa').format('YYYY/MM/DD') @@ -57,8 +62,10 @@ methods.completeForm =(userId)=>{ userInfo.writtenOrderDate = jalali( user.writtenOrderDate, 'YYYY/MM/DD').locale('fa').format('YYYY/MM/DD') return docxTemp(userInfo, 'Hform4.docx') - }).then(text=>{ - resolve(text) + }).then(link=>{ + + resolve(link) + }).catch(err => { if (err.eText) { if (typeof (err.eText) !== 'string') {