Skip to content

Commit 01da0e5

Browse files
authored
Merge pull request #61 from HackDavis/feat/team-grouping
Feat/team grouping
2 parents ccaaac9 + 6241d7a commit 01da0e5

File tree

29 files changed

+199
-35
lines changed

29 files changed

+199
-35
lines changed
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
'use server';
22
import FormToJSON from '@utils/form/FormToJSON';
3-
import { updateSubmission } from '@datalib/submissions/updateSubmission';
3+
import { UpdateSubmission } from '@datalib/submissions/updateSubmission';
44

5-
export default async function UpdateSubmission(formData: FormData) {
5+
export default async function updateSubmission(formData: FormData) {
66
const { judge_id, team_id, ...rest } = FormToJSON(formData);
7-
await updateSubmission(judge_id, team_id, rest);
7+
await UpdateSubmission(judge_id, team_id, rest);
88
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
'use server';
2+
3+
export default async function groupJudges() {}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use server';
2+
import { GetManyJudgeGroups } from '@datalib/judgeGroups/getJudgeGroup';
3+
import { GetManyTeams } from '@datalib/teams/getTeam';
4+
import { LinkManyJudgeGroupsToTeams } from '@datalib/judgeGroups/linkJudgeGroupToTeam';
5+
import matchingAlgorithm from '@utils/grouping/matchingAlgorithm';
6+
7+
export default async function matchTeams() {
8+
const judgeGroups = await (await GetManyJudgeGroups()).json();
9+
const teams = await (await GetManyTeams()).json();
10+
const matches = matchingAlgorithm(judgeGroups.body, teams.body);
11+
LinkManyJudgeGroupsToTeams(matches);
12+
}

app/(api)/_actions/logic/pairJudges.ts

Whitespace-only changes.

app/(api)/_datalib/judgeGroups/linkJudgeGroupToTeam.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,46 @@ import { ObjectId } from 'mongodb';
33

44
import { getDatabase } from '@utils/mongodb/mongoClient.mjs';
55
import isBodyEmpty from '@utils/request/isBodyEmpty';
6+
import parseAndReplace from '@utils/request/parseAndReplace';
67
import NoContentError from '@utils/response/NoContentError';
78
import HttpError from '@utils/response/HttpError';
89
import { NotFoundError, DuplicateError } from '@utils/response/Errors';
910

11+
export const LinkManyJudgeGroupsToTeams = async (body: object[]) => {
12+
try {
13+
if (isBodyEmpty(body)) {
14+
throw new NoContentError();
15+
}
16+
17+
const parsedBody = parseAndReplace(body);
18+
19+
const db = await getDatabase();
20+
const creationStatus = await db
21+
.collection('judgeGroupToTeams')
22+
.insertMany(parsedBody);
23+
24+
const links = await db
25+
.collection('judgeGroupToTeams')
26+
.find({
27+
_id: {
28+
$in: Object.values(creationStatus.insertedIds).map((id: any) => id),
29+
},
30+
})
31+
.toArray();
32+
33+
return NextResponse.json(
34+
{ ok: true, body: await links, error: null },
35+
{ status: 201 }
36+
);
37+
} catch (e) {
38+
const error = e as HttpError;
39+
return NextResponse.json(
40+
{ ok: false, body: null, error: error.message },
41+
{ status: error.status || 400 }
42+
);
43+
}
44+
};
45+
1046
export const LinkJudgeGroupToTeam = async (body: {
1147
judge_group_id: string;
1248
team_id: string;

app/(api)/_datalib/submissions/createSubmission.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import HttpError from '@utils/response/HttpError';
88
import parseAndReplace from '@utils/request/parseAndReplace';
99
import { NotFoundError, DuplicateError } from '@utils/response/Errors';
1010

11-
export const createSubmission = async (body: {
11+
export const CreateSubmission = async (body: {
1212
judge_id: string;
1313
team_id: string;
1414
}) => {

app/(api)/_datalib/submissions/deleteSubmission.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { getDatabase } from '@utils/mongodb/mongoClient.mjs';
55
import NotFoundError from '@utils/response/NotFoundError';
66
import HttpError from '@utils/response/HttpError';
77

8-
export const deleteSubmission = async (judge_id: string, team_id: string) => {
8+
export const DeleteSubmission = async (judge_id: string, team_id: string) => {
99
try {
1010
const judge_object_id = new ObjectId(judge_id);
1111
const team_object_id = new ObjectId(team_id);

app/(api)/_datalib/submissions/getSubmissions.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { getDatabase } from '@utils/mongodb/mongoClient.mjs';
33
import HttpError from '@utils/response/HttpError';
44
import { ObjectId } from 'mongodb';
55

6-
export const getSubmissions = async (query: object = {}) => {
6+
export const GetManySubmissions = async (query: object = {}) => {
77
try {
88
const db = await getDatabase();
99
const submissions = await db
@@ -24,7 +24,7 @@ export const getSubmissions = async (query: object = {}) => {
2424
}
2525
};
2626

27-
export const getSubmission = async (judge_id: string, team_id: string) => {
27+
export const GetSubmission = async (judge_id: string, team_id: string) => {
2828
try {
2929
const judge_object_id = new ObjectId(judge_id);
3030
const team_object_id = new ObjectId(team_id);

app/(api)/_datalib/submissions/updateSubmission.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import isBodyEmpty from '@utils/request/isBodyEmpty';
88
import parseAndReplace from '@utils/request/parseAndReplace';
99
import NoContentError from '@utils/response/NoContentError';
1010

11-
export const updateSubmission = async (
11+
export const UpdateSubmission = async (
1212
judge_id: string,
1313
team_id: string,
1414
body: object

app/(api)/_datalib/teams/createTeams.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
import Team from '@typeDefs/teams';
1111
import tracks from '../../_data/tracks.json' assert { type: 'json' };
1212

13-
export const createTeams = async (body: object[]) => {
13+
export const CreateManyTeams = async (body: object[]) => {
1414
try {
1515
if (isBodyEmpty(body)) {
1616
throw new NoContentError();

app/(api)/_datalib/teams/deleteTeam.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { getDatabase } from '@utils/mongodb/mongoClient.mjs';
55
import NotFoundError from '@utils/response/NotFoundError';
66
import HttpError from '@utils/response/HttpError';
77

8-
export const deleteTeam = async (id: string) => {
8+
export const DeleteTeam = async (id: string) => {
99
try {
1010
const object_id = new ObjectId(id);
1111
const db = await getDatabase();

app/(api)/_datalib/teams/getTeam.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { getDatabase } from '@utils/mongodb/mongoClient.mjs';
33
import { HttpError, NotFoundError } from '@utils/response/Errors';
44
import { ObjectId } from 'mongodb';
55

6-
export const getTeam = async (id: string) => {
6+
export const GetTeam = async (id: string) => {
77
try {
88
const object_id = new ObjectId(id);
99
const db = await getDatabase();
@@ -28,7 +28,7 @@ export const getTeam = async (id: string) => {
2828
}
2929
};
3030

31-
export const getTeams = async (query: object = {}) => {
31+
export const GetManyTeams = async (query: object = {}) => {
3232
try {
3333
const db = await getDatabase();
3434
const teams = await db

app/(api)/_datalib/teams/updateTeam.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
NotFoundError,
1212
} from '@utils/response/Errors';
1313

14-
export const updateTeam = async (id: string, body: object) => {
14+
export const UpdateTeam = async (id: string, body: object) => {
1515
try {
1616
const object_id = new ObjectId(id);
1717
if (isBodyEmpty(body)) {

app/(api)/_schema/Team.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const Team = {
1919
},
2020
tracks: {
2121
bsonType: 'array',
22-
maxItems: 4,
22+
maxItems: 5,
2323
items: {
2424
enum: tracks.map((track) => track.name),
2525
description: 'track must be one of the valid tracks',

app/(api)/_utils/grouping/groupJudges.ts renamed to app/(api)/_utils/grouping/groupingAlgorithm.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Judge from '@typeDefs/judges';
22

3-
export default function groupJudges(judges: Judge[]) {
3+
export default function groupingAlgorithm(judges: Judge[]) {
44
const techJudges = judges.filter((judge) => judge.specialty === 'tech');
55
const generalJudges = judges.filter((judge) => judge.specialty === 'general');
66
const desJudges = judges.filter((judge) => judge.specialty === 'design');
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import JudgeGroup from '@typeDefs/judgeGroups';
2+
import Team from '@typeDefs/teams';
3+
import tracks from '../../_data/tracks.json' assert { type: 'json' };
4+
5+
interface Match {
6+
judge_group_id: string;
7+
team_id: string;
8+
}
9+
10+
function createMatches(
11+
judgeGroups: JudgeGroup[],
12+
teams: Team[],
13+
type: string,
14+
rounds: number
15+
) {
16+
const matches: Match[] = [];
17+
18+
judgeGroups.forEach((judgeGroup) => {
19+
let count = 0;
20+
for (const team of teams) {
21+
if (count == rounds) break;
22+
for (const chosenTrack in team.tracks) {
23+
if (judgeGroup._id == undefined) continue;
24+
const foundTrack = tracks.find((track) => track.name === chosenTrack);
25+
if (foundTrack == undefined) continue;
26+
27+
if (foundTrack.type === type) {
28+
matches.push({
29+
judge_group_id: judgeGroup._id,
30+
team_id: team._id,
31+
});
32+
count++;
33+
}
34+
}
35+
}
36+
});
37+
38+
return matches;
39+
}
40+
41+
export default function matchingAlgorithm(
42+
judgeGroups: JudgeGroup[],
43+
teams: Team[]
44+
) {
45+
const techGroups = judgeGroups.filter((group) => group.type === 'T');
46+
const generalGroups = judgeGroups.filter((group) => group.type === 'G');
47+
const designGroups = judgeGroups.filter((group) => group.type === 'D');
48+
49+
let totalTech = 0;
50+
let totalGeneral = 0;
51+
let totalDesign = 0;
52+
53+
teams.forEach((team) => {
54+
team.tracks.forEach((chosenTrack) => {
55+
const foundTrack = tracks.find((track) => track.name === chosenTrack);
56+
57+
if (
58+
foundTrack == undefined ||
59+
foundTrack.name === 'Best Hack for Social Good'
60+
) {
61+
return;
62+
}
63+
64+
switch (foundTrack!.type) {
65+
case 'tech':
66+
totalTech++;
67+
break;
68+
case 'general':
69+
totalGeneral++;
70+
break;
71+
case 'design':
72+
totalDesign++;
73+
break;
74+
default:
75+
return;
76+
}
77+
});
78+
});
79+
80+
const techRounds = Math.ceil(totalTech / techGroups.length);
81+
const generalRounds = Math.ceil(totalGeneral / generalGroups.length);
82+
const designRounds = Math.ceil(totalDesign / designGroups.length);
83+
84+
const techMatches = createMatches(techGroups, teams, 'tech', techRounds);
85+
const generalMatches = createMatches(
86+
generalGroups,
87+
teams,
88+
'general',
89+
generalRounds
90+
);
91+
const designMatches = createMatches(
92+
designGroups,
93+
teams,
94+
'design',
95+
designRounds
96+
);
97+
98+
return techMatches.concat(generalMatches, designMatches);
99+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { NextRequest } from 'next/server';
2-
import { deleteSubmission } from '@datalib/submissions/deleteSubmission';
2+
import { DeleteSubmission } from '@datalib/submissions/deleteSubmission';
33

44
export async function DELETE(
55
_: NextRequest,
66
{ params }: { params: { judge_id: string; team_id: string } }
77
) {
8-
return deleteSubmission(params.judge_id, params.team_id);
8+
return DeleteSubmission(params.judge_id, params.team_id);
99
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { getSubmission } from '@datalib/submissions/getSubmissions';
1+
import { GetSubmission } from '@datalib/submissions/getSubmissions';
22
import { NextRequest } from 'next/server';
33

44
export async function GET(
55
_: NextRequest,
66
{ params }: { params: { judge_id: string; team_id: string } }
77
) {
8-
return getSubmission(params.judge_id, params.team_id);
8+
return GetSubmission(params.judge_id, params.team_id);
99
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { NextRequest } from 'next/server';
2-
import { updateSubmission } from '@datalib/submissions/updateSubmission';
2+
import { UpdateSubmission } from '@datalib/submissions/updateSubmission';
33

44
export async function PUT(
55
request: NextRequest,
66
{ params }: { params: { judge_id: string; team_id: string } }
77
) {
88
const body = await request.json();
9-
return updateSubmission(params.judge_id, params.team_id, body);
9+
return UpdateSubmission(params.judge_id, params.team_id, body);
1010
}

app/(api)/api/submissions/get.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { NextRequest } from 'next/server';
22

33
import getQueries from '@utils/request/getQueries';
4-
import { getSubmissions } from '@datalib/submissions/getSubmissions';
4+
import { GetManySubmissions } from '@datalib/submissions/getSubmissions';
55

66
export async function GET(request: NextRequest) {
77
const queries = getQueries(request);
8-
return getSubmissions(queries);
8+
return GetManySubmissions(queries);
99
}

app/(api)/api/submissions/post.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { createSubmission } from '@datalib/submissions/createSubmission';
1+
import { CreateSubmission } from '@datalib/submissions/createSubmission';
22
import { NextRequest } from 'next/server';
33

44
export async function POST(request: NextRequest) {
55
const body = await request.json();
6-
return createSubmission(body);
6+
return CreateSubmission(body);
77
}

app/(api)/api/teams/[id]/delete.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { type NextRequest } from 'next/server';
2-
import { deleteTeam } from '@datalib/teams/deleteTeam';
2+
import { DeleteTeam } from '@datalib/teams/deleteTeam';
33

44
export async function DELETE(
55
_: NextRequest,
66
{ params }: { params: { id: string } }
77
) {
8-
return deleteTeam(params.id);
8+
return DeleteTeam(params.id);
99
}

app/(api)/api/teams/[id]/get.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { type NextRequest } from 'next/server';
2-
import { getTeam } from '@datalib/teams/getTeam';
2+
import { GetTeam } from '@datalib/teams/getTeam';
33

44
export async function GET(
55
_: NextRequest,
66
{ params }: { params: { id: string } }
77
) {
8-
return getTeam(params.id);
8+
return GetTeam(params.id);
99
}

app/(api)/api/teams/[id]/put.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { type NextRequest } from 'next/server';
2-
import { updateTeam } from '@datalib/teams/updateTeam';
2+
import { UpdateTeam } from '@datalib/teams/updateTeam';
33

44
export async function PUT(
55
request: NextRequest,
66
{ params }: { params: { id: string } }
77
) {
88
const body = await request.json();
9-
return updateTeam(params.id, body);
9+
return UpdateTeam(params.id, body);
1010
}

app/(api)/api/teams/get.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { type NextRequest } from 'next/server';
22

33
import getQueries from '@utils/request/getQueries';
4-
import { getTeams } from '@datalib/teams/getTeam';
4+
import { GetManyTeams } from '@datalib/teams/getTeam';
55

66
export async function GET(request: NextRequest) {
77
const queries = getQueries(request);
8-
return getTeams(queries);
8+
return GetManyTeams(queries);
99
}

0 commit comments

Comments
 (0)