-
Notifications
You must be signed in to change notification settings - Fork 0
/
handle-document.js
221 lines (190 loc) · 10.3 KB
/
handle-document.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
const { logger, logConfig } = require('@vtfk/logger')
const { ENCRYPTION_KEY, RETRY_INTERVAL_MINUTES } = require('../config')
const { decryptContent } = require('@vtfk/encryption')
const { renameSync, writeFileSync } = require('fs')
// Import jobs
const krr = require('../jobs/krr')
const freg = require('../jobs/freg')
const syncElevmappe = require('../jobs/sync-elevmappe')
const addParentsIfUnder18 = require('../jobs/add-parents-if-under-18')
const syncEnterprise = require('../jobs/sync-enterprise')
const createPdf = require('../jobs/create-pdf')
const archive = require('../jobs/archive')
const svarut = require('../jobs/svarut')
const getContactTeachers = require('../jobs/get-contact-teachers')
const sendEmail = require('../jobs/send-email')
const updateDocumentStatus = require('../jobs/update-document-status')
const statistics = require('../jobs/statistics')
/*
Trenger følgende jobber:
- krr sjekk (hvilket språk de foretrekker, goddammit...) // Done
- freg sjekk // Done
- Kan de over slås sammen i azf-archive mon tro? Njæh, kjør heller krr sjekk manuelt (det er vel bare for foretrukket språk?)
- sjekk etter adressesperring og pass på at det blir riktig!
- sjekk om man har en gyldig postadresse / svarut-godkjent (postnr korrekt)
- lag pdf via pdf-generator (tror vi driter i å opprette automatisk i azf-archive, for ressurskrevende der...)
- arkiver dokument
- send ut om det skal sendes ut. (Om det er adressesperring må det gå internt notat til skolen for oppfølging)
- Send ut varsel på e-post til de som trenger det
- Oppdater status i mongodb
- Opprett statistikk i stat-db
- Fullfør flyten
- Hold track på flyten, lagre ulike jobber sin status i json-fila. Ta en av gangen, arkiv-endepunktet er så treigt allikevel
- Om vi har den dataen vi trenger, så blir det jo ikke så galt :D
.then(prepareDocument)
.then(lookupKrr) // Ferdig
.then(lookupDsf) // Ferdig
.then(filterSecretAddress) // Vi har info om grader adresse om vi trenger i freg
.then(setupDistribution) // Vi har adresser til foreldre og elev
.then(lookup360) // Vi har sikret at privatpersoner og elevmappe er opprettet i 360 (vi gidder ikke sjekke hemmelig adresse i 360)
.then(checkSecretAddress) // Vi har adresser allerede
.then(gotAddressForDistribution) // Vi har adresser allerede
.then(setupDocuments) // Vi må nå, lage pdf-er
.then(setupArchive) // Done
.then(saveToDone)
.then(saveToNotifications)
.then(saveToArchive)
.then(saveToDistribution)
.then(saveToErrors)
.then(removeFromQueue)
.then(data => {
return resolve(data)
})
*/
const setUpJob = (flow, jobName, documentData) => {
if (flow[jobName]?.enabled) {
if (!documentData.flowStatus[jobName]) documentData.flowStatus[jobName] = { finished: false }
}
return documentData.flowStatus
}
const shouldRunJob = (flow, jobName, documentData) => {
// Check if flow has already failed or jobStatus is not already finished and if it is enabled in the flow for the document type
if (!documentData.flowStatus.failed && !documentData.flowStatus[jobName]?.finished && flow[jobName]?.enabled) return true
return false
}
const handleFailedJob = (jobName, documentData, document, error) => {
// If has run too many times - do something
documentData.flowStatus.runs++ // It ran one more time, increase runs
// Handle error msg
const errorMsg = error.response?.data || error.stack || error.toString()
documentData.flowStatus[jobName].error = errorMsg
if (documentData.flowStatus.runs >= RETRY_INTERVAL_MINUTES.length) {
logger('error', ['Job failed, will NOT run again', `Failed in job: ${jobName}`, `Runs: ${documentData.flowStatus.runs}/${RETRY_INTERVAL_MINUTES.length}. Reset flowStatus.runs to try again`, 'error:', errorMsg])
try {
writeFileSync(`./documents/queue/${document}`, JSON.stringify(documentData, null, 2))
renameSync(`./documents/queue/${document}`, `./documents/failed/${document}`)
} catch (error) {
logger('error', ['Offh, could not save and move file, stuff might run twice...', `Failed in job: ${jobName}`, 'Probs smart to check it out when you have the time', 'save-error:', error])
}
// Move pdf as well - if it exists
if (documentData.flowStatus?.createPdf?.finished) {
try {
renameSync(`./documents/queue/${documentData._id}_pdf.txt`, `./documents/failed/${documentData._id}_pdf.txt`)
} catch (error) {
logger('error', ['Offh, could not move pdf file', 'You have to move it manually, if needed', 'save-error:', error])
}
}
return documentData
}
// Still some retries left
const minutesToWait = RETRY_INTERVAL_MINUTES[documentData.flowStatus.runs]
const now = new Date()
const nextRun = new Date(now.setMinutes(now.getMinutes() + minutesToWait)).toISOString().replaceAll(':', '--') // Filenames can't contain :
logger(documentData.flowStatus.runs > 1 ? 'warn' : 'info', [`Job failed, will try again in ${RETRY_INTERVAL_MINUTES[documentData.flowStatus.runs]} minutes`, `Failed in job: ${jobName}`, `Runs: ${documentData.flowStatus.runs}/${RETRY_INTERVAL_MINUTES.length}`, 'error:', errorMsg])
try {
writeFileSync(`./documents/queue/${document}`, JSON.stringify(documentData, null, 2)) // Save status
renameSync(`./documents/queue/${document}`, `./documents/queue/${documentData._id}_nextrun_${nextRun}.json`) // Rename file with new retry timestamp
} catch (error) {
logger('error', ['Offh, could not save file with new status, stuff might run twice...', `Failed in job: ${jobName}`, 'Probs smart to check it out when you have the time', 'save-error:', error])
}
return documentData
}
const runJob = async (document, flow, jobName, documentData, jobFunction) => {
documentData.flowStatus = setUpJob(flow, jobName, documentData)
if (shouldRunJob(flow, jobName, documentData)) {
logger('info', ['running job', jobName, 'student', documentData.student.feidenavn])
try {
documentData.flowStatus[jobName].result = await jobFunction(flow[jobName], documentData)
documentData.flowStatus[jobName].finished = true
documentData.flowStatus[jobName].finishedTimestamp = new Date().getTime()
logger('info', ['finished job', jobName, 'student', documentData.student.feidenavn])
} catch (error) {
documentData.flowStatus.failed = true
handleFailedJob(jobName, documentData, document, error)
}
}
return documentData.flowStatus
}
const finishFlow = (document, documentData) => {
if (!documentData.flowStatus.failed) {
logger('info', ['Wohoo, flow has finished, all jobs succeeded. Moving document to finished'])
documentData.flowStatus.finished = true
try {
writeFileSync(`./documents/queue/${document}`, JSON.stringify(documentData, null, 2)) // Save status
renameSync(`./documents/queue/${document}`, `./documents/finished/${document}`) // Rename file into finished folder
} catch (error) {
logger('error', ['Offh, could not save file with new status, stuff might run twice...', 'Failed when trying to move file to finished', 'Probs smart to check it out when you have the time', 'save-error:', error])
}
// Move pdf as well - if it exists
if (documentData.flowStatus?.createPdf?.finished) {
try {
renameSync(`./documents/queue/${documentData._id}_pdf.txt`, `./documents/finished/${documentData._id}_pdf.txt`)
} catch (error) {
logger('error', ['Offh, could not move pdf file', 'You have to move it manually, if needed', 'save-error:', error])
}
}
}
}
// "document" is the filename
const handleDocument = async (document) => {
const documentData = require(`../documents/queue/${document}`)
logConfig({ prefix: `handle-document - ${documentData._id} - ${documentData.type} - ${documentData.variant}` })
// Get flow for document
let flow
try {
flow = require(`../flows/${documentData.type}-${documentData.variant}`)
} catch (error) {
throw new Error(`Aiaiai, mangler flow-fil for ${documentData.type}-${documentData.variant}`)
}
// Set up flowStatus if missing
if (!documentData.flowStatus) documentData.flowStatus = { runs: 0, createdTimestamp: new Date().toISOString() }
// New run, reset failed
documentData.flowStatus.failed = false
// Check if content is encrypted, and decrypt if necessary
if (documentData.isEncrypted) {
logger('info', ['Content is encrypted. Decrypting.'])
const decrypted = decryptContent(documentData.content, ENCRYPTION_KEY)
documentData.content = decrypted
documentData.isEncrypted = false
}
// RUN JOBS BASED ON FLOW (Only jobs defined and enabled in the flow will actually be run)
// KRR check
documentData.flowStatus = await runJob(document, flow, 'krr', documentData, krr)
// FREG check
documentData.flowStatus = await runJob(document, flow, 'freg', documentData, freg)
// SyncElevmappe
documentData.flowStatus = await runJob(document, flow, 'syncElevmappe', documentData, syncElevmappe)
// SyncParents
documentData.flowStatus = await runJob(document, flow, 'addParentsIfUnder18', documentData, addParentsIfUnder18)
// SyncEnterprise
documentData.flowStatus = await runJob(document, flow, 'syncEnterprise', documentData, syncEnterprise)
// Create PDF
documentData.flowStatus = await runJob(document, flow, 'createPdf', documentData, createPdf)
// Archive document
documentData.flowStatus = await runJob(document, flow, 'archive', documentData, archive)
// Send on Svarut (or creates internal note to school)
documentData.flowStatus = await runJob(document, flow, 'svarut', documentData, svarut)
// Get student's contactTeachers
documentData.flowStatus = await runJob(document, flow, 'getContactTeachers', documentData, getContactTeachers)
// Send emails to receivers
documentData.flowStatus = await runJob(document, flow, 'sendEmail', documentData, sendEmail)
// Update document status
documentData.flowStatus = await runJob(document, flow, 'updateDocumentStatus', documentData, updateDocumentStatus)
// Create statistics element
documentData.flowStatus = await runJob(document, flow, 'statistics', documentData, statistics)
// Fail on purpose
documentData.flowStatus = await runJob(document, flow, 'failOnPurpose', documentData, async () => { throw new Error('Æ feilja med vilje') })
// Finish flow (will move file to finished - if everything is done)
finishFlow(document, documentData)
}
module.exports = { handleDocument }