Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
466 lines (387 loc) · 14.2 KB
/
Copy pathserver.js
File metadata and controls
466 lines (387 loc) · 14.2 KB
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
require('dotenv').config();
var express = require('express');
var express_graphql = require('express-graphql');
const { graphqlHTTP } = require('express-graphql');
var { buildSchema } = require('graphql');
// MySQL operational DB connection BEGIN
//env set in .env file
var mysql = require('mysql');
const con_mysql = mysql.createConnection({
host: process.env.MYSQL_HOST,
user: process.env.MYSQL_USERNAME,
password: process.env.MYSQL_PASS,
database: process.env.MYSQL_DB_NAME
});
con_mysql.connect(function(error){
if (!!error) {
console.log("\n" + "-- > NO!, connection to the MySQL operational DB " + process.env.MYSQL_DB_NAME + " on HOST "+process.env.MYSQL_HOST + " has failed!" + "\n");
} else {
console.log("\n" + "-- > YES, connection to the MySQL operational DB " + process.env.MYSQL_DB_NAME + " on HOST "+process.env.MYSQL_HOST +" has been successfully established !." + "\n");
}
});
// MySQL operational DB connection END
// PostgreSQL decision DB connection BEGIN
const { Client } = require('pg');
const con_pg = new Client({
user: process.env.POSTGRESQL_USERNAME,
host: process.env.POSTGRESQL_HOST,
database: process.env.POSTGRESQL_DB_NAME,
password: process.env.POSTGRESQL_PASS,
port: process.env.POSTGRESQL_PORT
});
con_pg.connect(function(error){
if (!!error) {
console.log("\n" + "-- > NO!, connection to the PostgreSQL decision DB " + process.env.POSTGRESQL_DB_NAME + " on HOST "+process.env.POSTGRESQL_HOST + " failed!" + "\n");
} else {
console.log("\n" + "-- > YES, connection to the PostgreSQL decision DB " + process.env.POSTGRESQL_DB_NAME + " on HOST "+process.env.POSTGRESQL_HOST +" has been successfully established!." + "\n");
}
});
// PostgreSQL decision DB connection END
// GraphQL schema
// ! means that the field is non-nullable, meaning that the GraphQL service promises to always give you a value
// when you query this field. In the type language, we'll represent those with an exclamation mark.
var schema = buildSchema(`
scalar DateTime
type Query {
buildings(id: Int!): Building
interventions(id: Int!): Intervention
employees(id: Int!): Employee
customers(email: String!): Customer
customerId(email: String!): Customer
buildingOfCustomer(email: String!): [Building]
batteries(email: String!): [Battery]
columns(email: String!): [Column]
elevators(email: String!): [Elevator]
batteriesOfBuilding(id: Int!): [Battery]
columnsOfBattery(id: Int!): [Column]
elevatorsOfColumn(id: Int!): [Elevator]
},
type Mutation {
intervention(
customer_id: Int,
building_id: Int,
battery_id: Int,
column_id: Int,
elevator_id: Int,
date_started: DateTime,
date_ended: DateTime,
result: String,
status: String,
report: String): InterventionSQL
},
type Building {
id: Int!
customer_id: Int
admin_full_name: String
admin_email: String
admin_phone: String
tech_contact_full_name: String
tech_contact_email: String
tech_contact_phone: String
building_details: [Building_detail]
interventions: [Intervention]
address: Address
customer: Customer
}
type Building_detail {
id: Int!
building_id: Int
key: String
value: String
}
type Address {
id: Int!
type_of_address: String
status: String
entity: String
number_and_street: String
appartment_or_suite: String
city: String
state: String
zip_code: String
country: String
note: String
longitude: Float
latitude: Float
}
type Customer {
id: Int!
company_name: String
full_name: String
email: String
address_id: Int
user_id: Int
phone: String
company_description: String
tech_authority_full_name: String
tech_authority_email: String
tech_authority_phone: String
}
type Employee {
id: Int!
user_id: Int
email: String
first_name: String
last_name: String
title: String
building_details: [Building_detail]
interventions: [Intervention]
building: [Building]
}
type Intervention {
id: Int!
employee_id: Int
building_id: Int
battery_id: String
column_id: String
elevator_id: String
building_details: [Building_detail]
start_date: DateTime
end_date: DateTime
result: String
report: String
status: String
address: Address
}
type InterventionSQL {
id: Int
customer_id: Int
building_id: Int
battery_id: String
column_id: String
elevator_id: String
date_started: DateTime
date_ended: DateTime
result: String
report: String
status: String
address: Address
}
type Battery {
id: Int!
building_id: Int
type_of_battery: String
status: String
employee_id: Int
date_of_comissioning: DateTime
date_of_last_inspection: DateTime
certificate_of_operation: String
information: String
notes: String
}
type Column {
id: Int!
battery_id: Int
type_of_column: String
number_of_floors_served: Int
status: String
information: String
notes: String
}
type Elevator {
id: Int!
column_id: Int
type_of_building: String
serial_number: String
model: String
status: String
date_of_commissioning: DateTime
date_of_last_inspection: DateTime
certificate_of_operations: String
information: String
notes: String
}
`);
// Root resolver
var root = {
buildings: getBuildings,
interventions: getInterventions,
employees: getEmployees,
customers: getCustomers,
customerId: getCustomerId,
buildingOfCustomer: getBuildingsOfCustomer,
batteriesOfBuilding: getBatteriesOfBuilding,
columnsOfBattery: getColumnsOfBattery,
elevatorsOfColumn: getElevatorsOfColumn,
batteries: getBatteries,
columns: getColumns,
elevators: getElevators,
intervention: createIntervention
};
//To answer Question 1 by intervention id
async function getInterventions({id}) {
// Query the factintervention table from the PostgreSQL
intervention = await query_postgresql('SELECT * FROM factintervention WHERE id = ' + id)
console.log(intervention)
resolve = intervention[0]
// Query the MySQL address table.
address = await query_mysql('SELECT * FROM addresses JOIN buildings ON buildings.address_id = addresses.id WHERE buildings.id = ' + resolve.building_id);
console.log(address)
resolve['address']= address[0];
return resolve
};
// WORKS
async function getCustomers({email}) {
var id = await query_mysql(`SELECT id, email FROM customers WHERE email = "${email}"`)
resolve = id[0]
console.log(id)
return resolve
};
async function getBatteries({email}) {
var email = await query_mysql(`
SELECT a.id,
a.building_id,
a.type_of_battery,
a.status,
a.date_of_comissioning,
a.date_of_last_inspection,
a.certificate_of_operation,
a.information,
a.notes
FROM batteries a JOIN buildings b ON a.building_id = b.id JOIN customers c ON b.customer_id = c.id WHERE c.email = "${email}"`)
resolve = email
console.log(email)
return resolve
}
async function getColumns({email}){
var email = await query_mysql(
`SELECT d.id,
d.battery_id,
d.type_of_column,
d.number_of_floors_served,
d.status,
d.information,
d.notes
FROM batteries a JOIN buildings b ON a.building_id = b.id JOIN columns d ON a.id = d.battery_id JOIN customers c ON b.customer_id = c.id WHERE c.email = "${email}"`)
resolve = email
console.log(email)
return resolve
}
async function getBatteriesOfBuilding({id}) {
var batteries = await query_mysql(`SELECT b.id, b.building_id FROM batteries b JOIN buildings a ON b.building_id = a.id WHERE a.id = "${id}"`)
resolve = batteries
return resolve
}
async function getColumnsOfBattery({id}) {
var columns = await query_mysql(`SELECT c.id, c.battery_id FROM columns c JOIN batteries b ON c.battery_id = b.id WHERE b.id = "${id}"`)
resolve = columns
return resolve
}
async function getElevatorsOfColumn({id}){
var elevators = await query_mysql(`SELECT e.id, e.column_id FROM elevators e JOIN columns c ON e.column_id = c.id WHERE c.id = "${id}"`)
resolve = elevators
return resolve
}
async function getElevators({email}){
var elevators = await query_mysql(
`SELECT e.id,
e.column_id,
e.type_of_building,
e.serial_number,
e.model,
e.status,
e.date_of_commissioning,
e.date_of_last_inspection,
e.certificate_of_operations,
e.information,
e.notes
FROM batteries a JOIN buildings b ON a.building_id = b.id JOIN columns d ON a.id = d.battery_id JOIN elevators e ON d.id = e.column_id JOIN customers c ON b.customer_id = c.id WHERE c.email = "${email}"`)
resolve = elevators
console.log(elevators)
return resolve
}
//To answer Question 2 by building id
async function getBuildings({id}) {
// Query building from the MySQL table
var buildings = await query_mysql('SELECT * FROM buildings WHERE id = ' + id )
resolve = buildings[0]
console.log(buildings)
// Query intervention from the PostgreSQL table
interventions = await query_postgresql('SELECT * FROM factintervention WHERE building_id = ' + id)
console.log(interventions)
//Query customer info from the MySQL table
customer = await query_mysql('SELECT * FROM customers WHERE id = ' + resolve.customer_id)
resolve['customer']= customer[0];
resolve['interventions']= interventions;
return resolve
};
// WORKS
async function getCustomerId({email}) {
//Query customer info from the MySQL table
customer = await query_mysql(`SELECT * FROM customers WHERE email = "${email}"`)
resolve= customer[0];
return resolve
};
async function getBuildingsOfCustomer({email}) {
// Query building from the MySQL table
var buildings = await query_mysql(`SELECT * FROM buildings b JOIN customers c ON b.customer_id = c.id WHERE c.email = "${email}"` )
resolve = buildings
console.log(buildings)
return resolve
};
//To answer Question 3 by employee id
async function getEmployees({id}) {
// Query employee from the MySQL table
var employees = await query_mysql('SELECT * FROM employees WHERE id = ' + id )
resolve = employees[0]
console.log(employees)
// Query intervention from the PostgreSQL table
interventions = await query_postgresql('SELECT * FROM factintervention WHERE employee_id = ' + id)
result = interventions[0]
console.log(interventions)
// Query building_details table in the MySQL DB to get key, value for each intervetion.
var j;
for (j = 0; j < interventions.length; j++){
building_details = await query_mysql('SELECT * FROM building_details WHERE building_id = ' + interventions[j].building_id)
interventions[j]['building_details']= building_details;
console.log(building_details)
}
resolve['interventions']= interventions;
return resolve
};
async function createIntervention({customer_id, building_id, battery_id, column_id, elevator_id, report, author_id}) {
intervention = await query_mysql(
"INSERT INTO interventions (report, created_at, updated_at, battery_id, column_id, elevator_id, building_id, customer_id ) VALUES('" + report +"', CURRENT_TIMESTAMP(), CURRENT_TIMESTAMP(),'" + battery_id +"','" + column_id +"','" + elevator_id +"','" + building_id + "','" + customer_id + "');"
);
intervStartShow = await query_mysql('SELECT * FROM interventions WHERE id = ' + intervention.insertId);
console.log(intervention)
resolve = intervStartShow[0];
return resolve
};
// Function used to query the MySQL operational DB BEGIN
function query_mysql (queryStr) {
console.log("-- > Run MySQL query : " + queryStr)
return new Promise((resolve, reject) => {
con_mysql.query(queryStr, function(err, result) {
if (err) {
return reject(err);
}
return resolve(result)
})
})
};
// Function used to query the MySQL DB END
// Function used to query the PostgreSQL decision DB BEGIN
function query_postgresql(queryStr) {
console.log("-- > Run PostgreSQL query : " + queryStr)
return new Promise((resolve, reject) => {
con_pg.query(queryStr, function(err, result) {
if (err) {
return reject(err);
}
return resolve(result.rows)
})
})
};
// Function used to query the PostgreSQL DB END
// Create an express server and a GraphQL endpoint
var app = express();
app.use('/graphql', graphqlHTTP({
schema: schema,
rootValue: root,
// Before prof, set to false to desactivate GraphiQL GUI on the route /graphql
graphiql: true
}));
//PORT as env variable so Heroku can set a port
const PORT = process.env.PORT || 4000;
app.listen(PORT, () => console.log(`-- > Express server started on port ${PORT}`));