Skip to content

Commit c9a874e

Browse files
*
1 parent c66fc4d commit c9a874e

File tree

3 files changed

+84
-81
lines changed

3 files changed

+84
-81
lines changed

graphql/resolvers/Freight/index.js

Lines changed: 64 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
4177
const 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.");

graphql/resolvers/Location/index.js

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,13 @@ const LocationFilterMapping = {
1111
city: {
1212
type: FILTER_CONDITION_TYPE.MATCH_1_TO_1
1313
},
14-
state: {
15-
uf: {
16-
type: FILTER_CONDITION_TYPE.MATCH_1_TO_1,
17-
key: "state.uf"
18-
}
14+
stateuf: {
15+
type: FILTER_CONDITION_TYPE.MATCH_1_TO_1,
16+
key: "state.uf"
1917
},
20-
state: {
21-
name: {
22-
type: FILTER_CONDITION_TYPE.MATCH_1_TO_1,
23-
key: "state.name"
24-
}
18+
statename: {
19+
type: FILTER_CONDITION_TYPE.MATCH_1_TO_1,
20+
key: "state.name"
2521
}
2622
};
2723

graphql/types/Freight/index.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,27 @@ export default `
4141
}
4242
4343
type Query {
44-
stateOrigin(destination: InputStateOrigin): [String!]!
45-
stateDestination(origin: InputStateOrigin): [String!]!
44+
stateOrigin(filter: InputQueryState): [String!]!
45+
stateDestination(filter: InputQueryState): [String!]!
46+
47+
cityOrigin(filter: InputQuerStateRequired): [String!]!
48+
cityDestination(filter: InputQuerStateRequired): [String!]!
49+
4650
freight(_id: ID!): Freight!
4751
freights(page: Int, perpage: Int, filter: InputFreights): OutputFreight!
4852
}
4953
50-
input InputStateOrigin {
54+
input InputQueryState {
55+
city: String
56+
stateuf: String
57+
statename: String
58+
}
59+
60+
input InputQuerStateRequired {
5161
city: String
5262
stateuf: String
5363
statename: String
64+
statebase: String!
5465
}
5566
5667
input InputFreights {

0 commit comments

Comments
 (0)