Skip to content
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

feat: Implemented create and delete postings for team matching api #254

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
56 changes: 56 additions & 0 deletions pages/api/posting/create.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
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;
}

async function createPosting(req: NextApiRequest, res: NextApiResponse) {
try {
const postingData: PostingData = JSON.parse(req.body);
await db.collection(POSTINGS_COLLECTION).add(postingData);
return res.status(201).json({
msg: 'Posting created',
});
} catch (error) {
return res.status(500).json({
msg: 'Unexpected error. Please try again later',
});
}
}

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({
statusCode: 403,
msg: 'Request is not authorized to perform admin functionality',
});
}

return createPosting(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',
});
}
}
}
61 changes: 61 additions & 0 deletions pages/api/posting/delete.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
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;
}

async function deletePosting(req: NextApiRequest, res: NextApiResponse) {
vvyn marked this conversation as resolved.
Show resolved Hide resolved
try {
const postingData: PostingData = JSON.parse(req.body);
const snapshot = await db.collection(POSTINGS_COLLECTION).doc(postingData.postingId).get();

if (!snapshot.exists) {
return res.status(404).json({
msg: 'Posting not found',
});
}

await db.collection(POSTINGS_COLLECTION).doc(postingData.postingId).delete();
return res.status(200).json({
msg: 'Posting deleted',
});
} catch (error) {
return res.status(500).json({
msg: 'Unexpected error. Please try again later',
});
}
}

async function handleDeletionRequest(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 deletePosting(req, res);
}

export default function handler(req: NextApiRequest, res: NextApiResponse) {
const { method } = req;
switch (method) {
case 'POST': {
return handleDeletionRequest(req, res);
}
default: {
return res.status(404).json({
msg: 'Route not found',
});
}
}
}