diff --git a/prisma/migrations/20240710211511_add_team_geo_location/migration.sql b/prisma/migrations/20240710211511_add_team_geo_location/migration.sql new file mode 100644 index 0000000..c500a25 --- /dev/null +++ b/prisma/migrations/20240710211511_add_team_geo_location/migration.sql @@ -0,0 +1,24 @@ +/* + Warnings: + + - Added the required column `city` to the `Team` table without a default value. This is not possible if the table is not empty. + - Added the required column `country` to the `Team` table without a default value. This is not possible if the table is not empty. + - Added the required column `state_prov` to the `Team` table without a default value. This is not possible if the table is not empty. + +*/ +-- RedefineTables +PRAGMA defer_foreign_keys=ON; +PRAGMA foreign_keys=OFF; +CREATE TABLE "new_Team" ( + "id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, + "name" TEXT NOT NULL, + "city" TEXT NOT NULL, + "state_prov" TEXT NOT NULL, + "country" TEXT NOT NULL +); +INSERT INTO "new_Team" ("id", "name") SELECT "id", "name" FROM "Team"; +DROP TABLE "Team"; +ALTER TABLE "new_Team" RENAME TO "Team"; +CREATE UNIQUE INDEX "Team_id_key" ON "Team"("id"); +PRAGMA foreign_keys=ON; +PRAGMA defer_foreign_keys=OFF; diff --git a/prisma/migrations/20240710211956_make_optional/migration.sql b/prisma/migrations/20240710211956_make_optional/migration.sql new file mode 100644 index 0000000..7525399 --- /dev/null +++ b/prisma/migrations/20240710211956_make_optional/migration.sql @@ -0,0 +1,16 @@ +-- RedefineTables +PRAGMA defer_foreign_keys=ON; +PRAGMA foreign_keys=OFF; +CREATE TABLE "new_Team" ( + "id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, + "name" TEXT NOT NULL, + "city" TEXT, + "state_prov" TEXT, + "country" TEXT +); +INSERT INTO "new_Team" ("city", "country", "id", "name", "state_prov") SELECT "city", "country", "id", "name", "state_prov" FROM "Team"; +DROP TABLE "Team"; +ALTER TABLE "new_Team" RENAME TO "Team"; +CREATE UNIQUE INDEX "Team_id_key" ON "Team"("id"); +PRAGMA foreign_keys=ON; +PRAGMA defer_foreign_keys=OFF; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 2845cec..cf62f25 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -11,6 +11,10 @@ model Team { id Int @id @unique name String + city String? + state_prov String? + country String? + entries Entry[] } diff --git a/src/app/api/push/route.ts b/src/app/api/push/route.ts new file mode 100644 index 0000000..a152bd1 --- /dev/null +++ b/src/app/api/push/route.ts @@ -0,0 +1,56 @@ +import { MD5 } from "crypto-js"; +import { PrismaClient } from '@prisma/client' +import { NextRequest, NextResponse } from "next/server"; +import { existsSync, mkdirSync, writeFileSync } from "fs"; + +interface Entry { + type: string; + user: string; + team: string; + event: string; + timestamp: string; + data: any; +} + +const prismaClient = new PrismaClient(); + +export async function POST(request: NextRequest) { + const data: Entry[] = await request.json(); + + const checksum = MD5(JSON.stringify(data)).toString(); + + const entryIds = (await prismaClient.entry.createManyAndReturn({ + data: data.map(entry => ({ + type: entry.type, + teamNumber: parseInt(entry.team) + })), + select: { + id: true + } + })).map(entry => entry.id); + + entryIds.forEach((id, index) => writeEntry(id, data[index])); + + if(entryIds.length == data.length) return NextResponse.json({ + hash: checksum, + entryIds: entryIds + }); + + return NextResponse.error(); +} + +function writeEntry(id: string, entry: Entry) { + const dataDir = useDataDir(); + + writeFileSync(`${dataDir}/${id}.turbo.json`, JSON.stringify(entry)); +} + +function useDataDir() { + const dataDir = "./turbo-data"; + + if (!existsSync(dataDir)) { + mkdirSync(dataDir); + } + + return dataDir; +} diff --git a/src/app/api/setup/route.ts b/src/app/api/setup/route.ts new file mode 100644 index 0000000..8ccefe6 --- /dev/null +++ b/src/app/api/setup/route.ts @@ -0,0 +1,35 @@ +import { TBA_BASE, TBA_OPTS } from "@/app/lib/tba_api"; +import { PrismaClient } from "@prisma/client"; +import { NextRequest, NextResponse } from "next/server"; + +const prismaClient = new PrismaClient(); + +interface TeamSimple { + key: string; + + team_number: number; + name: string; + nickname: string; + + city: string; + state_prov: string; + country: string; +} + +export async function GET(req: NextRequest) { + const teams: TeamSimple[] = (await Promise.all([...Array(40).keys()].map(async (pageNumber) => { + return await fetch(TBA_BASE + `/teams/${pageNumber}/simple`, TBA_OPTS).then(resp => resp.json()); + }))).flat(); + + const result = await prismaClient.team.createMany({ + data: teams.map(team => ({ + id: team.team_number, + name: team.nickname, + city: team.city, + state_prov: team.state_prov, + country: team.country + })) + }); + + return NextResponse.json({msg: "Added " + result.count + " teams!"}); +} \ No newline at end of file diff --git a/src/app/lib/server.tsx b/src/app/lib/server.tsx index cf8d8be..568b609 100644 --- a/src/app/lib/server.tsx +++ b/src/app/lib/server.tsx @@ -7,12 +7,6 @@ import MD5 from "crypto-js/md5"; import React from "react"; export async function exportData(sendQueue: any, clearSendQueue: any) { - modals.open({ - title: "This is still being worked on!", - }) -} - -export async function exportDataOLD(sendQueue: any, clearSendQueue: any) { fetch("/api/push", { method: 'post', headers: {