Skip to content

Commit

Permalink
Merge pull request #145 from HackDavis/feat/infrastructure-changes
Browse files Browse the repository at this point in the history
Feat/infrastructure changes
  • Loading branch information
JayJ104 authored Nov 4, 2024
2 parents 8947df1 + f651e22 commit f7a6b94
Show file tree
Hide file tree
Showing 85 changed files with 375 additions and 411 deletions.
13 changes: 6 additions & 7 deletions app/(api)/_actions/auth/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { HttpError, NotAuthenticatedError } from '@utils/response/Errors';
import FormToJSON from '@utils/form/FormToJSON';

import type AuthTokenInt from '@typeDefs/authToken';
import JudgeInt from '@typeDefs/judges';
import JudgeInt from '@typeDefs/judge';

export default async function LoginAction(
prevState: any,
Expand All @@ -19,11 +19,10 @@ export default async function LoginAction(
}> {
try {
const body = FormToJSON(formData) as JudgeInt;
const res = await Login(body);
const data = await res.json();
const data = await Login(body);

if (!data.ok) {
throw new NotAuthenticatedError(data.error);
if (!data.ok || !data.body) {
throw new NotAuthenticatedError(data.error as string);
}

const payload = jwt.decode(data.body) as AuthTokenInt;
Expand All @@ -36,9 +35,9 @@ export default async function LoginAction(
httpOnly: true,
});

return { ok: true, body: payload || null };
return { ok: true, body: payload || null, error: null };
} catch (e) {
const error = e as HttpError;
return { ok: false, error: error.message };
return { ok: false, body: null, error: error.message };
}
}
9 changes: 4 additions & 5 deletions app/(api)/_actions/auth/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { HttpError, NotAuthenticatedError } from '@utils/response/Errors';
import FormToJSON from '@utils/form/FormToJSON';

import type AuthTokenInt from '@typeDefs/authToken';
import type JudgeInt from '@typeDefs/judges';
import type JudgeInt from '@typeDefs/judge';

export default async function RegisterAction(
prevState: any,
Expand All @@ -20,11 +20,10 @@ export default async function RegisterAction(
try {
const body = FormToJSON(formData) as JudgeInt;

const res = await Register(body);
const data = await res.json();
const data = await Register(body);

if (!data.ok) {
throw new NotAuthenticatedError(data.error);
if (!data.ok || !data.body) {
throw new NotAuthenticatedError(data.error as string);
}

const payload = jwt.decode(data.body) as AuthTokenInt;
Expand Down
5 changes: 2 additions & 3 deletions app/(api)/_actions/auth/resetPassword.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@ export default async function ResetPasswordAction(
}> {
try {
const body = FormToJSON(formData) as { email: string; password: string };
const res = await ResetPassword(body);
const data = await res.json();
const data = await ResetPassword(body);

if (!data.ok) {
throw new HttpError(data.error);
throw new HttpError(data.error as string);
}

return { ok: true, body: data || null, error: null };
Expand Down
4 changes: 2 additions & 2 deletions app/(api)/_actions/helpTimers/getHelpTimer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { GetHelpTimers, GetNextTimer } from '@datalib/helpTimers/getHelpTimer';

export async function getHelperTimers(query: object = {}) {
const timers = await GetHelpTimers(query);
return timers.json();
return timers;
}

export async function getNextTimer() {
const timer = await GetNextTimer();
return timer.json();
return timer;
}
4 changes: 2 additions & 2 deletions app/(api)/_actions/judgeGroups/getJudgeGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import parseAndReplace from '@utils/request/parseAndReplace';

export async function getJudgeGroup(group_id: string) {
const judgeGroupRes = await GetJudgeGroup(group_id);
return judgeGroupRes.json();
return judgeGroupRes;
}

export async function getManyJudgeGroups(query: object = {}) {
const newQuery = await parseAndReplace(query);
const judgeGroupRes = await GetManyJudgeGroups(newQuery);
return judgeGroupRes.json();
return judgeGroupRes;
}
4 changes: 2 additions & 2 deletions app/(api)/_actions/judges/getJudge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { GetJudge, GetManyJudges } from '@datalib/judges/getJudge';

export async function getJudge(id: string) {
const judgeRes = await GetJudge(id);
return judgeRes.json();
return judgeRes;
}

export async function getManyJudges(query: object = {}) {
const judgeRes = await GetManyJudges(query);
return judgeRes.json();
return judgeRes;
}
2 changes: 1 addition & 1 deletion app/(api)/_actions/logic/groupJudges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default async function groupJudges() {
try {
const allPromises = await Promise.all(
groups.map(async (group) => {
return await (await CreateJudgeGroup(group)).json();
return await CreateJudgeGroup(group);
})
);

Expand Down
5 changes: 3 additions & 2 deletions app/(api)/_actions/logic/ingestCSV.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
'use server';

import { CreateManyTeams } from '@datalib/teams/createTeams';
import parsedRecordInt from '@typeDefs/parsedRecord';

export default async function ingestCSV(parsedData: []) {
const res = await (await CreateManyTeams(parsedData)).json();
export default async function ingestCSV(parsedData: parsedRecordInt[]) {
const res = await CreateManyTeams(parsedData);
return { ok: res.ok, error: res.error };
}
2 changes: 1 addition & 1 deletion app/(api)/_actions/logic/scoreTeams.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use server';

import { getManyTeams } from '@actions/teams/getTeams';
import Team from '@typeDefs/teams';
import Team from '@typeDefs/team';
import rankTeams from '@utils/scoring/rankTeams';

export default async function scoreTeams() {
Expand Down
2 changes: 1 addition & 1 deletion app/(api)/_actions/logic/uploadFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ export default async function uploadFile(formData: FormData) {
const blob = new Blob([data], { type: file.type });

const res = await csvAlgorithm(blob);
return await res.json();
return res;
}
8 changes: 4 additions & 4 deletions app/(api)/_actions/submissions/getSubmission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import {
import parseAndReplace from '@utils/request/parseAndReplace';

export async function getSubmission(judge_id: string, team_id: string) {
const submissionRes = await GetSubmission(judge_id, team_id);
return submissionRes.json();
const submission = await GetSubmission(judge_id, team_id);
return submission;
}

export async function getManySubmissions(query: object = {}) {
const newQuery = await parseAndReplace(query);
const submissionRes = await GetManySubmissions(newQuery);
return submissionRes.json();
const submissions = await GetManySubmissions(newQuery);
return submissions;
}
2 changes: 1 addition & 1 deletion app/(api)/_actions/submissions/updateSubmission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ export default async function updateSubmission(
};

const updateRes = await UpdateSubmission(judge_id, team_id, updateBody);
return await updateRes.json();
return updateRes;
}
8 changes: 4 additions & 4 deletions app/(api)/_actions/teams/getTeams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import { GetManyTeams, GetTeam } from '@datalib/teams/getTeam';
import parseAndReplace from '@utils/request/parseAndReplace';

export async function getTeam(id: string) {
const teamRes = await GetTeam(id);
return teamRes.json();
const team = await GetTeam(id);
return team;
}

export async function getManyTeams(query: object = {}) {
const newQuery = await parseAndReplace(query);
const teamRes = await GetManyTeams(newQuery);
return teamRes.json();
const teams = await GetManyTeams(newQuery);
return teams;
}
4 changes: 2 additions & 2 deletions app/(api)/_datalib/auth/authToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ export async function verifyAuthToken(token: string) {
if (decodedToken.exp && decodedToken.exp < currentTimestamp) {
throw new HttpError('token has expired');
}
return { ok: true, body: decodedToken };
return { ok: true, body: decodedToken, error: null };
} catch (e) {
const error = e as HttpError;
return { ok: false, error: error.message };
return { ok: false, body: null, error: error.message };
}
}
15 changes: 3 additions & 12 deletions app/(api)/_datalib/auth/login.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
'use server';
import { NextResponse } from 'next/server';

import bcrypt from 'bcryptjs';

import { HttpError, NotAuthenticatedError } from '@utils/response/Errors';
Expand All @@ -12,8 +10,7 @@ export async function Login(body: { email: string; password: string }) {
const { email, password } = body;

// Find Judge
const res = await GetManyJudges({ email });
const data = await res.json();
const data = await GetManyJudges({ email });
if (!data.ok || data.body.length === 0) {
throw new NotAuthenticatedError('Judge not found');
}
Expand All @@ -30,15 +27,9 @@ export async function Login(body: { email: string; password: string }) {
}

const token = await createAuthToken(judge);
return NextResponse.json(
{ ok: true, body: token, error: null },
{ status: 200 }
);
return { ok: true, body: token, error: null };
} catch (e) {
const error = e as HttpError;
return NextResponse.json(
{ ok: false, body: null, error: error.message },
{ status: error.status || 400 }
);
return { ok: false, body: null, error: error.message };
}
}
20 changes: 9 additions & 11 deletions app/(api)/_datalib/auth/register.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,38 @@
'use server';
import { NextResponse } from 'next/server';
import bcrypt from 'bcryptjs';

import { CreateJudge } from '@datalib/judges/createJudge';
import { DuplicateError, HttpError } from '@utils/response/Errors';
import { GetManyJudges } from '@datalib/judges/getJudge';
import { createAuthToken } from './authToken';
import JudgeInt from '@typeDefs/judges';
import JudgeInt from '@typeDefs/judge';

export async function Register(body: JudgeInt) {
try {
const { email, password, ...rest } = body;
const hashedPassword = await bcrypt.hash(password as string, 10);

// Find Judge
const judgeRes = await GetManyJudges({ email });
const judgeData = await judgeRes.json();
const judgeData = await GetManyJudges({ email });
if (!judgeData.ok || judgeData.body.length !== 0) {
throw new DuplicateError('Judge already exists');
}

// Create Judge
const res = await CreateJudge({ email, password: hashedPassword, ...rest });
const data = await res.json();
const data = await CreateJudge({
email,
password: hashedPassword,
...rest,
});

if (!data.ok) {
throw new HttpError('Failed to create judge');
}

const token = await createAuthToken(data.body);
return NextResponse.json({ ok: true, body: token }, { status: 200 });
return { ok: true, body: token, error: null };
} catch (e) {
const error = e as HttpError;
return NextResponse.json(
{ ok: false, error: error.message },
{ status: error.status || 400 }
);
return { ok: false, error: error.message };
}
}
18 changes: 4 additions & 14 deletions app/(api)/_datalib/auth/resetPassword.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
'use server';
import { NextResponse } from 'next/server';
import bcrypt from 'bcryptjs';

import { GetManyJudges } from '@datalib/judges/getJudge';
Expand All @@ -12,30 +11,21 @@ export async function ResetPassword(body: { email: string; password: string }) {
const hashedPassword = await bcrypt.hash(password as string, 10);

// Find Judge
const judge_res = await GetManyJudges({ email });
const judge_data = await judge_res.json();
const judge_data = await GetManyJudges({ email });
if (!judge_data.ok || judge_data.body.length === 0) {
throw new HttpError('Judge not found');
}

// UpdateJudge
const updateRes = await UpdateJudge(judge_data.body[0]._id, {
const updateData = await UpdateJudge(judge_data.body[0]._id, {
$set: {
password: hashedPassword,
},
});

const updateData = await updateRes.json();

return NextResponse.json(
{ ok: true, body: updateData, error: null },
{ status: 200 }
);
return { ok: true, body: updateData, error: null };
} catch (e) {
const error = e as HttpError;
return NextResponse.json(
{ ok: false, body: null, error: error.message },
{ status: error.status || 400 }
);
return { ok: false, body: null, error: error.message };
}
}
11 changes: 2 additions & 9 deletions app/(api)/_datalib/helpTimers/createHelpTimer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { NextResponse } from 'next/server';
import { ObjectId } from 'mongodb';
import { getDatabase } from '@utils/mongodb/mongoClient.mjs';
import isBodyEmpty from '@utils/request/isBodyEmpty';
Expand All @@ -18,15 +17,9 @@ export const CreateHelpTimer = async (body: object) => {
_id: new ObjectId(creationStatus.insertedId),
});

return NextResponse.json(
{ ok: true, body: helpTimer, error: null },
{ status: 201 }
);
return { ok: true, body: helpTimer, error: null };
} catch (e) {
const error = e as HttpError;
return NextResponse.json(
{ ok: false, body: null, error: error.message },
{ status: error.status || 400 }
);
return { ok: false, body: null, error: error.message };
}
};
21 changes: 4 additions & 17 deletions app/(api)/_datalib/helpTimers/getHelpTimer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { NextResponse } from 'next/server';
import { getDatabase } from '@utils/mongodb/mongoClient.mjs';
import { HttpError } from '@utils/response/Errors';

Expand Down Expand Up @@ -33,16 +32,10 @@ export const GetNextTimer = async () => {
throw new HttpError('No upcoming event');
}

return NextResponse.json(
{ ok: true, body: timers_sorted[out_index], error: null },
{ status: 200 }
);
return { ok: true, body: timers_sorted[out_index], error: null };
} catch (e) {
const error = e as HttpError;
return NextResponse.json(
{ ok: false, body: null, error: error.message },
{ status: error.status || 400 }
);
return { ok: false, body: null, error: error.message };
}
};

Expand All @@ -52,15 +45,9 @@ export const GetHelpTimers = async (query: object = {}) => {

const timers = await db.collection('helpTimers').find(query).toArray();

return NextResponse.json(
{ ok: true, body: timers, error: null },
{ status: 200 }
);
return { ok: true, body: timers, error: null };
} catch (e) {
const error = e as HttpError;
return NextResponse.json(
{ ok: false, body: null, error: error.message },
{ status: error.status || 400 }
);
return { ok: false, body: null, error: error.message };
}
};
Loading

0 comments on commit f7a6b94

Please sign in to comment.