@@ -10,34 +10,70 @@ const stringToRegexQuery = val => {
1010 return { $regex : new RegExp ( val ) } ;
1111} ;
1212
13- const StateOriginFilterMapping = {
13+ const OriginFilterMapping = {
1414 city : {
15- type : FILTER_CONDITION_TYPE . MATCH_1_TO_1
15+ type : FILTER_CONDITION_TYPE . MATCH_1_TO_1 ,
16+ key : "destination.city"
1617 } ,
1718 stateuf : {
1819 type : FILTER_CONDITION_TYPE . MATCH_1_TO_1 ,
1920 key : "destination.state.uf"
2021 } ,
2122 statename : {
2223 type : FILTER_CONDITION_TYPE . MATCH_1_TO_1 ,
23- key : "destination.state.name"
24+ key : "destination.state.name"
25+ } ,
26+ statebase : {
27+ type : FILTER_CONDITION_TYPE . MATCH_1_TO_1 ,
28+ key : "origin.state.uf"
2429 }
2530} ;
2631
27- const StateDestinationFilterMapping = {
32+ const DestinationFilterMapping = {
2833 city : {
29- type : FILTER_CONDITION_TYPE . MATCH_1_TO_1
34+ type : FILTER_CONDITION_TYPE . MATCH_1_TO_1 ,
35+ key : "origin.city"
3036 } ,
3137 stateuf : {
3238 type : FILTER_CONDITION_TYPE . MATCH_1_TO_1 ,
3339 key : "origin.state.uf"
3440 } ,
3541 statename : {
3642 type : FILTER_CONDITION_TYPE . MATCH_1_TO_1 ,
37- key : "origin.state.name"
43+ key : "origin.state.name"
44+ } ,
45+ statebase : {
46+ type : FILTER_CONDITION_TYPE . MATCH_1_TO_1 ,
47+ key : "destination.state.uf"
3848 }
3949} ;
4050
51+ const freightaggregate = async ( groupby , conditions ) => {
52+ const data = await Freight . aggregate ( [
53+ {
54+ $lookup : {
55+ from : "locations" ,
56+ localField : "origin" ,
57+ foreignField : "_id" ,
58+ as : "origin"
59+ }
60+ } ,
61+ {
62+ $lookup : {
63+ from : "locations" ,
64+ localField : "destination" ,
65+ foreignField : "_id" ,
66+ as : "destination"
67+ }
68+ } ,
69+ { $unwind : "$destination" } ,
70+ { $unwind : "$origin" } ,
71+ { $match : conditions } ,
72+ { $group : { _id : `$${ groupby } ` } }
73+ ] ) . exec ( ) ;
74+ return Array . from ( Object . keys ( data ) , p => data [ p ] . _id ) ;
75+ } ;
76+
4177const FreightFilterMapping = {
4278 site : {
4379 type : FILTER_CONDITION_TYPE . MATCH_1_TO_1
@@ -170,74 +206,34 @@ export default {
170206 stateOrigin : async ( parent , args , context , info ) => {
171207 const { conditions } = buildMongoConditionsFromFilters (
172208 null ,
173- args . destination ,
174- StateOriginFilterMapping
209+ args . filter ,
210+ OriginFilterMapping
175211 ) ;
176- const states = await Freight . aggregate ( [ {
177- $lookup : {
178- from : 'locations' ,
179- localField : 'origin' ,
180- foreignField : '_id' ,
181- as : 'origin' ,
182- } ,
183- } ,
184- {
185- $lookup : {
186- from : 'locations' ,
187- localField : 'destination' ,
188- foreignField : '_id' ,
189- as : 'destination' ,
190- } ,
191- } ,
192- {
193- $unwind :'$destination'
194- } ,
195- {
196- $unwind :'$origin' ,
197- } ,
198- { $match : conditions } ,
199- {
200- $group : {
201- _id : "$origin.state.uf" ,
202- }
203- } ] ) . exec ( ) ;
204- return Array . from ( Object . keys ( states ) , p => states [ p ] . _id ) ;
212+ return await freightaggregate ( "origin.state.uf" , conditions ) ;
205213 } ,
206214 stateDestination : async ( parent , args , context , info ) => {
207215 const { conditions } = buildMongoConditionsFromFilters (
208216 null ,
209- args . origin ,
210- StateDestinationFilterMapping
217+ args . filter ,
218+ DestinationFilterMapping
211219 ) ;
212- const states = await Freight . aggregate ( [ {
213- $lookup : {
214- from : 'locations' ,
215- localField : 'origin' ,
216- foreignField : '_id' ,
217- as : 'origin' ,
218- } ,
219- } ,
220- {
221- $lookup : {
222- from : 'locations' ,
223- localField : 'destination' ,
224- foreignField : '_id' ,
225- as : 'destination' ,
226- } ,
227- } ,
228- {
229- $unwind :'$destination'
230- } ,
231- {
232- $unwind :'$origin' ,
233- } ,
234- { $match : conditions } ,
235- {
236- $group : {
237- _id : "$destination.state.uf" ,
238- }
239- } ] ) . exec ( ) ;
240- return Array . from ( Object . keys ( states ) , p => states [ p ] . _id ) ;
220+ return await freightaggregate ( "destination.state.uf" , conditions ) ;
221+ } ,
222+ cityOrigin : async ( parent , args , context , info ) => {
223+ const { conditions } = buildMongoConditionsFromFilters (
224+ null ,
225+ args . filter ,
226+ OriginFilterMapping
227+ ) ;
228+ return await freightaggregate ( "origin.city" , conditions ) ;
229+ } ,
230+ cityDestination : async ( parent , args , context , info ) => {
231+ const { conditions } = buildMongoConditionsFromFilters (
232+ null ,
233+ args . filter ,
234+ DestinationFilterMapping
235+ ) ;
236+ return await freightaggregate ( "destination.city" , conditions ) ;
241237 } ,
242238 freight : async ( parent , { _id } , context , info ) => {
243239 if ( ! _id ) throw new Error ( "Insert id." ) ;
0 commit comments