@@ -284,7 +284,7 @@ DataForm.prototype.models = function (req, res, next) {
284
284
285
285
return function ( req , res , next ) {
286
286
res . send ( that . resources ) ;
287
- }
287
+ } ;
288
288
289
289
// return _.bind(function (req, res, next) {
290
290
// res.send(this.resources)
@@ -364,7 +364,7 @@ DataForm.prototype.preprocess = function (paths, formSchema) {
364
364
}
365
365
}
366
366
}
367
- var returnObj = { paths : outPath }
367
+ var returnObj = { paths : outPath } ;
368
368
if ( hiddenFields . length > 0 ) {
369
369
returnObj . hide = hiddenFields ;
370
370
}
@@ -408,80 +408,92 @@ DataForm.prototype.report = function () {
408
408
var schemaCopy = { } ;
409
409
extend ( schemaCopy , reportSchema ) ;
410
410
schemaCopy . params = schemaCopy . params || [ ] ;
411
- // Bit crap here switching back and forth to string
412
- runPipeline = JSON . stringify ( schemaCopy . pipeline ) ;
413
- for ( var param in url_parts . query ) {
414
- if ( param !== 'r' ) { // we don't want to copy the whole report schema (again!)
415
- schemaCopy . params [ param ] . value = url_parts . query [ param ] ;
416
- }
417
- }
418
- runPipeline = runPipeline . replace ( / \" \( .+ ?\) \" / g, function ( match ) {
419
- param = schemaCopy . params [ match . slice ( 2 , - 2 ) ] ;
420
- return param . type === 'number' ? param . value : '"' + param . value + '"' ;
421
- } )
422
- runPipeline = JSON . parse ( runPipeline ) ;
423
411
424
- var toDo = { runAggregation : function ( cb , results ) {
425
- req . resource . model . aggregate ( runPipeline , cb )
426
- }
427
- } ;
428
-
429
- // if we need to do any column translations add the function to the tasks list
430
- if ( reportSchema . columnTranslations ) {
431
- toDo . apply_translations = [ 'runAggregation' , function ( cb , results ) {
432
- reportSchema . columnTranslations . forEach ( function ( columnTranslation ) {
433
- results . runAggregation . forEach ( function ( resultRow ) {
434
- var thisTranslation = _ . find ( columnTranslation . translations , function ( option ) {
435
- return resultRow [ columnTranslation . field ] . toString ( ) === option . value . toString ( )
436
- } ) ;
437
- resultRow [ columnTranslation . field ] = thisTranslation . display ;
438
- } )
412
+ self . doFindFunc ( req , req . resource , function ( err , queryObj ) {
413
+
414
+ if ( err ) {
415
+ return self . renderError ( new Error ( "There was a problem with the findFunc for model " + req . resource . modelName ) , null , req , res , next ) ;
416
+ } else {
417
+ if ( queryObj ) {
418
+ schemaCopy . pipeline . unshift ( { $match :queryObj } ) ;
419
+ }
420
+
421
+ // Bit crap here switching back and forth to string
422
+ runPipeline = JSON . stringify ( schemaCopy . pipeline ) ;
423
+ for ( var param in url_parts . query ) {
424
+ if ( param !== 'r' ) { // we don't want to copy the whole report schema (again!)
425
+ schemaCopy . params [ param ] . value = url_parts . query [ param ] ;
426
+ }
427
+ }
428
+ runPipeline = runPipeline . replace ( / \" \( .+ ?\) \" / g, function ( match ) {
429
+ param = schemaCopy . params [ match . slice ( 2 , - 2 ) ] ;
430
+ return param . type === 'number' ? param . value : '"' + param . value + '"' ;
439
431
} ) ;
440
- cb ( null , null ) ;
441
- } ] ;
442
-
443
- // if any of the column translations are refs, set up the tasks to look up the values and populate the translations
444
- for ( var i = 0 ; i < reportSchema . columnTranslations . length ; i ++ ) {
445
- var thisColumnTranslation = reportSchema . columnTranslations [ i ]
446
- , translateName = thisColumnTranslation . field ;
447
- if ( translateName ) {
448
- if ( thisColumnTranslation . ref ) {
449
- var lookup = self . getResource ( thisColumnTranslation . ref ) ;
450
- if ( lookup ) {
451
- if ( toDo [ translateName ] ) {
452
- return self . renderError ( new Error ( "Cannot have two columnTranslations for field " + translateName ) , null , req , res , next ) ;
453
- } else {
454
- thisColumnTranslation . translations = thisColumnTranslation . translations || [ ] ;
455
- toDo [ translateName ] = function ( cb , results ) { lookup . model . find ( { } , { } , { lean :true } , function ( err , findResults ) {
456
- if ( err ) {
457
- cb ( err ) ;
432
+ runPipeline = JSON . parse ( runPipeline ) ;
433
+
434
+ var toDo = { runAggregation : function ( cb , results ) {
435
+ req . resource . model . aggregate ( runPipeline , cb )
436
+ }
437
+ } ;
438
+
439
+ // if we need to do any column translations add the function to the tasks list
440
+ if ( reportSchema . columnTranslations ) {
441
+ toDo . apply_translations = [ 'runAggregation' , function ( cb , results ) {
442
+ reportSchema . columnTranslations . forEach ( function ( columnTranslation ) {
443
+ results . runAggregation . forEach ( function ( resultRow ) {
444
+ var thisTranslation = _ . find ( columnTranslation . translations , function ( option ) {
445
+ return resultRow [ columnTranslation . field ] . toString ( ) === option . value . toString ( )
446
+ } ) ;
447
+ resultRow [ columnTranslation . field ] = thisTranslation . display ;
448
+ } )
449
+ } ) ;
450
+ cb ( null , null ) ;
451
+ } ] ;
452
+
453
+ // if any of the column translations are refs, set up the tasks to look up the values and populate the translations
454
+ for ( var i = 0 ; i < reportSchema . columnTranslations . length ; i ++ ) {
455
+ var thisColumnTranslation = reportSchema . columnTranslations [ i ]
456
+ , translateName = thisColumnTranslation . field ;
457
+ if ( translateName ) {
458
+ if ( thisColumnTranslation . ref ) {
459
+ var lookup = self . getResource ( thisColumnTranslation . ref ) ;
460
+ if ( lookup ) {
461
+ if ( toDo [ translateName ] ) {
462
+ return self . renderError ( new Error ( "Cannot have two columnTranslations for field " + translateName ) , null , req , res , next ) ;
458
463
} else {
459
- for ( var j = 0 ; j < findResults . length ; j ++ ) {
460
- thisColumnTranslation . translations [ j ] = { value : findResults [ j ] . _id , display : self . getListFields ( lookup , findResults [ j ] ) } ;
461
- }
462
- cb ( null , null ) ;
464
+ thisColumnTranslation . translations = thisColumnTranslation . translations || [ ] ;
465
+ toDo [ translateName ] = function ( cb , results ) { lookup . model . find ( { } , { } , { lean :true } , function ( err , findResults ) {
466
+ if ( err ) {
467
+ cb ( err ) ;
468
+ } else {
469
+ for ( var j = 0 ; j < findResults . length ; j ++ ) {
470
+ thisColumnTranslation . translations [ j ] = { value : findResults [ j ] . _id , display : self . getListFields ( lookup , findResults [ j ] ) } ;
471
+ }
472
+ cb ( null , null ) ;
473
+ }
474
+ } ) } ;
475
+ toDo . apply_translations . unshift ( translateName ) ; // Make sure we populate lookup before doing translation
463
476
}
464
- } ) } ;
465
- toDo . apply_translations . unshift ( translateName ) ; // Make sure we populate lookup before doing translation
477
+ } else {
478
+ return self . renderError ( new Error ( "Invalid ref property of " + thisColumnTranslation . ref + " in columnTranslations " + translateName ) , null , req , res , next ) ;
479
+ }
480
+ } else if ( ! thisColumnTranslation . translations ) {
481
+ return self . renderError ( new Error ( "A column translation needs a ref or a translations property - " + translateName + " has neither" ) , null , req , res , next ) ;
466
482
}
467
483
} else {
468
- return self . renderError ( new Error ( "Invalid ref property of " + thisColumnTranslation . ref + " in columnTranslations " + translateName ) , null , req , res , next ) ;
484
+ return self . renderError ( new Error ( "A column translation needs a field property" ) , null , req , res , next ) ;
469
485
}
470
- } else if ( ! thisColumnTranslation . translations ) {
471
- return self . renderError ( new Error ( "A column translation needs a ref or a translations property - " + translateName + " has neither" ) , null , req , res , next ) ;
472
486
}
473
- } else {
474
- return self . renderError ( new Error ( "A column translation needs a field property" ) , null , req , res , next ) ;
475
487
}
476
- }
477
- }
478
488
479
- async . auto ( toDo , function ( err , results ) {
480
- if ( err ) {
481
- return self . renderError ( err , null , req , res , next ) ;
482
- } else {
483
- // TODO: Could loop through schemaCopy.params and just send back the values
484
- res . send ( { success :true , schema :reportSchema , report : results . runAggregation , paramsUsed : schemaCopy . params } ) ;
489
+ async . auto ( toDo , function ( err , results ) {
490
+ if ( err ) {
491
+ return self . renderError ( err , null , req , res , next ) ;
492
+ } else {
493
+ // TODO: Could loop through schemaCopy.params and just send back the values
494
+ res . send ( { success :true , schema :reportSchema , report : results . runAggregation , paramsUsed : schemaCopy . params } ) ;
495
+ }
496
+ } ) ;
485
497
}
486
498
} ) ;
487
499
} , this ) ;
@@ -566,8 +578,18 @@ DataForm.prototype.collectionGet = function () {
566
578
} , this ) ;
567
579
} ;
568
580
581
+ DataForm . prototype . doFindFunc = function ( req , resource , cb ) {
582
+ if ( resource . options . findFunc ) {
583
+ resource . options . findFunc ( req , cb )
584
+ } else {
585
+ cb ( null ) ;
586
+ }
587
+ } ;
588
+
569
589
DataForm . prototype . filteredFind = function ( resource , req , aggregationParam , findParam , sortOrder , limit , skip , callback ) {
570
- var hidden_fields = this . generateHiddenFields ( resource , false ) ;
590
+
591
+ var that = this
592
+ , hidden_fields = this . generateHiddenFields ( resource , false ) ;
571
593
572
594
function doAggregation ( cb ) {
573
595
if ( aggregationParam ) {
@@ -585,33 +607,24 @@ DataForm.prototype.filteredFind = function (resource, req, aggregationParam, fin
585
607
}
586
608
}
587
609
588
- function doFindFunc ( cb ) {
589
- if ( resource . options . findFunc ) {
590
- resource . options . findFunc ( req , function ( err , query ) {
591
- if ( err ) {
592
- throw err ;
593
- } else {
594
- cb ( query ) ;
595
- }
596
- } )
597
- } else {
598
- cb ( resource . model . find ( { } ) ) ;
599
- }
600
- }
601
-
602
610
doAggregation ( function ( idArray ) {
603
611
if ( aggregationParam && idArray . length === 0 ) {
604
612
callback ( null , [ ] )
605
613
} else {
606
- doFindFunc ( function ( query ) {
607
- if ( idArray . length > 0 ) {
608
- query = query . where ( '_id' ) . in ( idArray )
614
+ that . doFindFunc ( req , resource , function ( err , queryObj ) {
615
+ if ( err ) {
616
+ callback ( err )
617
+ } else {
618
+ var query = resource . model . find ( queryObj ) ;
619
+ if ( idArray . length > 0 ) {
620
+ query = query . where ( '_id' ) . in ( idArray )
621
+ }
622
+ query = query . find ( findParam ) . select ( hidden_fields ) ;
623
+ if ( limit ) query = query . limit ( limit ) ;
624
+ if ( skip ) query = query . skip ( skip ) ;
625
+ if ( sortOrder ) query = query . sort ( sortOrder ) ;
626
+ query . exec ( callback ) ;
609
627
}
610
- query = query . find ( findParam ) . select ( hidden_fields ) ;
611
- if ( limit ) query = query . limit ( limit ) ;
612
- if ( skip ) query = query . skip ( skip ) ;
613
- if ( sortOrder ) query = query . sort ( sortOrder ) ;
614
- query . exec ( callback ) ;
615
628
} )
616
629
}
617
630
} )
@@ -729,7 +742,7 @@ DataForm.prototype.replaceHiddenFields = function (record, data) {
729
742
}
730
743
} ) ;
731
744
delete self . _replacingHiddenFields ;
732
- }
745
+ } ;
733
746
734
747
DataForm . prototype . entityPut = function ( ) {
735
748
return _ . bind ( function ( req , res , next ) {
@@ -757,8 +770,6 @@ DataForm.prototype.entityPut = function () {
757
770
} else {
758
771
that . saveAndRespond ( req , res ) ;
759
772
}
760
- ;
761
-
762
773
} , this ) ;
763
774
} ;
764
775
0 commit comments