Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Back end #35

Merged
merged 2 commits into from
Dec 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions server/controllers/alert.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,33 +57,52 @@ 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]
)
} else {
// 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]
)
}

res.status(200).json({
message: 'Alert successfully sent',
body: result.rows[0],
body: result.rows,
})
} catch (error) {
console.error(error)
Expand Down
50 changes: 27 additions & 23 deletions server/controllers/sector.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
Expand All @@ -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({
Expand All @@ -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({
Expand All @@ -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;
export default sectorController
37 changes: 31 additions & 6 deletions server/controllers/term.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 *;`,
Expand Down Expand Up @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions server/routes/captain.route.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
17 changes: 8 additions & 9 deletions server/routes/sector.route.js
Original file line number Diff line number Diff line change
@@ -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;
export default sectorRouter