forked from Juniiorf/sgdexpress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
87 lines (82 loc) · 2.22 KB
/
index.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
import Parser from "xml2json";
import Axios from "axios";
import {
xmlConductor,
xmlVehicle,
url,
clean,
valid,
cleanCnh,
validCnh
} from "./lib/util";
import DetranError from "./lib/error";
import mapError from "./lib/mapErrors";
class Service {
constructor(code, user, pass) {
this.code = code;
this.user = user;
this.pass = pass;
}
async vehicle(plate) {
let isValid;
try {
const body = xmlVehicle(this.code, this.user, this.pass, plate);
const res = await Axios({
method: "post",
url: url.vehicle,
data: body,
headers: {
"Content-Type": "text/xml"
}
});
const resParse = Parser.toJson(res.data, {
object: true,
trim: true
});
// return resParse;
const access = clean(resParse["soap:Envelope"]["soap:Body"]);
if (!access) return false;
isValid = valid(access.query);
if (isValid.status) return access.query;
throw new Error(access.query.DESCRICAO);
} catch (err) {
let _err = {};
if (await mapError.has(isValid.code)) {
_err = mapError.get(isValid.code);
}
const data = await { ...{ status: 500, message: err.message }, ..._err };
return new DetranError(data.message, data.status, data.name);
}
}
async conductor(cnh, cpf) {
let isValid;
try {
const body = xmlConductor(this.code, this.user, this.pass, cpf, cnh);
const res = await Axios({
method: "post",
url: url.conductor,
data: body,
headers: {
"Content-Type": "text/xml"
}
});
const resParse = Parser.toJson(res.data, {
object: true,
trim: true
});
const access = cleanCnh(resParse["soap:Envelope"]["soap:Body"]);
if (!access) return false;
isValid = validCnh(access.query);
if (isValid.status) return access.query;
throw new Error(access.query.DESCRICAO);
} catch (err) {
let _err = {};
if (mapError.has(isValid.code)) {
_err = mapError.get(isValid.code);
}
const data = { ...{ status: 500, message: err.message }, ..._err };
return new DetranError(data.message, data.status);
}
}
}
export default Service;