From 4f8e135ec8f08cada9001df85d3d9ef9f6e275e6 Mon Sep 17 00:00:00 2001 From: shoaibalmasi Date: Mon, 15 Mar 2021 01:02:46 +0330 Subject: [PATCH] add privates func and complete route --- routes/users.js | 31 ++++++++++++++++++- services/user.js | 79 +++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 108 insertions(+), 2 deletions(-) diff --git a/routes/users.js b/routes/users.js index 69b757f..4f827be 100644 --- a/routes/users.js +++ b/routes/users.js @@ -3,7 +3,7 @@ var router = express.Router(); const userServeice = require('../services/user') /* GET users listing. */ -router.post('/add', function(req, res, next) { +router.post('/add', function(req, res) { console.log('hereeeeeeeeee'); const userInfo ={ firstName : req.body.firstName, @@ -47,4 +47,33 @@ router.post('/add', function(req, res, next) { }) }); +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 + }) + }) + .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() + }) + } + + }) +}) + module.exports = router; diff --git a/services/user.js b/services/user.js index a1ffb1f..36e0cd5 100644 --- a/services/user.js +++ b/services/user.js @@ -1,7 +1,53 @@ let models = require('../models') +let mammoth = require("mammoth"); let methods = {} +let privates = { + findUser : (userId)=>{ + return new Promise((resolve, reject) => { + models.User.findByPk(userId) + .then((user) => { + if(user){ + resolve(user.dataValues) + }else{ + reject({ + eCode: 404, + eText: 'کاربر یافت نشد' + }) + } + + }).catch(err => { + reject({ + eCode: 500, + eText: err + }) + }) + + }) + }, + replaceInfo : (user)=>{ + console.log(user); + return new Promise((resolve, reject) => { + mammoth.convertToHtml({path: "./template/Hform4.docx"}) + .then((result)=>{ + let text = result.value; + for (const property in user) { + console.log('++++++++++++++++++++++++'); + console.log(property); + result.value =result.value.replace(`[${property}]`, `${user[property]}`); + } + resolve(result.value) + }).catch(err => { + reject({ + eCode: 500, + eText: err + }) + }) + + }) + } +} methods.addUser = (userInfo) => { return new Promise((resolve, reject) => { models.User.create(userInfo).then((user) => { @@ -9,12 +55,43 @@ methods.addUser = (userInfo) => { resolve(user) }).catch(err => { - reject(err) + reject({ + eCode: 500, + eText: err + }) }) }) } +methods.completeForm =(userId)=>{ + return new Promise((resolve, reject) => { + privates.findUser(userId) + .then(user=>{ + return privates.replaceInfo(user) + }).then(text=>{ + resolve(text) + }).catch(err => { + if (err.eText) { + if (typeof (err.eText) !== 'string') { + err.eText = err.eText.toString() + } + reject({ + eCode: err.eCode, + eText: err.eText + }) + + } else { + reject({ + eCode: 500, + eText: err + }) + } + + }) + + }) +}