diff --git a/server/controllers/finance.controller.js b/server/controllers/finance.controller.js index 9874f2ad..8d8ecf4e 100644 --- a/server/controllers/finance.controller.js +++ b/server/controllers/finance.controller.js @@ -1,246 +1,240 @@ -import db from "../database/db.js"; +import db from '../database/db.js' const financeController = { - // @desc Get a budget - // @route GET /api/finance/budget - // @access Private - getBudget: async (req, res) => { - try { - // get income - let result = await db.query( - `SELECT COALESCE(SUM(value), 0) AS sum FROM "FinanceItem" - WHERE "type" = 'income';` - ); - const income = result.rows[0].sum; - - // get expense - result = await db.query( - `SELECT COALESCE(SUM("value"), 0) AS sum FROM "FinanceItem" - WHERE "type" = 'expense';` - ); - const expense = result.rows[0].sum; - - const budget = income - expense; - res.status(200).json({ - message: "Get budget successfully", - body: budget, - }); - } catch (error) { - console.log(error); - res.status(500).json({ - error: "An error occurred while getting the budget", - }); - } - }, - - // @desc Get income - // @route GET /api/finance/income - // @access Private - getIncome: async (req, res) => { - try { - const result = await db.query( - `SELECT * FROM "FinanceItem" AS F, "OtherItem" AS O - WHERE F."itemId" = O."itemId" AND F."type" = 'income';` - ); - - const subscriptionIncome = await db.query( - `SELECT * FROM "FinanceItem" AS F , "Subscription" AS S - WHERE F."itemId" = S."itemId";` - ); - const income = [...subscriptionIncome.rows, ...result.rows]; - - res.status(200).json({ - message: "Get income successfully", - body: income, - }); - } catch (error) { - console.log(error); - res.status(500).json({ - error: "An error occurred while getting the income", - }); - } - }, - - // @desc Get expense - // @route GET /api/finance/expense - // @access Private - getExpense: async (req, res) => { - try { - const result = await db.query( - `SELECT * FROM "FinanceItem" AS F , "OtherItem" AS O - WHERE F."itemId"=O."itemId" AND "type" = 'expense';` - ); - const expense = result.rows; - - res.status(200).json({ - message: "Get expense successfully", - body: expense, - }); - } catch (error) { - console.log(error); - res.status(500).json({ - error: "An error occurred while getting the expense", - }); - } - }, - - // @desc Add a subscription - // @route POST /api/finance/subscription - // @access Private - addSubscription: async (req, res) => { - try { - const { - value, - sectorBaseName, - sectorSuffixName, - termNumber, - weekNumber, - } = req.body; - - let result = await db.query( - `SELECT "itemId" AS id + // @desc Get a budget + // @route GET /api/finance/budget + // @access Private + getBudget: async (req, res) => { + try { + // get income + let result = await db.query(`CALL "getIncome"($1);`, [null]) + const { income } = result.rows[0] + + // get expense + result = await db.query(`CALL "getExpense"($1);`, [null]) + const { expense } = result.rows[0] + + const budget = income - expense + res.status(200).json({ + message: 'Get budget successfully', + body: budget, + }) + } catch (error) { + console.log(error) + res.status(500).json({ + error: 'An error occurred while getting the budget', + }) + } + }, + + // @desc Get income + // @route GET /api/finance/income + // @access Private + getIncome: async (req, res) => { + try { + const result = await db.query( + `SELECT * FROM "FinanceItem" AS F, "OtherItem" AS O + WHERE F."itemId" = O."itemId" AND F."type" = 'income';` + ) + + const subscriptionIncome = await db.query( + `SELECT * FROM "FinanceItem" AS F , "Subscription" AS S + WHERE F."itemId" = S."itemId";` + ) + const income = [...subscriptionIncome.rows, ...result.rows] + + res.status(200).json({ + message: 'Get income successfully', + body: income, + }) + } catch (error) { + console.log(error) + res.status(500).json({ + error: 'An error occurred while getting the income', + }) + } + }, + + // @desc Get expense + // @route GET /api/finance/expense + // @access Private + getExpense: async (req, res) => { + try { + const result = await db.query( + `SELECT * FROM "FinanceItem" AS F , "OtherItem" AS O + WHERE F."itemId"=O."itemId" AND "type" = 'expense';` + ) + const expense = result.rows + + res.status(200).json({ + message: 'Get expense successfully', + body: expense, + }) + } catch (error) { + console.log(error) + res.status(500).json({ + error: 'An error occurred while getting the expense', + }) + } + }, + + // @desc Add a subscription + // @route POST /api/finance/subscription + // @access Private + addSubscription: async (req, res) => { + try { + const { + value, + sectorBaseName, + sectorSuffixName, + termNumber, + weekNumber, + } = req.body + + let result = await db.query( + `SELECT "itemId" AS id FROM "Subscription" WHERE "sectorBaseName" = $1 AND "sectorSuffixName" = $2 AND "termNumber" = $3 AND "weekNumber" = $4;`, - [sectorBaseName, sectorSuffixName, termNumber, weekNumber] - ); + [sectorBaseName, sectorSuffixName, termNumber, weekNumber] + ) - if (!result.rowCount) { - result = await db.query( - `INSERT INTO "FinanceItem" ("value", "timestamp", "type") + if (!result.rowCount) { + result = await db.query( + `INSERT INTO "FinanceItem" ("value", "timestamp", "type") VALUES ($1, NOW(), 'income') RETURNING *;`, - [value] - ); - } else { - result = await db.query( - `UPDATE "FinanceItem" + [value] + ) + } else { + result = await db.query( + `UPDATE "FinanceItem" SET "value"=$1, "timestamp"=NOW() WHERE "itemId"=$2 RETURNING *;`, - [value, result.rows[0].id] - ); - } + [value, result.rows[0].id] + ) + } - const financeItem = result.rows[0]; + const financeItem = result.rows[0] - result = await db.query( - `INSERT INTO "Subscription" ("itemId", "sectorBaseName", "sectorSuffixName", "weekNumber", "termNumber") + result = await db.query( + `INSERT INTO "Subscription" ("itemId", "sectorBaseName", "sectorSuffixName", "weekNumber", "termNumber") VALUES ($1, $2, $3, $4, $5) ON CONFLICT ("itemId") DO UPDATE SET "sectorBaseName" = $2, "sectorSuffixName" = $3, "weekNumber" = $4, "termNumber" = $5 RETURNING *;`, - [ - financeItem.itemId, - sectorBaseName, - sectorSuffixName, - weekNumber, - termNumber, - ] - ); - - const subscription = result.rows[0]; - - res.status(200).json({ - message: "Add subscription successfully", - body: { financeItem: financeItem, subscription: subscription }, - }); - } catch (error) { - console.log(error); - res.status(500).json({ - error: "An error occurred while adding subscription", - }); - } - }, - - // @desc Get a subscription - // @route GET /api/finance/subscription - // @access Private - getSubscription: async (req, res) => { - try { - const { sectorBaseName, sectorSuffixName, weekNumber, termNumber } = - req.query; - - const result = await db.query( - `SELECT F."value" + [ + financeItem.itemId, + sectorBaseName, + sectorSuffixName, + weekNumber, + termNumber, + ] + ) + + const subscription = result.rows[0] + + res.status(200).json({ + message: 'Add subscription successfully', + body: { financeItem: financeItem, subscription: subscription }, + }) + } catch (error) { + console.log(error) + res.status(500).json({ + error: 'An error occurred while adding subscription', + }) + } + }, + + // @desc Get a subscription + // @route GET /api/finance/subscription + // @access Private + getSubscription: async (req, res) => { + try { + const { sectorBaseName, sectorSuffixName, weekNumber, termNumber } = + req.query + + const result = await db.query( + `SELECT F."value" FROM "Subscription" AS S, "FinanceItem" AS F WHERE S."itemId" = F."itemId" AND "sectorBaseName" = $1 AND "sectorSuffixName" = $2 AND "weekNumber" = $3 AND "termNumber" = $4;`, - [sectorBaseName, sectorSuffixName, weekNumber, termNumber] - ); - const subscription = result.rows[0].value; - - res.status(200).json({ - message: "Get subscription successfully", - body: subscription, - }); - } catch (error) { - console.log(error); - res.status(500).json({ - error: "An error occurred while getting the subscription", - }); - } - }, - - // @desc Get all subscriptions of current week - // @route GET /api/finance/subscription/all - // @access Private - getAllSubscriptionsOfCurrentWeek: async (req, res) => { - try { - const result = await db.query( - `SELECT COALESCE(SUM(F."value"), 0) AS sum + [sectorBaseName, sectorSuffixName, weekNumber, termNumber] + ) + const subscription = result.rows[0].value + + res.status(200).json({ + message: 'Get subscription successfully', + body: subscription, + }) + } catch (error) { + console.log(error) + res.status(500).json({ + error: 'An error occurred while getting the subscription', + }) + } + }, + + // @desc Get all subscriptions of current week + // @route GET /api/finance/subscription/all + // @access Private + getAllSubscriptionsOfCurrentWeek: async (req, res) => { + try { + const result = await db.query( + `SELECT COALESCE(SUM(F."value"), 0) AS sum FROM "Subscription" AS S, "FinanceItem" AS F WHERE S."itemId" = F."itemId" AND S."termNumber" = $1 AND S."weekNumber" = $2;`, - [req.currentWeek.termNumber, req.currentWeek.weekNumber] - ); - const subscription = result.rows[0].sum; - - res.status(200).json({ - message: "Get subscription successfully", - body: subscription, - }); - } catch (error) { - console.log(error); - res.status(500).json({ - error: "An error occurred while getting the subscription", - }); - } - }, - - // @desc Add an other item - // @route POST /api/finance/otherItem - // @access Private - addOtherItem: async (req, res) => { - try { - const { value, type, description } = req.body; - - let result = await db.query( - `INSERT INTO "FinanceItem" ("value", "timestamp", "type") + [req.currentWeek.termNumber, req.currentWeek.weekNumber] + ) + const subscription = result.rows[0].sum + + res.status(200).json({ + message: 'Get subscription successfully', + body: subscription, + }) + } catch (error) { + console.log(error) + res.status(500).json({ + error: 'An error occurred while getting the subscription', + }) + } + }, + + // @desc Add an other item + // @route POST /api/finance/otherItem + // @access Private + addOtherItem: async (req, res) => { + try { + const { value, type, description } = req.body + + let result = await db.query( + `INSERT INTO "FinanceItem" ("value", "timestamp", "type") VALUES ($1, NOW(), $2) RETURNING *;`, - [value, type] - ); + [value, type] + ) - const financeItem = result.rows[0]; + const financeItem = result.rows[0] - result = await db.query( - `INSERT INTO "OtherItem" ("description", "itemId", "generalCaptainId") + result = await db.query( + `INSERT INTO "OtherItem" ("description", "itemId", "generalCaptainId") VALUES ($1, $2, $3) RETURNING *;`, - [description, financeItem.itemId, req.captain.captainId] - ); - - const otherItem = result.rows[0]; - - res.status(200).json({ - message: "Add other item successfully", - body: { financeItem: financeItem, otherItem: otherItem }, - }); - } catch (error) { - console.log(error); - res.status(500).json({ - error: "An error occurred while adding other item", - }); - } - }, -}; - -export default financeController; + [description, financeItem.itemId, req.captain.captainId] + ) + + const otherItem = result.rows[0] + + res.status(200).json({ + message: 'Add other item successfully', + body: { financeItem: financeItem, otherItem: otherItem }, + }) + } catch (error) { + console.log(error) + res.status(500).json({ + error: 'An error occurred while adding other item', + }) + } + }, +} + +export default financeController diff --git a/server/controllers/sector.controller.js b/server/controllers/sector.controller.js index a4e13303..37e4d92a 100644 --- a/server/controllers/sector.controller.js +++ b/server/controllers/sector.controller.js @@ -19,7 +19,32 @@ const sectorController = { } catch (error) { console.log(error) res.status(500).json({ - error: 'An error occured while retrieving the captains info', + error: 'An error occured while retrieving sector info', + body: error, + }) + } + }, + + getAllSectorsInUnit: async (req, res) => { + try { + const { unitCaptainId } = req.params + + const result = await db.query( + `SELECT * + FROM "Sector" + WHERE "unitCaptainId" = $1;`, + [unitCaptainId] + ) + + 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 sectors info', body: error, }) } @@ -31,7 +56,7 @@ const sectorController = { getSector: async (req, res) => { try { const { baseName, suffixName } = req.query - + const result = await db.query( ` SELECT * @@ -68,12 +93,12 @@ const sectorController = { if (!baseName) { return res.status(404).json({ - error: "You must insert a baseName for the sector" + error: 'You must insert a baseName for the sector', }) } if (!suffixName) { - suffixName = "" + suffixName = '' } const result = await db.query( @@ -102,8 +127,8 @@ const sectorController = { // @access Private setUnitCaptain: async (req, res) => { try { - const { baseName, suffixName } = req.query; - const { unitCaptainId } = req.body; + const { baseName, suffixName } = req.query + const { unitCaptainId } = req.body if (!unitCaptainId) { return res.status(400).json({ @@ -111,35 +136,39 @@ const sectorController = { }) } - const sectorInfo = await db.query(` + const sectorInfo = await db.query( + ` SELECT * FROM "Sector" WHERE "baseName" = $1 AND "suffixName" = $2 `, - [baseName, suffixName]) + [baseName, suffixName] + ) if (sectorInfo.rowCount === 0) { return res.status(404).json({ - error: "No sector exists with these ids" + error: 'No sector exists with these ids', }) } - const captainInfo = await db.query(` + const captainInfo = await db.query( + ` SELECT * FROM "Captain" WHERE "captainId" = $1 `, - [unitCaptainId]) + [unitCaptainId] + ) if (captainInfo.rowCount === 0) { return res.status(404).json({ - error: "No captain exist with this id" + error: 'No captain exist with this id', }) } if (captainInfo.rows[0].type !== 'unit') { return res.status(401).json({ - error: "The provided captain id is not for a unit captain" + error: 'The provided captain id is not for a unit captain', }) } @@ -170,26 +199,27 @@ const sectorController = { const { baseName, suffixName } = req.query const { captainId } = req.body - const result = await db.query(` + const result = await db.query( + ` UPDATE "Captain" SET "rSectorBaseName" = $1, "rSectorSuffixName" = $2 WHERE "captainId" = $3 RETURNING * `, - [baseName, suffixName, captainId]) + [baseName, suffixName, captainId] + ) if (result.rowCount === 0) { return res.status(400).json({ - error: "Error occured while assigning captain" + error: 'Error occured while assigning captain', }) } res.status(200).json({ - message: "Successful assignment", + message: 'Successful assignment', body: result.rows, - count: result.rowCount + count: result.rowCount, }) - } catch (error) { console.log(error) res.status(500).json({ @@ -197,7 +227,7 @@ const sectorController = { body: error, }) } - } + }, } export default sectorController diff --git a/server/controllers/stats.controller.js b/server/controllers/stats.controller.js index 032aecbe..391a0903 100644 --- a/server/controllers/stats.controller.js +++ b/server/controllers/stats.controller.js @@ -7,23 +7,22 @@ const statsController = { // @access Private getAllScoutsAbsenceRate: async (req, res) => { try { - if (req.currentTerm.termNumber === 0) { + if (req.currentWeek.termNumber === 0) { return res.status(400).json({ error: 'Cannot get absence rate before the term starts', }) } + const result = await db.query( - `SELECT - COUNT(*) FILTER (WHERE "attendanceStatus" = 'absent') AS absence_count, - COUNT(*) FILTER (WHERE "attendanceStatus" = 'attended') AS attendance_count - FROM "ScoutAttendance" AS S, "Week" AS W - WHERE - S."weekNumber" = W."weekNumber" AND - S."termNumber" = W."termNumber" AND - W."cancelled" = false AND - S."termNumber" = $1;`, - [req.currentTerm.termNumber] + `CALL "getAllScoutsAbsenceRate"($1, $2, $3, $4);`, + [ + req.currentWeek.termNumber, + req.currentWeek.weekNumber, + null, + null, + ] ) + const absenceRate = computeAbsenceRate(result.rows[0]) if (absenceRate == null) { return res.status(400).json({ @@ -50,28 +49,23 @@ const statsController = { try { const { unitCaptainId } = req.params - if (req.currentTerm.termNumber === 0) { + if (req.currentWeek.termNumber === 0) { return res.status(400).json({ error: 'Cannot get absence rate before the term starts', }) } const result = await db.query( - `SELECT - COUNT(*) FILTER (WHERE "attendanceStatus" = 'absent') AS absence_count, - COUNT(*) FILTER (WHERE "attendanceStatus" = 'attended') AS attendance_count - FROM "ScoutAttendance" AS SA, "Scout" AS SC, "Sector" AS SE, "Week" AS W - WHERE - SA."weekNumber" = W."weekNumber" AND - SA."termNumber" = W."termNumber" AND - SA."scoutId" = SC."scoutId" AND - SC."sectorBaseName" = SE."baseName" AND - SC."sectorSuffixName" = SE."suffixName" AND - W."cancelled" = false AND - SA."termNumber" = $1 AND - SE."unitCaptainId" = $2;`, - [req.currentTerm.termNumber, unitCaptainId] + `CALL "getScoutsInUnitAbsenceRate"($1, $2, $3, $4, $5);`, + [ + req.currentWeek.termNumber, + req.currentWeek.weekNumber, + Number(unitCaptainId), + null, + null, + ] ) + const absenceRate = computeAbsenceRate(result.rows[0]) if (absenceRate == null) { return res.status(400).json({ @@ -98,26 +92,22 @@ const statsController = { try { const { sectorBaseName, sectorSuffixName } = req.query - if (req.currentTerm.termNumber === 0) { + if (req.currentWeek.termNumber === 0) { return res.status(400).json({ error: 'Cannot get absence rate before the term starts', }) } const result = await db.query( - `SELECT - COUNT(*) FILTER (WHERE "attendanceStatus" = 'absent') AS absence_count, - COUNT(*) FILTER (WHERE "attendanceStatus" = 'attended') AS attendance_count - FROM "ScoutAttendance" AS SA, "Scout" AS SC, "Week" AS W - WHERE - SA."weekNumber" = W."weekNumber" AND - SA."termNumber" = W."termNumber" AND - SA."scoutId" = SC."scoutId" AND - SC."sectorBaseName" = $1 AND - SC."sectorSuffixName" = $2 AND - W."cancelled" = false AND - SA."termNumber" = $3;`, - [sectorBaseName, sectorSuffixName, req.currentTerm.termNumber] + `CALL "getScoutsInSectorAbsenceRate"($1, $2, $3, $4, $5, $6);`, + [ + req.currentWeek.termNumber, + req.currentWeek.weekNumber, + sectorBaseName, + sectorSuffixName, + null, + null, + ] ) const absenceRate = computeAbsenceRate(result.rows[0]) if (absenceRate == null) { @@ -138,6 +128,124 @@ const statsController = { } }, + ////////////////////////////////////////// + getAllScoutsAbsenceRateGraph: async (req, res) => { + try { + if (req.currentWeek.termNumber === 0) { + return res.status(400).json({ + error: 'Cannot get absence rate before the term starts', + }) + } + let result + let ans = [] + + for (let i = 1; i <= req.currentWeek.weekNumber; i++) { + result = await db.query( + `CALL "getAllScoutsAbsenceRate"($1, $2, $3, $4);`, + [ + req.currentWeek.termNumber, + i, + null, + null, + ] + ) + const absenceRate = computeAbsenceRate(result.rows[0]) + ans.push({ weekNumber: i, absenceRate: absenceRate }) + } + + return res.status(200).json({ + message: 'Get absence rate successfully', + body: ans, + }) + } catch (error) { + console.log(error) + return res + .status(500) + .json({ error: 'An error occurred while getting absence rate' }) + } + }, + + getScoutsInUnitAbsenceRateGraph: async (req, res) => { + try { + const { unitCaptainId } = req.params + + if (req.currentWeek.termNumber === 0) { + return res.status(400).json({ + error: 'Cannot get absence rate before the term starts', + }) + } + + let result + let ans = [] + + for (let i = 1; i <= req.currentWeek.weekNumber; i++) { + result = await db.query( + `CALL "getScoutsInUnitAbsenceRate"($1, $2, $3, $4, $5);`, + [ + req.currentWeek.termNumber, + i, + Number(unitCaptainId), + null, + null, + ] + ) + const absenceRate = computeAbsenceRate(result.rows[0]) + ans.push({ weekNumber: i, absenceRate: absenceRate }) + } + + return res.status(200).json({ + message: 'Get absence rate successfully', + body: ans, + }) + } catch (error) { + console.log(error) + return res + .status(500) + .json({ error: 'An error occurred while getting absence rate' }) + } + }, + + getScoutsInSectorAbsenceRateGraph: async (req, res) => { + try { + const { sectorBaseName, sectorSuffixName } = req.query + + if (req.currentWeek.termNumber === 0) { + return res.status(400).json({ + error: 'Cannot get absence rate before the term starts', + }) + } + + let result + let ans = [] + + for (let i = 1; i <= req.currentWeek.weekNumber; i++) { + result = await db.query( + `CALL "getScoutsInSectorAbsenceRate"($1, $2, $3, $4, $5, $6);`, + [ + req.currentWeek.termNumber, + i, + sectorBaseName, + sectorSuffixName, + null, + null, + ] + ) + const absenceRate = computeAbsenceRate(result.rows[0]) + ans.push({ weekNumber: i, absenceRate: absenceRate }) + } + + return res.status(200).json({ + message: 'Get absence rate successfully', + body: ans, + }) + } catch (error) { + console.log(error) + return res + .status(500) + .json({ error: 'An error occurred while getting absence rate' }) + } + }, + // @desc Get absence rate for a scout // @route GET /api/stats/scout/:id // @access Private @@ -145,26 +253,23 @@ const statsController = { try { const { scoutId } = req.params - if (req.currentTerm.termNumber === 0) { + if (req.currentWeek.termNumber === 0) { return res.status(400).json({ error: 'Cannot get absence rate before the term starts', }) } const result = await db.query( - `SELECT - COUNT(*) FILTER (WHERE "attendanceStatus" = 'absent') AS absence_count, - COUNT(*) FILTER (WHERE "attendanceStatus" = 'attended') AS attendance_count - FROM "ScoutAttendance" AS SA, "Scout" AS SC, "Week" AS W - WHERE - SA."weekNumber" = W."weekNumber" AND - SA."termNumber" = W."termNumber" AND - SA."scoutId" = SC."scoutId" AND - SC."scoutId" = $1 AND - W."cancelled" = false AND - SA."termNumber" = $2;`, - [scoutId, req.currentTerm.termNumber] + `CALL "getScoutAbsenceRate"($1, $2, $3, $4, $5);`, + [ + req.currentWeek.termNumber, + req.currentWeek.weekNumber, + scoutId, + null, + null, + ] ) + const absenceRate = computeAbsenceRate(result.rows[0]) if (absenceRate == null) { return res.status(400).json({ diff --git a/server/controllers/term.controller.js b/server/controllers/term.controller.js index f223aa40..aa648ee5 100644 --- a/server/controllers/term.controller.js +++ b/server/controllers/term.controller.js @@ -179,6 +179,7 @@ const termController = { error: 'There is no weeks or terms in the system to be cancelled', }) } + const result = await db.query( `UPDATE "Week" SET "cancelled" = true WHERE "termNumber" = $1 AND "weekNumber" = $2 diff --git a/server/middlewares/current.middleware.js b/server/middlewares/current.middleware.js index aeb1a9b4..7e6f2bdf 100644 --- a/server/middlewares/current.middleware.js +++ b/server/middlewares/current.middleware.js @@ -23,11 +23,11 @@ const getCurrentTermMiddleware = async (req, res, next) => { const getCurrentWeekMiddleware = async (req, res, next) => { try { - - const result = await db.query('CALL get_current_week();') - - console.log(result) - + const result = await db.query( + `SELECT * FROM "Week" WHERE "weekNumber" IN + (SELECT COALESCE(MAX("weekNumber"), 0) FROM "Week" WHERE "termNumber" IN + (SELECT COALESCE(MAX("termNumber"), 0) FROM "Term"));` + ) if (!result.rows.length) { req.currentWeek = { termNumber: 0, diff --git a/server/routes/sector.route.js b/server/routes/sector.route.js index 6946775a..7647d922 100644 --- a/server/routes/sector.route.js +++ b/server/routes/sector.route.js @@ -4,14 +4,11 @@ import checkRankMiddleware from '../middlewares/checkRank.middleware.js' const sectorRouter = Router() -sectorRouter.post('/', sectorController.insertSector) sectorRouter.get('/all', sectorController.getAllSectors) +sectorRouter.post('/', sectorController.insertSector) sectorRouter.get('/', sectorController.getSector) -sectorRouter.patch( - '/unit', - //TODO: Check if the captain id is for a unit captain - sectorController.setUnitCaptain -) +sectorRouter.patch('/unit', sectorController.setUnitCaptain) sectorRouter.patch('/captain/assign', sectorController.assignCaptain) +sectorRouter.get('/:unitCaptainId', sectorController.getAllSectorsInUnit) export default sectorRouter diff --git a/server/routes/stats.route.js b/server/routes/stats.route.js index f7d14841..2f96c445 100644 --- a/server/routes/stats.route.js +++ b/server/routes/stats.route.js @@ -1,29 +1,50 @@ import { Router } from 'express' import statsController from '../controllers/stats.controller.js' -import { getCurrentTermMiddleware } from '../middlewares/current.middleware.js' +import { getCurrentWeekMiddleware } from '../middlewares/current.middleware.js' import checkRankMiddleware from '../middlewares/checkRank.middleware.js' const statsRouter = Router() + +// Detailed stats +statsRouter.get( + '/scouts/graph', + checkRankMiddleware('general'), + getCurrentWeekMiddleware, + statsController.getAllScoutsAbsenceRateGraph +) +statsRouter.get( + '/scouts/graph/unit/:unitCaptainId', + checkRankMiddleware('general', 'unit'), + getCurrentWeekMiddleware, + statsController.getScoutsInUnitAbsenceRateGraph +) +statsRouter.get( + '/scouts/graph/sector', + getCurrentWeekMiddleware, + statsController.getScoutsInSectorAbsenceRateGraph +) + +// General stats statsRouter.get( '/scouts', checkRankMiddleware('general'), - getCurrentTermMiddleware, + getCurrentWeekMiddleware, statsController.getAllScoutsAbsenceRate ) statsRouter.get( '/scouts/unit/:unitCaptainId', checkRankMiddleware('general', 'unit'), - getCurrentTermMiddleware, + getCurrentWeekMiddleware, statsController.getScoutsInUnitAbsenceRate ) statsRouter.get( '/scouts/sector', - getCurrentTermMiddleware, + getCurrentWeekMiddleware, statsController.getScoutsInSectorAbsenceRate ) statsRouter.get( '/scouts/:scoutId', - getCurrentTermMiddleware, + getCurrentWeekMiddleware, statsController.getScoutAbsenceRate )