Skip to content

Commit

Permalink
added the others
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastián committed Jan 16, 2025
1 parent 0146c1d commit 9f89590
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 2 deletions.
31 changes: 31 additions & 0 deletions api/email_id.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const express = require('express');
const router = express.Router();
const { connectToDatabase } = require('../db/dbConfig');

// Route to get employee_id from email
router.get('/:email', async (req, res) => {
const { email } = req.params;
try {
const pool = await connectToDatabase();
const result = await pool.request()
.input('email', email)
.query(`
SELECT employee_id
FROM roster
WHERE email_surgical = @email
`);

if (result.recordset.length === 0) {
console.log(`No employee found for email: ${email}`);
return res.status(404).json({ message: 'No employee found with this email' });
}

res.json(result.recordset[0]); // Return the first matching record
} catch (err) {
console.error(`Error fetching employee ID for email ${email}:`, err);
res.status(500).json({ message: 'Server error', error: err.message });
}
});

// Export the router
module.exports = router;
43 changes: 43 additions & 0 deletions api/employees_by_leader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const express = require('express');
const router = express.Router();
const { connectToDatabase } = require('../db/dbConfig');

// Route to fetch all employees under a specific leader
router.get('/:leader_email', async (req, res) => {
const { leader_email } = req.params;

try {
const pool = await connectToDatabase();

// Query using a recursive CTE to get hierarchical employees
const result = await pool.request()
.input('leader_email', leader_email)
.query(`
WITH RecursiveEmployees AS (
SELECT *
FROM dbo.roster
WHERE leader_email = @leader_email
UNION ALL
SELECT r.*
FROM dbo.roster r
INNER JOIN RecursiveEmployees re
ON r.leader_email = re.email_surgical
)
SELECT *
FROM RecursiveEmployees
ORDER BY name ASC;
`);

if (result.recordset.length === 0) {
console.log(`No employees found under leader email: ${leader_email}`);
return res.status(404).json({ message: 'No employees found under this leader' });
}

res.json(result.recordset); // Return hierarchical records
} catch (err) {
console.error(`Error fetching employees under leader email ${leader_email}:`, err);
res.status(500).json({ message: 'Server error', error: err.message });
}
});

module.exports = router;
Empty file removed api/vacations_by_employee.js
Empty file.
34 changes: 34 additions & 0 deletions api/vacations_by_leader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const express = require('express');
const router = express.Router();
const { connectToDatabase } = require('../db/dbConfig');


router.get('/:employee_id', async (req, res) => {
const { employee_id } = req.params;
try {
const pool = await connectToDatabase();
const result = await pool.request()

.input('employee_id', employee_id)
.query(`
SELECT *
FROM vacations
WHERE employee_id = @employee_id
`);

if (result.recordset.length === 0) {
// Log if no records are found
console.log(`No vacation data found for employee ID: ${employee_id}`);
return res.status(404).json({ message: 'No vacation data found for this employee' });
}

res.json(result.recordset[0]); // Return the first matching record
} catch (err) {
// Log detailed error for debugging
console.error(`Error fetching vacation data for employee ID ${employee_id}:`, err);
res.status(500).json({ message: 'Server error', error: err.message });
}
});


module.exports = router;
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ require('dotenv').config();
// routes
const employeesInfoRoute = require('./routes/employeesInfo');
const vacationsInfoRoute = require('./routes/vacationsInfo');
const email_id = require('./routes/email_id');
const requestsInfoRoute = require('./routes/requestsInfo');
const liquidationRequestsInfo = require('./routes/liquidationRequestsInfo');
const api_liquidationRequestsInfo = require('./routes/api_liquidationRequestsInfo');
Expand All @@ -19,11 +18,13 @@ const cancelRequestRoute = require('./routes/cancelRequestRoute');
const updateEmployee = require('./routes/employeeUpdateRoute');
const addEmployee = require('./routes/employeeAddRoute');
const deleteEmployee = require('./routes/EmployeeRemoveRoute');
const employeesByLeader = require('./routes/employeesByLeader');

const photoRoute = require('./routes/photoRoute');
const employee_endpoint = require('./api/employee/routes');
const request_endpoint = require('./api/request/routes')
const liquidation_request_endpoint = require('./api/liquidation_request/routes')
const email_id = require('./api/email_id');
const employeesByLeader = require('./api/employeesByLeader');

const app = express();
const port = process.env.PORT || 8080;
Expand Down

0 comments on commit 9f89590

Please sign in to comment.