Skip to content

Commit

Permalink
Fix the volunteer log bug and add better error
Browse files Browse the repository at this point in the history
handling
  • Loading branch information
JiashuHarryHuang committed Apr 16, 2024
1 parent 9f64c99 commit 53eacd2
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 12 deletions.
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 3 additions & 5 deletions pages/api/admin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,10 @@ export default async function handler(
case 'POST':
try {
if (session.user.status !== 'superadmin') {
res.status(200).json({
return res.status(200).json({
message: 'Sorry, only super admin is allowed to create new admins',
status: 'error',
});
throw new Error('Only super admin is allowed to create new admins');
}
// start a try catch block to catch any errors in parsing the request body
const admin = req.body as QueriedAdminData;
Expand All @@ -47,7 +46,7 @@ export default async function handler(

// Check if the user's email and password are valid
if (!email || !email.includes('@') || !password) {
res
return res
.status(200)
.json({ message: 'Invalid email or password', status: 'error' });
}
Expand All @@ -62,10 +61,9 @@ export default async function handler(

// If the user already exists, return an error
if (checkExisting) {
res
return res
.status(200)
.json({ message: 'Admin email already exists', status: 'error' });
throw new Error('Admin email already exists');
}

// Hash the user's password
Expand Down
4 changes: 4 additions & 0 deletions pages/api/volunteer-logs/approved.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import dbConnect from '@/lib/dbConnect';
import Users from 'bookem-shared/src/models/Users';
import VolunteerEvents from 'bookem-shared/src/models/VolunteerEvents';
import VolunteerLogs from 'bookem-shared/src/models/VolunteerLogs';
import { QueriedVolunteerLogDTO } from 'bookem-shared/src/types/database';
import { NextApiRequest, NextApiResponse } from 'next';
Expand All @@ -14,6 +16,8 @@ export default async function handler(
await dbConnect();

// TODO: remove this after development
await Users.find();
await VolunteerEvents.find();

const logs = (await VolunteerLogs.find({ status: 'approved' })
.populate({ path: 'user' })
Expand Down
2 changes: 2 additions & 0 deletions pages/api/volunteer-logs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export default async function handler(
await dbConnect();

// TODO: remove this after development
await Users.find();
await VolunteerEvents.find();

const logs = (await VolunteerLogs.find()
.populate({ path: 'user' })
Expand Down
4 changes: 4 additions & 0 deletions pages/api/volunteer-logs/pending.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import dbConnect from '@/lib/dbConnect';
import Users from 'bookem-shared/src/models/Users';
import VolunteerEvents from 'bookem-shared/src/models/VolunteerEvents';
import VolunteerLogs from 'bookem-shared/src/models/VolunteerLogs';
import { QueriedVolunteerLogDTO } from 'bookem-shared/src/types/database';
import { NextApiRequest, NextApiResponse } from 'next';
Expand All @@ -14,6 +16,8 @@ export default async function handler(
await dbConnect();

// TODO: remove this after development
await Users.find();
await VolunteerEvents.find();

const logs = (await VolunteerLogs.find({ status: 'pending' })
.populate({ path: 'user' })
Expand Down
4 changes: 4 additions & 0 deletions pages/api/volunteer-logs/rejected.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import dbConnect from '@/lib/dbConnect';
import Users from 'bookem-shared/src/models/Users';
import VolunteerEvents from 'bookem-shared/src/models/VolunteerEvents';
import VolunteerLogs from 'bookem-shared/src/models/VolunteerLogs';
import { QueriedVolunteerLogDTO } from 'bookem-shared/src/types/database';
import { NextApiRequest, NextApiResponse } from 'next';
Expand All @@ -14,6 +16,8 @@ export default async function handler(
await dbConnect();

// TODO: remove this after development
await Users.find();
await VolunteerEvents.find();

const logs = (await VolunteerLogs.find({ status: 'rejected' })
.populate({ path: 'user' })
Expand Down

0 comments on commit 53eacd2

Please sign in to comment.