@@ -4,39 +4,46 @@ const SearchBuilder = require('./search-builder');
44const utils = require ( '../utils/schema' ) ;
55const FiltersParser = require ( './filters-parser' ) ;
66
7- function HasManyGetter ( parentModel , childModel , opts , params ) {
8- const OBJECTID_REGEXP = / ^ [ 0 - 9 a - f A - F ] { 24 } $ / ;
9- const schema = Interface . Schemas . schemas [ utils . getModelName ( childModel ) ] ;
10- const searchBuilder = new SearchBuilder ( childModel , opts , params ) ;
11- const filtersParser = new FiltersParser ( childModel , params . timezone , opts ) ;
12-
13- function hasPagination ( ) {
14- return params . page && params . page . number ;
7+ const OBJECTID_REGEXP = / ^ [ 0 - 9 a - f A - F ] { 24 } $ / ;
8+
9+ class HasManyGetter {
10+ constructor ( parentModel , childModel , opts , params ) {
11+ this . _parentModel = parentModel ;
12+ this . _childModel = childModel ;
13+ this . _params = params ;
14+ this . _opts = opts ;
15+ this . _searchBuilder = new SearchBuilder ( childModel , opts , params ) ;
1516 }
1617
17- function getLimit ( ) {
18- if ( hasPagination ( ) ) {
19- return parseInt ( params . page . number , 10 ) * params . page . size ;
18+ _hasPagination ( ) {
19+ return this . _params . page && this . _params . page . number ;
20+ }
21+
22+ _getLimit ( ) {
23+ if ( this . _hasPagination ( ) ) {
24+ return parseInt ( this . _params . page . number , 10 ) * this . _params . page . size ;
2025 }
2126 return 5 ;
2227 }
2328
24- function getSkip ( ) {
25- if ( hasPagination ( ) ) {
26- return ( parseInt ( params . page . number , 10 ) - 1 ) * params . page . size ;
29+ _getSkip ( ) {
30+ if ( this . _hasPagination ( ) ) {
31+ return ( parseInt ( this . _params . page . number , 10 ) - 1 ) * this . _params . page . size ;
2732 }
2833 return 0 ;
2934 }
3035
31- function getProjection ( ) {
36+ _getProjection ( ) {
3237 const projection = { } ;
33- projection [ params . associationName ] = 1 ;
38+ projection [ this . _params . associationName ] = 1 ;
3439 projection . _id = 0 ; // eslint-disable-line
3540
3641 return projection ;
3742 }
3843
39- function handlePopulate ( query ) {
44+ _handlePopulate ( query ) {
45+ const schema = Interface . Schemas . schemas [ utils . getModelName ( this . _childModel ) ] ;
46+
4047 _ . each ( schema . fields , ( field ) => {
4148 if ( field . reference ) {
4249 query . populate ( {
@@ -46,55 +53,55 @@ function HasManyGetter(parentModel, childModel, opts, params) {
4653 } ) ;
4754 }
4855
49- async function buildConditions ( recordIds ) {
56+ async _buildConditions ( recordIds ) {
5057 const conditions = {
5158 $and : [ { _id : { $in : recordIds } } ] ,
5259 } ;
5360
54- if ( params . search ) {
55- const conditionsSearch = await searchBuilder . getConditions ( ) ;
61+ if ( this . _params . search ) {
62+ const conditionsSearch = await this . _searchBuilder . getConditions ( ) ;
5663 conditions . $and . push ( conditionsSearch ) ;
5764 }
5865
59- if ( params . filters ) {
60- const newFilters = await filtersParser . replaceAllReferences ( params . filters ) ;
66+ if ( this . _params . filters ) {
67+ const filtersParser = new FiltersParser ( this . _childModel , this . _params . timezone , this . _opts ) ;
68+ const newFilters = await filtersParser . replaceAllReferences ( this . _params . filters ) ;
6169 const newFiltersString = JSON . stringify ( newFilters ) ;
6270 conditions . $and . push ( await filtersParser . perform ( newFiltersString ) ) ;
6371 }
6472
6573 return conditions ;
6674 }
6775
68- async function getRecordsAndRecordIds ( ) {
69- let id = params . recordId ;
70- if ( OBJECTID_REGEXP . test ( params . recordId ) ) {
71- id = opts . Mongoose . Types . ObjectId ( id ) ;
76+ async _getRecordsAndRecordIds ( ) {
77+ let id = this . _params . recordId ;
78+ if ( OBJECTID_REGEXP . test ( this . _params . recordId ) ) {
79+ id = this . _opts . Mongoose . Types . ObjectId ( id ) ;
7280 }
7381
74- const parentRecords = await parentModel
82+ const parentRecords = await this . _parentModel
7583 . aggregate ( )
7684 . match ( { _id : id } )
77- . unwind ( params . associationName )
78- . project ( getProjection ( ) )
85+ . unwind ( this . _params . associationName )
86+ . project ( this . _getProjection ( ) )
7987 . exec ( ) ;
8088
81- const childRecordIds = _ . map ( parentRecords , ( record ) => record [ params . associationName ] ) ;
82- const conditions = await buildConditions ( childRecordIds ) ;
83- const query = childModel . find ( conditions ) ;
84- handlePopulate ( query ) ;
89+ const childRecordIds = _ . map ( parentRecords , ( record ) => record [ this . _params . associationName ] ) ;
90+ const conditions = await this . _buildConditions ( childRecordIds ) ;
91+ const query = this . _childModel . find ( conditions ) ;
92+ this . _handlePopulate ( query ) ;
8593
8694 const childRecords = await query ;
8795 return [ childRecords , childRecordIds ] ;
8896 }
8997
90- this . perform = async ( ) => {
91- const [ childRecords , childRecordIds ] = await getRecordsAndRecordIds ( ) ;
92-
93- let fieldSort = params . sort ;
98+ async perform ( ) {
99+ const [ childRecords , childRecordIds ] = await this . _getRecordsAndRecordIds ( ) ;
100+ let fieldSort = this . _params . sort ;
94101 let descending = false ;
95102
96- if ( params . sort && ( params . sort [ 0 ] === '-' ) ) {
97- fieldSort = params . sort . substring ( 1 ) ;
103+ if ( this . _params . sort && ( this . _params . sort [ 0 ] === '-' ) ) {
104+ fieldSort = this . _params . sort . substring ( 1 ) ;
98105 descending = true ;
99106 }
100107
@@ -111,19 +118,21 @@ function HasManyGetter(parentModel, childModel, opts, params) {
111118 let sortedChildRecords = descending ? recordsSorted . reverse ( ) : recordsSorted ;
112119 let fieldsSearched = null ;
113120
114- if ( params . search ) {
115- fieldsSearched = searchBuilder . getFieldsSearched ( ) ;
121+ if ( this . _params . search ) {
122+ fieldsSearched = this . _searchBuilder . getFieldsSearched ( ) ;
116123 }
117124
118- sortedChildRecords = _ . slice ( sortedChildRecords , getSkip ( ) , getSkip ( ) + getLimit ( ) ) ;
125+ sortedChildRecords = _ . slice (
126+ sortedChildRecords , this . _getSkip ( ) , this . _getSkip ( ) + this . _getLimit ( ) ,
127+ ) ;
119128
120129 return [ sortedChildRecords , fieldsSearched ] ;
121- } ;
130+ }
122131
123- this . count = async ( ) => {
124- const recordsAndRecordIds = await getRecordsAndRecordIds ( ) ;
132+ async count ( ) {
133+ const recordsAndRecordIds = await this . _getRecordsAndRecordIds ( ) ;
125134 return recordsAndRecordIds [ 0 ] . length ;
126- } ;
135+ }
127136}
128137
129138module . exports = HasManyGetter ;
0 commit comments