Issues (23)

src/pipes/Credequity.js (5 issues)

1
const httpProcessor = require('../HttpProcessor');
2
const services = require('../config/services');
3
const constants = require('../config/constants');
4
5
class Credequity
6
{
7
8
    constructor() {
9
        this.client = services.credequity.client;
10
        this.apiKey = services.credequity.api_key;
11
        this.baseUrl = services.credequity.api_url;
12
13
    }
14
15
    /**
16
    * Process axios calls
17
    * 
18
    * @param {string} method The call method get|post|put|delete|patch
19
    * @param {string} url The url to call
20
    * @param {object|formData} payload The payload data to send with the call
21
    */
22
    process(method, url, payload) {
23
        //HttpProcessor class to handle axios calls
24
        let processor = new httpProcessor(this.baseUrl, this.apiKey, this.client);
0 ignored issues
show
Coding Style Best Practice introduced by
By convention, constructors like httpProcessor should be capitalized.
Loading history...
25
26
        return processor.process(method, url, payload)
27
    }
28
29
    /**
30
    * Handles the ID request
31
    *
32
    * @param IdFilter IdFilter
33
    * @return response
34
    */
35
    async handle(IdFilter)
36
    {
37
        if (! IdFilter.isSuccessful()) {
0 ignored issues
show
Complexity Best Practice introduced by
There is no return statement if !IdFilter.isSuccessful() is false. Are you sure this is correct? If so, consider adding return; explicitly.

This check looks for functions where a return statement is found in some execution paths, but not in all.

Consider this little piece of code

function isBig(a) {
    if (a > 5000) {
        return "yes";
    }
}

console.log(isBig(5001)); //returns yes
console.log(isBig(42)); //returns undefined

The function isBig will only return a specific value when its parameter is bigger than 5000. In any other case, it will implicitly return undefined.

This behaviour may not be what you had intended. In any case, you can add a return undefined to the other execution path to make the return value explicit.

Loading history...
38
            if (IdFilter.isWithImage()) {
39
                this.getWithImage(IdFilter);
0 ignored issues
show
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
40
            } else {
41
42
                const idNumber =  IdFilter.getIDNumber();
43
                const firstName =  IdFilter.getFirstName();
44
                const lastName =  IdFilter.getLastName();
45
                const phone =  IdFilter.getPhoneNumber();
46
47
                if (IdFilter.getIDType() === constants.idValues.TYPE_BVN) {
48
                    const url = '/CredBvn/api/v1/Bvn/GetCustomerBvn';
49
50
                    const body = {
51
                        'bvn' : idNumber,
52
                        'PhoneNumber' : phone
53
                    };
54
55
                    return this.postData(IdFilter,body,url);
56
                }
57
                if (IdFilter.getIDType() === constants.idValues.TYPE_NIN) {
58
                    if (! IdFilter.isSuccessful()) {
59
                        const url = '/CredNin/api/v1/Identity?phoneNo=' + phone;
60
                        const body = {};
61
62
                        return this.postData(IdFilter,body,url);
63
                    }
64
65
                    //If request is not successful makes post with IdNumber
66
                    if (! IdFilter.isSuccessful()) {
67
                        const url = '/CredNin/api/v1/IdentityByNin?nin=' + idNumber;
68
                        const body = {};
69
70
                        return this.postData(IdFilter,body,url);
71
                    }
72
                }
73
74
                if (IdFilter.getIDType() === constants.idValues.TYPE_DRIVERS_LICENSE) {
75
                    const url = '/Verify/api/v1/FrscInfo';
76
77
                    const body = {
78
                        'firstname' : firstName,
79
                        'lastname' : lastName,
80
                        'phoneNo' : phone,
81
                        'frscidentityNo' : idNumber
82
                    };
83
                    return this.postData(IdFilter,body,url);
84
                }
85
86
                if (IdFilter.getIDType() === constants.idValues.TYPE_CUSTOMER_PROFILE) {
0 ignored issues
show
Complexity Best Practice introduced by
There is no return statement if IdFilter.getIDType() ===...s.TYPE_CUSTOMER_PROFILE is false. Are you sure this is correct? If so, consider adding return; explicitly.

This check looks for functions where a return statement is found in some execution paths, but not in all.

Consider this little piece of code

function isBig(a) {
    if (a > 5000) {
        return "yes";
    }
}

console.log(isBig(5001)); //returns yes
console.log(isBig(42)); //returns undefined

The function isBig will only return a specific value when its parameter is bigger than 5000. In any other case, it will implicitly return undefined.

This behaviour may not be what you had intended. In any case, you can add a return undefined to the other execution path to make the return value explicit.

Loading history...
87
                    const url = '/CredBvn/api/v1/CustomerProfile';
88
                    IdFilter.setCredequityProfile();
89
                    const profile = IdFilter.getCredequityProfile();
90
91
                    const body = {
92
                        "Nin" : profile['nin'],
93
                        "FrscNo" : profile['frscno'],
94
                        "phoneNo" : phone,
95
                        "Bvn" : profile['bvn']
96
                    };
97
                    
98
                    return this.postData(IdFilter,body,url);
99
                }
100
            }
101
        }
102
103
    }
104
105
    /**
106
    * Get ID information via images
107
    *
108
    * @param {object} IdFilter
109
    * @return response
110
    */
111
    getWithImage(IdFilter){
112
         const relative = (IdFilter.getIDType() === 'NIN') ? 'VerifyNinWithFace' : 'VerifyFrscWithFace';
113
         
114
         const url = '/CredOcr/api/v1/' + relative;
115
 
116
                
117
        const body = {
118
            'IdentificationProof' : IdFilter.getIdentificationProof(),
119
            'FaceProof' : IdFilter.getFaceProof()
120
        };
121
122
        this.postData(IdFilter,body,url);
123
 
124
         
125
     }
126
127
    /**
128
     * Make API call and get the required data from Credequity
129
     *
130
     * @param {object} IdFilter
131
     * @param {object} body
132
     * @param {string} url
133
     * @return {object}
134
     */
135
    async postData(IdFilter, body, url){
136
      
137
        try {
138
            const response =  await this.process('POST', url, body);
139
        
140
            if (response.message === 'Successful') {
141
142
                IdFilter.confirmSuccess();
143
144
                IdFilter.setHandler(this.client);
145
                
146
                IdFilter.setData({
147
                    'handler' : IdFilter.getHandler(),
148
                    'message' : IdFilter.getIDType() + ' Verified' + ' Successfully',
149
                    'data' : response.data
150
                });
151
                
152
                return IdFilter.getData();
153
             } else {
154
                IdFilter.setError({'error' : response.error});
0 ignored issues
show
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
155
             }
156
        } catch (error) {
157
            IdFilter.setError({'error' : error});
158
                
159
            return IdFilter.getError();
160
        }   
161
    }
162
}
163
164
module.exports = Credequity;