11import Freight from "../../../server/models/Freight" ;
22import Company from "../../../server/models/Company" ;
33import Location from "../../../server/models/Location" ;
4- import {
5- buildMongoConditionsFromFilters ,
6- FILTER_CONDITION_TYPE
7- } from "@entria/graphql-mongo-helpers" ;
84
95const stringToRegexQuery = val => ( { $regex : new RegExp ( val ) } ) ;
106
11- const OriginFilterMapping = {
12- city : {
13- type : FILTER_CONDITION_TYPE . MATCH_1_TO_1 ,
14- key : "destination.city"
15- } ,
16- stateuf : {
17- type : FILTER_CONDITION_TYPE . MATCH_1_TO_1 ,
18- key : "destination.state.uf"
19- } ,
20- statename : {
21- type : FILTER_CONDITION_TYPE . MATCH_1_TO_1 ,
22- key : "destination.state.name"
23- } ,
24- statebase : {
25- type : FILTER_CONDITION_TYPE . MATCH_1_TO_1 ,
26- key : "origin.state.uf"
7+ const convertJsonToDot = ( obj , parent = [ ] , keyValue = { } ) => {
8+ for ( let key in obj ) {
9+ let keyPath = [ ...parent , key ] ;
10+ if ( obj [ key ] !== null && typeof obj [ key ] === "object" ) {
11+ Object . assign ( keyValue , convertJsonToDot ( obj [ key ] , keyPath , keyValue ) ) ;
12+ } else keyValue [ keyPath . join ( "." ) ] = obj [ key ] ;
2713 }
14+ return keyValue ;
2815} ;
2916
30- const DestinationFilterMapping = {
31- city : {
32- type : FILTER_CONDITION_TYPE . MATCH_1_TO_1 ,
33- key : "origin.city"
34- } ,
35- stateuf : {
36- type : FILTER_CONDITION_TYPE . MATCH_1_TO_1 ,
37- key : "origin.state.uf"
38- } ,
39- statename : {
40- type : FILTER_CONDITION_TYPE . MATCH_1_TO_1 ,
41- key : "origin.state.name"
42- } ,
43- statebase : {
44- type : FILTER_CONDITION_TYPE . MATCH_1_TO_1 ,
45- key : "destination.state.uf"
46- }
47- } ;
48-
49- const querysfreight = [ ] ;
50-
5117const freightaggregate = async ( groupby , conditions ) => {
5218 const data = await Freight . aggregate ( [
5319 {
@@ -74,141 +40,22 @@ const freightaggregate = async (groupby, conditions) => {
7440 return Array . from ( Object . keys ( data ) , p => data [ p ] . _id ) ;
7541} ;
7642
77- const FreightFilterMapping = {
78- site : {
79- type : FILTER_CONDITION_TYPE . MATCH_1_TO_1
80- } ,
81- status : {
82- type : FILTER_CONDITION_TYPE . MATCH_1_TO_1
83- } ,
84- km : {
85- type : FILTER_CONDITION_TYPE . MATCH_1_TO_1 ,
86- format : stringToRegexQuery
87- } ,
88- price : {
89- type : FILTER_CONDITION_TYPE . MATCH_1_TO_1 ,
90- format : stringToRegexQuery
91- } ,
92- weight : {
93- type : FILTER_CONDITION_TYPE . MATCH_1_TO_1 ,
94- format : stringToRegexQuery
95- } ,
96- cargo : {
97- type : FILTER_CONDITION_TYPE . MATCH_1_TO_1 ,
98- format : stringToRegexQuery
99- } ,
100- especie : {
101- type : FILTER_CONDITION_TYPE . MATCH_1_TO_1 ,
102- format : stringToRegexQuery
103- } ,
104- complement : {
105- type : FILTER_CONDITION_TYPE . MATCH_1_TO_1 ,
106- format : stringToRegexQuery
107- } ,
108- tracking : {
109- type : FILTER_CONDITION_TYPE . MATCH_1_TO_1 ,
110- format : stringToRegexQuery
111- } ,
112- note : {
113- type : FILTER_CONDITION_TYPE . MATCH_1_TO_1 ,
114- format : stringToRegexQuery
115- } ,
116- vehicles : {
117- type : FILTER_CONDITION_TYPE . CUSTOM_CONDITION ,
118- format : vehicles => {
119- if ( ! vehicles ) return [ ] ;
120- return { vehicles : { $in : vehicles } } ;
121- }
122- } ,
123- bodies : {
124- type : FILTER_CONDITION_TYPE . CUSTOM_CONDITION ,
125- format : bodies => {
126- if ( ! bodies ) return [ ] ;
127- return { bodies : { $in : bodies } } ;
128- }
129- } ,
130- origincode : {
131- type : FILTER_CONDITION_TYPE . MATCH_1_TO_1 ,
132- key : "origin.code"
133- } ,
134- origincity : {
135- type : FILTER_CONDITION_TYPE . MATCH_1_TO_1 ,
136- key : "origin.city"
137- } ,
138- originstateuf : {
139- type : FILTER_CONDITION_TYPE . MATCH_1_TO_1 ,
140- key : "origin.state.uf"
141- } ,
142- originstatename : {
143- type : FILTER_CONDITION_TYPE . MATCH_1_TO_1 ,
144- key : "origin.state.name"
145- } ,
146- destinationcode : {
147- type : FILTER_CONDITION_TYPE . MATCH_1_TO_1 ,
148- key : "destination.code"
149- } ,
150- destinationcity : {
151- type : FILTER_CONDITION_TYPE . MATCH_1_TO_1 ,
152- key : "destination.city"
153- } ,
154- destinationstateuf : {
155- type : FILTER_CONDITION_TYPE . MATCH_1_TO_1 ,
156- key : "destination.state.uf"
157- } ,
158- destinationstatename : {
159- type : FILTER_CONDITION_TYPE . MATCH_1_TO_1 ,
160- key : "destination.state.name"
161- } ,
162- companyname : {
163- type : FILTER_CONDITION_TYPE . MATCH_1_TO_1 ,
164- key : "company.name"
165- } ,
166- companylevel : {
167- type : FILTER_CONDITION_TYPE . MATCH_1_TO_1 ,
168- key : "company.level"
169- } ,
170- companystatus : {
171- type : FILTER_CONDITION_TYPE . MATCH_1_TO_1 ,
172- key : "company.status"
173- } ,
174- company_id : {
175- type : FILTER_CONDITION_TYPE . MATCH_1_TO_1 ,
176- key : "company._id"
177- }
178- } ;
179-
18043export default {
18144 Query : {
18245 stateOrigin : async ( parent , args , context , info ) => {
183- const { conditions } = buildMongoConditionsFromFilters (
184- null ,
185- args . filter ,
186- OriginFilterMapping
187- ) ;
46+ const conditions = convertJsonToDot ( args . filter ) ;
18847 return await freightaggregate ( "origin.state.uf" , conditions ) ;
18948 } ,
19049 stateDestination : async ( parent , args , context , info ) => {
191- const { conditions } = buildMongoConditionsFromFilters (
192- null ,
193- args . filter ,
194- DestinationFilterMapping
195- ) ;
50+ const conditions = convertJsonToDot ( args . filter ) ;
19651 return await freightaggregate ( "destination.state.uf" , conditions ) ;
19752 } ,
19853 cityOrigin : async ( parent , args , context , info ) => {
199- const { conditions } = buildMongoConditionsFromFilters (
200- null ,
201- args . filter ,
202- OriginFilterMapping
203- ) ;
54+ const conditions = convertJsonToDot ( args . filter ) ;
20455 return await freightaggregate ( "origin.city" , conditions ) ;
20556 } ,
20657 cityDestination : async ( parent , args , context , info ) => {
207- const { conditions } = buildMongoConditionsFromFilters (
208- null ,
209- args . filter ,
210- DestinationFilterMapping
211- ) ;
58+ const conditions = convertJsonToDot ( args . filter ) ;
21259 return await freightaggregate ( "destination.city" , conditions ) ;
21360 } ,
21461 freight : async ( parent , { _id } , context , info ) => {
@@ -218,14 +65,13 @@ export default {
21865 . exec ( ) ;
21966 } ,
22067 freights : async ( parent , args , context , info ) => {
221- const { page, perpage, filter } = args ;
222-
223- const { conditions } = buildMongoConditionsFromFilters (
224- null ,
225- filter ,
226- FreightFilterMapping
227- ) ;
228-
68+ const { page = 1 , perpage = 20 , filter } = args ;
69+ const { vehicles, bodies, ...resto } = convertJsonToDot ( filter ) ;
70+ const conditions = {
71+ vehicles : ! vehicles ? [ ] : { vehicles : { $in : vehicles } } ,
72+ bodies : ! bodies ? [ ] : { bodies : { $in : bodies } } ,
73+ ...resto
74+ } ;
22975 const res = await Freight . aggregate ( [
23076 {
23177 $lookup : {
@@ -254,9 +100,9 @@ export default {
254100 { $unwind : "$destination" } ,
255101 { $unwind : "$origin" } ,
256102 { $unwind : "$company" } ,
257- { $match : conditions }
258- // skip
259- // limit
103+ { $match : conditions } ,
104+ { $ skip: ( page - 1 ) * perpage } ,
105+ { $ limit: perpage }
260106 ] ) . exec ( ) ;
261107
262108 const totalcount = await Freight . countDocuments ( conditions ) . exec ( ) ;
0 commit comments