First part of backend rework
- Added the base data structure for the new database - Added the new routes for the new database - Reworked the users endpoints
This commit is contained in:
156
routes/hospitals.js
Normal file
156
routes/hospitals.js
Normal file
@@ -0,0 +1,156 @@
|
||||
import express from 'express';
|
||||
import { pool } from '../modules/databaseManager';
|
||||
import { verifyToken } from '../modules/tokenManager';
|
||||
import { checkPermissions, checkBanned } from '../modules/permissionManager';
|
||||
import { respondWithStatus, respondWithStatusJSON } from '../modules/requestHandler';
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
router.get('/', verifyToken, checkBanned, checkPermissions('hospitals', 1), async (req, res) => {
|
||||
try {
|
||||
const [rows] = await pool.execute('SELECT * FROM hospitals WHERE 1');
|
||||
|
||||
if (rows.length === 0) {
|
||||
return await respondWithStatus(res, 404, 'Hospitals not found');
|
||||
}
|
||||
return await respondWithStatusJSON(res, 200, rows);
|
||||
}
|
||||
catch (err) {
|
||||
console.error(err);
|
||||
return await respondWithStatus(res, 500, 'An error has occured');
|
||||
}
|
||||
});
|
||||
|
||||
router.post('/', verifyToken, checkBanned, checkPermissions('hospitals', 2), async (req, res) => {
|
||||
const { company_id, name, code, country, region, city, address } = req.body;
|
||||
if ([ company_id, name, code, country, region, city, address ].every(Boolean)) {
|
||||
try {
|
||||
const [result] = await pool.execute(
|
||||
'INSERT INTO hospitals (company_id, name, code, country, region, city, address) VALUES (?, ?, ?, ?, ?, ?, ?)',
|
||||
[ company_id, name, code, country, region, city, address ],
|
||||
);
|
||||
if (result.affectedRows === 0) {
|
||||
return await respondWithStatus(res, 500, 'Error storing hospital');
|
||||
}
|
||||
return await respondWithStatus(res, 200, 'Hospital created successfully');
|
||||
}
|
||||
catch (err) {
|
||||
console.error(err);
|
||||
return await respondWithStatus(res, 500, 'An error has occured');
|
||||
}
|
||||
}
|
||||
else {
|
||||
return await respondWithStatus(res, 400, 'Missing fields');
|
||||
}
|
||||
});
|
||||
|
||||
router.get('/:hospitalId', verifyToken, checkBanned, checkPermissions('hospitals', 1), async (req, res) => {
|
||||
try {
|
||||
const id = req.params.hospitalId;
|
||||
const [rows] = await pool.execute('SELECT * FROM hospitals WHERE id = ? LIMIT 1', [id]);
|
||||
|
||||
if (rows.length === 0) {
|
||||
return await respondWithStatus(res, 404, 'Hospitals not found');
|
||||
}
|
||||
return await respondWithStatusJSON(res, 200, rows[0]);
|
||||
}
|
||||
catch (err) {
|
||||
console.error(err);
|
||||
return await respondWithStatus(res, 500, 'An error has occured');
|
||||
}
|
||||
});
|
||||
|
||||
router.patch('/:hospitalId', verifyToken, checkBanned, checkPermissions('hospitals', 2), async (req, res) => {
|
||||
try {
|
||||
const id = req.params.hospitalId;
|
||||
const { type, value } = req.body;
|
||||
const [rows] = await pool.execute('SELECT * FROM hospitals WHERE id = ? LIMIT 1', [id]);
|
||||
|
||||
if (rows.length === 0) {
|
||||
return await respondWithStatus(res, 404, 'Hospital not found');
|
||||
}
|
||||
const fields = rows.map(row => Object.keys(row));
|
||||
if (fields[0].includes(type)) {
|
||||
const [result] = await pool.execute(`UPDATE hospitals SET ${type} = ? WHERE id = ?`, [value, id]);
|
||||
|
||||
if (result.affectedRows === 0) {
|
||||
return await respondWithStatus(res, 500, 'Error updating hospital');
|
||||
}
|
||||
return await respondWithStatus(res, 200, 'Hospital updated successfully');
|
||||
}
|
||||
else {
|
||||
return await respondWithStatus(res, 400, 'Invalid type');
|
||||
}
|
||||
}
|
||||
catch (err) {
|
||||
console.error(err);
|
||||
return await respondWithStatus(res, 500, 'An error has occured');
|
||||
}
|
||||
});
|
||||
|
||||
router.put('/:hospitalId', verifyToken, checkBanned, checkPermissions('hospitals', 2), async (req, res) => {
|
||||
const id = req.params.hospitalId;
|
||||
const { company_id, name, code, country, region, city, address } = req.body;
|
||||
if ([company_id, name, code, country, region, city, address].every(Boolean)) {
|
||||
try {
|
||||
const [rows] = await pool.execute('SELECT * FROM hospitals WHERE id = ? LIMIT 1', [id]);
|
||||
|
||||
if (rows.length === 0) {
|
||||
return await respondWithStatus(res, 404, 'Hospital not found');
|
||||
}
|
||||
const [result] = await pool.execute(
|
||||
'UPDATE hospitals SET company_id = ?, name = ?, country = ?, region = ?, city = ?, address = ? WHERE id = ?',
|
||||
[company_id, name, code, country, region, city, address, id],
|
||||
);
|
||||
|
||||
if (result.affectedRows === 0) {
|
||||
return await respondWithStatus(res, 500, 'Error updating hospital');
|
||||
}
|
||||
return await respondWithStatus(res, 200, 'Hospital updated successfully');
|
||||
}
|
||||
catch (err) {
|
||||
console.error(err);
|
||||
return await respondWithStatus(res, 500, 'An error has occured');
|
||||
}
|
||||
}
|
||||
else {
|
||||
return await respondWithStatus(res, 400, 'Missing fields');
|
||||
}
|
||||
});
|
||||
|
||||
router.delete('/:hospitalId', verifyToken, checkBanned, checkPermissions('hospitals', 4), async (req, res) => {
|
||||
try {
|
||||
const id = req.params.hospitalId;
|
||||
const [rows] = await pool.execute('SELECT * FROM hospitals WHERE id = ? LIMIT 1', [id]);
|
||||
|
||||
if (rows.length === 0) {
|
||||
return await respondWithStatus(res, 404, 'Hospital not found');
|
||||
}
|
||||
|
||||
const [result] = await pool.execute('DELETE FROM hospitals WHERE id = ?', [id]);
|
||||
|
||||
if (result.affectedRows === 0) {
|
||||
return await respondWithStatus(res, 500, 'Error removing hospital');
|
||||
}
|
||||
return await respondWithStatus(res, 200, 'Hospital deleted successfully');
|
||||
}
|
||||
catch (err) {
|
||||
console.error(err);
|
||||
return await respondWithStatus(res, 500, 'An error has occured');
|
||||
}
|
||||
});
|
||||
|
||||
// Doctor endpoints
|
||||
// GET all doctors in a hospital
|
||||
// POST a new doctor to a hospital
|
||||
// DELETE a doctor from a hospital
|
||||
|
||||
// Service endpoints
|
||||
// GET all services in a hospital
|
||||
|
||||
// Room endpoints
|
||||
// GET all rooms in a hospital
|
||||
// POST a new room to a hospital
|
||||
// DELETE a room from a hospital
|
||||
|
||||
export default router;
|
||||
Reference in New Issue
Block a user