|
|
|
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) => {
|
|
|
|
console.log(user);
|
|
|
|
resolve(user)
|
|
|
|
|
|
|
|
}).catch(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
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = methods
|