@@ -14,32 +14,78 @@ const convertJsonToDot = (obj, parent = [], keyValue = {}) => {
1414 return keyValue ;
1515} ;
1616
17+ const freightquery = [
18+ {
19+ $lookup : {
20+ from : "locations" ,
21+ localField : "origin" ,
22+ foreignField : "_id" ,
23+ as : "origin"
24+ }
25+ } ,
26+ {
27+ $lookup : {
28+ from : "locations" ,
29+ localField : "destination" ,
30+ foreignField : "_id" ,
31+ as : "destination"
32+ }
33+ } ,
34+ {
35+ $lookup : {
36+ from : "companies" ,
37+ localField : "company" ,
38+ foreignField : "_id" ,
39+ as : "company"
40+ }
41+ } ,
42+ { $unwind : "$destination" } ,
43+ { $unwind : "$origin" } ,
44+ { $unwind : "$company" }
45+ ] ;
46+
1747const freightaggregate = async ( groupby , conditions ) => {
1848 const data = await Freight . aggregate ( [
19- {
20- $lookup : {
21- from : "locations" ,
22- localField : "origin" ,
23- foreignField : "_id" ,
24- as : "origin"
25- }
26- } ,
27- {
28- $lookup : {
29- from : "locations" ,
30- localField : "destination" ,
31- foreignField : "_id" ,
32- as : "destination"
33- }
34- } ,
35- { $unwind : "$destination" } ,
36- { $unwind : "$origin" } ,
49+ ...freightquery ,
3750 { $match : conditions } ,
3851 { $group : { _id : `$${ groupby } ` } }
3952 ] ) . exec ( ) ;
4053 return Array . from ( Object . keys ( data ) , p => data [ p ] . _id ) ;
4154} ;
4255
56+ const freightfinalquery = async ( res , conditions , page , perpage ) => {
57+ const totalcount = await Freight . countDocuments ( conditions ) . exec ( ) ;
58+ const hasnextpage = page < totalcount / perpage ;
59+ return {
60+ totalcount,
61+ hasnextpage,
62+ freights : res . map ( u => ( {
63+ _id : u . _id . toString ( ) ,
64+ url : u . url ,
65+ site : u . site ,
66+ origin : u . origin ,
67+ destination : u . destination ,
68+ status : u . status ,
69+ km : u . km ,
70+ price : u . price ,
71+ weight : u . weight ,
72+ cargo : u . cargo ,
73+ especie : u . especie ,
74+ complement : u . complement ,
75+ tracking : u . tracking ,
76+ note : u . note ,
77+ vehicles : u . vehicles ,
78+ bodies : u . bodies ,
79+ nextel : u . nextel ,
80+ cellphone : u . cellphone ,
81+ telephone : u . telephone ,
82+ whatsapp : u . whatsapp ,
83+ sac : u . sac ,
84+ company : u . company
85+ } ) )
86+ } ;
87+ } ;
88+
4389export default {
4490 Query : {
4591 stateOrigin : async ( parent , args , context , info ) => {
@@ -71,70 +117,35 @@ export default {
71117 if ( vehicles ) conditions . vehicles = { vehicles : { $in : vehicles } } ;
72118 if ( bodies ) conditions . bodies = { bodies : { $in : bodies } } ;
73119 const res = await Freight . aggregate ( [
74- {
75- $lookup : {
76- from : "locations" ,
77- localField : "origin" ,
78- foreignField : "_id" ,
79- as : "origin"
80- }
81- } ,
82- {
83- $lookup : {
84- from : "locations" ,
85- localField : "destination" ,
86- foreignField : "_id" ,
87- as : "destination"
88- }
89- } ,
90- {
91- $lookup : {
92- from : "companies" ,
93- localField : "company" ,
94- foreignField : "_id" ,
95- as : "company"
96- }
97- } ,
98- { $unwind : "$destination" } ,
99- { $unwind : "$origin" } ,
100- { $unwind : "$company" } ,
120+ ...freightquery ,
101121 { $match : conditions } ,
102122 { $skip : ( page - 1 ) * perpage } ,
103123 { $limit : perpage }
104124 ] ) . exec ( ) ;
105-
106- const totalcount = await Freight . countDocuments ( conditions ) . exec ( ) ;
107-
108- const hasnextpage = page < totalcount / perpage ;
109-
110- return {
111- totalcount,
112- hasnextpage,
113- freights : res . map ( u => ( {
114- _id : u . _id . toString ( ) ,
115- url : u . url ,
116- site : u . site ,
117- origin : u . origin ,
118- destination : u . destination ,
119- status : u . status ,
120- km : u . km ,
121- price : u . price ,
122- weight : u . weight ,
123- cargo : u . cargo ,
124- especie : u . especie ,
125- complement : u . complement ,
126- tracking : u . tracking ,
127- note : u . note ,
128- vehicles : u . vehicles ,
129- bodies : u . bodies ,
130- nextel : u . nextel ,
131- cellphone : u . cellphone ,
132- telephone : u . telephone ,
133- whatsapp : u . whatsapp ,
134- sac : u . sac ,
135- company : u . company
136- } ) )
137- } ;
125+ return freightfinalquery ( res , conditions , page , perpage ) ;
126+ } ,
127+ freightsrange : async ( parent , args , context , info ) => {
128+ const { page = 1 , perpage = 20 , filter, coordinates, range } = args ;
129+ const { vehicles, bodies, ...resto } = convertJsonToDot ( filter ) ;
130+ const conditions = { ...resto } ;
131+ if ( vehicles ) conditions . vehicles = { vehicles : { $in : vehicles } } ;
132+ if ( bodies ) conditions . bodies = { bodies : { $in : bodies } } ;
133+ const res = await Freight . aggregate ( [
134+ ...freightquery ,
135+ { $match : conditions } ,
136+ /*{
137+ $geoNear: {
138+ near: { type: "Point", coordinates },
139+ distanceField: "dist.calculated",
140+ spherical: true,
141+ key: "origin.location",
142+ maxDistance: range
143+ }
144+ },*/
145+ { $skip : ( page - 1 ) * perpage } ,
146+ { $limit : perpage }
147+ ] ) . exec ( ) ;
148+ return freightfinalquery ( res , conditions , page , perpage ) ;
138149 }
139150 } ,
140151 Mutation : {
0 commit comments