From 028266c05891a4d727022bffdc1f972d3259805d Mon Sep 17 00:00:00 2001 From: Dlurak <84224239+Dlurak@users.noreply.github.com> Date: Wed, 20 Dec 2023 22:10:25 +0100 Subject: [PATCH] Add filters to the notes --- index.js | 3 +++ src/routes/notes/getNotes.ts | 27 ++++++++++++++++++++++----- 2 files changed, 25 insertions(+), 5 deletions(-) create mode 100644 index.js diff --git a/index.js b/index.js new file mode 100644 index 0000000..4e405ab --- /dev/null +++ b/index.js @@ -0,0 +1,3 @@ +const jwt = require('./dist/utils/jwt') + +console.log(jwt.generateToken('Anzie')) diff --git a/src/routes/notes/getNotes.ts b/src/routes/notes/getNotes.ts index 6e08c41..742e1af 100644 --- a/src/routes/notes/getNotes.ts +++ b/src/routes/notes/getNotes.ts @@ -7,6 +7,7 @@ import { import { noteCollection } from '../../database/notes/notes'; import { authenticateOptional } from '../../middleware/auth'; import findUsername from '../../database/user/findUser'; +import { findClassBySchoolNameAndClassName } from '../../database/classes/findClass'; const router = express.Router(); @@ -41,7 +42,26 @@ const router = express.Router(); */ router.get('/', pagination, authenticateOptional, async (req, res) => { const { page, pageSize } = res.locals.pagination; - let query = { visibility: 'public' } as any; + const { school, class: className } = req.query as { + school: undefined | string; + class: undefined | string; + }; + + let query = { visibility: 'public', class: null } as any; + + if (school && className) { + const classId = await findClassBySchoolNameAndClassName( + school, + className, + ).then((c) => c?._id); + + if (!classId) + return res + .status(404) + .json({ status: 'error', message: 'Class not found' }); + + query = { visibility: 'public', class: classId }; + } const isAuthedResLocal = res.locals.authenticated as boolean; const username = res.locals.jwtPayload?.username as string | undefined; @@ -55,10 +75,7 @@ router.get('/', pagination, authenticateOptional, async (req, res) => { const userId = userObj._id; query = { - $or: [ - { visibility: 'public' }, - { visibility: 'private', creator: userId }, - ], + $or: [query, { visibility: 'private', creator: userId }], }; }