Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
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',
});
}
}
}