From 87cef72bcaff647b4c3345d3ab6128a4992639eb Mon Sep 17 00:00:00 2001 From: Rayyan Waris Date: Sun, 31 Mar 2024 01:08:51 -0500 Subject: [PATCH 1/7] feat: Get All Postings and Edit Postings Two new features. Get All Postings gets all of the postings currently stored in the firebase and displays them. Edit Postings gets a single postings and allows hacker to edit/update that posting --- pages/team-match-making/editPosting.ts | 81 ++++++++++++++++++++++++++ pages/team-match-making/getPostings.ts | 60 +++++++++++++++++++ 2 files changed, 141 insertions(+) create mode 100644 pages/team-match-making/editPosting.ts create mode 100644 pages/team-match-making/getPostings.ts diff --git a/pages/team-match-making/editPosting.ts b/pages/team-match-making/editPosting.ts new file mode 100644 index 00000000..5507435a --- /dev/null +++ b/pages/team-match-making/editPosting.ts @@ -0,0 +1,81 @@ +import { firestore } from 'firebase-admin'; +import { NextApiRequest, NextApiResponse } from 'next'; +import initializeApi from '../../lib/admin/init'; +import { userIsAuthorized } from '../../lib/authorization/check-authorization'; + +initializeApi(); +const db = firestore(); +const POSTINGS_COLLECTION = '/postings'; + +interface PostingData { + postingId: string; + numberOfPeopleWanted: number; + skillSet: string; +} + +// Define the function to edit a posting, which is asynchronous and takes a Next.js API request and response. +async function editPosting(req: NextApiRequest, res: NextApiResponse) { + // Destructure and extract postingId, numberOfPeopleWanted, and skillSet from the request body. + const { postingId, numberOfPeopleWanted, skillSet }: PostingData = req.body; + + // Validate the request body to ensure that postingId, numberOfPeopleWanted, and skillSet are provided. + if (!postingId || numberOfPeopleWanted === undefined || !skillSet) { + // If any required fields are missing, return a 400 status code and an error message. + return res.status(400).json({ msg: 'Missing required fields' }); + } + + try { + // Reference the specific posting document in the Firestore database using the postingId. + const postingRef = db.collection(POSTINGS_COLLECTION).doc(postingId); + // Attempt to retrieve the document from Firestore. + const postingDoc = await postingRef.get(); + + // Check if the document exists. If not, return a 404 status code and an error message. + if (!postingDoc.exists) { + return res.status(404).json({ msg: 'Posting not found' }); + } + + // If the document exists, update it with the new numberOfPeopleWanted and skillSet values. + await postingRef.update({ + numberOfPeopleWanted, + skillSet, + }); + + // After updating, retrieve the updated document to include in the response. + const updatedDoc = await postingRef.get(); + // Return a 200 status code, success message, and the updated posting data. + return res + .status(200) + .json({ msg: 'Posting updated successfully', posting: updatedDoc.data() }); + } catch (error) { + // If an error occurs during the process, log the error and return a 500 status code with an error message. + console.error('Error updating posting:', error); + return res.status(500).json({ msg: 'Internal server error' }); + } +} + +async function handlePostRequest(req: NextApiRequest, res: NextApiResponse) { + const userToken = req.headers['authorization'] as string; + const isAuthorized = await userIsAuthorized(userToken, ['hacker']); + if (!isAuthorized) { + return res.status(403).json({ + msg: 'Request is not allowed to perform hacker functionality', + }); + } + + return editPosting(req, res); +} + +export default function handler(req: NextApiRequest, res: NextApiResponse) { + const { method } = req; + switch (method) { + case 'POST': { + return handlePostRequest(req, res); + } + default: { + return res.status(404).json({ + msg: 'Route not found', + }); + } + } +} diff --git a/pages/team-match-making/getPostings.ts b/pages/team-match-making/getPostings.ts new file mode 100644 index 00000000..0c7cc39f --- /dev/null +++ b/pages/team-match-making/getPostings.ts @@ -0,0 +1,60 @@ +import { firestore } from 'firebase-admin'; +import { NextApiRequest, NextApiResponse } from 'next'; +import initializeApi from '../../lib/admin/init'; +import { userIsAuthorized } from '../../lib/authorization/check-authorization'; + +initializeApi(); +const db = firestore(); +const POSTINGS_COLLECTION = '/postings'; + +interface PostingData { + postingId: string; +} + +// Define an asynchronous function to retrieve all postings from the database. +async function getAllPostings(req: NextApiRequest, res: NextApiResponse) { + try { + // Attempt to get all documents from the 'POSTINGS_COLLECTION' in Firestore. + const postingsSnapshot = await db.collection(POSTINGS_COLLECTION).get(); + + // Transform the documents snapshot into an array of objects. Each object contains + // the document ID and the document's data (fields like numberOfPeopleWanted, skillSet, etc.). + const postings = postingsSnapshot.docs.map((doc) => ({ id: doc.id, ...doc.data() })); + + // Return a 200 OK status with the array of postings as JSON. This response + // includes all postings currently in the 'POSTINGS_COLLECTION'. + return res.status(200).json(postings); + } catch (error) { + // If an error occurs during the process of retrieving or processing the postings, + // log the error to the console for debugging purposes and return a 500 Internal Server + // Error status with an error message. + console.error('Error retrieving postings:', error); + return res.status(500).json({ msg: 'Internal server error' }); + } +} + +async function handleRequest(req: NextApiRequest, res: NextApiResponse) { + const userToken = req.headers['authorization'] as string; + const isAuthorized = await userIsAuthorized(userToken, ['hacker']); + if (!isAuthorized) { + return res.status(403).json({ + msg: 'Request is not allowed to perform this functionality', + }); + } + + return getAllPostings(req, res); +} + +export default function handler(req: NextApiRequest, res: NextApiResponse) { + const { method } = req; + switch (method) { + case 'GET': { + return handleRequest(req, res); + } + default: { + return res.status(404).json({ + msg: 'Route not found', + }); + } + } +} From 186edb6b363bffcd8bf87a53b56cfaa2cb147028 Mon Sep 17 00:00:00 2001 From: Rayyan Waris Date: Sun, 31 Mar 2024 02:01:53 -0500 Subject: [PATCH 2/7] feat: Get All Postings and Edit Postings Two new features. Get All Postings gets all of the postings currently stored in the firebase and displays them. Edit Postings gets a single postings and allows hacker to edit/update that posting *Fixed initial issue when deploying the code* --- pages/{ => api}/team-match-making/editPosting.ts | 4 ++-- pages/{ => api}/team-match-making/getPostings.ts | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) rename pages/{ => api}/team-match-making/editPosting.ts (95%) rename pages/{ => api}/team-match-making/getPostings.ts (93%) diff --git a/pages/team-match-making/editPosting.ts b/pages/api/team-match-making/editPosting.ts similarity index 95% rename from pages/team-match-making/editPosting.ts rename to pages/api/team-match-making/editPosting.ts index 5507435a..a6b189e5 100644 --- a/pages/team-match-making/editPosting.ts +++ b/pages/api/team-match-making/editPosting.ts @@ -1,7 +1,7 @@ import { firestore } from 'firebase-admin'; import { NextApiRequest, NextApiResponse } from 'next'; -import initializeApi from '../../lib/admin/init'; -import { userIsAuthorized } from '../../lib/authorization/check-authorization'; +import initializeApi from '../../../lib/admin/init'; +import { userIsAuthorized } from '../../../lib/authorization/check-authorization'; initializeApi(); const db = firestore(); diff --git a/pages/team-match-making/getPostings.ts b/pages/api/team-match-making/getPostings.ts similarity index 93% rename from pages/team-match-making/getPostings.ts rename to pages/api/team-match-making/getPostings.ts index 0c7cc39f..8fa1dc25 100644 --- a/pages/team-match-making/getPostings.ts +++ b/pages/api/team-match-making/getPostings.ts @@ -1,7 +1,7 @@ import { firestore } from 'firebase-admin'; import { NextApiRequest, NextApiResponse } from 'next'; -import initializeApi from '../../lib/admin/init'; -import { userIsAuthorized } from '../../lib/authorization/check-authorization'; +import initializeApi from '../../../lib/admin/init'; +import { userIsAuthorized } from '../../../lib/authorization/check-authorization'; initializeApi(); const db = firestore(); From c7b24322495c4686f25ca655501e41fab3d205da Mon Sep 17 00:00:00 2001 From: Rayyan Waris Date: Sat, 6 Apr 2024 21:42:51 -0500 Subject: [PATCH 3/7] feat: Added the get interested feature --- pages/api/team-match-making/getInterests.ts | 72 +++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 pages/api/team-match-making/getInterests.ts diff --git a/pages/api/team-match-making/getInterests.ts b/pages/api/team-match-making/getInterests.ts new file mode 100644 index 00000000..c31bd08e --- /dev/null +++ b/pages/api/team-match-making/getInterests.ts @@ -0,0 +1,72 @@ +import { firestore } from 'firebase-admin'; +import { NextApiRequest, NextApiResponse } from 'next'; +import initializeApi from '../../../lib/admin/init'; +import { userIsAuthorized } from '../../../lib/authorization/check-authorization'; + +initializeApi(); +const db = firestore(); + +// Enhanced function to get all users interested in a specific posting +async function getInterestedUsers(req: NextApiRequest, res: NextApiResponse) { + try { + // Validate the input: ensure a postingId is provided + const postingId = req.query.postingId as string; + if (!postingId) { + return res.status(400).json({ error: 'Posting ID must be provided.' }); + } + + // Reference to the interestedPeople sub-collection for the given postingId + const interestedRef = db.collection('postings').doc(postingId).collection('interestedPeople'); + + // Fetch the documents from the sub-collection + const snapshot = await interestedRef.get(); + + // If there are no interested users, return an empty array with a message + if (snapshot.empty) { + return res + .status(404) + .json({ message: 'No interested users found for this posting.', interestedUsers: [] }); + } + + // Map over the documents to extract the data + const interestedUsers = snapshot.docs.map((doc) => { + const userData = doc.data(); + return { + name: userData.name, + email: userData.email, + }; + }); + + // Return the list of interested users + return res.status(200).json(interestedUsers); + } catch (error) { + console.error('Failed to retrieve interested users:', error); + // Return a generic error message + return res.status(500).json({ error: 'An error occurred while fetching interested users.' }); + } +} + +async function handleGetRequest(req: NextApiRequest, res: NextApiResponse) { + const userToken = req.headers['authorization'] as string; + const isAuthorized = await userIsAuthorized(userToken, ['hacker']); + if (!isAuthorized) { + return res.status(403).json({ + msg: 'Request is not allowed to perform this functionality', + }); + } + return getInterestedUsers(req, res); +} + +export default function handler(req: NextApiRequest, res: NextApiResponse) { + const { method } = req; + switch (method) { + case 'GET': { + return handleGetRequest(req, res); + } + default: { + return res.status(404).json({ + msg: 'Route not found', + }); + } + } +} From ee347b3492e0a8b2c3287e243a2dd56e3f070b45 Mon Sep 17 00:00:00 2001 From: Rayyan Waris Date: Mon, 22 Apr 2024 21:25:56 -0500 Subject: [PATCH 4/7] fix: Fixed file name, and edited editPosting to make it less intensive on reads --- pages/api/matching/editPosting.ts | 79 ++++++++++++++++++++++++++++++ pages/api/matching/getInterests.ts | 72 +++++++++++++++++++++++++++ pages/api/matching/getPostings.ts | 60 +++++++++++++++++++++++ 3 files changed, 211 insertions(+) create mode 100644 pages/api/matching/editPosting.ts create mode 100644 pages/api/matching/getInterests.ts create mode 100644 pages/api/matching/getPostings.ts diff --git a/pages/api/matching/editPosting.ts b/pages/api/matching/editPosting.ts new file mode 100644 index 00000000..49549501 --- /dev/null +++ b/pages/api/matching/editPosting.ts @@ -0,0 +1,79 @@ +import { firestore } from 'firebase-admin'; +import { NextApiRequest, NextApiResponse } from 'next'; +import initializeApi from '../../../lib/admin/init'; +import { userIsAuthorized } from '../../../lib/authorization/check-authorization'; + +initializeApi(); +const db = firestore(); +const POSTINGS_COLLECTION = '/postings'; + +interface PostingData { + postingId: string; + numberOfPeopleWanted: number; + skillSet: string; +} + +// Define the function to edit a posting, which is asynchronous and takes a Next.js API request and response. +async function editPosting(req: NextApiRequest, res: NextApiResponse) { + // Destructure and extract postingId, numberOfPeopleWanted, and skillSet from the request body. + const { postingId, numberOfPeopleWanted, skillSet }: PostingData = req.body; + + // Validate the request body to ensure that postingId, numberOfPeopleWanted, and skillSet are provided. + if (!postingId || numberOfPeopleWanted === undefined || !skillSet) { + // If any required fields are missing, return a 400 status code and an error message. + return res.status(400).json({ msg: 'Missing required fields' }); + } + + try { + // Reference the specific posting document in the Firestore database using the postingId. + const postingRef = db.collection(POSTINGS_COLLECTION).doc(postingId); + // Attempt to retrieve the document from Firestore. + const postingDoc = await postingRef.get(); + + // Check if the document exists. If not, return a 404 status code and an error message. + if (!postingDoc.exists) { + return res.status(404).json({ msg: 'Posting not found' }); + } + + // If the document exists, update it with the new numberOfPeopleWanted and skillSet values. + await postingRef.update({ + numberOfPeopleWanted, + skillSet, + }); + + return res.status(200).json({ + msg: 'Posting updated successfully', + posting: { ...postingDoc.data(), numberOfPeopleWanted, skillSet }, + }); + } catch (error) { + // If an error occurs during the process, log the error and return a 500 status code with an error message. + console.error('Error updating posting:', error); + return res.status(500).json({ msg: 'Internal server error' }); + } +} + +async function handlePostRequest(req: NextApiRequest, res: NextApiResponse) { + const userToken = req.headers['authorization'] as string; + const isAuthorized = await userIsAuthorized(userToken, ['hacker']); + if (!isAuthorized) { + return res.status(403).json({ + msg: 'Request is not allowed to perform hacker functionality', + }); + } + + return editPosting(req, res); +} + +export default function handler(req: NextApiRequest, res: NextApiResponse) { + const { method } = req; + switch (method) { + case 'POST': { + return handlePostRequest(req, res); + } + default: { + return res.status(404).json({ + msg: 'Route not found', + }); + } + } +} diff --git a/pages/api/matching/getInterests.ts b/pages/api/matching/getInterests.ts new file mode 100644 index 00000000..c31bd08e --- /dev/null +++ b/pages/api/matching/getInterests.ts @@ -0,0 +1,72 @@ +import { firestore } from 'firebase-admin'; +import { NextApiRequest, NextApiResponse } from 'next'; +import initializeApi from '../../../lib/admin/init'; +import { userIsAuthorized } from '../../../lib/authorization/check-authorization'; + +initializeApi(); +const db = firestore(); + +// Enhanced function to get all users interested in a specific posting +async function getInterestedUsers(req: NextApiRequest, res: NextApiResponse) { + try { + // Validate the input: ensure a postingId is provided + const postingId = req.query.postingId as string; + if (!postingId) { + return res.status(400).json({ error: 'Posting ID must be provided.' }); + } + + // Reference to the interestedPeople sub-collection for the given postingId + const interestedRef = db.collection('postings').doc(postingId).collection('interestedPeople'); + + // Fetch the documents from the sub-collection + const snapshot = await interestedRef.get(); + + // If there are no interested users, return an empty array with a message + if (snapshot.empty) { + return res + .status(404) + .json({ message: 'No interested users found for this posting.', interestedUsers: [] }); + } + + // Map over the documents to extract the data + const interestedUsers = snapshot.docs.map((doc) => { + const userData = doc.data(); + return { + name: userData.name, + email: userData.email, + }; + }); + + // Return the list of interested users + return res.status(200).json(interestedUsers); + } catch (error) { + console.error('Failed to retrieve interested users:', error); + // Return a generic error message + return res.status(500).json({ error: 'An error occurred while fetching interested users.' }); + } +} + +async function handleGetRequest(req: NextApiRequest, res: NextApiResponse) { + const userToken = req.headers['authorization'] as string; + const isAuthorized = await userIsAuthorized(userToken, ['hacker']); + if (!isAuthorized) { + return res.status(403).json({ + msg: 'Request is not allowed to perform this functionality', + }); + } + return getInterestedUsers(req, res); +} + +export default function handler(req: NextApiRequest, res: NextApiResponse) { + const { method } = req; + switch (method) { + case 'GET': { + return handleGetRequest(req, res); + } + default: { + return res.status(404).json({ + msg: 'Route not found', + }); + } + } +} diff --git a/pages/api/matching/getPostings.ts b/pages/api/matching/getPostings.ts new file mode 100644 index 00000000..8fa1dc25 --- /dev/null +++ b/pages/api/matching/getPostings.ts @@ -0,0 +1,60 @@ +import { firestore } from 'firebase-admin'; +import { NextApiRequest, NextApiResponse } from 'next'; +import initializeApi from '../../../lib/admin/init'; +import { userIsAuthorized } from '../../../lib/authorization/check-authorization'; + +initializeApi(); +const db = firestore(); +const POSTINGS_COLLECTION = '/postings'; + +interface PostingData { + postingId: string; +} + +// Define an asynchronous function to retrieve all postings from the database. +async function getAllPostings(req: NextApiRequest, res: NextApiResponse) { + try { + // Attempt to get all documents from the 'POSTINGS_COLLECTION' in Firestore. + const postingsSnapshot = await db.collection(POSTINGS_COLLECTION).get(); + + // Transform the documents snapshot into an array of objects. Each object contains + // the document ID and the document's data (fields like numberOfPeopleWanted, skillSet, etc.). + const postings = postingsSnapshot.docs.map((doc) => ({ id: doc.id, ...doc.data() })); + + // Return a 200 OK status with the array of postings as JSON. This response + // includes all postings currently in the 'POSTINGS_COLLECTION'. + return res.status(200).json(postings); + } catch (error) { + // If an error occurs during the process of retrieving or processing the postings, + // log the error to the console for debugging purposes and return a 500 Internal Server + // Error status with an error message. + console.error('Error retrieving postings:', error); + return res.status(500).json({ msg: 'Internal server error' }); + } +} + +async function handleRequest(req: NextApiRequest, res: NextApiResponse) { + const userToken = req.headers['authorization'] as string; + const isAuthorized = await userIsAuthorized(userToken, ['hacker']); + if (!isAuthorized) { + return res.status(403).json({ + msg: 'Request is not allowed to perform this functionality', + }); + } + + return getAllPostings(req, res); +} + +export default function handler(req: NextApiRequest, res: NextApiResponse) { + const { method } = req; + switch (method) { + case 'GET': { + return handleRequest(req, res); + } + default: { + return res.status(404).json({ + msg: 'Route not found', + }); + } + } +} From 674676280404564aca0f051f9f78ea7f9ab6b343 Mon Sep 17 00:00:00 2001 From: raywa04 <66043704+raywa04@users.noreply.github.com> Date: Mon, 22 Apr 2024 21:29:33 -0500 Subject: [PATCH 5/7] Delete pages/api/team-match-making/editPosting.ts --- pages/api/team-match-making/editPosting.ts | 81 ---------------------- 1 file changed, 81 deletions(-) delete mode 100644 pages/api/team-match-making/editPosting.ts diff --git a/pages/api/team-match-making/editPosting.ts b/pages/api/team-match-making/editPosting.ts deleted file mode 100644 index a6b189e5..00000000 --- a/pages/api/team-match-making/editPosting.ts +++ /dev/null @@ -1,81 +0,0 @@ -import { firestore } from 'firebase-admin'; -import { NextApiRequest, NextApiResponse } from 'next'; -import initializeApi from '../../../lib/admin/init'; -import { userIsAuthorized } from '../../../lib/authorization/check-authorization'; - -initializeApi(); -const db = firestore(); -const POSTINGS_COLLECTION = '/postings'; - -interface PostingData { - postingId: string; - numberOfPeopleWanted: number; - skillSet: string; -} - -// Define the function to edit a posting, which is asynchronous and takes a Next.js API request and response. -async function editPosting(req: NextApiRequest, res: NextApiResponse) { - // Destructure and extract postingId, numberOfPeopleWanted, and skillSet from the request body. - const { postingId, numberOfPeopleWanted, skillSet }: PostingData = req.body; - - // Validate the request body to ensure that postingId, numberOfPeopleWanted, and skillSet are provided. - if (!postingId || numberOfPeopleWanted === undefined || !skillSet) { - // If any required fields are missing, return a 400 status code and an error message. - return res.status(400).json({ msg: 'Missing required fields' }); - } - - try { - // Reference the specific posting document in the Firestore database using the postingId. - const postingRef = db.collection(POSTINGS_COLLECTION).doc(postingId); - // Attempt to retrieve the document from Firestore. - const postingDoc = await postingRef.get(); - - // Check if the document exists. If not, return a 404 status code and an error message. - if (!postingDoc.exists) { - return res.status(404).json({ msg: 'Posting not found' }); - } - - // If the document exists, update it with the new numberOfPeopleWanted and skillSet values. - await postingRef.update({ - numberOfPeopleWanted, - skillSet, - }); - - // After updating, retrieve the updated document to include in the response. - const updatedDoc = await postingRef.get(); - // Return a 200 status code, success message, and the updated posting data. - return res - .status(200) - .json({ msg: 'Posting updated successfully', posting: updatedDoc.data() }); - } catch (error) { - // If an error occurs during the process, log the error and return a 500 status code with an error message. - console.error('Error updating posting:', error); - return res.status(500).json({ msg: 'Internal server error' }); - } -} - -async function handlePostRequest(req: NextApiRequest, res: NextApiResponse) { - const userToken = req.headers['authorization'] as string; - const isAuthorized = await userIsAuthorized(userToken, ['hacker']); - if (!isAuthorized) { - return res.status(403).json({ - msg: 'Request is not allowed to perform hacker functionality', - }); - } - - return editPosting(req, res); -} - -export default function handler(req: NextApiRequest, res: NextApiResponse) { - const { method } = req; - switch (method) { - case 'POST': { - return handlePostRequest(req, res); - } - default: { - return res.status(404).json({ - msg: 'Route not found', - }); - } - } -} From 8188db2c1588fe5439ae7ac0e6ffede67b8e95be Mon Sep 17 00:00:00 2001 From: raywa04 <66043704+raywa04@users.noreply.github.com> Date: Mon, 22 Apr 2024 21:29:49 -0500 Subject: [PATCH 6/7] Delete pages/api/team-match-making/getInterests.ts --- pages/api/team-match-making/getInterests.ts | 72 --------------------- 1 file changed, 72 deletions(-) delete mode 100644 pages/api/team-match-making/getInterests.ts diff --git a/pages/api/team-match-making/getInterests.ts b/pages/api/team-match-making/getInterests.ts deleted file mode 100644 index c31bd08e..00000000 --- a/pages/api/team-match-making/getInterests.ts +++ /dev/null @@ -1,72 +0,0 @@ -import { firestore } from 'firebase-admin'; -import { NextApiRequest, NextApiResponse } from 'next'; -import initializeApi from '../../../lib/admin/init'; -import { userIsAuthorized } from '../../../lib/authorization/check-authorization'; - -initializeApi(); -const db = firestore(); - -// Enhanced function to get all users interested in a specific posting -async function getInterestedUsers(req: NextApiRequest, res: NextApiResponse) { - try { - // Validate the input: ensure a postingId is provided - const postingId = req.query.postingId as string; - if (!postingId) { - return res.status(400).json({ error: 'Posting ID must be provided.' }); - } - - // Reference to the interestedPeople sub-collection for the given postingId - const interestedRef = db.collection('postings').doc(postingId).collection('interestedPeople'); - - // Fetch the documents from the sub-collection - const snapshot = await interestedRef.get(); - - // If there are no interested users, return an empty array with a message - if (snapshot.empty) { - return res - .status(404) - .json({ message: 'No interested users found for this posting.', interestedUsers: [] }); - } - - // Map over the documents to extract the data - const interestedUsers = snapshot.docs.map((doc) => { - const userData = doc.data(); - return { - name: userData.name, - email: userData.email, - }; - }); - - // Return the list of interested users - return res.status(200).json(interestedUsers); - } catch (error) { - console.error('Failed to retrieve interested users:', error); - // Return a generic error message - return res.status(500).json({ error: 'An error occurred while fetching interested users.' }); - } -} - -async function handleGetRequest(req: NextApiRequest, res: NextApiResponse) { - const userToken = req.headers['authorization'] as string; - const isAuthorized = await userIsAuthorized(userToken, ['hacker']); - if (!isAuthorized) { - return res.status(403).json({ - msg: 'Request is not allowed to perform this functionality', - }); - } - return getInterestedUsers(req, res); -} - -export default function handler(req: NextApiRequest, res: NextApiResponse) { - const { method } = req; - switch (method) { - case 'GET': { - return handleGetRequest(req, res); - } - default: { - return res.status(404).json({ - msg: 'Route not found', - }); - } - } -} From e5ffae9a85eefcb40cd1534e859b9f948169c97e Mon Sep 17 00:00:00 2001 From: raywa04 <66043704+raywa04@users.noreply.github.com> Date: Mon, 22 Apr 2024 21:30:04 -0500 Subject: [PATCH 7/7] Delete pages/api/team-match-making/getPostings.ts --- pages/api/team-match-making/getPostings.ts | 60 ---------------------- 1 file changed, 60 deletions(-) delete mode 100644 pages/api/team-match-making/getPostings.ts diff --git a/pages/api/team-match-making/getPostings.ts b/pages/api/team-match-making/getPostings.ts deleted file mode 100644 index 8fa1dc25..00000000 --- a/pages/api/team-match-making/getPostings.ts +++ /dev/null @@ -1,60 +0,0 @@ -import { firestore } from 'firebase-admin'; -import { NextApiRequest, NextApiResponse } from 'next'; -import initializeApi from '../../../lib/admin/init'; -import { userIsAuthorized } from '../../../lib/authorization/check-authorization'; - -initializeApi(); -const db = firestore(); -const POSTINGS_COLLECTION = '/postings'; - -interface PostingData { - postingId: string; -} - -// Define an asynchronous function to retrieve all postings from the database. -async function getAllPostings(req: NextApiRequest, res: NextApiResponse) { - try { - // Attempt to get all documents from the 'POSTINGS_COLLECTION' in Firestore. - const postingsSnapshot = await db.collection(POSTINGS_COLLECTION).get(); - - // Transform the documents snapshot into an array of objects. Each object contains - // the document ID and the document's data (fields like numberOfPeopleWanted, skillSet, etc.). - const postings = postingsSnapshot.docs.map((doc) => ({ id: doc.id, ...doc.data() })); - - // Return a 200 OK status with the array of postings as JSON. This response - // includes all postings currently in the 'POSTINGS_COLLECTION'. - return res.status(200).json(postings); - } catch (error) { - // If an error occurs during the process of retrieving or processing the postings, - // log the error to the console for debugging purposes and return a 500 Internal Server - // Error status with an error message. - console.error('Error retrieving postings:', error); - return res.status(500).json({ msg: 'Internal server error' }); - } -} - -async function handleRequest(req: NextApiRequest, res: NextApiResponse) { - const userToken = req.headers['authorization'] as string; - const isAuthorized = await userIsAuthorized(userToken, ['hacker']); - if (!isAuthorized) { - return res.status(403).json({ - msg: 'Request is not allowed to perform this functionality', - }); - } - - return getAllPostings(req, res); -} - -export default function handler(req: NextApiRequest, res: NextApiResponse) { - const { method } = req; - switch (method) { - case 'GET': { - return handleRequest(req, res); - } - default: { - return res.status(404).json({ - msg: 'Route not found', - }); - } - } -}