Skip to content

Commit a0b7d34

Browse files
*
1 parent cd6eb01 commit a0b7d34

File tree

2 files changed

+107
-86
lines changed

2 files changed

+107
-86
lines changed

graphql/resolvers/Freight/index.js

Lines changed: 89 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -14,32 +14,78 @@ const convertJsonToDot = (obj, parent = [], keyValue = {}) => {
1414
return keyValue;
1515
};
1616

17+
const freightquery = [
18+
{
19+
$lookup: {
20+
from: "locations",
21+
localField: "origin",
22+
foreignField: "_id",
23+
as: "origin"
24+
}
25+
},
26+
{
27+
$lookup: {
28+
from: "locations",
29+
localField: "destination",
30+
foreignField: "_id",
31+
as: "destination"
32+
}
33+
},
34+
{
35+
$lookup: {
36+
from: "companies",
37+
localField: "company",
38+
foreignField: "_id",
39+
as: "company"
40+
}
41+
},
42+
{ $unwind: "$destination" },
43+
{ $unwind: "$origin" },
44+
{ $unwind: "$company" }
45+
];
46+
1747
const freightaggregate = async (groupby, conditions) => {
1848
const data = await Freight.aggregate([
19-
{
20-
$lookup: {
21-
from: "locations",
22-
localField: "origin",
23-
foreignField: "_id",
24-
as: "origin"
25-
}
26-
},
27-
{
28-
$lookup: {
29-
from: "locations",
30-
localField: "destination",
31-
foreignField: "_id",
32-
as: "destination"
33-
}
34-
},
35-
{ $unwind: "$destination" },
36-
{ $unwind: "$origin" },
49+
...freightquery,
3750
{ $match: conditions },
3851
{ $group: { _id: `$${groupby}` } }
3952
]).exec();
4053
return Array.from(Object.keys(data), p => data[p]._id);
4154
};
4255

56+
const freightfinalquery = async (res, conditions, page, perpage) => {
57+
const totalcount = await Freight.countDocuments(conditions).exec();
58+
const hasnextpage = page < totalcount / perpage;
59+
return {
60+
totalcount,
61+
hasnextpage,
62+
freights: res.map(u => ({
63+
_id: u._id.toString(),
64+
url: u.url,
65+
site: u.site,
66+
origin: u.origin,
67+
destination: u.destination,
68+
status: u.status,
69+
km: u.km,
70+
price: u.price,
71+
weight: u.weight,
72+
cargo: u.cargo,
73+
especie: u.especie,
74+
complement: u.complement,
75+
tracking: u.tracking,
76+
note: u.note,
77+
vehicles: u.vehicles,
78+
bodies: u.bodies,
79+
nextel: u.nextel,
80+
cellphone: u.cellphone,
81+
telephone: u.telephone,
82+
whatsapp: u.whatsapp,
83+
sac: u.sac,
84+
company: u.company
85+
}))
86+
};
87+
};
88+
4389
export default {
4490
Query: {
4591
stateOrigin: async (parent, args, context, info) => {
@@ -71,70 +117,35 @@ export default {
71117
if (vehicles) conditions.vehicles = { vehicles: { $in: vehicles } };
72118
if (bodies) conditions.bodies = { bodies: { $in: bodies } };
73119
const res = await Freight.aggregate([
74-
{
75-
$lookup: {
76-
from: "locations",
77-
localField: "origin",
78-
foreignField: "_id",
79-
as: "origin"
80-
}
81-
},
82-
{
83-
$lookup: {
84-
from: "locations",
85-
localField: "destination",
86-
foreignField: "_id",
87-
as: "destination"
88-
}
89-
},
90-
{
91-
$lookup: {
92-
from: "companies",
93-
localField: "company",
94-
foreignField: "_id",
95-
as: "company"
96-
}
97-
},
98-
{ $unwind: "$destination" },
99-
{ $unwind: "$origin" },
100-
{ $unwind: "$company" },
120+
...freightquery,
101121
{ $match: conditions },
102122
{ $skip: (page - 1) * perpage },
103123
{ $limit: perpage }
104124
]).exec();
105-
106-
const totalcount = await Freight.countDocuments(conditions).exec();
107-
108-
const hasnextpage = page < totalcount / perpage;
109-
110-
return {
111-
totalcount,
112-
hasnextpage,
113-
freights: res.map(u => ({
114-
_id: u._id.toString(),
115-
url: u.url,
116-
site: u.site,
117-
origin: u.origin,
118-
destination: u.destination,
119-
status: u.status,
120-
km: u.km,
121-
price: u.price,
122-
weight: u.weight,
123-
cargo: u.cargo,
124-
especie: u.especie,
125-
complement: u.complement,
126-
tracking: u.tracking,
127-
note: u.note,
128-
vehicles: u.vehicles,
129-
bodies: u.bodies,
130-
nextel: u.nextel,
131-
cellphone: u.cellphone,
132-
telephone: u.telephone,
133-
whatsapp: u.whatsapp,
134-
sac: u.sac,
135-
company: u.company
136-
}))
137-
};
125+
return freightfinalquery(res, conditions, page, perpage);
126+
},
127+
freightsrange: async (parent, args, context, info) => {
128+
const { page = 1, perpage = 20, filter, coordinates, range } = args;
129+
const { vehicles, bodies, ...resto } = convertJsonToDot(filter);
130+
const conditions = { ...resto };
131+
if (vehicles) conditions.vehicles = { vehicles: { $in: vehicles } };
132+
if (bodies) conditions.bodies = { bodies: { $in: bodies } };
133+
const res = await Freight.aggregate([
134+
...freightquery,
135+
{ $match: conditions },
136+
/*{
137+
$geoNear: {
138+
near: { type: "Point", coordinates },
139+
distanceField: "dist.calculated",
140+
spherical: true,
141+
key: "origin.location",
142+
maxDistance: range
143+
}
144+
},*/
145+
{ $skip: (page - 1) * perpage },
146+
{ $limit: perpage }
147+
]).exec();
148+
return freightfinalquery(res, conditions, page, perpage);
138149
}
139150
},
140151
Mutation: {

graphql/types/Freight/index.js

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,25 +66,35 @@ export default `
6666
stateOrigin(filter: InputQueryState): [String!]!
6767
stateDestination(filter: InputQueryState): [String!]!
6868
69-
cityOrigin(filter: InputQueryCityOrigin): [String!]!
70-
cityDestination(filter: InputQueryCityDestination): [String!]!
69+
cityOrigin(filter: InputQueryCityOrigin!): [String!]!
70+
cityDestination(filter: InputQueryCityDestination!): [String!]!
7171
7272
freight(_id: ID!): Freight!
7373
freights(page: Int, perpage: Int, filter: InputFreights): OutputFreight!
74+
freightsrange(page: Int, perpage: Int, filter: InputFreightsRange, range: Int!, coordinates: [Float!]!): OutputFreight!
7475
}
7576
7677
input InputFreights {
7778
status: Boolean
7879
vehicles: [String]
7980
bodies: [String]
80-
8181
origin: InputLocation
8282
destination: InputLocation
83-
84-
company_id: ID
85-
companyname: String
86-
companylevel: Int
87-
companystatus: Int
83+
company: QueryFreigth
84+
}
85+
86+
input InputFreightsRange {
87+
status: Boolean
88+
vehicles: [String]
89+
bodies: [String]
90+
company: QueryFreigth
91+
}
92+
93+
input QueryFreigth {
94+
_id: ID
95+
name: String
96+
level: Int
97+
status: Int
8898
}
8999
90100
type Mutation {

0 commit comments

Comments
 (0)