Skip to content

Commit

Permalink
allow testing API endpoints with fake session in development
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidHuang2002 committed Feb 10, 2024
1 parent 136b708 commit d799b56
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 35 deletions.
15 changes: 3 additions & 12 deletions pages/api/applications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,15 @@ import ApplicationResponse from 'bookem-shared/src/models/ApplicationResponse';
import VolunteerEvents from 'bookem-shared/src/models/VolunteerEvents';
import { authOptions } from '@/pages/api/auth/[...nextauth]';
import { getServerSession } from 'next-auth';
import { makeSessionForAPITest } from '@/utils/api-testing';

export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
// Get session user
const session = await getServerSession(req, res, authOptions);
// for API testing only
// TODO extract this logic
// const session = {
// user: {
// id: '60f3f5f8f0f6f6f6f6f6f6f6',
// },
// };
const session =
(await getServerSession(req, res, authOptions)) || makeSessionForAPITest();

// Get request parameter
const {
Expand All @@ -46,10 +41,6 @@ export default async function handler(
userId: session.user.id,
}).populate('eventId');

// const appliedEvents = applicationResponses.map((applicationResponse) => {
// return applicationResponse.eventId;
// };

return res.status(200).json({ message: applicationResponses });
} catch (error) {
console.error(error);
Expand Down
19 changes: 4 additions & 15 deletions pages/api/event/[id]/apply.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,15 @@ import { ApplicationResponseData } from 'bookem-shared/src/types/database';
import ApplicationResponse from 'bookem-shared/src/models/ApplicationResponse';
import { authOptions } from '@/pages/api/auth/[...nextauth]';
import { getServerSession } from 'next-auth';
import { makeSessionForAPITest } from '@/utils/api-testing';

export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
// Get session user
const session = await getServerSession(req, res, authOptions);
// for API testing only
// TODO extract this logic
// const session = {
// user: {
// id: '60f3f5f8f0f6f6f6f6f6f6f6',
// },
// }
let session =
(await getServerSession(req, res, authOptions)) || makeSessionForAPITest();

// Get request parameter
const {
Expand All @@ -39,7 +34,6 @@ export default async function handler(
try {
await dbConnect();

// TODO - refactor common id checking to a common util
if (!id) return res.status(400).json({ message: 'Missing id' });

// check if id is a valid mongoose id
Expand All @@ -51,7 +45,6 @@ export default async function handler(
eventId: id,
});

// TODO - refactor a common util for when something is not found
if (!volunteerApplication) {
return res
.status(404)
Expand All @@ -77,13 +70,9 @@ export default async function handler(
try {
await dbConnect();

// start a try catch block to catch any errors in parsing the request body
// TODO maybe define a DTO for this
console.log(req.body);
const response = req.body as ApplicationResponseData;
const { answers } = response;


// Declare the following ops to be an atomic transaction
const mongoSession = await mongoose.startSession();
await mongoSession.withTransaction(async () => {
Expand All @@ -95,7 +84,7 @@ export default async function handler(
answers,
});

await newResponse.save()
await newResponse.save();

// use the id of the saved response to update the volunteerApplications data
await VolunteerApplications.updateOne(
Expand Down
11 changes: 3 additions & 8 deletions pages/api/event/[id]/submitted-application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,15 @@ import type { NextApiRequest, NextApiResponse } from 'next';
import ApplicationResponse from 'bookem-shared/src/models/ApplicationResponse';
import { getServerSession } from 'next-auth';
import { authOptions } from '@/pages/api/auth/[...nextauth]';
import { makeSessionForAPITest } from '@/utils/api-testing';

export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
// Get session user
const session = await getServerSession(req, res, authOptions);
// for API testing only
// const session = {
// user: {
// id: '60f3f5f8f0f6f6f6f6f6f6f6',
// },
// };
let session =
(await getServerSession(req, res, authOptions)) || makeSessionForAPITest();

// Get request parameter
const {
Expand All @@ -35,7 +31,6 @@ export default async function handler(
try {
await dbConnect();

// TODO - refactor common id checking to a common util
if (!id) return res.status(400).json({ message: 'Missing id' });

// check if id is a valid mongoose id
Expand Down

0 comments on commit d799b56

Please sign in to comment.