models/user.ts   A
last analyzed

Complexity

Total Complexity 4
Complexity/F 0

Size

Lines of Code 181
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 151
mnd 4
bc 4
fnc 0
dl 0
loc 181
rs 10
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
1
import { API_KEY } from '@env';
2
import config from '../config/config.json';
3
import storage from './storage';
4
5
const userModel = {
6
    getUserData: async function getUserData(userData) {
7
        const token = await storage.readToken();                
8
        const user = userData['userData']
9
        
10
        const response = await fetch(`${config.base_url}users/${user['id']}?api_key=${API_KEY}`, {
11
            method: 'GET',
12
            headers: {
13
                'x-access-token': token['token']
14
            }
15
        });
16
        
17
        const result = await response.json();
18
19
        return result;
20
    },
21
22
    getBalance: async function getBalance(): Promise<any> {
23
        const userData = await storage.readUser();        
24
        const user = await userModel.getUserData(userData);
25
        
26
        const userBalance = user['user']['balance'];
27
        
28
        return userBalance;
29
    },
30
31
    getHistory: async function getHistory(): Promise<any> {
32
        const user = await storage.readUser();
33
        const userId = user['userData']['id']
34
        
35
36
        const token = await storage.readToken();                
37
        const respone = await fetch(`${config.base_url}users/${userId}?api_key=${API_KEY}`, {
38
            method: 'GET',
39
            headers: {
40
                'x-access-token': token['token']
41
            }
42
        }
43
        );
44
45
        const result = await respone.json();
46
47
        const userData = result['user'];
48
                
49
        const userHistory = userData['history'];
50
        
51
        return userHistory;
52
    },
53
54
    addFunds: async function addFunds(prepaid: string): Promise<any> {
55
        const token = await storage.readToken();
56
        const user = await storage.readUser();        
57
        const userData = await userModel.getUserData(user);
58
59
        const userId = userData['user']['_id'];
60
        
61
        const requestBody = {
62
            'user_id': userId,
63
            'prepaid_code': prepaid
64
        };            
65
        
66
        const response = await fetch(`${config.base_url}users/addfund?api_key=${API_KEY}`, {
67
            method: 'POST',
68
            headers: {
69
                'Content-Type': 'application/json',
70
                'x-access-token': token['token']
71
            },
72
            body: JSON.stringify(requestBody)
73
        });
74
75
        const result = await response;        
76
        
77
        return result;
78
    },
79
80
    getProfile: async function getProfile() {
81
        const userData = await storage.readUser();        
82
        const user = await userModel.getUserData(userData);
83
84
        return user;
85
    },
86
87
    updateUser: async function updateUser(userData: object): Promise<any> {
88
        const token = await storage.readToken();
89
        const userId = await storage.readUser();
90
        const user = await userModel.getUserData(userId);
91
        
92
        const body = {
93
            'user_id': user['user']['_id'],
94
            'firstName': userData['firstname'],
95
            'lastName': userData['lastname'],
96
            'phoneNumber': userData['phonenumber'],
97
            'email': userData['email'],
98
            'api_key': API_KEY
99
        };
100
101
        
102
103
        // Prepare body to be urlencoded
104
        const formBody = [];
105
106
        for (const property in body) {
107
            const encodedKey = encodeURIComponent(property);
108
            const encodedValue = encodeURIComponent(body[property]);
109
                formBody.push(encodedKey + "=" + encodedValue);
110
        };
111
        
112
        const requestBody = formBody.join("&");
113
114
        const response = await fetch(`${config.base_url}users`, {
115
            method: 'PUT',
116
            headers: {
117
                'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
118
                'x-access-token': token['token']
119
            },
120
            body: requestBody
121
        });
122
123
        if (response.status === 204) {
124
            const message = {
125
                message: 'Profile Changed',
126
            };
127
128
            return message;
129
        };
130
131
        const result = await response.json();
132
133
        return result;
134
        
135
    },
136
137
    deleteAccount: async function deleteAccount(): Promise<any> {
138
        const token = await storage.readToken();
139
        const user = await storage.readUser();
140
        
141
        const userId = user['userData']['id'];
142
143
        const body = {
144
            'user_id': userId,
145
            'api_key': API_KEY
146
        };
147
148
        const formBody = [];
149
150
        for (const property in body) {
151
            const encodedKey = encodeURIComponent(property);
152
            const encodedValue = encodeURIComponent(body[property]);
153
                formBody.push(encodedKey + "=" + encodedValue);
154
        };
155
        
156
        const requestBody = formBody.join("&");
157
158
        const response = await fetch(`${config.base_url}users`, {
159
            method: 'DELETE',
160
            headers: {
161
                'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
162
                'x-access-token': token['token']
163
            },
164
            body: requestBody
165
        });
166
167
        if (response.status === 204) {
168
            const message = {
169
                message: 'Account deleted',
170
            };
171
172
            return message;
173
        };
174
175
        const result = await response.json();
176
177
        return result;
178
    }
179
};
180
181
export default userModel;