|
|
@ -2,6 +2,7 @@ const PizZip = require('pizzip'); |
|
|
|
const Docxtemplater = require('docxtemplater'); |
|
|
|
const Docxtemplater = require('docxtemplater'); |
|
|
|
const fs = require('fs'); |
|
|
|
const fs = require('fs'); |
|
|
|
const path = require('path'); |
|
|
|
const path = require('path'); |
|
|
|
|
|
|
|
const _ = require("lodash"); |
|
|
|
|
|
|
|
|
|
|
|
// The error object contains additional information when logged with JSON.stringify (it contains a properties object containing all suberrors).
|
|
|
|
// The error object contains additional information when logged with JSON.stringify (it contains a properties object containing all suberrors).
|
|
|
|
function replaceErrors(key, value) { |
|
|
|
function replaceErrors(key, value) { |
|
|
@ -30,26 +31,69 @@ function errorHandler(error) { |
|
|
|
throw error; |
|
|
|
throw error; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class InspectModule { |
|
|
|
|
|
|
|
constructor() { |
|
|
|
|
|
|
|
this.inspect = {}; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
set(obj) { |
|
|
|
|
|
|
|
if (obj.inspect) { |
|
|
|
|
|
|
|
this.inspect = _.merge({}, this.inspect, obj.inspect); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const getTags = function (postParsed) { |
|
|
|
|
|
|
|
return postParsed.filter(function (part) { |
|
|
|
|
|
|
|
return part.type === "placeholder"; |
|
|
|
|
|
|
|
}).reduce(function (tags, part) { |
|
|
|
|
|
|
|
tags[part.value] = {}; |
|
|
|
|
|
|
|
if (part.subparsed) { |
|
|
|
|
|
|
|
tags[part.value] = getTags(part.subparsed); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return tags; |
|
|
|
|
|
|
|
}, {}); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
module.exports = function (templateInfo, fileName) { |
|
|
|
module.exports = function (templateInfo, fileName) { |
|
|
|
return new Promise((resolve, reject) => { |
|
|
|
return new Promise((resolve, reject) => { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//check empty fields
|
|
|
|
for (const key in templateInfo) { |
|
|
|
for (const key in templateInfo) { |
|
|
|
if (!templateInfo[key]) { |
|
|
|
if (!templateInfo[key]) { |
|
|
|
throw({ |
|
|
|
throw({ |
|
|
|
eCode: 406, |
|
|
|
eCode: 406, |
|
|
|
eText: 'empty fields' |
|
|
|
eText: 'هیچ یک از فیلدها نباید خالی باشند' |
|
|
|
}) |
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
let content = fs.readFileSync(path.resolve(__dirname, `../template/${fileName}`), 'binary'); |
|
|
|
let content = fs.readFileSync(path.resolve(__dirname, `../template/${fileName}`), 'binary'); |
|
|
|
let zip = new PizZip(content); |
|
|
|
let zip = new PizZip(content); |
|
|
|
let doc; |
|
|
|
let doc; |
|
|
|
|
|
|
|
let inspectModule = new InspectModule(); |
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
try { |
|
|
|
doc = new Docxtemplater(zip); |
|
|
|
doc = new Docxtemplater(zip,{ |
|
|
|
|
|
|
|
modules: [inspectModule] |
|
|
|
|
|
|
|
}); |
|
|
|
} catch (error) { |
|
|
|
} catch (error) { |
|
|
|
|
|
|
|
|
|
|
|
errorHandler(error); |
|
|
|
errorHandler(error); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let postParsed = inspectModule.inspect.postparsed; |
|
|
|
|
|
|
|
let tags = Object.keys(getTags(postParsed)) |
|
|
|
|
|
|
|
let templateInfoKeys = Object.keys(templateInfo) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//compare template tags and received fields from client(templateInfo)
|
|
|
|
|
|
|
|
for(let i = 0; i < tags.length; i++){ |
|
|
|
|
|
|
|
if(!templateInfoKeys.includes(tags[i]) ){ |
|
|
|
|
|
|
|
throw({ |
|
|
|
|
|
|
|
eCode: 406, |
|
|
|
|
|
|
|
eText: 'اطلاعات وارد شده ناقص است' |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
doc.setData(templateInfo); |
|
|
|
doc.setData(templateInfo); |
|
|
|
|
|
|
|
|
|
|
|