Skip to content
This repository has been archived by the owner on Apr 1, 2024. It is now read-only.

Commit

Permalink
Add filters to the notes
Browse files Browse the repository at this point in the history
  • Loading branch information
Dlurak committed Dec 20, 2023
1 parent 5daebef commit 028266c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const jwt = require('./dist/utils/jwt')

console.log(jwt.generateToken('Anzie'))
27 changes: 22 additions & 5 deletions src/routes/notes/getNotes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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;
Expand All @@ -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 }],
};
}

Expand Down

0 comments on commit 028266c

Please sign in to comment.