Skip to content

Commit f6cb610

Browse files
committed
refactor code in users.put handler
1 parent b3602e3 commit f6cb610

File tree

2 files changed

+21
-20
lines changed

2 files changed

+21
-20
lines changed

lib/handlers.js

+19-19
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,19 @@ handlers._users.post = (data, callback) => {
4444
// check that all required fields are filled out
4545
const firstName = helpers.requiredParamValidator(data, 'firstName', {
4646
type: 'string',
47-
minLength: 1
47+
minLength: 1,
4848
});
4949
const lastName = helpers.requiredParamValidator(data, 'lastName', {
5050
type: 'string',
51-
minLength: 1
51+
minLength: 1,
5252
});
5353
const phone = helpers.requiredParamValidator(data, 'phone', {
5454
type: 'string',
55-
exactLength: 12
55+
exactLength: 12,
5656
});
5757
const password = helpers.requiredParamValidator(data, 'password', {
5858
type: 'string',
59-
minLength: 1
59+
minLength: 1,
6060
});
6161
const tosAgreement = helpers.requiredParamValidator(data, 'tosAgreement', {type: 'boolean'});
6262

@@ -102,25 +102,25 @@ handlers._users.post = (data, callback) => {
102102
handlers._users.put = (data, callback) => {
103103
const firstName = helpers.requiredParamValidator(data, 'firstName', {
104104
type: 'string',
105-
minLength: 1
105+
minLength: 1,
106106
});
107107
const lastName = helpers.requiredParamValidator(data, 'lastName', {
108108
type: 'string',
109-
minLength: 1
109+
minLength: 1,
110110
});
111111
const phone = helpers.requiredParamValidator(data, 'phone', {
112112
type: 'string',
113-
exactLength: 12
113+
exactLength: 12,
114114
});
115115
const password = helpers.requiredParamValidator(data, 'password', {
116116
type: 'string',
117-
minLength: 1
117+
minLength: 1,
118118
});
119119

120-
const token = typeof (data.headers.token) === "string" ? data.headers.token : false;
121-
handlers._tokens.verifyToken(token, phoneValidated, (tokenIsValid) => {
122-
if (tokenIsValid) {
123-
if (phone) {
120+
if (phone) {
121+
const token = typeof (data.headers.token) === "string" ? data.headers.token : false;
122+
handlers._tokens.verifyToken(token, phone, (tokenIsValid) => {
123+
if (tokenIsValid) {
124124
if (firstName || lastName || password) {
125125
DataLibrary.read('users', phone, (err, data) => {
126126
if (!err && data) {
@@ -150,12 +150,12 @@ handlers._users.put = (data, callback) => {
150150
callback(400, {'Error': "One field at least should be specified"});
151151
}
152152
} else {
153-
callback(400, {'Error': "Missing required fields"});
153+
callback(403, {"Error": "Missing required token in header or token is invalid"})
154154
}
155-
} else {
156-
callback(403, {"Error": "Missing required token in header or token is invalid"})
157-
}
158-
});
155+
});
156+
} else {
157+
callback(400, {'Error': "Missing required fields"});
158+
}
159159
};
160160

161161
// Required fields: phone
@@ -216,12 +216,12 @@ handlers._tokens.get = (data, callback) => {
216216
handlers._tokens.post = (data, callback) => {
217217
const phone = helpers.requiredParamValidator(data, 'phone', {
218218
type: 'string',
219-
exactLength: 12
219+
exactLength: 12,
220220
});
221221

222222
const password = helpers.requiredParamValidator(data, 'password', {
223223
type: 'string',
224-
minLength: 1
224+
minLength: 1,
225225
});
226226

227227
if (phone && password) {

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
"lib": "lib"
99
},
1010
"scripts": {
11-
"test": "echo \"Error: no test specified\" && exit 1"
11+
"test": "echo \"Error: no test specified\" && exit 1",
12+
"serve": "npx nodemon index.js"
1213
},
1314
"repository": {
1415
"type": "git",

0 commit comments

Comments
 (0)