diff --git a/.vscode/settings.json b/.vscode/settings.json index 8978ebbe..baf98fae 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -39,6 +39,7 @@ "nitropack", "Pressable", "prettiercache", + "sluggable", "subrouters", "thatguyinabenaie", "tsbuildinfo", @@ -57,7 +58,7 @@ "name": "postgres", "group": "postgres", "database": "fuecoco-db-dev", - "username": "postgres", + "username": "postgres" } - ], + ] } diff --git a/apps/nextjs/src/app/server-actions/pokemon/actions.ts b/apps/nextjs/src/app/server-actions/pokemon/actions.ts index c6123d80..a402ed9a 100644 --- a/apps/nextjs/src/app/server-actions/pokemon/actions.ts +++ b/apps/nextjs/src/app/server-actions/pokemon/actions.ts @@ -1,72 +1,61 @@ "use server"; -import type { FetchOptions } from "openapi-fetch"; - -import type { PokemonTeam } from "@battle-stadium/db/schema"; import { db } from "@battle-stadium/db"; +import { pokemon, pokemonTeams } from "@battle-stadium/db/schema"; -import type { paths } from "~/lib/api/openapi-v1"; import type { PokePasteMetadata, ValidatedPokemon } from "~/lib/pokemon/common"; -import { BattleStadiumApiClient, defaultConfig } from "~/lib/api"; - -interface PostPokemonTeamBody { - pokepaste_id?: string; - profile_id: number | null; - name: string; - format_id: number; - game_id: number; - pokemon: { - species: string; - item: string; - ability: string; - tera_type: string; - nature: string; - form: string | null; - nickname?: string | null; - gender?: string; - move1: string | null; - move2: string | null; - move3: string | null; - move4: string | null; - pokemon_team_id?: number; - }[]; -} -export async function getPokemonTeams(): Promise { +export async function getPokemonTeams() { return await db.query.pokemonTeams.findMany(); } export async function postPokemonTeam( validatedTeam: ValidatedPokemon[], metadata: PokePasteMetadata, - options?: FetchOptions, ) { - const body: PostPokemonTeamBody = { - pokepaste_id: metadata.id, - profile_id: null, + const body = { + pokepasteId: metadata.id, + profileId: null, name: metadata.title, - format_id: 1, - game_id: 1, - pokemon: validatedTeam.map(({ pokemon }) => ({ - nickname: pokemon.name, - species: pokemon.species, - item: pokemon.item, - ability: pokemon.ability, - tera_type: pokemon.teraType ?? "", - nature: pokemon.nature, - form: null, - move1: pokemon.moves[0] ?? null, - move2: pokemon.moves[1] ?? null, - move3: pokemon.moves[2] ?? null, - move4: pokemon.moves[3] ?? null, - })), + createdAt: new Date().toISOString(), + updatedAt: new Date().toISOString(), + gameId: BigInt(1), + formatId: BigInt(1), }; - const pokemonOptions = { - ...defaultConfig("postPokemonTeam"), - ...options, - body, + const pokemonTeamResult = await db + .insert(pokemonTeams) + .values(body) + .returning(); + + if (!pokemonTeamResult.length) { + throw new Error("Failed to insert Pokemon Team"); + } + + const pokemonList = validatedTeam.map(({ pokemon }) => ({ + pokemonTeamId: pokemonTeamResult[0]?.id, + nickname: pokemon.name, + species: pokemon.species, + item: pokemon.item, + ability: pokemon.ability, + teraType: pokemon.teraType ?? "", + nature: pokemon.nature, + form: null, + move1: pokemon.moves[0] ?? null, + move2: pokemon.moves[1] ?? null, + move3: pokemon.moves[2] ?? null, + move4: pokemon.moves[3] ?? null, + createdAt: new Date().toISOString(), + updatedAt: new Date().toISOString(), + })); + + const pokemonResult = await db + .insert(pokemon) + .values(pokemonList) + .returning(); + + return { + pokemonTeam: pokemonTeamResult[0], + pokemon: pokemonResult, }; - - return BattleStadiumApiClient().POST("/pokemon_teams", pokemonOptions); } diff --git a/packages/db/src/schemas/accounts.ts b/packages/db/src/schemas/accounts.ts index d5709c90..cd8d14cd 100644 --- a/packages/db/src/schemas/accounts.ts +++ b/packages/db/src/schemas/accounts.ts @@ -33,8 +33,7 @@ export const accounts = pgTable( imageUrl: text("image_url"), admin: boolean().default(false).notNull(), id: bigserial({ mode: "bigint" }).primaryKey().notNull(), - // You can use { mode: "bigint" } if numbers are exceeding js number limitations - defaultProfileId: bigint("default_profile_id", { mode: "number" }), + defaultProfileId: bigint("default_profile_id", { mode: "bigint" }), archivedAt: timestamp("archived_at", { precision: 6, mode: "string" }), country: varchar(), timezone: varchar(), diff --git a/packages/db/src/schemas/chat-messages.ts b/packages/db/src/schemas/chat-messages.ts index fc5d2ef0..624b9f3d 100644 --- a/packages/db/src/schemas/chat-messages.ts +++ b/packages/db/src/schemas/chat-messages.ts @@ -16,16 +16,13 @@ import { profiles } from "./profiles"; export const chatMessages = pgTable( "chat_messages", { - // You can use { mode: "bigint" } if numbers are exceeding js number limitations - matchId: bigint("match_id", { mode: "number" }).notNull(), + matchId: bigint("match_id", { mode: "bigint" }).notNull(), content: text(), messageType: varchar("message_type"), sentAt: timestamp("sent_at", { precision: 6, mode: "string" }), id: bigserial({ mode: "bigint" }).primaryKey().notNull(), - // You can use { mode: "bigint" } if numbers are exceeding js number limitations - accountId: bigint("account_id", { mode: "number" }), - // You can use { mode: "bigint" } if numbers are exceeding js number limitations - profileId: bigint("profile_id", { mode: "number" }).notNull(), + accountId: bigint("account_id", { mode: "bigint" }), + profileId: bigint("profile_id", { mode: "bigint" }).notNull(), }, (table) => { return { diff --git a/packages/db/src/schemas/clerk-users.ts b/packages/db/src/schemas/clerk-users.ts index 125a2f80..41d0bec6 100644 --- a/packages/db/src/schemas/clerk-users.ts +++ b/packages/db/src/schemas/clerk-users.ts @@ -24,8 +24,7 @@ export const clerkUsers = pgTable( precision: 6, mode: "string", }).notNull(), - // You can use { mode: "bigint" } if numbers are exceeding js number limitations - accountId: bigint("account_id", { mode: "number" }), + accountId: bigint("account_id", { mode: "bigint" }), }, (table) => { return { diff --git a/packages/db/src/schemas/formats.ts b/packages/db/src/schemas/formats.ts index c7c45605..7dee7f54 100644 --- a/packages/db/src/schemas/formats.ts +++ b/packages/db/src/schemas/formats.ts @@ -16,8 +16,7 @@ export const formats = pgTable( { id: bigserial({ mode: "bigint" }).primaryKey().notNull(), name: varchar(), - // You can use { mode: "bigint" } if numbers are exceeding js number limitations - gameId: bigint("game_id", { mode: "number" }), + gameId: bigint("game_id", { mode: "bigint" }), createdAt: timestamp("created_at", { precision: 6, mode: "string", diff --git a/packages/db/src/schemas/match-games.ts b/packages/db/src/schemas/match-games.ts index 9b5e28ee..4dd082f5 100644 --- a/packages/db/src/schemas/match-games.ts +++ b/packages/db/src/schemas/match-games.ts @@ -16,12 +16,9 @@ export const matchGames = pgTable( "match_games", { id: bigserial({ mode: "bigint" }).primaryKey().notNull(), - // You can use { mode: "bigint" } if numbers are exceeding js number limitations - matchId: bigint("match_id", { mode: "number" }).notNull(), - // You can use { mode: "bigint" } if numbers are exceeding js number limitations - winnerId: bigint("winner_id", { mode: "number" }), - // You can use { mode: "bigint" } if numbers are exceeding js number limitations - loserId: bigint("loser_id", { mode: "number" }), + matchId: bigint("match_id", { mode: "bigint" }).notNull(), + winnerId: bigint("winner_id", { mode: "bigint" }), + loserId: bigint("loser_id", { mode: "bigint" }), createdAt: timestamp("created_at", { precision: 6, mode: "string", @@ -33,8 +30,7 @@ export const matchGames = pgTable( gameNumber: integer("game_number").default(1).notNull(), endedAt: timestamp("ended_at", { precision: 6, mode: "string" }), startedAt: timestamp("started_at", { precision: 6, mode: "string" }), - // You can use { mode: "bigint" } if numbers are exceeding js number limitations - reporterProfileId: bigint("reporter_profile_id", { mode: "number" }), + reporterProfileId: bigint("reporter_profile_id", { mode: "bigint" }), }, (table) => { return { diff --git a/packages/db/src/schemas/matches.ts b/packages/db/src/schemas/matches.ts index a8d25c78..0ad9255d 100644 --- a/packages/db/src/schemas/matches.ts +++ b/packages/db/src/schemas/matches.ts @@ -20,15 +20,11 @@ export const matches = pgTable( "matches", { id: bigserial({ mode: "bigint" }).primaryKey().notNull(), - // You can use { mode: "bigint" } if numbers are exceeding js number limitations - roundId: bigint("round_id", { mode: "number" }).notNull(), + roundId: bigint("round_id", { mode: "bigint" }).notNull(), tableNumber: integer("table_number"), - // You can use { mode: "bigint" } if numbers are exceeding js number limitations - playerOneId: bigint("player_one_id", { mode: "number" }), - // You can use { mode: "bigint" } if numbers are exceeding js number limitations - playerTwoId: bigint("player_two_id", { mode: "number" }), - // You can use { mode: "bigint" } if numbers are exceeding js number limitations - winnerId: bigint("winner_id", { mode: "number" }), + playerOneId: bigint("player_one_id", { mode: "bigint" }), + playerTwoId: bigint("player_two_id", { mode: "bigint" }), + winnerId: bigint("winner_id", { mode: "bigint" }), createdAt: timestamp("created_at", { precision: 6, mode: "string", @@ -45,16 +41,12 @@ export const matches = pgTable( precision: 6, mode: "string", }), - // You can use { mode: "bigint" } if numbers are exceeding js number limitations - loserId: bigint("loser_id", { mode: "number" }), + loserId: bigint("loser_id", { mode: "bigint" }), endedAt: timestamp("ended_at", { precision: 6, mode: "string" }), - // You can use { mode: "bigint" } if numbers are exceeding js number limitations - tournamentId: bigint("tournament_id", { mode: "number" }), - // You can use { mode: "bigint" } if numbers are exceeding js number limitations - phaseId: bigint("phase_id", { mode: "number" }), + tournamentId: bigint("tournament_id", { mode: "bigint" }), + phaseId: bigint("phase_id", { mode: "bigint" }), bye: boolean().default(false).notNull(), - // You can use { mode: "bigint" } if numbers are exceeding js number limitations - resetById: bigint("reset_by_id", { mode: "number" }), + resetById: bigint("reset_by_id", { mode: "bigint" }), }, (table) => { return { diff --git a/packages/db/src/schemas/organization-staff-members.ts b/packages/db/src/schemas/organization-staff-members.ts index f4462fc3..b976bcae 100644 --- a/packages/db/src/schemas/organization-staff-members.ts +++ b/packages/db/src/schemas/organization-staff-members.ts @@ -14,8 +14,7 @@ export const organizationStaffMembers = pgTable( "organization_staff_members", { id: bigserial({ mode: "bigint" }).primaryKey().notNull(), - // You can use { mode: "bigint" } if numbers are exceeding js number limitations - organizationId: bigint("organization_id", { mode: "number" }).notNull(), + organizationId: bigint("organization_id", { mode: "bigint" }).notNull(), createdAt: timestamp("created_at", { precision: 6, mode: "string", @@ -24,8 +23,7 @@ export const organizationStaffMembers = pgTable( precision: 6, mode: "string", }).notNull(), - // You can use { mode: "bigint" } if numbers are exceeding js number limitations - accountId: bigint("account_id", { mode: "number" }), + accountId: bigint("account_id", { mode: "bigint" }), }, (table) => { return { diff --git a/packages/db/src/schemas/organizations.ts b/packages/db/src/schemas/organizations.ts index 321b5d1a..c5db6e6f 100644 --- a/packages/db/src/schemas/organizations.ts +++ b/packages/db/src/schemas/organizations.ts @@ -31,10 +31,8 @@ export const organizations = pgTable( partner: boolean().default(false).notNull(), hidden: boolean().default(false).notNull(), slug: varchar(), - // You can use { mode: "bigint" } if numbers are exceeding js number limitations - limitlessOrgId: bigint("limitless_org_id", { mode: "number" }), - // You can use { mode: "bigint" } if numbers are exceeding js number limitations - ownerId: bigint("owner_id", { mode: "number" }), + limitlessOrgId: bigint("limitless_org_id", { mode: "bigint" }), + ownerId: bigint("owner_id", { mode: "bigint" }), }, (table) => { return { diff --git a/packages/db/src/schemas/phase-players.ts b/packages/db/src/schemas/phase-players.ts index 46335d6f..43e08b97 100644 --- a/packages/db/src/schemas/phase-players.ts +++ b/packages/db/src/schemas/phase-players.ts @@ -14,11 +14,9 @@ export const phasePlayers = pgTable( "phase_players", { id: bigserial({ mode: "bigint" }).primaryKey().notNull(), - // You can use { mode: "bigint" } if numbers are exceeding js number limitations - playerId: bigint("player_id", { mode: "number" }).notNull(), + playerId: bigint("player_id", { mode: "bigint" }).notNull(), phaseType: varchar("phase_type").notNull(), - // You can use { mode: "bigint" } if numbers are exceeding js number limitations - phaseId: bigint("phase_id", { mode: "number" }).notNull(), + phaseId: bigint("phase_id", { mode: "bigint" }).notNull(), createdAt: timestamp("created_at", { precision: 6, mode: "string", diff --git a/packages/db/src/schemas/phases.ts b/packages/db/src/schemas/phases.ts index 41e76e7f..722f1fda 100644 --- a/packages/db/src/schemas/phases.ts +++ b/packages/db/src/schemas/phases.ts @@ -33,8 +33,7 @@ export const phases = pgTable( startedAt: timestamp("started_at", { precision: 6, mode: "string" }), endedAt: timestamp("ended_at", { precision: 6, mode: "string" }), order: integer().default(0).notNull(), - // You can use { mode: "bigint" } if numbers are exceeding js number limitations - currentRoundId: bigint("current_round_id", { mode: "number" }), + currentRoundId: bigint("current_round_id", { mode: "bigint" }), }, // eslint-disable-next-line @typescript-eslint/no-explicit-any (table): Record => { diff --git a/packages/db/src/schemas/pokemon-teams.ts b/packages/db/src/schemas/pokemon-teams.ts index 690e3253..90491ddb 100644 --- a/packages/db/src/schemas/pokemon-teams.ts +++ b/packages/db/src/schemas/pokemon-teams.ts @@ -27,14 +27,11 @@ export const pokemonTeams = pgTable( }).notNull(), published: boolean().default(true).notNull(), name: varchar(), - // You can use { mode: "bigint" } if numbers are exceeding js number limitations - formatId: bigint("format_id", { mode: "number" }).notNull(), - // You can use { mode: "bigint" } if numbers are exceeding js number limitations - gameId: bigint("game_id", { mode: "number" }).notNull(), + formatId: bigint("format_id", { mode: "bigint" }).notNull(), + gameId: bigint("game_id", { mode: "bigint" }).notNull(), archivedAt: timestamp("archived_at", { precision: 6, mode: "string" }), pokepasteId: varchar("pokepaste_id"), - // You can use { mode: "bigint" } if numbers are exceeding js number limitations - profileId: bigint("profile_id", { mode: "number" }), + profileId: bigint("profile_id", { mode: "bigint" }), }, (table) => { return { diff --git a/packages/db/src/schemas/pokemon.ts b/packages/db/src/schemas/pokemon.ts index 48861313..0b9315c0 100644 --- a/packages/db/src/schemas/pokemon.ts +++ b/packages/db/src/schemas/pokemon.ts @@ -35,9 +35,8 @@ export const pokemon = pgTable( mode: "string", }).notNull(), nickname: varchar(), - // You can use { mode: "bigint" } if numbers are exceeding js number limitations - pokemonTeamId: bigint("pokemon_team_id", { mode: "number" }) - .default(0) + pokemonTeamId: bigint("pokemon_team_id", { mode: "bigint" }) + .default(BigInt(0)) .notNull(), form: varchar(), position: integer().default(0).notNull(), diff --git a/packages/db/src/schemas/profiles.ts b/packages/db/src/schemas/profiles.ts index 4f6da6f4..ab310c39 100644 --- a/packages/db/src/schemas/profiles.ts +++ b/packages/db/src/schemas/profiles.ts @@ -26,8 +26,7 @@ export const profiles = pgTable( }).notNull(), imageUrl: varchar("image_url"), slug: varchar(), - // You can use { mode: "bigint" } if numbers are exceeding js number limitations - accountId: bigint("account_id", { mode: "number" }), + accountId: bigint("account_id", { mode: "bigint" }), id: bigserial({ mode: "bigint" }).primaryKey().notNull(), archivedAt: timestamp("archived_at", { precision: 6, mode: "string" }), default: boolean().default(false).notNull(), diff --git a/packages/db/src/schemas/rounds.ts b/packages/db/src/schemas/rounds.ts index 386ff0c1..c2b7aafd 100644 --- a/packages/db/src/schemas/rounds.ts +++ b/packages/db/src/schemas/rounds.ts @@ -12,8 +12,7 @@ export const rounds = pgTable( "rounds", { id: bigserial({ mode: "bigint" }).primaryKey().notNull(), - // You can use { mode: "bigint" } if numbers are exceeding js number limitations - phaseId: bigint("phase_id", { mode: "number" }).notNull(), + phaseId: bigint("phase_id", { mode: "bigint" }).notNull(), createdAt: timestamp("created_at", { precision: 6, mode: "string", diff --git a/packages/db/src/schemas/tournaments-formats.ts b/packages/db/src/schemas/tournaments-formats.ts index 840183af..62193d64 100644 --- a/packages/db/src/schemas/tournaments-formats.ts +++ b/packages/db/src/schemas/tournaments-formats.ts @@ -14,10 +14,8 @@ export const tournamentFormats = pgTable( "tournament_formats", { id: bigserial({ mode: "bigint" }).primaryKey().notNull(), - // You can use { mode: "bigint" } if numbers are exceeding js number limitations - tournamentId: bigint("tournament_id", { mode: "number" }).notNull(), - // You can use { mode: "bigint" } if numbers are exceeding js number limitations - formatId: bigint("format_id", { mode: "number" }).notNull(), + tournamentId: bigint("tournament_id", { mode: "bigint" }).notNull(), + formatId: bigint("format_id", { mode: "bigint" }).notNull(), createdAt: timestamp("created_at", { precision: 6, mode: "string", diff --git a/packages/db/src/schemas/tournaments.ts b/packages/db/src/schemas/tournaments.ts index 6108db7a..a59877c4 100644 --- a/packages/db/src/schemas/tournaments.ts +++ b/packages/db/src/schemas/tournaments.ts @@ -31,16 +31,13 @@ export const tournaments = pgTable( precision: 6, mode: "string", }).notNull(), - // You can use { mode: "bigint" } if numbers are exceeding js number limitations - organizationId: bigint("organization_id", { mode: "number" }), + organizationId: bigint("organization_id", { mode: "bigint" }), checkInStartAt: timestamp("check_in_start_at", { precision: 6, mode: "string", }), - // You can use { mode: "bigint" } if numbers are exceeding js number limitations - gameId: bigint("game_id", { mode: "number" }), - // You can use { mode: "bigint" } if numbers are exceeding js number limitations - formatId: bigint("format_id", { mode: "number" }), + gameId: bigint("game_id", { mode: "bigint" }), + formatId: bigint("format_id", { mode: "bigint" }), endedAt: timestamp("ended_at", { precision: 6, mode: "string" }), registrationStartAt: timestamp("registration_start_at", { precision: 6, @@ -57,11 +54,9 @@ export const tournaments = pgTable( teamlistsRequired: boolean("teamlists_required").default(true).notNull(), openTeamSheets: boolean("open_team_sheets").default(true).notNull(), endAt: timestamp("end_at", { precision: 6, mode: "string" }), - // You can use { mode: "bigint" } if numbers are exceeding js number limitations - limitlessId: bigint("limitless_id", { mode: "number" }), + limitlessId: bigint("limitless_id", { mode: "bigint" }), published: boolean().default(false).notNull(), - // You can use { mode: "bigint" } if numbers are exceeding js number limitations - currentPhaseId: bigint("current_phase_id", { mode: "number" }), + currentPhaseId: bigint("current_phase_id", { mode: "bigint" }), }, (table) => { return {