@@ -2,6 +2,7 @@ import mongoose from 'mongoose';
22import { getDateForMongo } from '@utils/filter/getDateForMongo' ;
33import { getTimeForMongo } from '@utils/filter/getTimeForMongo' ;
44import { MULTISELECT_TYPES , DATE_TYPES } from '@const/fieldTypes' ;
5+ import { Resource } from '@models' ;
56
67/** The default fields */
78const DEFAULT_FIELDS = [
@@ -64,16 +65,20 @@ export const extractFilterFields = (filter: any): string[] => {
6465 * @param prefix prefix to access field
6566 * @returns Mongo filter.
6667 */
67- const buildMongoFilter = (
68+ const buildMongoFilter = async (
6869 filter : any ,
6970 fields : any [ ] ,
7071 context : any ,
7172 prefix = ''
72- ) : any => {
73+ ) : Promise < any > => {
7374 if ( filter . filters ) {
74- const filters = filter . filters
75- . map ( ( x : any ) => buildMongoFilter ( x , fields , context , prefix ) )
76- . filter ( ( x ) => x ) ;
75+ const filters = (
76+ await Promise . all (
77+ filter . filters . map ( ( x : any ) =>
78+ buildMongoFilter ( x , fields , context , prefix )
79+ )
80+ )
81+ ) . filter ( ( x ) => x ) ;
7782 if ( filters . length > 0 ) {
7883 switch ( filter . logic ) {
7984 case 'and' : {
@@ -101,6 +106,28 @@ const buildMongoFilter = (
101106 ( x ) =>
102107 x . name === filter . field || x . name === filter . field . split ( '.' ) [ 0 ]
103108 ) ?. type || '' ;
109+
110+ // If type is resource and refers to a nested field, get the type of the nested field
111+ if ( type === 'resource' ) {
112+ // find the resource field
113+ const resourceField = fields . find (
114+ ( x ) => x . name === filter . field . split ( '.' ) [ 0 ]
115+ ) ;
116+
117+ if ( resourceField ?. resource ) {
118+ const nestedResource = await Resource . findById (
119+ resourceField . resource
120+ ) ;
121+
122+ // find the nested field
123+ const nestedField = nestedResource ?. fields . find (
124+ ( x ) => x . name === filter . field . split ( '.' ) [ 1 ]
125+ ) ;
126+
127+ // get the type of the nested field
128+ type = nestedField ?. type || type ;
129+ }
130+ }
104131 if ( filter . field === 'ids' ) {
105132 return {
106133 _id : { $in : filter . value . map ( ( x ) => mongoose . Types . ObjectId ( x ) ) } ,
0 commit comments