You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 lines
3.6 KiB

const models = require('../models')
const jalali = require("jalali-moment");
4 years ago
const persianjs = require('persianjs')
const docxTemp = require('../tools/docxtemp')
4 years ago
const imageResizer = require('../tools/imageResizer')
const addImageToWord = require('../tools/addImageToWord');
const { result } = require('lodash');
let methods = {}
let privates = {
findUser : (userId)=>{
return new Promise((resolve, reject) => {
4 years ago
models.User.findByPk(userId)
4 years ago
.then((user) => {
if(user){
4 years ago
resolve(user.dataValues)
4 years ago
}else{
reject({
eCode: 404,
eText: 'کاربر یافت نشد'
})
}
}).catch(err => {
reject({
eCode: 500,
eText: err
})
})
})
}
}
4 years ago
methods.addUser = (userInfo,imgOriginalName) => {
return new Promise((resolve, reject) => {
4 years ago
4 years ago
imageResizer(imgOriginalName)
.then(()=>{
return models.User.create(userInfo)
4 years ago
}).then((user) => {
resolve(user)
}).catch(err => {
reject({
eCode: 500,
eText: err
})
})
})
}
methods.completeForm =(userId)=>{
return new Promise((resolve, reject) => {
let userInfo;
privates.findUser(userId)
.then(user=>{
4 years ago
userInfo = user
4 years ago
userInfo.fullName = user.firstName + ' ' + user.lastName
4 years ago
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();
4 years ago
return docxTemp(userInfo, userInfo.fileName)
}).then(docxFileName=>{
4 years ago
return addImageToWord(docxFileName, userInfo.imageFileName + '.png')
}).then(result=>{
resolve(`localhost:3000/users/download/${result}`)
4 years ago
}).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