Skip to content

Commit

Permalink
Fix podium service?
Browse files Browse the repository at this point in the history
  • Loading branch information
VictiniX888 committed Nov 2, 2024
1 parent 940d697 commit 9dbc9aa
Show file tree
Hide file tree
Showing 11 changed files with 3,295 additions and 911 deletions.
37 changes: 32 additions & 5 deletions apps/podium-service/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import { getEnv } from '@hibiscus/env';
import { createClient } from '@supabase/supabase-js';
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';

export async function middleware(req: NextRequest) {
console.log(`Middeware ${req.method}`);
// Handle preflight requests
if (req.method === 'OPTIONS') {
return NextResponse.json(null, { status: 200 });
}

const path = req.nextUrl;

if (
Expand Down Expand Up @@ -41,9 +48,29 @@ export async function middleware(req: NextRequest) {
return NextResponse.next();
}

const res = await fetch(`${authServiceUrl}/verify-token/${accessToken}`);

if (res.status !== 200) {
const supabase = createClient(
getEnv().Hibiscus.Supabase.apiUrl,
getEnv().Hibiscus.Supabase.serviceKey
);
const user = await supabase.auth.getUser(accessToken);
if (user.error != null) {
req.nextUrl.searchParams.set('status', '401');
req.nextUrl.searchParams.set('message', 'Invalid access token');
req.nextUrl.pathname = '/api/error';
return NextResponse.redirect(req.nextUrl);
}
const userId = user.data.user?.id;
if (!userId) {
req.nextUrl.searchParams.set('status', '401');
req.nextUrl.searchParams.set('message', 'Invalid access token');
req.nextUrl.pathname = '/api/error';
return NextResponse.redirect(req.nextUrl);
}
const { data: userData, error: userError } = await supabase
.from('user_profiles')
.select('*, role (name)')
.eq('user_id', userId);
if (userError || userData?.length === 0) {
req.nextUrl.searchParams.set('status', '401');
req.nextUrl.searchParams.set('message', 'Invalid access token');
req.nextUrl.pathname = '/api/error';
Expand All @@ -65,6 +92,7 @@ export async function middleware(req: NextRequest) {
{ route: '/comments/id/[commentId]', methods: ['PUT'] },
{ route: '/comments/[projectId]', methods: ['GET'] },
{ route: '/judges/[judgeId]', methods: ['GET'] },
{ route: '/projects/[verticalId]', methods: ['GET'] },
];

const isAccessingJudgeRoute = judgeRoutes.some(({ route, methods }) => {
Expand All @@ -73,8 +101,7 @@ export async function middleware(req: NextRequest) {
});

try {
const data = await res.json();
const role = data.role;
const role = userData[0].role.name;

if (role === 'SUPERADMIN') {
return NextResponse.next();
Expand Down
22 changes: 22 additions & 0 deletions apps/podium-service/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,28 @@ const nextConfig = {
// See: https://github.com/gregberge/svgr
svgr: false,
},

async headers() {
return [
{
// matching all API routes
source: '/api/:path*',
headers: [
{ key: 'Access-Control-Allow-Credentials', value: 'true' },
{ key: 'Access-Control-Allow-Origin', value: '*' },
{
key: 'Access-Control-Allow-Methods',
value: 'GET,OPTIONS,PATCH,DELETE,POST,PUT',
},
{
key: 'Access-Control-Allow-Headers',
value:
'X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Authorization, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version',
},
],
},
];
},
};

module.exports = withNx(nextConfig);
17 changes: 14 additions & 3 deletions apps/podium-service/pages/api/judges/[judgeId].ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import { supabase } from 'apps/podium-service/libs/supabase';
import { NextApiRequest, NextApiResponse } from 'next';

export default async function handler(req: NextApiRequest, res: NextApiResponse) {
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
const { method, query, body } = req;
const judgeId = query.judgeId as string;

console.log(judgeId);
console.log(method);

switch (method) {
case 'GET':
try {
Expand All @@ -14,6 +20,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
.eq('user_id', judgeId);

if (error) {
console.log(error);
throw new Error('Failed to fetch judges');
}

Expand All @@ -29,18 +36,22 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)

return res.json(judge);
} catch (error) {
console.log(error);
return res.status(500).json({ error: 'Internal Server Error' });
}
case 'POST':
try {
const { verticalId } = body;
if (!verticalId || typeof verticalId !== 'string') {
return res.status(400).json({ error: 'Invalid request! Vertical ID is required and must be a string.'});
return res.status(400).json({
error:
'Invalid request! Vertical ID is required and must be a string.',
});
}

const { error } = await supabase
.from('judges')
.upsert({ 'user_id': judgeId, 'vertical_id': verticalId });
.upsert({ user_id: judgeId, vertical_id: verticalId });

if (error) {
throw new Error('Failed to set judge vertical');
Expand Down
29 changes: 22 additions & 7 deletions apps/podium-service/pages/api/projects/[verticalId].ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { supabase } from 'apps/podium-service/libs/supabase';
import { NextApiRequest, NextApiResponse } from 'next';

export default async function handler(req: NextApiRequest, res: NextApiResponse) {
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
const { method, query, body } = req;
const verticalId = query.verticalId as string;

Expand Down Expand Up @@ -38,8 +41,22 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
return res.status(500).json({ error: 'Internal Server Error' });
}
case 'POST':
const fields = ['name', 'teamMembers', 'description', 'imageUrl', 'devpostUrl', 'videoUrl'];
const fieldsDB = ['name', 'team', 'description', 'image_url', 'devpost_url', 'video_url'];
const fields = [
'name',
'teamMembers',
'description',
'imageUrl',
'devpostUrl',
'videoUrl',
];
const fieldsDB = [
'name',
'team',
'description',
'image_url',
'devpost_url',
'video_url',
];
const addProject = {};

try {
Expand All @@ -59,10 +76,8 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
}

addProject['vertical_id'] = verticalId;

const { error } = await supabase
.from('projects')
.insert(addProject);

const { error } = await supabase.from('projects').insert(addProject);

if (error) {
throw new Error('Failed to add new project');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { supabase } from 'apps/podium-service/libs/supabase';
import { NextApiRequest, NextApiResponse } from 'next';

export default async function handler(req: NextApiRequest, res: NextApiResponse) {
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
const { method, query, body } = req;
const verticalId = query.verticalId as string;
const userId = query.userId as string;
Expand Down Expand Up @@ -35,7 +38,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
rank: p.ranking.length > 0 ? p.ranking[0].rank : null,
}));

return res.json({ projects });
return res.json({ rankings: projects });
} catch (error) {
return res.status(500).json({ error: 'Internal Server Error' });
}
Expand Down
3 changes: 2 additions & 1 deletion apps/podium-service/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"defaultConfiguration": "development",
"options": {
"buildTarget": "podium-service:build",
"dev": true
"dev": true,
"port": 8080
},
"configurations": {
"development": {
Expand Down
Loading

0 comments on commit 9dbc9aa

Please sign in to comment.