-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraphQlRequest.js
316 lines (298 loc) · 6.67 KB
/
graphQlRequest.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
//Launch request with the choosen query
function request(query) {
const axios = require("axios");
const endpoint = process.env.LogcicaApiUrl;
const headers = {
"content-type": "application/json",
apiKey: process.env.LogcicaApiKey,
};
try {
const options = {
method: "POST",
headers: headers,
body: JSON.stringify(query),
};
return axios.post(endpoint, JSON.stringify(query), {
headers: headers,
});
} catch (error) {
console.log(error);
}
}
module.exports = {
//Create a new tour with user data
addTour: function addTour(tourData) {
var query = {};
if (tourData.tourVehicle != null) {
query = {
query: `
mutation insertOneTour($data: TourInsertInput!) {
insertOneTour(data: $data) {
name
date
vehicle{
_id
}
owner{
workspace{
_id
}
}
}
}`,
variables: {
data: {
createdAt: new Date(),
name: tourData.tourName,
date: tourData.tourDate,
vehicle: {
link: tourData.tourVehicle.value,
},
owner: {
workspace: {
link: tourData.channelId,
},
},
},
},
};
} else {
query = {
query: `
mutation insertOneTour($data: TourInsertInput!) {
insertOneTour(data: $data) {
name
date
owner{
workspace{
_id
}
}
}
}`,
variables: {
data: {
name: tourData.tourName,
date: tourData.tourDate,
owner: {
workspace: {
link: tourData.channelId,
},
},
},
},
};
}
return request(query);
},
//Get every tours
fetchTour: function fetchTour() {
const query = {
query: `
query {
tours{
name
date
_id
}
}`,
variables: {},
};
return request(query);
},
//Get every tours
fetchLatestToursForChannel: function fetchLatestToursForChannel(channelId) {
const query = {
query: `{
tours(
query: {owner: { workspace: { coconut: { slackChannel: { id: "${channelId}"} } } }},
sortBy: DATE_DESC
) {
name
date
_id,
owner {
workspace {
_id
key
name
person
type
}
}
}
}`,
variables: {},
};
return request(query);
},
//Get every workspace's vehicles
fetchVehicles: function fetchVehicles(workspaceId) {
const query = {
query: `
query {
vehicles(query:{owner:{workspace:{_id:"${workspaceId}"}}}){
_id
name
brand{
name
}
model{
name
}
capacity{
volume{
unit{
symbol
}
value
}
weight{
unit{
symbol
}
value
}
}
}
}`,
variables: {},
};
return request(query);
},
//Get tour informations with its Id
getTourData: function getTourData(id) {
const query = {
query: `
query{tours(query: { _id_in: "${id}" })
{
distance{
unit{
symbol
}
value
}
duration{
unit{
symbol
}
value
}
name
date
vehicle{
name
brand{
name
}
model{
name
}
}
stops{
name
geo{
coordinates
}
order
estimatedTimeOfArrival
estimatedTimeOfDeparture
duration{
unit{
symbol
}
value
}
place
delivery{
shipments
}
pickup{
shipments
}
}
shipments{
_id
key
order{
number
}
delivery{
place{
address{
postalCode
street
locality
country
}
gln
}
}
}
}
}
`,
variables: {},
};
return request(query);
},
//Get tour draft status
getTourDraftStatus: function getTourDraftStatus(tourDraftId) {
const query = {
query: `query{
tourDraft(query :{_id :"${tourDraftId}"}){
validation{
status
}
}
}`,
variables: {},
};
return request(query);
},
//Get tour id by name
findWorkspaceId: function findWorkspaceId(channelName) {
console.log(channelName);
const query = {
query: `query{workspace(query : { key : "${channelName}"}){
_id
}
}`,
variables: {},
};
return request(query);
},
//Create a new tour draft
insertOneTourDraft: async function insertOneTourDraft(tourId, shipmentId) {
const getTourData = require("./graphQlRequest.js");
const { data } = await getTourData.getTourData(`${tourId}`);
const shipmentsList = data.data.tours[0].shipments.map((e) => e._id);
shipmentsList.push(shipmentId);
var query = {
query: `mutation insertOneTourDraft($data: TourDraftInsertInput!) {
insertOneTourDraft(data: $data){
_id
}
}`,
variables: {
data: {
tour: { link: tourId },
shipments: { link: shipmentsList },
},
},
};
return request(query);
},
//Set stop's shipments status
setStopStatus: async function setStopStatus(tourId, stopOrder) {
const getTourData = require("./graphQlRequest.js");
const { data } = await getTourData.getTourData(`${tourId}`);
const stopData = data.data.tours[0].stops[stopOrder];
const pickup = stopData?.pickup?.shipments ?? "";
const delivery = stopData?.delivery?.shipments ?? "";
/*var query = query qui met les shipments qui ont été pickup dans l'état "pickup"
request(query);
var query = query qui met les shipments qui ont été delivery dans l'état "delivery"
request(query);*/
},
};