This repository has been archived by the owner on Oct 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
approval_worker.js
186 lines (185 loc) · 7.17 KB
/
approval_worker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
const Queue = require('rethinkdb-job-queue')
const daex = require('json-daex')
const func = require('./function')
const app = require('./config')
const { mjml2html } = require('mjml')
const config = app.rethinkdb
const cxnOptions = app.rethinkdb
var pino = require('pino')
var rdash = require('rethinkdbdash')(config)
var fs = require('fs')
var axios = require('axios')
var _ = require('lodash')
var $ = require('jquery')
const qOptions = {
name: 'approvalClass',
masterInterval: 10000,
changeFeed: true,
concurrency: 100,
removeFinishedJobs: false
}
var approval_log = []
var approvalClass = {}
var input = []
const q = new Queue(cxnOptions, qOptions)
q.process(async(job, next) => {
input = job.data.input
try {
for (let i = 0; i < job.data.capacity; i++) {
job.data.output[i] = {}
for (key in job.data.input[i]) {
job.data.output[i][key] = job.data.input[i][key]
}
}
let emailTemplate, emailTemplateUrl, emailTemplateHtml, runningProcess, processLog
let approvar = []
let rolesEmail = []
await axios({
method: 'get',
url: 'http://localhost:3030/flowz-instance/' + job.data.fId
})
.then(async function (response) {
runningProcess = _.find(response.data.processList, ['id', job.data.job])
processLog = response.data.process_log
emailTemplate = runningProcess.inputProperty[0].entityschema.emailTemplate
let processRoles = runningProcess.inputProperty[0].approvalClass.items[0].role
await axios({
method: 'get',
url: app.roleURI + processRoles
})
.then(async function (res) {
rolesEmail = await res.data.data.roles
})
emailTemplateUrl = _.find(runningProcess.inputProperty[0].entityschema.emailTemplate, ['filename', runningProcess.inputProperty[0].emailTemplate])
emailTemplateUrl = emailTemplateUrl.url
})
await axios({
method: 'get',
url: emailTemplateUrl
})
.then(function (response) {
emailTemplateHtml = response.data
processLog = _.chain(processLog).orderBy(['lastModified'], ['asc']).findLast((f) => { return f.jobId === job.data.jobId }).value()
for (var i = 0; i < runningProcess.inputProperty[0].entityschema.entity.length; i++) {
let element = runningProcess.inputProperty[0].entityschema.entity[i].name
// element = element.toLowerCase()
let index = emailTemplateHtml.search('"' + element + '"')
// element = _.capitalize(element)
emailTemplateHtml = emailTemplateHtml.substr(0, index + element.length + 3) + processLog.input[0][element] + emailTemplateHtml.substr(index + element.length + 3)
}
emailTemplateHtml = mjml2html(emailTemplateHtml)
})
.catch(function (error) {
console.log('Error : ', error)
})
if (rolesEmail.length > 0) {
for (var j = 0; j < rolesEmail.length; j++) {
let submitLink = 'http://localhost:8000/mail/reply/' + rolesEmail[j] + '/' + job.data.job + '/' + job.data.jobId + '/' + job.data.fId
let myData = {
"to": rolesEmail[j],
"subject": "From " + rolesEmail[j],
"body": emailTemplateHtml.html + '<br><button><a href="' + submitLink + '" style="text-decoration:none; color:#000">Submit</a></button>'
}
console.log(myData)
await axios({
method: 'post',
url: app.login,
data: app.credential
})
.then(async function (response) {
await axios({
method: 'post',
url: app.sendEmail,
data: myData,
headers: {
'authorization': response.data.logintoken
}
})
.then(function (response) {
console.log('Mail Sent to ' + rolesEmail[j])
})
.catch(function (error) {
console.log('Error : ', error)
})
})
.catch(error => {
console.log('Error :', error)
})
}
}
await job.update()
return next(null, 'success')
} catch (err) {
console.log(err)
pino().error('Error in process', err)
return next(new Error('error'))
}
})
q.on('terminated', (queueId, jobId) => {
q.getJob(jobId).then((job) => {
pino().info({ 'jobId': job[0].data.id }, 'Approval Type Job terminated');
pino(fs.createWriteStream('./mylog')).info({ 'fId': job[0].data.fId, 'jobId': job[0].data.id }, 'Approval Type Job terminated')
}).catch(err => {
pino().error(new Error(err));
pino(fs.createWriteStream('./mylog')).error({ "error": err }, 'Error in Approval Type Job terminated')
})
})
q.on('completed', (queueId, jobId, isRepeating) => {
q.getJob(jobId).then(async(job) => {
// let resultData = job[0].data
// let approval = 0
// let instanceTable = app.flowz_table
// let instanceList = await rdash.table(instanceTable).get(job[0].data.fId).run()
// let processlog = _.find(instanceList.process_log, { 'job': job[0].data.jobId })
// for (let m = 0; m < approvalClass.process.length; m++) {
// let check = _.find(approval_log, { 'id': approvalClass.process[m].id })
// if (check != undefined) {
// approval++
// }
// }
// if (approval == approvalClass.process.length) {
// console.log('=====================')
// if (_.isArray(input)) {
// for (let arr in processlog.input) {}
// } else {
// var newObject = _.reduce(input, function(result, value, key) {
// if (!_.has(processlog.input[0], key)) {
// result[key] = value;
// }
// return result
// }, {});
// console.log('newObject', newObject)
// console.log('--->', resultData)
// const Queue = require('rethinkdb-job-queue')
// //--------------- Connection Options -----------------
// const cxnOptions = config
// //--------------- Queue Options -----------------
// const qOptions = {
// name: app.scheduler_table
// }
// const q = new Queue(cxnOptions, qOptions)
// var jobOptions = {}
// jobOptions.data = {}
// // jobOptions.data.fId = id
// jobOptions.data = {
// "fId": resultData.fId,
// "input": newObject,
// "isExternalInput": true,
// "jobId": resultData.jobId,
// }
// jobOptions.timeout = app.qJobTimeout
// jobOptions.retryMax = app.qJobRetryMax
// console.log('jobOptions', jobOptions)
// //--------------- Create new job -----------------
// const job = q.createJob(jobOptions)
// //--------------- Add job -----------------
// q.addJob(job).then((savedJobs) => {}).catch(err => console.error(err))
// }
// }
pino().info({ 'jobId': job[0].data.id }, 'Approval Type Job completed')
pino(fs.createWriteStream('./mylog')).info({ 'fId': job[0].data.fId, 'jobId': job[0].data.id }, 'Approval Type Job completed')
}).catch(err => {
pino().error(new Error(err));
pino(fs.createWriteStream('./mylog')).error({ "error": err }, 'Error in Approval Type Job compleated')
})
})