|
6 | 6 | FILTER_CONDITION_TYPE |
7 | 7 | } from "@entria/graphql-mongo-helpers"; |
8 | 8 |
|
9 | | -const stringToRegexQuery = val => { |
10 | | - return { $regex: new RegExp(val) }; |
11 | | -}; |
| 9 | +const stringToRegexQuery = val => ({ $regex: new RegExp(val) }); |
12 | 10 |
|
13 | 11 | const OriginFilterMapping = { |
14 | 12 | city: { |
@@ -48,6 +46,8 @@ const DestinationFilterMapping = { |
48 | 46 | } |
49 | 47 | }; |
50 | 48 |
|
| 49 | +const querysfreight = []; |
| 50 | + |
51 | 51 | const freightaggregate = async (groupby, conditions) => { |
52 | 52 | const data = await Freight.aggregate([ |
53 | 53 | { |
@@ -127,77 +127,53 @@ const FreightFilterMapping = { |
127 | 127 | return { bodies: { $in: bodies } }; |
128 | 128 | } |
129 | 129 | }, |
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" |
149 | 133 | }, |
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" |
169 | 137 | }, |
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" |
201 | 177 | } |
202 | 178 | }; |
203 | 179 |
|
@@ -242,28 +218,48 @@ export default { |
242 | 218 | .exec(); |
243 | 219 | }, |
244 | 220 | freights: async (parent, args, context, info) => { |
245 | | - const { page, perpage } = args; |
| 221 | + const { page, perpage, filter } = args; |
246 | 222 |
|
247 | | - const filterResult = buildMongoConditionsFromFilters( |
| 223 | + const { conditions } = buildMongoConditionsFromFilters( |
248 | 224 | null, |
249 | | - args.filter, |
250 | | - FreightFilterMapping // TODOFIX: Agregações |
| 225 | + filter, |
| 226 | + FreightFilterMapping |
251 | 227 | ); |
252 | 228 |
|
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(); |
263 | 261 |
|
264 | | - const totalcount = await Freight.countDocuments( |
265 | | - filterResult.conditions |
266 | | - ).exec(); |
| 262 | + const totalcount = await Freight.countDocuments(conditions).exec(); |
267 | 263 |
|
268 | 264 | const hasnextpage = page < totalcount / perpage; |
269 | 265 |
|
|
0 commit comments