-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathjudges.ts
More file actions
64 lines (55 loc) 路 1.79 KB
/
Copy pathjudges.ts
File metadata and controls
64 lines (55 loc) 路 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { supabase } from 'apps/podium-service/libs/supabase';
import { NextApiRequest, NextApiResponse } from 'next';
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
const { method, body } = req;
switch (method) {
case 'OPTIONS':
return res.status(200).send('ok');
case 'GET':
try {
const { data: judgeData, error: judgeError } = await supabase
.schema('public')
.from('user_profiles')
.select('*')
.eq('role', 7);
const { data: judgeVerticalData, error: judgeVerticalError } =
await supabase.from('judges').select('*, verticals!inner(name)');
if (judgeError || judgeVerticalError) {
throw new Error('Failed to fetch judges');
}
if (judgeData === null) {
return res.status(404).json({ error: 'No judges found' });
}
const judges: JudgeData[] = judgeData.map((j: any) => ({
id: j.user_id,
name: j.first_name + ' ' + j.last_name,
email: j.email,
verticalId: null,
verticalName: null,
}));
// For faster lookup later
const judgesMap = new Map();
judges.forEach((judge) => {
judgesMap.set(judge.id, judge);
});
judgeVerticalData.forEach((j: any) => {
const judge = judgesMap.get(j.user_id);
if (judge) {
judge.verticalId = j.vertical_id;
judge.verticalName = j.verticals.name;
}
});
return res.json({ judges });
} catch (error) {
return res.status(500).json({ error: 'Internal Server Error' });
}
case 'POST':
return res.status(501).json({ error: 'Not implemented' });
default:
res.status(405).end();
break;
}
}