@@ -10,6 +10,20 @@ const stringToRegexQuery = val => {
1010 return { $regex : new RegExp ( val ) } ;
1111} ;
1212
13+ const StateOriginFilterMapping = {
14+ city : {
15+ type : FILTER_CONDITION_TYPE . MATCH_1_TO_1
16+ } ,
17+ stateuf : {
18+ type : FILTER_CONDITION_TYPE . MATCH_1_TO_1 ,
19+ key : "destination.state.uf"
20+ } ,
21+ statename : {
22+ type : FILTER_CONDITION_TYPE . MATCH_1_TO_1 ,
23+ key : "destination.state.name"
24+ }
25+ } ;
26+
1327const FreightFilterMapping = {
1428 site : {
1529 type : FILTER_CONDITION_TYPE . MATCH_1_TO_1
@@ -139,6 +153,42 @@ const FreightFilterMapping = {
139153
140154export default {
141155 Query : {
156+ stateOrigin : async ( parent , args , context , info ) => {
157+ const { conditions } = buildMongoConditionsFromFilters (
158+ null ,
159+ args . destination ,
160+ StateOriginFilterMapping
161+ ) ;
162+ const states = await Freight . aggregate ( [ {
163+ $lookup : {
164+ from : 'locations' ,
165+ localField : 'origin' ,
166+ foreignField : '_id' ,
167+ as : 'origin' ,
168+ } ,
169+ } ,
170+ {
171+ $lookup : {
172+ from : 'locations' ,
173+ localField : 'destination' ,
174+ foreignField : '_id' ,
175+ as : 'destination' ,
176+ } ,
177+ } ,
178+ {
179+ $unwind :'$destination'
180+ } ,
181+ {
182+ $unwind :'$origin' ,
183+ } ,
184+ { $match : conditions } ,
185+ {
186+ $group : {
187+ _id : "$origin.state.uf" ,
188+ }
189+ } ] ) . exec ( ) ;
190+ return Array . from ( Object . keys ( states ) , p => states [ p ] . _id ) ;
191+ } ,
142192 freight : async ( parent , { _id } , context , info ) => {
143193 if ( ! _id ) throw new Error ( "Insert id." ) ;
144194 return await Freight . findOne ( { _id } )
@@ -151,22 +201,23 @@ export default {
151201 const filterResult = buildMongoConditionsFromFilters (
152202 null ,
153203 args . filter ,
154- FreightFilterMapping
204+ FreightFilterMapping // TODOFIX: Agregações
155205 ) ;
206+
156207 const { conditions, pipeline } = filterResult . conditions ;
157- // const finalPipeline = [{ $match: conditions }, ...pipeline];
208+ const finalPipeline = [ { $match : conditions } , ...pipeline ] ;
158209
159210 console . log ( conditions , pipeline ) ;
160211
161212 const res = await Freight . find ( filterResult . conditions )
162213 . skip ( perpage * ( page - 1 ) )
163214 . limit ( perpage )
164- . populate ( "origin destination" ) // company
215+ . populate ( "origin destination company" )
165216 . exec ( ) ;
166217
167- const totalcount = await Freight . countDocuments ( filterResult . conditions )
168- . populate ( "origin destination" ) // company
169- . exec ( ) ;
218+ const totalcount = await Freight . countDocuments (
219+ filterResult . conditions
220+ ) . exec ( ) ;
170221
171222 const hasnextpage = page < totalcount / perpage ;
172223
@@ -202,30 +253,6 @@ export default {
202253 } ,
203254 Mutation : {
204255 createFreight : async ( parent , { freight } , context , info ) => {
205- const newFreight = await Freight . create ( {
206- url : freight . url ,
207- site : freight . site ,
208- origin : freight . origin ,
209- destination : freight . destination ,
210- status : freight . status ,
211- km : freight . km ,
212- price : freight . price ,
213- weight : freight . weight ,
214- cargo : freight . cargo ,
215- especie : freight . especie , // TODOFIX: Translate
216- complement : freight . complement ,
217- tracking : freight . tracking ,
218- note : freight . note ,
219- vehicles : freight . vehicles ,
220- bodies : freight . bodies ,
221- nextel : freight . nextel ,
222- cellphone : freight . cellphone ,
223- telephone : freight . telephone ,
224- whatsapp : freight . whatsapp ,
225- sac : freight . sac , // TODOFIX: Translate
226- company : freight . company
227- } ) ;
228-
229256 const creator = await Company . findById ( freight . company ) ;
230257 if ( ! creator ) throw new Error ( "Company not found." ) ;
231258
@@ -234,18 +261,7 @@ export default {
234261
235262 const destination = await Location . findById ( freight . destination ) ;
236263 if ( ! destination ) throw new Error ( "Destination not found." ) ;
237-
238- try {
239- // const result = await newFreight.save(); // TODOFIX: DEVERIA RETORNAR O FRETE COM AS AGREGACOES
240- return new Promise ( ( resolve , reject ) => {
241- newFreight . save ( ( err , res ) => {
242- err ? reject ( err ) : resolve ( res ) ;
243- } ) ;
244- } ) ;
245- } catch ( error ) {
246- console . log ( error ) ;
247- throw error ;
248- }
264+ return await Freight . create ( freight ) ;
249265 } ,
250266 updateFreight : async ( parent , { _id, freight } , context , info ) => {
251267 const update = await Freight . updateOne (
@@ -272,4 +288,18 @@ export default {
272288 }
273289 }
274290 }
291+ /*
292+ // Pode ser usado para inserir campos a mais.. com querys prontas
293+ ou fazer relacionamentos
294+
295+ Freight: {
296+ origin: async ({ origin }, args, context, info) => { // parent
297+ console.log(origin);
298+ return await Location.findOne({ _id: origin });
299+ },
300+ destination: async ({ destination }, args, context, info) => {
301+ console.log(destination);
302+ return await Location.findOne({ _id: destination });
303+ }
304+ }*/
275305} ;
0 commit comments