22
22
23
23
var storage = require ( './storage' ) ;
24
24
var utils = require ( './utils' ) ;
25
+ const IS = ( check , type ) =>
26
+ check === null || ( check . type && check . type === ( type || 'error' ) ) ;
25
27
26
28
module . exports = main ;
27
29
@@ -34,17 +36,17 @@ function main (args) {
34
36
var rtn , props , reqd , enums ;
35
37
var action , id , filter , item , fields , defs , elm , profile ;
36
38
37
- elm = args . name || "" ;
39
+ elm = args . name || '' ;
38
40
props = args . props || [ ] ;
39
41
reqd = args . reqd || [ ] ;
40
- action = args . action || " list" ;
41
- id = args . id || "" ;
42
- filter = args . filter || "" ;
42
+ action = args . action || ' list' ;
43
+ id = args . id || '' ;
44
+ filter = args . filter || '' ;
43
45
item = args . item || { } ;
44
46
reqd = args . reqd || [ ] ;
45
47
enums = args . enums || [ ] ;
46
48
defs = args . defs || [ ] ;
47
- fields = args . fields || "" ;
49
+ fields = args . fields || '' ;
48
50
49
51
// confirm existence of object storage
50
52
storage ( { action : 'create' , object : elm } ) ;
@@ -98,7 +100,7 @@ function main (args) {
98
100
if ( rtn ) {
99
101
resolve ( rtn ) ;
100
102
} else {
101
- reject ( { error : " unable to process component request" } ) ;
103
+ reject ( { error : ' unable to process component request' } ) ;
102
104
}
103
105
} ) ;
104
106
}
@@ -115,42 +117,42 @@ function main (args) {
115
117
*/
116
118
function addEntry ( elm , entry , props , reqd , enums , defs ) {
117
119
var rtn , item , error , id , i , x ;
118
-
120
+
119
121
item = { } ;
120
122
121
123
// ensure correct properties
122
124
for ( i = 0 , x = props . length ; i < x ; i ++ ) {
123
- if ( props [ i ] !== " id" ) {
124
- item [ props [ i ] ] = ( entry [ props [ i ] ] || "" ) ;
125
+ if ( props [ i ] !== ' id' ) {
126
+ item [ props [ i ] ] = ( entry [ props [ i ] ] || '' ) ;
125
127
} else {
126
128
id = entry [ props [ i ] ] ;
127
129
}
128
130
}
129
-
131
+
130
132
// fix up any missing defaults
131
133
for ( i = 0 , x = defs . length ; i < x ; i ++ ) {
132
- if ( item [ defs [ i ] . name ] === "" ) {
134
+ if ( item [ defs [ i ] . name ] === '' ) {
133
135
item [ defs [ i ] . name ] = defs [ i ] . value ;
134
136
}
135
137
}
136
-
137
- error = "" ;
138
-
138
+
139
+ error = '' ;
140
+
139
141
// check for missing properties
140
142
for ( i = 0 , x = reqd . length ; i < x ; i ++ ) {
141
- if ( item [ reqd [ i ] ] === "" ) {
142
- error += " Missing " + reqd [ i ] + " " ;
143
+ if ( item [ reqd [ i ] ] === '' ) {
144
+ error += ' Missing ' + reqd [ i ] + ' ' ;
143
145
}
144
146
}
145
147
146
148
// validate enumerated properties
147
149
for ( i = 0 , x = enums . length ; i < x ; i ++ ) {
148
150
for ( var key in enums [ i ] ) {
149
- if ( item [ key ] !== "" ) {
150
- if ( enums [ i ] [ key ] . indexOf ( item [ key ] ) === - 1 ) {
151
- error += " Invalid enumerator [" + item [ key ] + " ] for " + key + " " ;
152
- }
153
- }
151
+ if ( item [ key ] !== '' ) {
152
+ if ( enums [ i ] [ key ] . indexOf ( item [ key ] ) === - 1 ) {
153
+ error += ' Invalid enumerator [' + item [ key ] + ' ] for ' + key + ' ' ;
154
+ }
155
+ }
154
156
}
155
157
}
156
158
@@ -159,13 +161,13 @@ function addEntry (elm, entry, props, reqd, enums, defs) {
159
161
rtn = utils . exception ( error ) ;
160
162
} else {
161
163
rtn = storage ( {
162
- object : elm ,
163
- action : 'add' ,
164
- item : utils . setProps ( item , props ) ,
165
- id : id
166
- } ) ;
164
+ object : elm ,
165
+ action : 'add' ,
166
+ item : utils . setProps ( item , props ) ,
167
+ id : id
168
+ } ) ;
167
169
}
168
-
170
+
169
171
return rtn ;
170
172
}
171
173
@@ -183,39 +185,42 @@ function updateEntry (elm, id, entry, props, reqd, enums) {
183
185
var rtn , check , item , error , i , x ;
184
186
185
187
check = storage ( {
186
- object : elm ,
187
- action : 'item' ,
188
- id : id
189
- } ) ;
188
+ object : elm ,
189
+ action : 'item' ,
190
+ id : id
191
+ } ) ;
192
+ const ERROR = 'error'
193
+ const HAS_ERROR = IS ( check , ERROR )
190
194
191
- if ( check === null || ( check . type && check . type === "error" ) ) {
192
- rtn = utils . exception ( "File Not Found" , "No record on file" , 404 ) ;
195
+ if ( HAS_ERROR ) {
196
+ // rtn = erm(404)
197
+ rtn = utils . exception ( 'File Not Found' , 'No record on file' , 404 ) ;
193
198
} else {
194
199
item = check ;
195
200
for ( i = 0 , x = props . length ; i < x ; i ++ ) {
196
- if ( props [ i ] !== "id" ) {
201
+ if ( props [ i ] !== 'id' ) {
197
202
item [ props [ i ] ] = ( entry [ props [ i ] ] === undefined ? check [ props [ i ] ] : entry [ props [ i ] ] ) ;
198
203
}
199
204
}
200
205
201
- error = "" ;
206
+ error = '' ;
202
207
for ( i = 0 , x = reqd . length ; i < x ; i ++ ) {
203
- if ( item [ reqd [ i ] ] === "" ) {
204
- error += " Missing " + reqd [ i ] + " " ;
208
+ if ( item [ reqd [ i ] ] === '' ) {
209
+ error += ' Missing ' + reqd [ i ] + ' ' ;
205
210
}
206
211
}
207
212
208
213
for ( i = 0 , x = enums . length ; i < x ; i ++ ) {
209
214
for ( var key in enums [ i ] ) {
210
- if ( item [ key ] !== "" ) {
215
+ if ( item [ key ] !== '' ) {
211
216
if ( enums [ i ] [ key ] . indexOf ( item [ key ] ) === - 1 ) {
212
- error += " Invalid enumerator [" + item [ key ] + " ] for " + key + " " ;
217
+ error += ' Invalid enumerator [' + item [ key ] + ' ] for ' + key + ' ' ;
213
218
}
214
219
}
215
220
}
216
221
}
217
222
218
- if ( error !== "" ) {
223
+ if ( error !== '' ) {
219
224
rtn = utils . exception ( error ) ;
220
225
} else {
221
226
rtn = storage ( {
@@ -246,7 +251,7 @@ function removeEntry (elm, id) {
246
251
} ) ;
247
252
248
253
if ( check === null ) {
249
- rtn = utils . exception ( " File Not Found" , " No record on file" , 404 ) ;
254
+ rtn = utils . exception ( ' File Not Found' , ' No record on file' , 404 ) ;
250
255
} else {
251
256
storage ( {
252
257
object : elm ,
0 commit comments