diff --git a/backend/controllers/AdminController.js b/backend/controllers/AdminController.js index 2eff601..2c7284a 100644 --- a/backend/controllers/AdminController.js +++ b/backend/controllers/AdminController.js @@ -4,6 +4,117 @@ const db = require('../database'); const bcrypt = require('bcryptjs'); const {hash, genSalt} = require('bcryptjs'); +const getEventRequests = async (req, res, next) => { + try { + const requests = await db.manyOrNone( + 'SELECT * FROM EventRequests WHERE event_id = $1', + [req.params.eventId], + ); + + if (requests.length) { + res.locals.data = requests; + next(); + } else { + res.status(404).json({message: 'No requests found for this event.'}); + } + } catch (err) { + console.error(err); + res.status(500).json({error: 'Internal Server Error'}); + } +}; + +const getAllEventRequests = async (req, res, next) => { + try { + const requests = await db.manyOrNone( + 'SELECT * FROM EventRequests', + ); + + if (requests.length) { + res.locals.data = requests; + next(); + } else { + res.status(404).json({message: 'No requests found for this event.'}); + } + } catch (err) { + console.error(err); + res.status(500).json({error: 'Internal Server Error'}); + } +} + +const getViolations = async (req, res, next) => { + try { + const violations = await db.manyOrNone( + 'SELECT * FROM VendorViolations WHERE vendor_id = $1', + [req.params.vendorId], + ); + + if (violations.length) { + res.locals.data = violations; + next(); + } else { + res.status(404).json({message: 'No violations found for this vendor.'}); + } + } catch (err) { + console.error(err); + res.status(500).json({error: 'Internal Server Error'}); + } +} + +const getAllViolations = async (req, res, next) => { + try { + const violations = await db.manyOrNone('SELECT * FROM VendorViolations'); + + if (violations.length) { + res.locals.data = violations; + next(); + } else { + res.status(404).json({message: 'No violations found.'}); + } + } catch (err) { + console.error(err); + res.status(500).json({error: 'Internal Server Error'}); + } +} + +const createVendorViolation = async (req, res, next) => { + try { + await db.none( + 'INSERT INTO VendorViolations (vendor_id) VALUES ($1)', + [req.params.vendorId], + ); + next(); + } catch (err) { + console.error(err); + res.status(500).json({error: 'Internal Server Error'}); + } +} + +const deleteVendorViolation = async (req, res, next) => { + try { + await db.none( + 'DELETE FROM VendorViolations WHERE violation_id = $1', + [req.params.violationId], + ); + next(); + } catch (err) { + console.error(err); + res.status(500).json({error: 'Internal Server Error'}); + } +} + +const processEventRequest = async (req, res, next) => { + try { + await db.none( + 'UPDATE EventRequests SET approved = $1 WHERE request_id = $2', + [req.body.approved, req.params.requestId], + ); + next(); + } catch (err) { + console.error(err); + res.status(500).json({error: 'Internal Server Error'}); + } +} + // Middleware given an email in the body, retireves the given admin // account or returns an error const getAdminByEmail = async (req, res, next) => { @@ -60,4 +171,14 @@ const createAdminMiddleware = async (req, res, next) => { } }; -module.exports = {getAdminByEmail, createAdminMiddleware}; +module.exports = { + getEventRequests, + getAllEventRequests, + getViolations, + getAllViolations, + createVendorViolation, + deleteVendorViolation, + processEventRequest, + getAdminByEmail, + createAdminMiddleware, +}; \ No newline at end of file diff --git a/backend/index.js b/backend/index.js index 134c499..09d28bf 100644 --- a/backend/index.js +++ b/backend/index.js @@ -26,6 +26,7 @@ app.use(cookieParser()); // Import router objects and direct the app to use them const VendorRouter = require('./routes/VendorRouter'); +const AdminRouter = require('./routes/AdminRouter'); const EventRouter = require('./routes/EventRouter'); const AdminRouter = require('./routes/AdminRouter'); diff --git a/backend/routes/AdminRouter.js b/backend/routes/AdminRouter.js index cce46b8..eb1d174 100644 --- a/backend/routes/AdminRouter.js +++ b/backend/routes/AdminRouter.js @@ -1,21 +1,48 @@ +const express = require('express'); +const router = express.Router(); + +// Auth Controller Imports const { verifyAdminToken, signAdminToken, verify, } = require('../controllers/AuthController'); -const {getAdminByEmail, createAdminMiddleware} = require('../controllers/AdminController'); -// Import express -const express = require('express'); +// Admin Controller Imports +const { + getEventRequests, + getAllEventRequests, + getViolations, + getAllViolations, + createVendorViolation, + deleteVendorViolation, + processEventRequest, + getAdminByEmail, + createAdminMiddleware, +} = require('../controllers/AdminController'); -// Create a router for admin authentication -const router = express.Router(); +const sendSuccessResponse = require('../middleware/successResponse'); + +router.get('/events/requests/:eventId', verify('admin'), getEventRequests, sendSuccessResponse); + +router.get('/events/requests', verify('admin'), getAllEventRequests, sendSuccessResponse); + +router.get('/violations/:vendorId', verify('admin'), getViolations, sendSuccessResponse); + +router.get('/violations', verify('admin'), getAllViolations, sendSuccessResponse); + +router.put('events/requests/:requestId', verify('admin'), processEventRequest, sendSuccessResponse); + +router.post('/violations/:vendorId', verify('admin'), createVendorViolation, sendSuccessResponse); + +router.delete('/violations/:violationId', verify('admin'), deleteVendorViolation, sendSuccessResponse); router.post('/login', getAdminByEmail, signAdminToken, (req, res) => { res.status(200).json({status: 'success'}); }); // UNFINISHED: Create an admin account +// Useful for creating an admin account for testing purposes. Password in database needs to be hashed for login to work properly. // router.post('/', createAdminMiddleware, (req, res) => { // res.status(200).json({status: 'success', admin: res.locals.data}); // });