utils/listeners.js   A
last analyzed

Complexity

Total Complexity 26
Complexity/F 1.3

Size

Lines of Code 112
Function Count 20

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 77
dl 0
loc 112
rs 10
c 0
b 0
f 0
wmc 26
mnd 6
bc 6
fnc 20
bpm 0.3
cpm 1.3
noi 3
1
var _require = require("./"),
2
    isEmpty = _require.isEmpty,
3
    isJson = _require.isJson;
4
const EventEmitter = require('events');
5
// Initializing event emitter instances 
6
const bSecurePaymentsEventHandler = new EventEmitter();
7
8
9
const bSecurePaymentsSubscribeEventHandler = {
10
    onErrorAlert() {
11
        bSecurePaymentsEventHandler.on('errorAlert', (data) => {
12
            return data;
13
        });
14
    },
15
    onSuccessAlert() {
16
        bSecurePaymentsEventHandler.on('successAlert', (data) => {
17
            return data;
18
        });
19
    },
20
    onValidationErrorAlert() {
21
        bSecurePaymentsEventHandler.on('validationAlert', (data) => {
22
            return data;
23
        });
24
    },
25
    onProcessPaymentSuccess() {
26
        bSecurePaymentsEventHandler.on('onProcessPaymentSuccess', (data) => {
27
            return data;
28
        });
29
    },
30
    onProcessPaymentFailure() {
31
        bSecurePaymentsEventHandler.on('onProcessPaymentFailure', (data) => {
32
            return data;
33
        });
34
    },
35
}
36
37
const bSecurePaymentsHandler = {
38
    initialize: function () {
39
        bSecurePaymentsSubscribeEventHandler.onErrorAlert();
40
        bSecurePaymentsSubscribeEventHandler.onSuccessAlert();
41
        bSecurePaymentsSubscribeEventHandler.onValidationErrorAlert();
42
        bSecurePaymentsSubscribeEventHandler.onValidationErrorAlert();
43
        bSecurePaymentsSubscribeEventHandler.onProcessPaymentSuccess();
44
        bSecurePaymentsSubscribeEventHandler.onProcessPaymentFailure();
45
    },
46
    onErrorAlert: function (data) {
47
        bSecurePaymentsEventHandler.emit('errorAlert', data)
48
        return data;
49
    },
50
    onSuccessAlert: function (data) {
51
        bSecurePaymentsEventHandler.emit('successAlert', data)
52
        return data;
53
    },
54
    onValidationErrorAlert: function (data) {
55
        bSecurePaymentsEventHandler.emit('validationAlert', data)
56
        return data;
57
    },
58
    onProcessPaymentSuccess: function (data) {
59
        bSecurePaymentsEventHandler.emit('onProcessPaymentSuccess', data)
60
        return data;
61
    },
62
    onProcessPaymentFailure: function (data) {
63
        bSecurePaymentsEventHandler.emit('onProcessPaymentFailure', data)
64
        return data;
65
    },
66
}
67
68
const errorHandlers = {
69
    formatValidationError: function (data) {
70
        let _data = { status: 422, message: data, body: {}, exception: {} };
71
        return bSecurePaymentsHandler.onValidationErrorAlert(isJson(_data));
72
    },
73
    onException: function (msg) {
74
        throw new Error(msg)
75
        let _data = { status: 419, message: msg, body: {}, exception: {} };
0 ignored issues
show
Bug introduced by
The variable _data seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window._data.
Loading history...
introduced by
This code is unreachable and can thus be removed without consequences.
Loading history...
76
        return bSecurePaymentsHandler.onErrorAlert(isJson(_data));
0 ignored issues
show
introduced by
This code is unreachable and can thus be removed without consequences.
Loading history...
77
    },
78
    handleErrors: function (data) {
79
        const responseCode = (data.status).toString();
80
        const responseException = data.exception;
81
        if (!isEmpty(responseException)) {
82
            throw new Error(responseException)
83
        }
84
85
        if (responseCode.startsWith(2)) {
86
            bSecurePaymentsHandler.onSuccessAlert(data);
87
        } else if (responseCode === "422") {
88
            bSecurePaymentsHandler.onValidationErrorAlert(data);
89
        } else if (!isEmpty(responseCode)) {
90
            const _dataBody = data.body;
91
            if (!isEmpty(_dataBody) && _dataBody.hasOwnProperty(_dataBody.result) && !isEmpty(_dataBody.result.original)) {
92
                bSecurePaymentsHandler.onProcessPaymentFailure(data.body.result.original);
93
            } else {
94
                bSecurePaymentsHandler.onErrorAlert(data);
95
            }
96
        }
97
    },
98
    handlePayments: function (data) {
99
        const responseCode = (data.status).toString();
100
        if (responseCode.startsWith(2)) {
101
            bSecurePaymentsHandler.onProcessPaymentSuccess(data);
102
        } else {
103
            bSecurePaymentsHandler.onProcessPaymentFailure(data);
104
        }
105
    }
106
}
107
108
module.exports = {
109
    errorHandlers: errorHandlers,
110
    bSecurePaymentsHandler: bSecurePaymentsHandler,
111
    bSecurePaymentsEventHandler: bSecurePaymentsEventHandler
112
};