-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitor.js
132 lines (116 loc) · 4.7 KB
/
monitor.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
// Author: Jean-Marie Thewes
//Modules:
const fs = require("fs").promises;
const nodemailer = require("nodemailer");
const config = require("./config.js");
const log = require("./log.js");
const almaPrint = require("./alma-print.js");
//Global vars:
const debug=false;
const resultLimit = 100;
const configFileName = "config/config.json";
const defaultLogFile = "log/default.log";
const dataFile = "data/data.json";
var transporter
//Environment vars:
process.env.configFileName = configFileName;
process.env.defaultLogFile = defaultLogFile;
//Functions:
async function initMailer(){
if(config.getMailEnabled()){
transporter = nodemailer.createTransport({
host: await config.getMailServer(),
port: await config.getMailPort(),
secure: false
});
}
}//------------------------------------------------------------------------------------------
function reportAndClose(error){
//console.log(error)
if("message" in error){
log.error(`${error.name}: ${error.message}`);
log.debug(error.stack);
}else{
log.error(error);
}
return true //to let the next function (close()) know that an error occured
}//------------------------------------------------------------------------------------------
async function close(error){
await log.flushLogs();
if(error){
process.exit(1);
}else{
process.exit(0);
}
}//------------------------------------------------------------------------------------------
async function generatePrinterArray(){// extract alma printers from config to query
var result = [];
for(element of await config.getAlmaPrinters()){
if(!result.includes(element.almaPrinter)){result.push(element.almaPrinter)}
}
log.debug("Printer Array for query")
log.debug(JSON.stringify(result));
return result;
}//------------------------------------------------------------------------------------------
async function getPrintOuts(printerArray){// request printouts from API, use multiple requests if necessary
var result = [];
var tmp = await almaPrint.getPrintOuts(printerArray,resultLimit);
if(("printout" in tmp)&&("total_record_count" in tmp)){
log.info(`total record count in response: ${tmp.total_record_count}`)
result.push(...tmp.printout);
if(resultLimit < tmp.total_record_count){
log.info("offset request required");
for(var offset = resultLimit;offset < tmp.total_record_count;offset += resultLimit){
var tmp2 = await almaPrint.getPrintOuts(printerArray,resultLimit,offset);
if("printout" in tmp2){
result.push(...tmp2.printout)
}else{throw new Error("API returned unexpected format/data while requesting with offset")}
}
}
return result;
}else{
if("total_record_count" in tmp){
if (tmp.total_record_count == 0){
log.info("API returned 0 results")
return result;
}
}else{throw new Error("API returned unexpected format/data")}
}
}//------------------------------------------------------------------------------------------
function extractIDs(letterArray){// extract the ID numbers for all letters returned from API
let result = [];
for (entry of letterArray){result.push(entry.id)}
return result
}//------------------------------------------------------------------------------------------
async function compare(newSet){
log.debug(`New Set:${newSet}`);
let oldData;
oldData = await fs.readFile(dataFile).catch(()=>oldData = "[]");
let oldSet;
try{oldSet = JSON.parse(oldData);}catch{oldSet = []}
log.debug(`Old Set:${oldSet}`);
if(oldSet.some(element=>newSet.includes(element))){// if some elements of oldSet are included in newSet => some letters are stuck
log.error("Some letters are stuck in queue, check Print Server");
if(await config.getMailEnabled() && (transporter !== undefined)){
var tmp = {//compose Mail
from: await config.getMailSender(), // sender address
to: await config.getMailRecipient(), // list of receivers
subject: await config.getMailSubject(), // Subject line
text: await config.getMailText(), // plain text body
}
await transporter.sendMail(tmp);
}
}
await fs.writeFile(dataFile,JSON.stringify(newSet))
}//------------------------------------------------------------------------------------------
//Main:
Promise.resolve()
.then(log.init)
.then(config.init)
.then(initMailer)
.then(generatePrinterArray)
.then(getPrintOuts)
.then(extractIDs)
.then(compare)
.catch(reportAndClose)
.then(close)