-
Notifications
You must be signed in to change notification settings - Fork 22
feat: Get All Postings, Edit Postings, and Get Interests #256
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
raywa04
wants to merge
7
commits into
develop
Choose a base branch
from
updated_TMM-Get/Edit-Postings
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
87cef72
feat: Get All Postings and Edit Postings
raywa04 186edb6
feat: Get All Postings and Edit Postings
raywa04 c7b2432
feat: Added the get interested feature
raywa04 ee347b3
fix: Fixed file name, and edited editPosting to make it less intensiv…
raywa04 6746762
Delete pages/api/team-match-making/editPosting.ts
raywa04 8188db2
Delete pages/api/team-match-making/getInterests.ts
raywa04 e5ffae9
Delete pages/api/team-match-making/getPostings.ts
raywa04 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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', | ||
| }); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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', | ||
| }); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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', | ||
| }); | ||
| } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.