mirror of
https://github.com/node-ebics/node-ebics-client.git
synced 2024-12-23 12:51:48 +00:00
Add traces storage
This commit is contained in:
parent
548c17a868
commit
e2af5c6ea8
2
index.js
2
index.js
@ -3,6 +3,7 @@
|
||||
const Client = require('./lib/Client');
|
||||
const Orders = require('./lib/predefinedOrders');
|
||||
const fsKeysStorage = require('./lib/storages/fsKeysStorage');
|
||||
const tracesStorage = require('./lib/storages/tracesStorage');
|
||||
const BankLetter = require('./lib/BankLetter');
|
||||
|
||||
module.exports = {
|
||||
@ -10,4 +11,5 @@ module.exports = {
|
||||
Orders,
|
||||
BankLetter,
|
||||
fsKeysStorage,
|
||||
tracesStorage,
|
||||
};
|
||||
|
@ -28,6 +28,7 @@ module.exports = class Client {
|
||||
hostId,
|
||||
passphrase,
|
||||
keyStorage,
|
||||
tracesStorage,
|
||||
}) {
|
||||
if (!url)
|
||||
throw new Error('EBICS URL is requierd');
|
||||
@ -49,6 +50,7 @@ module.exports = class Client {
|
||||
this.hostId = hostId;
|
||||
this.keyStorage = keyStorage;
|
||||
this.keyEncryptor = defaultKeyEncryptor({ passphrase });
|
||||
this.tracesStorage = tracesStorage || null;
|
||||
}
|
||||
|
||||
async send(order) {
|
||||
@ -71,6 +73,8 @@ module.exports = class Client {
|
||||
const keys = await this.keys();
|
||||
if (keys === null) this._generateKeys();
|
||||
|
||||
if (this.tracesStorage)
|
||||
this.tracesStorage.new().ofType('ORDER.INI');
|
||||
const res = await this.ebicsRequest(order);
|
||||
const xml = res.orderData();
|
||||
|
||||
@ -96,12 +100,18 @@ module.exports = class Client {
|
||||
}
|
||||
|
||||
async download(order) {
|
||||
if (this.tracesStorage)
|
||||
this.tracesStorage.new().ofType('ORDER.DOWNLOAD');
|
||||
const res = await this.ebicsRequest(order);
|
||||
|
||||
order.transactionId = res.transactionId();
|
||||
|
||||
if (res.isSegmented() && res.isLastSegment())
|
||||
if (res.isSegmented() && res.isLastSegment()) {
|
||||
if (this.tracesStorage)
|
||||
this.tracesStorage.connect().ofType('RECEIPT.ORDER.DOWNLOAD');
|
||||
|
||||
await this.ebicsRequest(order);
|
||||
}
|
||||
|
||||
const returnedTechnicalCode = res.technicalCode();
|
||||
const returnedBusinessCode = res.businessCode();
|
||||
@ -123,12 +133,16 @@ module.exports = class Client {
|
||||
}
|
||||
|
||||
async upload(order) {
|
||||
if (this.tracesStorage)
|
||||
this.tracesStorage.new().ofType('ORDER.UPLOAD');
|
||||
let res = await this.ebicsRequest(order);
|
||||
const transactionId = res.transactionId();
|
||||
const orderId = res.orderId();
|
||||
|
||||
order.transactionId = transactionId;
|
||||
|
||||
if (this.tracesStorage)
|
||||
this.tracesStorage.connect().ofType('TRANSFER.ORDER.UPLOAD');
|
||||
res = await this.ebicsRequest(order);
|
||||
|
||||
return [transactionId, orderId];
|
||||
@ -138,12 +152,25 @@ module.exports = class Client {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
const { version } = order;
|
||||
const keys = await this.keys();
|
||||
const r = signer.version(version).sign((await serializer.use(order, this)).toXML(), keys.x());
|
||||
|
||||
if (this.tracesStorage)
|
||||
this.tracesStorage.label(`REQUEST.${order.orderDetails.OrderType}`).data(r).persist();
|
||||
|
||||
$request.post({
|
||||
url: this.url,
|
||||
body: signer.version(version).sign((await serializer.use(order, this)).toXML(), keys.x()),
|
||||
body: r,
|
||||
headers: { 'content-type': 'text/xml;charset=UTF-8' },
|
||||
}, (err, res, data) => (err ? reject(err) : resolve(response.version(version)(data, keys))));
|
||||
}, (err, res, data) => {
|
||||
if (err) reject(err);
|
||||
|
||||
const ebicsResponse = response.version(version)(data, keys);
|
||||
|
||||
if (this.tracesStorage)
|
||||
this.tracesStorage.label(`RESPONSE.${order.orderDetails.OrderType}`).connect().data(ebicsResponse.toXML()).persist();
|
||||
|
||||
resolve(ebicsResponse);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
67
lib/storages/tracesStorage.js
Normal file
67
lib/storages/tracesStorage.js
Normal file
@ -0,0 +1,67 @@
|
||||
'use strict';
|
||||
|
||||
const fs = require('fs');
|
||||
|
||||
const uuidv1 = require('uuid/v1');
|
||||
|
||||
const traceName = (uuid, label, type, ext = 'xml') => {
|
||||
return `${uuid}_${label}_${type}.${ext}`;
|
||||
};
|
||||
|
||||
module.exports = dir => ({
|
||||
traceData: '',
|
||||
traceLabel: '',
|
||||
lastTraceID: null,
|
||||
connectToLastTrace: false,
|
||||
|
||||
label(str) {
|
||||
this.traceLabel = str;
|
||||
|
||||
return this;
|
||||
},
|
||||
|
||||
data(data) {
|
||||
if (!data)
|
||||
throw Error('No trace given to be persisted.');
|
||||
|
||||
this.traceData = data;
|
||||
|
||||
return this;
|
||||
},
|
||||
|
||||
ofType(type) {
|
||||
this.type = type;
|
||||
|
||||
return this;
|
||||
},
|
||||
|
||||
new() {
|
||||
this.connectToLastTrace = false;
|
||||
|
||||
return this;
|
||||
},
|
||||
|
||||
connect() {
|
||||
this.connectToLastTrace = true;
|
||||
|
||||
return this;
|
||||
},
|
||||
|
||||
persist() {
|
||||
if (!dir)
|
||||
throw Error('No directory to save the traces to provided.');
|
||||
|
||||
this.lastTraceID = this.connectToLastTrace ? this.lastTraceID : uuidv1();
|
||||
|
||||
const name = traceName(this.lastTraceID, this.traceLabel, this.type);
|
||||
const path = `${dir}/${name}`;
|
||||
|
||||
try {
|
||||
fs.writeFileSync(path, this.traceData);
|
||||
console.log("Data written to file");
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
});
|
@ -25,6 +25,7 @@
|
||||
"moment": "^2.22.1",
|
||||
"node-rsa": "^0.4.2",
|
||||
"request": "^2.87.0",
|
||||
"uuid": "^3.3.2",
|
||||
"xml-crypto": "^0.10.1",
|
||||
"xmldom": "^0.1.27",
|
||||
"xpath": "0.0.27"
|
||||
|
Loading…
Reference in New Issue
Block a user