Skip to content

Commit 0b67fc7

Browse files
*
1 parent cc7e1ef commit 0b67fc7

File tree

2 files changed

+102
-101
lines changed

2 files changed

+102
-101
lines changed

graphql/resolvers/Freight/index.js

Lines changed: 85 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ import {
66
FILTER_CONDITION_TYPE
77
} from "@entria/graphql-mongo-helpers";
88

9-
const stringToRegexQuery = val => {
10-
return { $regex: new RegExp(val) };
11-
};
9+
const stringToRegexQuery = val => ({ $regex: new RegExp(val) });
1210

1311
const OriginFilterMapping = {
1412
city: {
@@ -48,6 +46,8 @@ const DestinationFilterMapping = {
4846
}
4947
};
5048

49+
const querysfreight = [];
50+
5151
const freightaggregate = async (groupby, conditions) => {
5252
const data = await Freight.aggregate([
5353
{
@@ -127,77 +127,53 @@ const FreightFilterMapping = {
127127
return { bodies: { $in: bodies } };
128128
}
129129
},
130-
origin: {
131-
code: {
132-
type: FILTER_CONDITION_TYPE.MATCH_1_TO_1
133-
},
134-
city: {
135-
type: FILTER_CONDITION_TYPE.MATCH_1_TO_1
136-
},
137-
state: {
138-
uf: {
139-
type: FILTER_CONDITION_TYPE.MATCH_1_TO_1,
140-
key: "state.name"
141-
}
142-
},
143-
state: {
144-
name: {
145-
type: FILTER_CONDITION_TYPE.MATCH_1_TO_1,
146-
key: "state.uf"
147-
}
148-
}
130+
origincode: {
131+
type: FILTER_CONDITION_TYPE.MATCH_1_TO_1,
132+
key: "origin.code"
149133
},
150-
destination: {
151-
code: {
152-
type: FILTER_CONDITION_TYPE.MATCH_1_TO_1
153-
},
154-
city: {
155-
type: FILTER_CONDITION_TYPE.MATCH_1_TO_1
156-
},
157-
state: {
158-
uf: {
159-
type: FILTER_CONDITION_TYPE.MATCH_1_TO_1,
160-
key: "state.name"
161-
}
162-
},
163-
state: {
164-
name: {
165-
type: FILTER_CONDITION_TYPE.MATCH_1_TO_1,
166-
key: "state.uf"
167-
}
168-
}
134+
origincity: {
135+
type: FILTER_CONDITION_TYPE.MATCH_1_TO_1,
136+
key: "origin.city"
169137
},
170-
company: {
171-
type: FILTER_CONDITION_TYPE.AGGREGATE_PIPELINE,
172-
pipeline: value => [
173-
{
174-
$lookup: {
175-
localField: "company",
176-
from: "company",
177-
foreignField: "_id",
178-
as: "company"
179-
}
180-
},
181-
{
182-
$unwind: "$company"
183-
}
184-
],
185-
name: {
186-
type: FILTER_CONDITION_TYPE.MATCH_1_TO_1,
187-
key: "company.name"
188-
},
189-
level: {
190-
type: FILTER_CONDITION_TYPE.MATCH_1_TO_1,
191-
key: "company.level"
192-
},
193-
status: {
194-
type: FILTER_CONDITION_TYPE.MATCH_1_TO_1,
195-
key: "company.status"
196-
},
197-
_id: {
198-
type: FILTER_CONDITION_TYPE.MATCH_1_TO_1,
199-
key: "company._id"
200-
}
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"
201177
}
202178
};
203179

@@ -242,28 +218,48 @@ export default {
242218
.exec();
243219
},
244220
freights: async (parent, args, context, info) => {
245-
const { page, perpage } = args;
221+
const { page, perpage, filter } = args;
246222

247-
const filterResult = buildMongoConditionsFromFilters(
223+
const { conditions } = buildMongoConditionsFromFilters(
248224
null,
249-
args.filter,
250-
FreightFilterMapping // TODOFIX: Agregações
225+
filter,
226+
FreightFilterMapping
251227
);
252228

253-
const { conditions, pipeline } = filterResult.conditions;
254-
const finalPipeline = [{ $match: conditions }, ...pipeline];
255-
256-
console.log(conditions, pipeline);
257-
258-
const res = await Freight.find(filterResult.conditions)
259-
.skip(perpage * (page - 1))
260-
.limit(perpage)
261-
.populate("origin destination company")
262-
.exec();
229+
const res = await Freight.aggregate([
230+
{
231+
$lookup: {
232+
from: "locations",
233+
localField: "origin",
234+
foreignField: "_id",
235+
as: "origin"
236+
}
237+
},
238+
{
239+
$lookup: {
240+
from: "locations",
241+
localField: "destination",
242+
foreignField: "_id",
243+
as: "destination"
244+
}
245+
},
246+
{
247+
$lookup: {
248+
from: "companies",
249+
localField: "company",
250+
foreignField: "_id",
251+
as: "company"
252+
}
253+
},
254+
{ $unwind: "$destination" },
255+
{ $unwind: "$origin" },
256+
{ $unwind: "$company" },
257+
{ $match: conditions }
258+
// skip
259+
// limit
260+
]).exec();
263261

264-
const totalcount = await Freight.countDocuments(
265-
filterResult.conditions
266-
).exec();
262+
const totalcount = await Freight.countDocuments(conditions).exec();
267263

268264
const hasnextpage = page < totalcount / perpage;
269265

graphql/types/Freight/index.js

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,6 @@ export default `
3232
freights: [Freight!]!
3333
}
3434
35-
input InputFreightCompany {
36-
_id: ID
37-
name: String
38-
logo: String
39-
level: Int
40-
status: Int
41-
}
42-
4335
type Query {
4436
stateOrigin(filter: InputQueryState): [String!]!
4537
stateDestination(filter: InputQueryState): [String!]!
@@ -66,12 +58,25 @@ export default `
6658
6759
input InputFreights {
6860
status: Boolean
69-
origin: InputLocation
70-
destination: InputLocation
71-
company: InputFreightCompany
72-
7361
vehicles: [String]
7462
bodies: [String]
63+
64+
origin_id: ID
65+
origincode: String
66+
origincity: String
67+
originstatename: String
68+
originstateuf: String
69+
70+
destination_id: ID
71+
destinationcode: String
72+
destinationcity: String
73+
destinationstatename: String
74+
destinationstateuf: String
75+
76+
company_id: ID
77+
companyname: String
78+
companylevel: Int
79+
companystatus: Int
7580
}
7681
7782
type Mutation {

0 commit comments

Comments
 (0)