From fd315ae1f10c72e3c124d21249108d7ad633d4a9 Mon Sep 17 00:00:00 2001 From: AhmedHamed3699 Date: Tue, 26 Dec 2023 00:15:18 +0200 Subject: [PATCH 1/3] =?UTF-8?q?=E2=9C=8F=EF=B8=8F=20fix:=20small=20edit?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/controllers/scout.controller.js | 10 ++++------ server/routes/scout.route.js | 2 +- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/server/controllers/scout.controller.js b/server/controllers/scout.controller.js index e2dd91d6..9cde1f4e 100644 --- a/server/controllers/scout.controller.js +++ b/server/controllers/scout.controller.js @@ -137,7 +137,6 @@ const scoutController = { if (result1.rowCount == 0) { return res.status(404).json({ error: 'No rows updated for the scout', - body: result1, }) } @@ -161,7 +160,7 @@ const scoutController = { // Respond with the updated data res.status(200).json({ message: 'Successful update', - body: { result1, result2 }, + body: { scout: result1.rows[0], scoutProfile: result2.rows[0] }, }) } catch (error) { console.log(error) @@ -207,7 +206,6 @@ const scoutController = { if (result1.rowCount == 0) { return res.status(400).json({ error: 'No data was inserted for the scout', - body: result1, }) } @@ -222,7 +220,7 @@ const scoutController = { schoolGrade, photo, birthCertificate, - result1.rows[0]['scoutId'], + result1.rows[0].scoutId, ] ) @@ -230,14 +228,14 @@ const scoutController = { if (result2.rowCount == 0) { return res.status(400).json({ error: 'No data was inserted for the scout profile', - body: result2, + body: { scout: result1.rows[0] }, }) } // Return the data res.status(200).json({ message: 'Successful insertion', - body: { result1, result2 }, + body: { scout: result1.rows[0], scoutProfile: result2.rows[0] }, }) } catch (error) { console.log(error) diff --git a/server/routes/scout.route.js b/server/routes/scout.route.js index acaf0687..fe5c4c1e 100644 --- a/server/routes/scout.route.js +++ b/server/routes/scout.route.js @@ -5,7 +5,7 @@ const scoutRouter = Router() scoutRouter.post('/', scoutController.insertScout) scoutRouter.get('/:id', scoutController.getScout) -scoutRouter.put('/:id', scoutController.updateScout) // or patch +scoutRouter.put('/:id', scoutController.updateScout) scoutRouter.get('/all', scoutController.getAllScouts) scoutRouter.get('/unit/:unitCaptainId', scoutController.getScoutsInUnit) scoutRouter.get( From 6ab093bc23262a27a75002aa51dc1cf36f4e3c94 Mon Sep 17 00:00:00 2001 From: AhmedHamed3699 Date: Tue, 26 Dec 2023 00:16:39 +0200 Subject: [PATCH 2/3] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20chore:=20alert=20is=20?= =?UTF-8?q?modefied=20to=20work=20properly?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/controllers/alert.controller.js | 164 ++++++++++++++++++------- server/routes/alert.route.js | 17 +-- server/routes/api.route.js | 2 +- 3 files changed, 127 insertions(+), 56 deletions(-) diff --git a/server/controllers/alert.controller.js b/server/controllers/alert.controller.js index 4ecb1510..67ea4deb 100644 --- a/server/controllers/alert.controller.js +++ b/server/controllers/alert.controller.js @@ -2,78 +2,148 @@ import db from '../database/db.js' const alertController = { getAlert: async (req, res) => { - const { id } = req.params - - const alert = await db.query( - `SELECT * FROM "Notification" WHERE "NotificationId" = $1;`, - [id] - ) + try { + const { id } = req.params + + // get alert + const result = await db.query( + `SELECT N.* + FROM "Notification" AS N, "RecieveNotification" AS R + WHERE N."notificationId" = R."notificationId" AND + R."captainId" = $1 AND + N."notificationId" = $2;`, + [req.captain.captainId, id] + ) - if (!alert.rowCount) - return res.status(404).json({ error: 'Alert not found' }) + if (!result.rowCount) + return res.status(404).json({ error: 'Alert not found' }) - res.status(200).json({ - message: 'Alert successfully found', - body: alert.rows[0], - }) + res.status(200).json({ + message: 'Alert successfully found', + body: result.rows[0], + }) + } catch (error) { + console.error(error) + res.status(500).json({ + error: 'An error occured while getting alert', + }) + } }, - CreateAlert: async (req, res) => { - const { message, type } = req.body - - const newAlert = await db.query( - `INSERT INTO "Notification" ("message") - VALUES($1) RETURNING *;`, - [message] - ) + createAlert: async (req, res) => { + try { + const { message, contentType } = req.body - if (!newAlert.rowCount) - return res.status(400).json({ error: 'Cannot Post' }) + const result = await db.query( + `INSERT INTO "Notification" ("timestamp", "message", "contentType") + VALUES(NOW(), $1, $2) RETURNING *;`, + [message, contentType] + ) - res.status(200).json({ - message: 'Alert successfully created', - body: newAlert.rows[0], - }) + res.status(200).json({ + message: 'Alert successfully created', + body: result.rows[0], + }) + } catch (error) { + console.error(error) + res.status(500).json({ + error: 'An error occured while creating alert', + }) + } }, - DeleteAlert: async (req, res) => { - const { id } = req.params - - const Alerts = await db.query(`SELECT * FROM "Notification";`) - - const alertsArr = Alerts.rows - - if (!alertsArr.find((item) => item.NotificationId === Number(id))) - return res - .status(404) - .json({ error: 'Alert to be deleted not found' }) + sendAlert: async (req, res) => { + try { + const { id } = req.params + const { sectorBaseName, sectorSuffixName } = req.body + + let result + + 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 + RETURNING *;`, + [id] + ) + } else { + // send alert to all captains in sector + result = await db.query( + `INSERT INTO "RecieveNotification" ("notificationId", "captainId", "status") + SELECT $1, C."captainId", 'unread' + FROM "Captain" AS C + WHERE C."rSectorBaseName" = $2 AND + C."rSectorSuffixName" = $3 + RETURNING *;`, + [id, sectorBaseName, sectorSuffixName] + ) + } + + res.status(200).json({ + message: 'Alert successfully sent', + body: result.rows[0], + }) + } catch (error) { + console.error(error) + res.status(500).json({ + error: 'An error occured while sending alert', + }) + } + }, + deleteAlert: async (req, res) => { try { - await db.query( - `DELETE FROM "Notification" WHERE "NotificationId" = $1;`, + const { id } = req.params + const result = await db.query( + `DELETE FROM "Notification" WHERE "notificationId" = $1 RETURNING *;`, [id] ) - alertsArr.filter((item) => item.NotificationId !== Number(id)) return res.status(200).json({ message: 'Alert successfully deleted', - body: alertsArr, + body: result.rows[0], }) } catch (error) { console.error(error) - res.status(400).json({ + res.status(500).json({ error: 'An error occured while deleting alert', }) } }, getAllAlerts: async (req, res) => { - const Alerts = await db.query(`SELECT * FROM "Notification";`) + try { + const { status, contentType } = req.body + const result = await db.query( + `SELECT N.*, R."status" + FROM "Notification" AS N, "RecieveNotification" AS R + WHERE N."notificationId" = R."notificationId" AND + R."captainId" = $1;`, + [req.captain.captainId] + ) - res.status(200).json({ - message: 'get Alerts successfully', - body: Alerts.rows, - }) + let alerts = result.rows + if (status) { + alerts = alerts.filter((alert) => alert.status === status) + } + if (contentType) { + alerts = alerts.filter( + (alert) => alert.contentType === contentType + ) + } + + res.status(200).json({ + message: 'get Alerts successfully', + body: alerts, + }) + } catch (error) { + console.error(error) + res.status(500).json({ + error: 'An error occured while getting alerts', + }) + } }, } diff --git a/server/routes/alert.route.js b/server/routes/alert.route.js index ac001482..d599f1d0 100644 --- a/server/routes/alert.route.js +++ b/server/routes/alert.route.js @@ -1,11 +1,12 @@ -import {Router} from "express" -import alertController from "../controllers/alert.controller.js" +import { Router } from 'express' +import alertController from '../controllers/alert.controller.js' -const alertRouter = Router(); +const alertRouter = Router() -alertRouter.get("/", alertController.getAllAlerts); -alertRouter.post("/", alertController.CreateAlert); -alertRouter.get("/:id", alertController.getAlert); -alertRouter.delete("/:id", alertController.DeleteAlert); +alertRouter.post('/', alertController.createAlert) +alertRouter.get('/:id', alertController.getAlert) +alertRouter.post('/:id', alertController.sendAlert) +alertRouter.delete('/:id', alertController.deleteAlert) +alertRouter.get('/', alertController.getAllAlerts) -export default alertRouter; +export default alertRouter diff --git a/server/routes/api.route.js b/server/routes/api.route.js index d42aab0a..0bb5eaed 100644 --- a/server/routes/api.route.js +++ b/server/routes/api.route.js @@ -14,7 +14,7 @@ apiRouter.use('/stats', authMiddleware, statsRouter) apiRouter.use('/finance', authMiddleware, financeRouter) apiRouter.use('/term', authMiddleware, termRouter) apiRouter.use('/captain', authMiddleware, captainRouter) -apiRouter.use('/alert', alertRouter) +apiRouter.use('/alert', authMiddleware, alertRouter) apiRouter.use('/scout', authMiddleware, scoutRouter) export default apiRouter From 369484431b7e5aa42395386b55b82b1c329870f1 Mon Sep 17 00:00:00 2001 From: AhmedHamed3699 Date: Tue, 26 Dec 2023 01:02:53 +0200 Subject: [PATCH 3/3] =?UTF-8?q?=F0=9F=9A=91=20fix:=20hotfix=20for=20captai?= =?UTF-8?q?n=20and=20scout=20routing?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/controllers/captain.controller.js | 4 ++-- server/controllers/scout.controller.js | 1 - server/routes/captain.route.js | 2 +- server/routes/scout.route.js | 2 +- 4 files changed, 4 insertions(+), 5 deletions(-) diff --git a/server/controllers/captain.controller.js b/server/controllers/captain.controller.js index 24f68fe3..117bde46 100644 --- a/server/controllers/captain.controller.js +++ b/server/controllers/captain.controller.js @@ -76,14 +76,14 @@ const captainController = { getCaptain: async (req, res) => { try { // Extract the captain ID from the request params - const { captainId } = req.params + const { id } = req.params // Query on the database to get that captain info const result = await db.query( `SELECT * FROM "Captain" WHERE "captainId" = $1`, - [captainId] + [id] ) // If captain doesn't exist return an error message diff --git a/server/controllers/scout.controller.js b/server/controllers/scout.controller.js index 9cde1f4e..a26d5472 100644 --- a/server/controllers/scout.controller.js +++ b/server/controllers/scout.controller.js @@ -4,7 +4,6 @@ const scoutController = { getAllScouts: async (req, res) => { try { const result = await db.query(`SELECT * FROM "Scout";`) - res.status(200).json({ message: 'Successful retrieval', body: result.rows, diff --git a/server/routes/captain.route.js b/server/routes/captain.route.js index 3cedbead..bcc21679 100644 --- a/server/routes/captain.route.js +++ b/server/routes/captain.route.js @@ -4,7 +4,7 @@ import captainController from '../controllers/captain.controller.js' const captainRouter = Router() captainRouter.get('/:id', captainController.getCaptain) -captainRouter.get('/all', captainController.getAllCaptains) +captainRouter.get('/', captainController.getAllCaptains) captainRouter.get('/unit/:unitCaptainId', captainController.getCaptainsInUnit) captainRouter.get( '/sector/:baseName/:suffixName', diff --git a/server/routes/scout.route.js b/server/routes/scout.route.js index fe5c4c1e..428282e0 100644 --- a/server/routes/scout.route.js +++ b/server/routes/scout.route.js @@ -6,7 +6,7 @@ const scoutRouter = Router() scoutRouter.post('/', scoutController.insertScout) scoutRouter.get('/:id', scoutController.getScout) scoutRouter.put('/:id', scoutController.updateScout) -scoutRouter.get('/all', scoutController.getAllScouts) +scoutRouter.get('/', scoutController.getAllScouts) scoutRouter.get('/unit/:unitCaptainId', scoutController.getScoutsInUnit) scoutRouter.get( '/sector/:baseName/:suffixName',