Skip to content

Commit c66fc4d

Browse files
Add two new querys
stateOrigin and stateDestination
1 parent fd9b277 commit c66fc4d

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

.vscode/settings.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{
2+
}

graphql/resolvers/Freight/index.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,20 @@ const StateOriginFilterMapping = {
2424
}
2525
};
2626

27+
const StateDestinationFilterMapping = {
28+
city: {
29+
type: FILTER_CONDITION_TYPE.MATCH_1_TO_1
30+
},
31+
stateuf: {
32+
type: FILTER_CONDITION_TYPE.MATCH_1_TO_1,
33+
key: "origin.state.uf"
34+
},
35+
statename: {
36+
type: FILTER_CONDITION_TYPE.MATCH_1_TO_1,
37+
key: "origin.state.name"
38+
}
39+
};
40+
2741
const FreightFilterMapping = {
2842
site: {
2943
type: FILTER_CONDITION_TYPE.MATCH_1_TO_1
@@ -189,6 +203,42 @@ export default {
189203
}]).exec();
190204
return Array.from(Object.keys(states), p => states[p]._id);
191205
},
206+
stateDestination: async (parent, args, context, info) => {
207+
const { conditions } = buildMongoConditionsFromFilters(
208+
null,
209+
args.origin,
210+
StateDestinationFilterMapping
211+
);
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);
241+
},
192242
freight: async (parent, { _id }, context, info) => {
193243
if (!_id) throw new Error("Insert id.");
194244
return await Freight.findOne({ _id })

graphql/types/Freight/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export default `
4242
4343
type Query {
4444
stateOrigin(destination: InputStateOrigin): [String!]!
45+
stateDestination(origin: InputStateOrigin): [String!]!
4546
freight(_id: ID!): Freight!
4647
freights(page: Int, perpage: Int, filter: InputFreights): OutputFreight!
4748
}

0 commit comments

Comments
 (0)