-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess.js
163 lines (138 loc) · 4.28 KB
/
process.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
const VERSAO_SERVICE = "1.0.1";
require("dotenv/config");
const { exec } = require("child_process");
var fs = require("fs");
const api = require("./api");
const schedule = require("node-schedule");
const Firebird = require("node-firebird");
const Certificado = require("./consultaCertificado");
var STATUS = true;
const options = {
host: "localhost",
port: 3050,
database: "c://syspdv//syspdv_srv.fdb",
user: "SYSDBA",
password: "masterkey",
lowercase_keys: false, // set to true to lowercase keys
role: null, // default
pageSize: 4096, // default when creating database
};
const streamCert = (dado) => {
return new Promise((resolve) => {
dado.PRPCERELE(function (err, name, e) {
if (err) throw err;
// +v0.2.4
e.pipe(fs.createWriteStream("foo.pfx"));
// e === EventEmitter
e.on("data", function (chunk) {
// reading data
});
e.on("end", async function () {
// end reading
resolve();
});
});
});
};
const consultaSysPDV = () => {
return new Promise((resolve) => {
try {
var pool = Firebird.pool(5, options);
// Get a free pool
pool.get(function (err, db) {
if (err) {
console.log(`Falha no firebird: ${err}`);
resolve({});
} else {
db.query(
"SELECT PRPCGC,PRPDES,PRPCERELE,PRPVER,PRPVERBUILD FROM PROPRIO",
async function (err, result) {
// IMPORTANT: release the pool connection
let cnpj = result[0].PRPCGC;
let razao = result[0].PRPDES;
//Se o arquivo raw de backup nao existir ele cria para usar em casos de erro no firebird
if (!fs.existsSync("rawdata.json")) {
let data = JSON.stringify({ rawcnpj: cnpj, rawrazao: razao });
fs.writeFileSync("rawdata.json", data);
}
let versaoServer =
String(result[0].PRPVER).trim() + "-" + result[0].PRPVERBUILD;
let certificado;
if (result[0].PRPCERELE != null) {
console.log("gerando o certificado");
await streamCert(result[0]);
}
// IMPORTANT: close the connection
db.detach();
// Destroy pool
pool.destroy();
try {
if (fs.existsSync("./foo.pfx")) {
//file exists
certificado = await Certificado(
"foo.pfx",
process.env.SENHACERT
);
}
} catch (error) {}
resolve({ cnpj, razao, certificado, versaoServer });
}
);
}
});
} catch (error) {
console.log("Falha no serviço com o Firebird!");
}
});
};
const handleServiceFirebird = () => {
exec(
`net ${
STATUS === false ? "stop" : "start"
} FirebirdGuardianDefaultInstance`,
(error, data, getter) => {
if (error) {
console.log("error: ", error.message);
return;
}
if (getter) {
console.log(`getter: ${getter}`);
return;
}
console.log(`Status firebird foi alterado para ${STATUS}`);
}
);
};
var sistema = schedule.scheduleJob("*/10 * * * * *", async function () {
let rawcnpj = "";
let rawrazao = "";
if (fs.existsSync("rawdata.json")) {
let rawdata = fs.readFileSync("rawdata.json");
let parseJson = JSON.parse(rawdata);
rawcnpj = parseJson.rawcnpj;
rawrazao = parseJson.rawrazao;
}
const responseSYS = await consultaSysPDV();
responseSYS.versaoService = VERSAO_SERVICE;
const responseAPI = await api.get(`/consulta`, {
data: {
cnpj: responseSYS.cnpj ? responseSYS.cnpj : rawcnpj,
razao: responseSYS.razao ? responseSYS.razao : rawrazao,
versaoServer: responseSYS.versaoServer
? responseSYS.versaoServer
: "ERRO FIREBIRD",
versaoService: responseSYS.versaoService,
certificado: responseSYS.certificado ? responseSYS.certificado : {},
ativo: true,
},
});
console.log(`Status da requisicao: ${responseAPI.status}`);
const { ativo } = responseAPI.data;
if (responseAPI.status !== 200) {
ativo = true;
}
if (ativo != STATUS) {
STATUS = !STATUS;
handleServiceFirebird();
}
});