This repository has been archived by the owner on Mar 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
331 lines (289 loc) · 13.3 KB
/
server.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
const express = require('express');
const bodyParser = require('body-parser');
const mysql = require('mysql2');
const path = require('path');
require('dotenv').config();
// Create an Express application
const app = express();
// Middleware to parse JSON bodies
app.use(bodyParser.json());
// Set up the MySQL connection
const pool = mysql.createPool({
host: process.env.SERVER_HOST,
user: process.env.SERVER_USER, // Your MySQL username
password: process.env.SERVER_PASSWORD, // Your MySQL password
database: 'kokua', // Your MySQL database name
port: process.env.SERVER_PORT,
waitForConnections: true,
connectionLimit: 10,
queueLimit: 0
});
// Test MySQL connection
pool.getConnection((err, connection) => {
if (err) {
console.error('Error connecting to the database:', err);
return;
}
console.log('Successfully connected to the database');
connection.release();
});
//------------------------------Persona de apoyo------------------------------------------------------------------
app.get('/api/persona-apoyo/contactos:id', (req, res) => {
const idPersonaApoyo = req.params.id;
const query = 'SELECT Distinct pacientes.IDPaciente, pacientes.Nombre, pacientes.Apellido, pacientes.Padecimento, pacientes.EstatusPaciente FROM pacientes, asignaciones, personasapoyo WHERE pacientes.IDPaciente = asignaciones.IDPaciente AND asignaciones.IDPersonaApoyo = ?';
pool.query(query, [idPersonaApoyo], (error, results, fields) => {
if (error) {
return res.status(500).json({ error: 'Database query error' });
}
res.json({ data: results });
});
});
app.get('/api/persona-apoyo/user:id', (req, res) => {
const idPersonaApoyo = req.params.id;
const query = 'SELECT Nombre from personasapoyo where IDPersonaApoyo = ?';
pool.query(query, [idPersonaApoyo], (error, results, fields) => {
if (error) {
return res.status(500).json({ error: 'Database query error' });
}
res.json({ data: results });
});
});
//------------------------------Paciente------------------------------------------------------------------
//-----------------------------Citas-------------------------------------------------------------
app.get('/api/paciente/appointments/:id', (req, res) => {
const idPersonaApoyo = req.params.id;
console.log(idPersonaApoyo);
const query = 'Select Distinct citas.IDCita, citas.IDPaciente, doctores.IDDoctor, doctores.Nombre, doctores.Apellido, citas.Fecha, citas.TipoCita, citas.EstatusCita from doctores, citas where doctores.IDDoctor = citas.IDDoctor and citas.IDPaciente = ?';
pool.query(query, [idPersonaApoyo], (error, results, fields) => {
if (error) {
return res.status(500).json({ error: 'Database query error' });
}
res.json({ data: results });
});
});
app.post('/api/paciente/appointments', (req, res) => {
const nuevaCita = req.body; // Datos de la nueva cita
console.log(nuevaCita.IDPaciente);
const query = 'INSERT INTO citas (IDPaciente, IDDoctor, TipoCita, EstatusCita, Fecha) VALUES (?, ?, ?, ?, ?)';
pool.query(query, [nuevaCita.IDPaciente, nuevaCita.IDDoctor, nuevaCita.TipoCita, nuevaCita.EstatusCita, nuevaCita.Fecha], (error, results, fields) => {
if (error) {
return res.status(500).json({ error: 'Error al insertar la cita' });
}
// Enviar una respuesta con el ID de la cita insertada
res.json({ message: 'Cita insertada con éxito', insertedId: results.insertId });
});
});
app.delete('/api/paciente/appointments/:id', (req, res) => {
const idCita = req.params.id;
const query = 'DELETE FROM citas WHERE IDCita = ?';
pool.query(query, [idCita], (error, results, fields) => {
if (error) {
return res.status(500).json({ error: 'Error al eliminar la cita' });
}
if (results.affectedRows === 0) {
return res.status(404).json({ message: 'Cita no encontrada' });
}
res.json({ message: 'Cita eliminada con éxito', deletedId: idCita });
});
});
//-----------------------------Chat-------------------------------------------------------------
app.get('/api/paciente/contactos:id', (req, res) => {
const idPaciente = req.params.id;
const query = 'SELECT Distinct doctores.IDDoctor, personasapoyo.IDPersonaApoyo, doctores.Nombre as NombreDoctor, doctores.Apellido as ApellidoDoctor, personasapoyo.Nombre as PersonaApoyoNombre, personasapoyo.Apellido as PersonaApoyoApellido FROM pacientes, asignaciones, personasapoyo, doctores WHERE asignaciones.IDDoctor = doctores.IDDoctor AND personasapoyo.IDPersonaApoyo = asignaciones.IDPersonaApoyo AND asignaciones.IDPaciente = ?';
pool.query(query, [idPaciente], (error, results, fields) => {
if (error) {
return res.status(500).json({ error: 'Database query error' });
}
res.json({ data: results });
});
});
app.get('/api/paciente/user:id', (req, res) => {
const idPersonaApoyo = req.params.id;
const query = 'SELECT Nombre from pacientes where IDPaciente = ?';
pool.query(query, [idPersonaApoyo], (error, results, fields) => {
if (error) {
return res.status(500).json({ error: 'Database query error' });
}
res.json({ data: results });
});
});
//-----------------------------ServiciosExtra-------------------------------------------------------------
app.get('/api/paciente/ServiciosExtra', (req, res) => {
const query = 'SELECT * from serviciosextra, empresasterceras where serviciosextra.IDEmpresaTercera = empresasterceras.idEmpresaTercera';
pool.query(query, (error, results, fields) => {
if (error) {
return res.status(500).json({ error: 'Database query error' });
}
res.json({ data: results });
});
});
//----------------------------Seguimiendo medico---------------------------------------------------------------
app.get('/api/paciente/Seguimiento:id', (req, res) => {
const idPersonaApoyo = req.params.id;
const query = `Select distinct
pacientes.Nombre as NombrePaciente, doctores.Nombre as NombreDoctor, personasapoyo.Nombre as NombrePersonaApoyo,
citas.Fecha as FechaCita, citas.EstatusCita, citas.TipoCita, receta.NombreMedicina, aseguradoras.NombreAseguradora,
asignaciones.FechaAsignacion, receta.CantidadDiaria, receta.Indicaciones
from
pacientes, doctores, personasapoyo, citas, receta, aseguradoras, asignaciones, polizas
where
pacientes.IDPaciente = asignaciones.IDPaciente AND
pacientes.IDPaciente = citas.IDPaciente AND
asignaciones.IDDoctor = doctores.IDDoctor AND
asignaciones.IDPersonaApoyo = personasapoyo.IDPersonaApoyo AND
citas.IDCita = receta.IDCita AND
asignaciones.IDPoliza = polizas.IDPoliza AND
polizas.IDAseguradora = aseguradoras.IDAseguradora AND
pacientes.IDPaciente = ?`;
pool.query(query, [idPersonaApoyo], (error, results, fields) => {
if (error) {
return res.status(500).json({ error: 'Database query error' });
}
res.json({ data: results });
});
});
//------------------------------------------------Doctor--------------------------------
//-----------------------------Citas-------------------------------------------------------------
app.get('/api/doctor/appointments/:id', (req, res) => {
const idDoctor = req.params.id;
console.log(idDoctor);
const query = 'Select Distinct citas.IDCita, citas.IDPaciente, doctores.IDDoctor, pacientes.Nombre, pacientes.Apellido, citas.Fecha, citas.TipoCita, citas.EstatusCita from doctores, citas, pacientes where doctores.IDDoctor = citas.IDDoctor AND citas.IDPaciente = pacientes.IDPaciente AND citas.IDDoctor = ?';
pool.query(query, [idDoctor], (error, results, fields) => {
if (error) {
return res.status(500).json({ error: 'Database query error' });
}
res.json({ data: results });
});
});
app.post('/api/doctor/appointments', (req, res) => {
const nuevaCita = req.body; // Datos de la nueva cita
console.log(nuevaCita.IDPaciente);
const query = 'INSERT INTO citas (IDPaciente, IDDoctor, TipoCita, EstatusCita, Fecha) VALUES (?, ?, ?, ?, ?)';
pool.query(query, [nuevaCita.IDPaciente, nuevaCita.IDDoctor, nuevaCita.TipoCita, nuevaCita.EstatusCita, nuevaCita.Fecha], (error, results, fields) => {
if (error) {
return res.status(500).json({ error: 'Error al insertar la cita' });
}
// Enviar una respuesta con el ID de la cita insertada
res.json({ message: 'Cita insertada con éxito', insertedId: results.insertId });
});
});
app.delete('/api/doctor/appointments/:id', (req, res) => {
const idCita = req.params.id;
const query = 'DELETE FROM citas WHERE IDCita = ?';
pool.query(query, [idCita], (error, results, fields) => {
if (error) {
return res.status(500).json({ error: 'Error al eliminar la cita' });
}
if (results.affectedRows === 0) {
return res.status(404).json({ message: 'Cita no encontrada' });
}
res.json({ message: 'Cita eliminada con éxito', deletedId: idCita });
});
});
//-----------------------------------------------Informacion doctor-----------
app.get('/api/doctor/profile/:id', (req, res) => {
const idDoctor = req.params.id;
console.log(idDoctor);
const query = 'SELECT * from doctores where IDDoctor = ?';
pool.query(query, [idDoctor], (error, results, fields) => {
if (error) {
return res.status(500).json({ error: 'Database query error' });
}
res.json({ data: results });
});
});
//------------------------------------------------Pacientes---------------------------
app.get('/api/doctor/patients/:id', (req, res) => {
const idDoctor = req.params.id;
console.log(idDoctor);
const query = 'SELECT pacientes.IDPaciente, pacientes.Nombre FROM pacientes, doctores, asignaciones WHERE pacientes.IDPaciente = asignaciones.IDPaciente AND asignaciones.IDDoctor = doctores.IDDoctor AND doctores.IDDoctor = ?';
pool.query(query, [idDoctor], (error, results, fields) => {
if (error) {
return res.status(500).json({ error: 'Database query error' });
}
res.json({ data: results });
});
});
//----------------------------------------------Seguimiento_Update------------------
app.post('/api/doctor/seguimiento/newReceta', (req, res) => {
const nuevaCita = req.body; // Datos de la nueva cita
console.log(nuevaCita.IDPaciente);
const query = 'INSERT INTO receta (IDCita, IDMedicina, CantidadDiaria, Indicaciones, Diagnostico, NombreMedicina) VALUES (?, ?, ?, ?, ?, ?)';
pool.query(query, [nuevaCita.IDCita, nuevaCita.IDMedicina, nuevaCita.CantidadDiaria, nuevaCita.Indicaciones, nuevaCita.Diagnostico, nuevaCita.NombreMedicina], (error, results, fields) => {
if (error) {
return res.status(500).json({ error: 'Error al insertar la cita' });
}
// Enviar una respuesta con el ID de la cita insertada
res.json({ message: 'Cita insertada con éxito', insertedId: results.insertId });
});
});
app.get('/api/doctor/medicina/:name', (req, res) => {
const idDoctor = req.params.name;
console.log(idDoctor);
const query = 'SELECT IDMedicina FROM medicinas WHERE medicinas.NombreMedicina = ?';
pool.query(query, [idDoctor], (error, results, fields) => {
if (error) {
return res.status(500).json({ error: 'Database query error' });
}
res.json({ data: results });
});
});
app.patch('/api/doctor/cita/modificar/:id', async (req, res) => {
const idCita = req.params.id; // Obtener el ID de la cita de los parámetros de la URL
try {
// Actualizar el estatus de la cita en la base de datos
const query = 'UPDATE citas SET EstatusCita = ? WHERE IDCita = ?';
const result = await pool.query(query, ['Realizada', idCita]);
if (result.affectedRows === 0) {
// No se encontró la cita con ese ID
return res.status(404).send('Cita no encontrada');
}
res.send('Cita actualizada con éxito');
} catch (error) {
console.error('Error al actualizar la cita:', error);
res.status(500).send('Error al procesar la solicitud');
}
});
//----------------------------------------------Chat----------------------------------
app.get('/api/persona-apoyo/chat/:id', (req, res) => {
const idPersona = req.params.id;
console.log(idPersona);
const query = 'SELECT * FROM mensajes WHERE id_emisor = ? or id_receptor = ?';
pool.query(query, [idPersona, idPersona], (error, results, fields) => {
if (error) {
return res.status(500).json({ error: 'Database query error' });
}
res.json({ data: results });
});
});
app.post('/api/persona-apoyo/chat/newMensaje', (req, res) => {
const nuevaCita = req.body; // Datos de la nueva cita
console.log(nuevaCita.IDPaciente);
const query = 'INSERT INTO Mensajes (id_receptor, id_emisor, Mensaje, tipo_emisor, tipo_receptor) VALUES (?, ?, ?, ?, ?)';
pool.query(query, [nuevaCita.id_receptor, nuevaCita.id_emisor, nuevaCita.Mensaje, nuevaCita.tipo_emisor, nuevaCita.tipo_receptor], (error, results, fields) => {
if (error) {
return res.status(500).json({ error: 'Error al insertar la cita' });
}
// Enviar una respuesta con el ID de la cita insertada
res.json({ message: 'Cita insertada con éxito', insertedId: results.insertId });
});
});
//-----------------------------------------------Fin back------------------------------
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public/login.html'));
});
app.get('/:url(*)', (req, res) => {
let url = req.params.url
console.log(url)
res.sendFile(path.join(__dirname, 'public/' + url));
});
app.get('*', (req, res) => {
let url = req.params.url
console.log(url)
res.send("Error");
});
// Start the server
const PORT = process.env.PORT;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});