Skip to content

Commit fd9b277

Browse files
*
1 parent 03b5776 commit fd9b277

File tree

6 files changed

+82
-70
lines changed

6 files changed

+82
-70
lines changed

graphql/resolvers/Company/index.js

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,7 @@ export default {
5252
},
5353
Mutation: {
5454
createCompany: async (parent, { company }, context, info) => {
55-
const newCompany = await new Company({
56-
name: company.name,
57-
logo: company.logo,
58-
level: company.level,
59-
status: company.status
60-
});
61-
return new Promise((resolve, reject) => {
62-
newCompany.save((err, res) => {
63-
err ? reject(err) : resolve(res);
64-
});
65-
});
55+
return Company.create(company);
6656
},
6757
updateCompany: async (parent, { _id, company }, context, info) => {
6858
const data = await Company.updateOne(

graphql/resolvers/Freight/index.js

Lines changed: 72 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,20 @@ const stringToRegexQuery = val => {
1010
return { $regex: new RegExp(val) };
1111
};
1212

13+
const StateOriginFilterMapping = {
14+
city: {
15+
type: FILTER_CONDITION_TYPE.MATCH_1_TO_1
16+
},
17+
stateuf: {
18+
type: FILTER_CONDITION_TYPE.MATCH_1_TO_1,
19+
key: "destination.state.uf"
20+
},
21+
statename: {
22+
type: FILTER_CONDITION_TYPE.MATCH_1_TO_1,
23+
key: "destination.state.name"
24+
}
25+
};
26+
1327
const FreightFilterMapping = {
1428
site: {
1529
type: FILTER_CONDITION_TYPE.MATCH_1_TO_1
@@ -139,6 +153,42 @@ const FreightFilterMapping = {
139153

140154
export default {
141155
Query: {
156+
stateOrigin: async (parent, args, context, info) => {
157+
const { conditions } = buildMongoConditionsFromFilters(
158+
null,
159+
args.destination,
160+
StateOriginFilterMapping
161+
);
162+
const states = await Freight.aggregate([{
163+
$lookup: {
164+
from: 'locations',
165+
localField: 'origin',
166+
foreignField: '_id',
167+
as: 'origin',
168+
},
169+
},
170+
{
171+
$lookup: {
172+
from: 'locations',
173+
localField: 'destination',
174+
foreignField: '_id',
175+
as: 'destination',
176+
},
177+
},
178+
{
179+
$unwind:'$destination'
180+
},
181+
{
182+
$unwind:'$origin',
183+
},
184+
{ $match: conditions },
185+
{
186+
$group: {
187+
_id: "$origin.state.uf",
188+
}
189+
}]).exec();
190+
return Array.from(Object.keys(states), p => states[p]._id);
191+
},
142192
freight: async (parent, { _id }, context, info) => {
143193
if (!_id) throw new Error("Insert id.");
144194
return await Freight.findOne({ _id })
@@ -151,22 +201,23 @@ export default {
151201
const filterResult = buildMongoConditionsFromFilters(
152202
null,
153203
args.filter,
154-
FreightFilterMapping
204+
FreightFilterMapping // TODOFIX: Agregações
155205
);
206+
156207
const { conditions, pipeline } = filterResult.conditions;
157-
//const finalPipeline = [{ $match: conditions }, ...pipeline];
208+
const finalPipeline = [{ $match: conditions }, ...pipeline];
158209

159210
console.log(conditions, pipeline);
160211

161212
const res = await Freight.find(filterResult.conditions)
162213
.skip(perpage * (page - 1))
163214
.limit(perpage)
164-
.populate("origin destination") // company
215+
.populate("origin destination company")
165216
.exec();
166217

167-
const totalcount = await Freight.countDocuments(filterResult.conditions)
168-
.populate("origin destination") // company
169-
.exec();
218+
const totalcount = await Freight.countDocuments(
219+
filterResult.conditions
220+
).exec();
170221

171222
const hasnextpage = page < totalcount / perpage;
172223

@@ -202,30 +253,6 @@ export default {
202253
},
203254
Mutation: {
204255
createFreight: async (parent, { freight }, context, info) => {
205-
const newFreight = await Freight.create({
206-
url: freight.url,
207-
site: freight.site,
208-
origin: freight.origin,
209-
destination: freight.destination,
210-
status: freight.status,
211-
km: freight.km,
212-
price: freight.price,
213-
weight: freight.weight,
214-
cargo: freight.cargo,
215-
especie: freight.especie, // TODOFIX: Translate
216-
complement: freight.complement,
217-
tracking: freight.tracking,
218-
note: freight.note,
219-
vehicles: freight.vehicles,
220-
bodies: freight.bodies,
221-
nextel: freight.nextel,
222-
cellphone: freight.cellphone,
223-
telephone: freight.telephone,
224-
whatsapp: freight.whatsapp,
225-
sac: freight.sac, // TODOFIX: Translate
226-
company: freight.company
227-
});
228-
229256
const creator = await Company.findById(freight.company);
230257
if (!creator) throw new Error("Company not found.");
231258

@@ -234,18 +261,7 @@ export default {
234261

235262
const destination = await Location.findById(freight.destination);
236263
if (!destination) throw new Error("Destination not found.");
237-
238-
try {
239-
// const result = await newFreight.save(); // TODOFIX: DEVERIA RETORNAR O FRETE COM AS AGREGACOES
240-
return new Promise((resolve, reject) => {
241-
newFreight.save((err, res) => {
242-
err ? reject(err) : resolve(res);
243-
});
244-
});
245-
} catch (error) {
246-
console.log(error);
247-
throw error;
248-
}
264+
return await Freight.create(freight);
249265
},
250266
updateFreight: async (parent, { _id, freight }, context, info) => {
251267
const update = await Freight.updateOne(
@@ -272,4 +288,18 @@ export default {
272288
}
273289
}
274290
}
291+
/*
292+
// Pode ser usado para inserir campos a mais.. com querys prontas
293+
ou fazer relacionamentos
294+
295+
Freight: {
296+
origin: async ({ origin }, args, context, info) => { // parent
297+
console.log(origin);
298+
return await Location.findOne({ _id: origin });
299+
},
300+
destination: async ({ destination }, args, context, info) => {
301+
console.log(destination);
302+
return await Location.findOne({ _id: destination });
303+
}
304+
}*/
275305
};

graphql/resolvers/User/index.js

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,7 @@ export default {
2424
},
2525
Mutation: {
2626
createUser: async (parent, { user }, context, info) => {
27-
const newUser = await new User({
28-
first_name: user.first_name,
29-
last_name: user.last_name,
30-
email: user.email,
31-
password: user.password,
32-
telephones: user.telephones,
33-
role: user.role,
34-
status: user.status
35-
});
36-
37-
return new Promise((resolve, reject) => {
38-
newUser.save((err, res) => {
39-
err ? reject(err) : resolve(res);
40-
});
41-
});
27+
return await User.create(user);
4228
},
4329
updateUser: async (parent, { _id, user }, context, info) => {
4430
const update = await Freight.updateOne({ _id }, user, {

graphql/types/Freight/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,17 @@ export default `
4141
}
4242
4343
type Query {
44+
stateOrigin(destination: InputStateOrigin): [String!]!
4445
freight(_id: ID!): Freight!
4546
freights(page: Int, perpage: Int, filter: InputFreights): OutputFreight!
4647
}
4748
49+
input InputStateOrigin {
50+
city: String
51+
stateuf: String
52+
statename: String
53+
}
54+
4855
input InputFreights {
4956
status: Boolean
5057
origin: InputLocation

graphql/types/Location/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ export default `
66
state: State!
77
location: Position!
88
}
9-
109
type State {
1110
uf: String!
1211
name: String!

server/models/Company.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const CompanySchema = new Schema(
1515
required: true
1616
},
1717
logo: {
18-
type: String, // Buffer,
18+
type: String, // TODOFIX: Buffer to use base64
1919
required: true
2020
},
2121
level: {

0 commit comments

Comments
 (0)