Skip to content

Commit 62b266c

Browse files
committed
Fixed submissions
1 parent 5e0cb99 commit 62b266c

File tree

4 files changed

+61
-29
lines changed

4 files changed

+61
-29
lines changed

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import JudgeGroupToTeam from '@typeDefs/judgeGroupToTeam';
66
import parseAndReplace from '@utils/request/parseAndReplace';
77
import { getManyJudgeGroups } from '@actions/judgeGroups/getJudgeGroup';
88
import { getManyTeams } from '@actions/teams/getTeams';
9+
import { createSubmission } from '@actions/submissions/createSubmission';
10+
import { getManyJudges } from '@actions/judges/getJudge';
911

1012
function checkMatches(matches: JudgeGroupToTeam[], teamsLength: number) {
1113
if (matches.length < 2 * teamsLength) return false;
@@ -35,5 +37,19 @@ export default async function matchTeams() {
3537
const parsedMatches = await parseAndReplace(matches);
3638
const valid = checkMatches(parsedMatches, teams.length);
3739

38-
if (valid) await LinkManyJudgeGroupsToTeams(matches);
40+
if (valid) {
41+
await LinkManyJudgeGroupsToTeams(matches);
42+
43+
for (const match of parsedMatches) {
44+
const judges = (
45+
await getManyJudges({
46+
judge_group_id: match.judge_group_id,
47+
})
48+
).body;
49+
50+
for (const judge of judges) {
51+
await createSubmission(judge._id, match.team_id.toString());
52+
}
53+
}
54+
}
3955
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'use server';
2+
3+
import { CreateSubmission } from '@datalib/submissions/createSubmission';
4+
5+
export async function createSubmission(judge_id: string, team_id: string) {
6+
await CreateSubmission({
7+
judge_id: {
8+
'*convertId': {
9+
id: judge_id,
10+
},
11+
},
12+
team_id: {
13+
'*convertId': {
14+
id: team_id,
15+
},
16+
},
17+
});
18+
}

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

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,11 @@ import isBodyEmpty from '@utils/request/isBodyEmpty';
66
import NoContentError from '@utils/response/NoContentError';
77
import HttpError from '@utils/response/HttpError';
88
import parseAndReplace from '@utils/request/parseAndReplace';
9-
import {
10-
NotFoundError,
11-
DuplicateError,
12-
BadRequestError,
13-
} from '@utils/response/Errors';
9+
import { NotFoundError, DuplicateError } from '@utils/response/Errors';
1410

1511
export const CreateSubmission = async (body: {
16-
judge_id: string;
17-
team_id: string;
12+
judge_id: object;
13+
team_id: object;
1814
}) => {
1915
try {
2016
if (isBodyEmpty(body)) {
@@ -28,6 +24,7 @@ export const CreateSubmission = async (body: {
2824
});
2925

3026
if (judge === null) {
27+
console.log(`judge with id: ${body.judge_id} not found.`);
3128
throw new NotFoundError(`judge with id: ${body.judge_id} not found.`);
3229
}
3330

@@ -36,6 +33,7 @@ export const CreateSubmission = async (body: {
3633
});
3734

3835
if (team === null) {
36+
console.log(`team with id: ${body.team_id} not found.`);
3937
throw new NotFoundError(`team with id: ${body.team_id} not found.`);
4038
}
4139

@@ -45,17 +43,10 @@ export const CreateSubmission = async (body: {
4543
});
4644

4745
if (existingSubmission) {
46+
console.log('Submission already exists');
4847
throw new DuplicateError('Submission already exists');
4948
}
5049

51-
const judgeGroupToTeam = await db.collection('judgeGroupToTeams').findOne({
52-
judge_group_id: judge.judge_group_id,
53-
});
54-
55-
if (judgeGroupToTeam.team_id.toString() !== body.team_id.toString()) {
56-
throw new BadRequestError('Judge is not judging this team.');
57-
}
58-
5950
const creationStatus = await db
6051
.collection('submissions')
6152
.insertOne(parsedBody);

app/(api)/_utils/grouping/matchingAlgorithm.ts

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ export default function matchingAlgorithm(
2020
const matches: JudgeGroupToTeam[] = [];
2121

2222
for (const team of filteredTeams) {
23+
console.log(team.name);
2324
teamAssignments.sort((group1, group2) => group1.teams - group2.teams);
2425

26+
let pairedGroup = '';
2527
for (let i = 0; i < 2; i++) {
2628
const chosenTrack = team.tracks[i];
2729
const foundTrack = tracks.find((track) => track.name === chosenTrack);
@@ -30,26 +32,31 @@ export default function matchingAlgorithm(
3032
let idx = 0;
3133
while (
3234
idx < teamAssignments.length &&
33-
teamAssignments[idx].judgeGroup.type !== foundTrack.type
35+
(teamAssignments[idx].judgeGroup.type !== foundTrack.type ||
36+
pairedGroup === teamAssignments[idx].judgeGroup._id)
3437
) {
3538
idx++;
3639
}
3740

38-
teamAssignments[idx].teams++;
41+
if (idx < teamAssignments.length) {
42+
teamAssignments[idx].teams++;
3943

40-
matches.push({
41-
judge_group_id: {
42-
'*convertId': {
43-
id: teamAssignments[idx].judgeGroup._id,
44+
matches.push({
45+
judge_group_id: {
46+
'*convertId': {
47+
id: teamAssignments[idx].judgeGroup._id,
48+
},
4449
},
45-
},
46-
team_id: {
47-
'*convertId': {
48-
id: team._id,
50+
team_id: {
51+
'*convertId': {
52+
id: team._id,
53+
},
4954
},
50-
},
51-
round: teamAssignments[idx].teams,
52-
});
55+
round: teamAssignments[idx].teams,
56+
});
57+
58+
pairedGroup = teamAssignments[idx].judgeGroup._id ?? '';
59+
}
5360
}
5461
}
5562

0 commit comments

Comments
 (0)