diff --git a/server/controllers/alert.controller.js b/server/controllers/alert.controller.js index 67ea4deb..e653a707 100644 --- a/server/controllers/alert.controller.js +++ b/server/controllers/alert.controller.js @@ -57,14 +57,28 @@ const alertController = { const { id } = req.params const { sectorBaseName, sectorSuffixName } = req.body + // what if alert is already sent? + let result + result = await db.query( + `SELECT EXISTS ( + (SELECT 1 + FROM "Notification" + WHERE "notificationId" = $1) + ) AS exist;`, + [id] + ) + if (!result.rows[0].exist) { + return res.status(404).json({ error: 'Alert not found' }) + } + if (!sectorBaseName || !sectorSuffixName) { // send alert to all captains result = await db.query( `INSERT INTO "RecieveNotification" ("notificationId", "captainId", "status") - SELECT $1, C."captainId", 'unread' - FROM "Captain" AS C + (SELECT $1::integer, C."captainId", 'unread'::"NotificationStatus" + FROM "Captain" AS C) RETURNING *;`, [id] ) @@ -72,10 +86,15 @@ const alertController = { // send alert to all captains in sector result = await db.query( `INSERT INTO "RecieveNotification" ("notificationId", "captainId", "status") - SELECT $1, C."captainId", 'unread' + ((SELECT $1::integer, C."captainId", 'unread'::"NotificationStatus" FROM "Captain" AS C WHERE C."rSectorBaseName" = $2 AND - C."rSectorSuffixName" = $3 + C."rSectorSuffixName" = $3) + UNION + (SELECT $1::integer, S."unitCaptainId", 'unread'::"NotificationStatus" + FROM "Sector" AS S + WHERE S."baseName" = $2 AND + S."suffixName" = $3)) RETURNING *;`, [id, sectorBaseName, sectorSuffixName] ) @@ -83,7 +102,7 @@ const alertController = { res.status(200).json({ message: 'Alert successfully sent', - body: result.rows[0], + body: result.rows, }) } catch (error) { console.error(error) diff --git a/server/controllers/sector.controller.js b/server/controllers/sector.controller.js index 9c32559f..e4af6b76 100644 --- a/server/controllers/sector.controller.js +++ b/server/controllers/sector.controller.js @@ -8,11 +8,11 @@ const sectorController = { try { const result = await db.query(` SELECT * - FROM "Sector" + FROM "Sector"; `) res.status(200).json({ - message: "Successful retrieval", + message: 'Successful retrieval', body: result.rows, count: result.rowCount, }) @@ -32,24 +32,25 @@ const sectorController = { try { const { baseName, suffixName } = req.params - const result = await db.query(` + const result = await db.query( + ` SELECT * FROM "Sector" WHERE "baseName" = $1 AND "suffixName" = $2; `, - [baseName, suffixName]); + [baseName, suffixName] + ) if (result.rowCount === 0) { return res.status(404).json({ - error: "No sector found with this name" + error: 'No sector found with this name', }) } res.status(200).json({ - message: "Successful retrieval", + message: 'Successful retrieval', body: result.rows, }) - } catch (error) { console.log(error) res.status(500).json({ @@ -65,17 +66,18 @@ const sectorController = { try { const { baseName, suffixName, unitCaptainId } = req.body - const result = await db.query(` + const result = await db.query( + ` INSERT INTO "Sector" VALUES ($1, $2, $3) - RETURNING * + RETURNING *; `, - [baseName, suffixName, unitCaptainId]) + [baseName, suffixName, unitCaptainId] + ) res.status(200).json({ - message: "Successful insertion", + message: 'Successful insertion', body: result.rows, }) - } catch (error) { console.log(error) res.status(500).json({ @@ -90,35 +92,37 @@ const sectorController = { // @access Private setUnitCaptain: async (req, res) => { try { - const { id, baseName, suffixName } = req.params + const { baseName, suffixName } = req.params + const { unitCaptainId } = req.body - if (!id || (!baseName && !suffixName)) { + if (!unitCaptainId) { res.status(400).json({ - error: "Please enter valid ids" + error: 'Please enter valid ids', }) } - const result = await db.query(` + const result = await db.query( + ` UPDATE "Sector" SET "unitCaptainId" = $1 WHERE "baseName" = $2 AND "suffixName" = $3 - RETURNING * + RETURNING *; `, - [id, baseName, suffixName]) + [unitCaptainId, baseName, suffixName] + ) res.status(200).json({ - message: "Successful update", + message: 'Successful update', body: result.rows, }) - } catch (error) { console.log(error) res.status(500).json({ error: 'An error occured while retrieving the captains info', body: error, }) - } - } + } + }, } -export default sectorController; \ No newline at end of file +export default sectorController diff --git a/server/controllers/term.controller.js b/server/controllers/term.controller.js index 7f853d42..7c0ff060 100644 --- a/server/controllers/term.controller.js +++ b/server/controllers/term.controller.js @@ -26,16 +26,11 @@ const termController = { const currentDate = new Date() const startDateObj = new Date(startDate) const endDateObj = new Date(endDate) - console.log(currentDate) - console.log(endDateObj) if (startDateObj >= endDateObj || endDateObj < currentDate) { return res.status(400).json({ error: 'Invalid dates', }) } - - const termNumber = req.currentTerm.termNumber + 1 - if ( req.currentTerm.termNumber && req.currentTerm.endDate >= startDateObj @@ -45,6 +40,7 @@ const termController = { }) } + const termNumber = req.currentTerm.termNumber + 1 const result = await db.query( `INSERT INTO "Term" VALUES ($1, $2, $3, $4) RETURNING *;`, @@ -72,7 +68,36 @@ const termController = { try { const { termName, startDate, endDate } = req.body - const result = await db.query( + const currentDate = new Date() + const startDateObj = new Date(startDate) + const endDateObj = new Date(endDate) + if (startDateObj >= endDateObj || endDateObj < currentDate) { + return res.status(400).json({ + error: 'Invalid dates', + }) + } + + let result = await db.query( + `SELECT * FROM "Term" + ORDER BY "termNumber" DESC + LIMIT 1 OFFSET 1;` + ) + if (!result.rowCount) { + req.previousTerm = { + termNumber: 0, + } + } else req.previousTerm = result.rows[0] + + if ( + req.previousTerm.termNumber && + req.previousTerm.endDate >= startDateObj + ) { + return res.status(400).json({ + error: 'Invalid start date: Overlapping terms', + }) + } + + result = await db.query( `UPDATE "Term" SET "termName" = $1, "startDate" = $2, "endDate" = $3 WHERE "termNumber" IN (SELECT COALESCE(MAX("termNumber"), 0) FROM "Term") diff --git a/server/routes/captain.route.js b/server/routes/captain.route.js index 455ecf94..d7d88d9b 100644 --- a/server/routes/captain.route.js +++ b/server/routes/captain.route.js @@ -3,13 +3,13 @@ import captainController from '../controllers/captain.controller.js' const captainRouter = Router() -captainRouter.get('/:id', captainController.getCaptain) captainRouter.get('/', captainController.getAllCaptains) captainRouter.get('/unit/:unitCaptainId', captainController.getCaptainsInUnit) captainRouter.get( '/sector/:baseName/:suffixName', captainController.getCaptainsInSector ) -captainRouter.patch('/type/change/:id', captainController.setCaptainType) +captainRouter.get('/:id', captainController.getCaptain) +captainRouter.patch('/:id', captainController.setCaptainType) export default captainRouter diff --git a/server/routes/sector.route.js b/server/routes/sector.route.js index 59cd2adc..f471ec07 100644 --- a/server/routes/sector.route.js +++ b/server/routes/sector.route.js @@ -1,17 +1,16 @@ -import {Router} from "express" -import sectorController from "../controllers/sector.controller.js" -import checkRankMiddleware from "../middlewares/checkRank.middleware.js"; +import { Router } from 'express' +import sectorController from '../controllers/sector.controller.js' +import checkRankMiddleware from '../middlewares/checkRank.middleware.js' -const sectorRouter = Router(); +const sectorRouter = Router() -sectorRouter.get('/all', sectorController.getAllSectors) +sectorRouter.post('/', sectorController.insertSector) +sectorRouter.get('/', sectorController.getAllSectors) sectorRouter.get('/:baseName/:suffixName', sectorController.getSector) -sectorRouter.post('/add', sectorController.insertSector) sectorRouter.patch( - '/unit/set/:id/:baseName/:suffixName', + '/:baseName/:suffixName', //TODO: Check if the captain id is for a unit captain sectorController.setUnitCaptain ) - -export default sectorRouter; \ No newline at end of file +export default sectorRouter