Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 62 additions & 14 deletions apps/scouting/backend/src/routes/tba.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
matchesProps,
scoreBreakdown2026,
tbaMatch,
type TBAMatchesProps,
} from "@repo/scouting_types";
import { right } from "fp-ts/lib/Either";
import { StatusCodes } from "http-status-codes";
Expand All @@ -20,17 +21,30 @@ import {
type TaskEither,
fromEither,
tryCatch,
map,
chainFirstTaskK,
} from "fp-ts/lib/TaskEither";
import { pipe } from "fp-ts/lib/function";
import { map } from "fp-ts/lib/Task";
import { flow, pipe } from "fp-ts/lib/function";
import { map as taskMap } from "fp-ts/lib/Task";
import * as t from "io-ts";
import type { Type } from "io-ts";
import { getDb } from "../middleware/db";
import { getMax } from "@repo/array-functions";
import { fold as booleanFold } from "fp-ts/boolean";

export const tbaRouter = Router();

const TBA_KEY = process.env.TBA_API_KEY ?? "yourtbakey";
const TBA_URL = "https://www.thebluealliance.com/api/v3";

const tbaMatches = t.array(tbaMatch(scoreBreakdown2026, t.type({})));
type TBAMatches = t.TypeOf<typeof tbaMatches>;

const getCollection = flow(
getDb,
map((db) => db.collection<TBAMatches[number]>("tba")),
);

const fetchTba = <U>(
route: string,
typeToCheck: Type<U, unknown>,
Expand All @@ -50,34 +64,68 @@ const fetchTba = <U>(
reason: `Error Fetching From TBA: error ${error}`,
}),
),
map(
taskMap(
createTypeCheckingEndpointFlow(typeToCheck, (errors) => ({
status: StatusCodes.INTERNAL_SERVER_ERROR,
reason: `Recieved incorrect response from the TBA. error: ${errors}`,
}))
)
})),
),
) satisfies TaskEither<EndpointError, U>;

const insertMatches = (matches: TBAMatches) =>
pipe(
getCollection(),
map((collection) => collection.insertMany(matches)),
);
const getStoredMatches = flow(
getCollection,
flatMap((collection) =>
tryCatch(collection.find().toArray, (error) => ({
reason: `Error getting from collection ${String(error)}`,
status: StatusCodes.BAD_REQUEST,
})),
),
);

const getMatches = flow(
flatMap((body: TBAMatchesProps) =>
pipe(
getStoredMatches(),
map((currentMatches) => ({ currentMatches, body })),
),
),
flatMap(({ currentMatches, body }) =>
pipe(
getMax(currentMatches, (match) => match.match_number).match_number <
body.maxMatch,
booleanFold(
() =>
pipe(
fetchTba(`/event/${body.event}/matches`, tbaMatches),
chainFirstTaskK(
(fetchedMatches) => async () =>
Promise.resolve(insertMatches(fetchedMatches)),
),
),
() => fromEither(right(currentMatches)),
),
),
),
);

tbaRouter.post("/matches", async (req, res) => {
await pipe(
right(req),
createBodyVerificationPipe(matchesProps),
fromEither,
flatMap((body) =>
fetchTba(
`/event/${body.event}/matches`,
t.array(tbaMatch(scoreBreakdown2026, t.type({}))),
),
),
getMatches,
fold(
(error) => () =>
new Promise((resolve) => {
resolve(res.status(error.status).send(error.reason));
}),
(matches) => () =>
new Promise((resolve) => {
resolve(res.status(StatusCodes.OK).json({ matches }));
}),
Promise.resolve(res.status(StatusCodes.OK).json({ matches })),
),
)();
});
5 changes: 5 additions & 0 deletions packages/array-functions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ export const calculateSum = <T>(
): number =>
arr.reduce((sum, value) => sum + transformation(value), startingSumValue);

export const getMax = <T>(arr: T[], transformation: (value: T) => number): T =>
arr
.map((item) => ({ value: transformation(item), item }))
.reduce((max, curr) => (curr.value > max.value ? curr : max)).item;

const FIRST_ELEMENT_ID = 0;
const LAST_ELEMENT_BACKWARDS_INDEX = 1;
export const firstElement = <T>(arr: T[]): T => arr[FIRST_ELEMENT_ID];
Expand Down
2 changes: 2 additions & 0 deletions packages/scouting_types/tba/ScoreBreakdown2026.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,5 @@ export const scoreBreakdown2026 = t.type({
totalTowerPoints: t.number,
totalPoints: t.number,
});

export type ScoreBreakdown2026 = t.TypeOf<typeof scoreBreakdown2026>;
1 change: 1 addition & 0 deletions packages/scouting_types/tba/TBAMatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as t from "io-ts";

export const matchesProps = t.type({
event: t.string,
maxMatch: t.number
});

export type TBAMatchesProps = t.TypeOf<typeof matchesProps>;
Expand Down
Loading