@@ -188,7 +188,8 @@ UserSchema.statics.authenticate = function (username, password, callback) {
188188 if ( result === true ) {
189189 return callback ( null , user ) ;
190190 } else {
191- return callback ( ) ;
191+ let err = new Error ( 'Bad password.' ) ;
192+ return callback ( err ) ;
192193 }
193194 } ) ;
194195 } ) ;
@@ -201,6 +202,8 @@ UserSchema.statics.authenticate = function (username, password, callback) {
201202 * @param {String } apikey the new api service key
202203 * @param {String } apisecret the new api service secret
203204 * @param {function } callback to get the result data or error
205+ * @return {Error } error if something wrong
206+ * @return {String } sucess if its all ok
204207 * @memberof module:models~UserSchema
205208 */
206209UserSchema . statics . addapi = function ( id ,
@@ -210,18 +213,24 @@ UserSchema.statics.addapi = function (id,
210213 key : apikey ,
211214 secret : apisecret ,
212215 } ;
213- Apis . create ( newapi , ( error , api ) => {
214- let elemtype = { } ;
215- elemtype [ 'Apis.' + apitype ] = api ;
216- User . findOneAndUpdate ( { _id : id } , { $push : elemtype } ,
217- ( error , success ) => {
218- if ( error ) {
219- callback && callback ( error ) ;
220- } else {
221- callback && callback ( null , success ) ;
222- }
223- } ) ;
224- } ) ;
216+ if ( [ 'Bank' , 'Crypto' , 'Markets' ] . includes ( apitype ) ) {
217+ Apis . create ( newapi , ( error , api ) => {
218+ let elemtype = { } ;
219+ elemtype [ 'Apis.' + apitype ] = api ;
220+ User . findOneAndUpdate ( { _id : id } , { $push : elemtype } ,
221+ ( error , success ) => {
222+ if ( error ) {
223+ return callback && callback ( error ) ;
224+ } else {
225+ let rt = 'Api ' + api . name + ' successfully added.' ;
226+ return callback && callback ( null , rt ) ;
227+ }
228+ } ) ;
229+ } ) ;
230+ } else {
231+ let rt = new Error ( 'No such api type.' ) ;
232+ return callback && callback ( rt , undefined ) ;
233+ }
225234} ;
226235
227236/** Add a new Asset object to a User
@@ -231,6 +240,8 @@ UserSchema.statics.addapi = function (id,
231240 * @param {String } assetticker the ticker / symbol
232241 * @param {String } assetqtt the qtt to parsed in float
233242 * @param {function } callback to get the result data or error
243+ * @return {Error } error if something wrong
244+ * @return {String } sucess if its all ok
234245 * @memberof module:models~UserSchema
235246 */
236247UserSchema . statics . addasset = ( id , assettype , assetid ,
@@ -240,24 +251,32 @@ UserSchema.statics.addasset = (id, assettype, assetid,
240251 ticker : assetticker ,
241252 qtt : assetqtt ,
242253 } ;
243- Assets . create ( newasset , ( error , asset ) => {
244- let elemtype = { } ;
245- elemtype [ 'Assets.' + assettype ] = asset ;
246- User . findOneAndUpdate ( { _id : id } , { $push : elemtype } ,
247- ( error , success ) => {
248- if ( error ) {
249- callback && callback ( error ) ;
250- } else {
251- callback && callback ( null , success ) ;
252- }
253- } ) ;
254- } ) ;
254+ if ( [ 'Bank' , 'Crypto' , 'Markets' ] . includes ( assettype ) ) {
255+ Assets . create ( newasset , ( error , asset ) => {
256+ let elemtype = { } ;
257+ elemtype [ 'Assets.' + assettype ] = asset ;
258+ User . findOneAndUpdate ( { _id : id } , { $push : elemtype } ,
259+ ( error , success ) => {
260+ if ( error ) {
261+ return callback && callback ( error , undefined ) ;
262+ } else {
263+ let rt = 'Asset ' + asset . name + ' successfully added.' ;
264+ return callback && callback ( null , rt ) ;
265+ }
266+ } ) ;
267+ } ) ;
268+ } else {
269+ let rt = new Error ( 'No such asset type.' ) ;
270+ return callback && callback ( rt , undefined ) ;
271+ }
255272} ;
256273
257274let Apis = mongoose . model ( 'Apis' , ApiSchema ) ;
258275let Assets = mongoose . model ( 'Assets' , AssetsSchema ) ;
259276let User = mongoose . model ( 'User' , UserSchema ) ;
260-
261277module . exports = User ;
262278module . exports . Apis = Apis ;
263279module . exports . Assets = Assets ;
280+ if ( process . env . TEST === 'ok' ) {
281+ module . exports . InFuncs = { toLower, toFloat} ;
282+ }
0 commit comments