diff --git a/server/controllers/attendance.controller.js b/server/controllers/attendance.controller.js new file mode 100644 index 00000000..64b62f83 --- /dev/null +++ b/server/controllers/attendance.controller.js @@ -0,0 +1,136 @@ +import db from "../database/db.js" + +const attendanceController = { + // @desc Insert a new attendance record for a scout in a certain sector + // @route POST /api/attendance/ + // @access Private + insertAttendance: async (req, res) => { + try { + const { scoutId, weekNumber, termNumber, attendanceStatus } = req.body + + // Get the scout info to check if the provided scout id exists or not + const scoutInfo = await db.query(` + SELECT * + FROM "Scout" + WHERE "scoutId" = $1; + `, + [scoutId] + ) + + if (scoutInfo.rowCount === 0) { + return res.status(404).json({ + error: "No scout exist with this id" + }) + } + + // Get the week info to check if the provided week & term numbers exist or not + const weekInfo = await db.query(` + SELECT * + FROM "Week" + WHERE "weekNumber" = $1 AND "termNumber" = $2; + `, + [weekNumber, termNumber]) + + if (weekInfo.rowCount === 0) { + return res.status(404).json({ + error: "Please enter an existing weekNumber & termNumber" + }) + } + + // Insert a new attendance record into the database + const result = await db.query(` + INSERT INTO "ScoutAttendance" VALUES ($1, $2, $3, $4) RETURNING *; + `, + [scoutId, weekNumber, termNumber, attendanceStatus]) + + // If insertion failed return an error + if (result.rowCount === 0) { + return res.status(500).json({ + error: "Insertion failed" + }) + } + + // Return a success message + res.status(200).json({ + message: "Successful insertion", + body: result.rows, + count: result.rowCount, + }) + + } catch (error) { + console.log(error) + res.status(500).json({ + error: 'An error occured while inserting a new attendance', + body: error, + }) + } + }, + + // @desc Get all attendance records for all the scouts in a certain sector in a certain week & term + // @route GET /api/attendance/sector/:baseName/:suffixName/:weekNumber/:termNumber + // @access Private + getSectorAttendance: async (req, res) => { + try { + const { baseName, suffixName, weekNumber, termNumber } = req.params + + const result = await db.query(` + SELECT "ScoutAttendance".* + FROM "Scout", "ScoutAttendance" + WHERE "Scout"."sectorBaseName" = $1 AND "Scout"."sectorSuffixName" = $2 + AND "ScoutAttendance"."weekNumber" = $3 AND "ScoutAttendance"."termNumber" = $4 + AND "ScoutAttendance"."scoutId" = "Scout"."scoutId"; + `, + [baseName, suffixName, weekNumber, termNumber]) + + if (result.rowCount === 0) { + return res.status(404).json({ + error: "No data exists for the provided info" + }) + } + + res.status(200).json({ + message: "Successful retrieval", + body: result.rows, + count: result.rowCount + }) + + } catch (error) { + console.log(error) + res.status(500).json({ + error: 'An error occured while retrieving sector attendance', + body: error, + }) + } + }, + + // @desc Get attendance records for a certain scout in a certain week & term + // @route GET /api/attendance/:scoutId/:weekNumber/:termNumber + // @access Private + getScoutAttendance: async (req, res) => { + try { + const { scoutId, weekNumber, termNumber } = req.params + + const result = await db.query(` + SELECT * + FROM "ScoutAttendance" + WHERE "scoutId" = $1 AND "weekNumber" = $2 AND "termNumber" = $3 + `, + [scoutId, weekNumber, termNumber]) + + res.status(200).json({ + message: "Successful retrieval", + body: result.rows, + count: result.rowCount, + }) + + } catch (error) { + console.log(error) + res.status(500).json({ + error: 'An error occured while retrieving sector attendance', + body: error, + }) + } + } +} + +export default attendanceController; \ No newline at end of file diff --git a/server/controllers/auth.controller.js b/server/controllers/auth.controller.js index a1c2c0f5..aab029a6 100644 --- a/server/controllers/auth.controller.js +++ b/server/controllers/auth.controller.js @@ -24,7 +24,7 @@ const authController = { `SELECT "email", "password" FROM "Captain" WHERE "email" = $1;`, - [email] + [email.toLowerCase()] ) if (captain.rows.length) { return res.status(400).json({ error: 'Email is taken!!' }) @@ -42,7 +42,7 @@ const authController = { middleName, lastName, phoneNumber, - email, + email.toLowerCase(), hashedPassword, gender, 'regular', @@ -79,7 +79,7 @@ const authController = { `SELECT * FROM "Captain" WHERE "email" = $1;`, - [email] + [email.toLowerCase()] ) if (!result.rows.length) { return res.status(400).json({ diff --git a/server/controllers/captain.controller.js b/server/controllers/captain.controller.js index 60676b3e..cc4609fe 100644 --- a/server/controllers/captain.controller.js +++ b/server/controllers/captain.controller.js @@ -89,14 +89,14 @@ const captainController = { getCaptain: async (req, res) => { try { // Extract the captain ID from the request params - const { id } = req.params + const { captainId } = req.params // Query on the database to get that captain info const result = await db.query( `SELECT * FROM "Captain" WHERE "captainId" = $1`, - [id] + [captainId] ) // If captain doesn't exist return an error message @@ -124,10 +124,10 @@ const captainController = { // @access Private setCaptainType: async (req, res) => { try { - const { id } = req.params + const { captainId } = req.params const { type } = req.body - if (!id) { + if (!captainId) { return res.status(400).json({ error: "Please enter a valid id", }) @@ -146,7 +146,7 @@ const captainController = { WHERE "captainId" = $1 RETURNING * `, - [id, type]) + [captainId, type]) res.status(200).json({ message: "Successful update", diff --git a/server/controllers/sector.controller.js b/server/controllers/sector.controller.js index 8b27335c..47a5677b 100644 --- a/server/controllers/sector.controller.js +++ b/server/controllers/sector.controller.js @@ -30,8 +30,13 @@ const sectorController = { // @access Private getSector: async (req, res) => { try { - const { baseName, suffixName } = req.params - + let { baseName, suffixName } = req.params + + // If the suffix name wasn't provided (meaning it would be :suffixName) then make it an empty string + if (suffixName === ':suffixName') { + suffixName = "" + } + const result = await db.query( ` SELECT * @@ -64,7 +69,17 @@ const sectorController = { // @access Private insertSector: async (req, res) => { try { - const { baseName, suffixName, unitCaptainId } = req.body + let { baseName, suffixName, unitCaptainId } = req.body + + if (!baseName) { + return res.status(404).json({ + error: "You must insert a baseName for the sector" + }) + } + + if (!suffixName) { + suffixName = "" + } const result = await db.query( ` diff --git a/server/routes/api.route.js b/server/routes/api.route.js index c776e688..46c4da97 100644 --- a/server/routes/api.route.js +++ b/server/routes/api.route.js @@ -8,6 +8,7 @@ import captainRouter from './captain.route.js' import alertRouter from './alert.route.js' import scoutRouter from './scout.route.js' import sectorRouter from './sector.route.js' +import attendanceRouter from './attendance.route.js' const apiRouter = Router() apiRouter.use('/auth', authRouter) @@ -18,5 +19,6 @@ apiRouter.use('/captain', authMiddleware, captainRouter) apiRouter.use('/alert', authMiddleware, alertRouter) apiRouter.use('/scout', authMiddleware, scoutRouter) apiRouter.use('/sector', authMiddleware, sectorRouter) +apiRouter.use('/attendance', authMiddleware, attendanceRouter) export default apiRouter diff --git a/server/routes/attendance.route.js b/server/routes/attendance.route.js new file mode 100644 index 00000000..44b6b1a7 --- /dev/null +++ b/server/routes/attendance.route.js @@ -0,0 +1,11 @@ +import { Router } from "express" +import attendanceController from "../controllers/attendance.controller.js" + +const attendanceRouter = Router() + +// Insert a new attendance for a scout +attendanceRouter.post('/', attendanceController.insertAttendance) +attendanceRouter.get('/sector/:baseName/:suffixName/:weekNumber/:termNumber', attendanceController.getSectorAttendance) +attendanceRouter.get('/:scoutId/:weekNumber/:termNumber', attendanceController.getScoutAttendance) + +export default attendanceRouter; \ No newline at end of file diff --git a/server/routes/captain.route.js b/server/routes/captain.route.js index d7d88d9b..189f6a98 100644 --- a/server/routes/captain.route.js +++ b/server/routes/captain.route.js @@ -9,7 +9,7 @@ captainRouter.get( '/sector/:baseName/:suffixName', captainController.getCaptainsInSector ) -captainRouter.get('/:id', captainController.getCaptain) -captainRouter.patch('/:id', captainController.setCaptainType) +captainRouter.get('/:captainId', captainController.getCaptain) +captainRouter.patch('/:captainId', captainController.setCaptainType) export default captainRouter diff --git a/server/routes/scout.route.js b/server/routes/scout.route.js index 428282e0..c528fbbb 100644 --- a/server/routes/scout.route.js +++ b/server/routes/scout.route.js @@ -4,8 +4,8 @@ import scoutController from '../controllers/scout.controller.js' const scoutRouter = Router() scoutRouter.post('/', scoutController.insertScout) -scoutRouter.get('/:id', scoutController.getScout) -scoutRouter.put('/:id', scoutController.updateScout) +scoutRouter.get('/:scoutId', scoutController.getScout) +scoutRouter.put('/:scoutId', scoutController.updateScout) scoutRouter.get('/', scoutController.getAllScouts) scoutRouter.get('/unit/:unitCaptainId', scoutController.getScoutsInUnit) scoutRouter.get(