2026-05-23 16:55:08 -07:00
|
|
|
import { eq, and, isNull, ne, sql } from "drizzle-orm";
|
2025-10-11 00:07:39 -07:00
|
|
|
import { database } from "~/database/context";
|
|
|
|
|
import * as schema from "~/database/schema";
|
2026-05-06 20:35:02 -07:00
|
|
|
import { generateFlagConfig } from "~/lib/flag-generator";
|
2025-10-11 00:07:39 -07:00
|
|
|
|
|
|
|
|
export type Team = typeof schema.teams.$inferSelect;
|
|
|
|
|
export type NewTeam = typeof schema.teams.$inferInsert;
|
|
|
|
|
|
|
|
|
|
export async function createTeam(data: NewTeam): Promise<Team> {
|
|
|
|
|
const db = database();
|
2026-05-06 20:35:02 -07:00
|
|
|
const id = data.id ?? crypto.randomUUID();
|
|
|
|
|
const [team] = await db
|
|
|
|
|
.insert(schema.teams)
|
|
|
|
|
.values({ ...data, id, flagConfig: data.flagConfig ?? generateFlagConfig(id) })
|
|
|
|
|
.returning();
|
2025-10-11 00:07:39 -07:00
|
|
|
return team;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function createManyTeams(teams: NewTeam[]): Promise<Team[]> {
|
|
|
|
|
const db = database();
|
2026-05-06 20:35:02 -07:00
|
|
|
const withFlags = teams.map((team) => {
|
|
|
|
|
const id = team.id ?? crypto.randomUUID();
|
|
|
|
|
return { ...team, id, flagConfig: team.flagConfig ?? generateFlagConfig(id) };
|
|
|
|
|
});
|
|
|
|
|
return await db.insert(schema.teams).values(withFlags).returning();
|
2025-10-11 00:07:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function findTeamById(id: string): Promise<Team | undefined> {
|
|
|
|
|
const db = database();
|
|
|
|
|
return await db.query.teams.findFirst({
|
|
|
|
|
where: eq(schema.teams.id, id),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function findTeamsBySeasonId(seasonId: string): Promise<Team[]> {
|
|
|
|
|
const db = database();
|
|
|
|
|
return await db.query.teams.findMany({
|
|
|
|
|
where: eq(schema.teams.seasonId, seasonId),
|
|
|
|
|
orderBy: (teams, { asc }) => [asc(teams.name)],
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function findTeamsByOwnerId(ownerId: string): Promise<Team[]> {
|
|
|
|
|
const db = database();
|
|
|
|
|
return await db.query.teams.findMany({
|
|
|
|
|
where: eq(schema.teams.ownerId, ownerId),
|
|
|
|
|
orderBy: (teams, { asc }) => [asc(teams.name)],
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-12 10:12:38 -07:00
|
|
|
export async function findTeamByOwnerAndSeason(
|
|
|
|
|
ownerId: string,
|
|
|
|
|
seasonId: string
|
|
|
|
|
): Promise<Team | undefined> {
|
|
|
|
|
const db = database();
|
|
|
|
|
return await db.query.teams.findFirst({
|
|
|
|
|
where: and(
|
|
|
|
|
eq(schema.teams.ownerId, ownerId),
|
|
|
|
|
eq(schema.teams.seasonId, seasonId)
|
|
|
|
|
),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-23 16:55:08 -07:00
|
|
|
export async function findTeamByNameInSeason(
|
|
|
|
|
seasonId: string,
|
|
|
|
|
name: string,
|
|
|
|
|
excludeTeamId?: string
|
|
|
|
|
): Promise<Team | undefined> {
|
|
|
|
|
const db = database();
|
|
|
|
|
return await db.query.teams.findFirst({
|
|
|
|
|
where: and(
|
|
|
|
|
eq(schema.teams.seasonId, seasonId),
|
|
|
|
|
sql`lower(${schema.teams.name}) = lower(${name})`,
|
|
|
|
|
excludeTeamId ? ne(schema.teams.id, excludeTeamId) : undefined
|
|
|
|
|
),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-11 00:07:39 -07:00
|
|
|
export async function findAvailableTeams(seasonId: string): Promise<Team[]> {
|
|
|
|
|
const db = database();
|
|
|
|
|
return await db.query.teams.findMany({
|
|
|
|
|
where: and(
|
|
|
|
|
eq(schema.teams.seasonId, seasonId),
|
|
|
|
|
isNull(schema.teams.ownerId)
|
|
|
|
|
),
|
|
|
|
|
orderBy: (teams, { asc }) => [asc(teams.name)],
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function updateTeam(
|
|
|
|
|
id: string,
|
|
|
|
|
data: Partial<NewTeam>
|
|
|
|
|
): Promise<Team> {
|
|
|
|
|
const db = database();
|
|
|
|
|
const [team] = await db
|
|
|
|
|
.update(schema.teams)
|
|
|
|
|
.set({ ...data, updatedAt: new Date() })
|
|
|
|
|
.where(eq(schema.teams.id, id))
|
|
|
|
|
.returning();
|
|
|
|
|
return team;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function assignTeamOwner(
|
|
|
|
|
id: string,
|
|
|
|
|
ownerId: string
|
|
|
|
|
): Promise<Team> {
|
|
|
|
|
return await updateTeam(id, { ownerId });
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-18 00:53:40 -07:00
|
|
|
export async function claimTeam(
|
|
|
|
|
id: string,
|
|
|
|
|
ownerId: string,
|
|
|
|
|
name: string
|
|
|
|
|
): Promise<Team> {
|
|
|
|
|
return await updateTeam(id, { ownerId, name });
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-11 00:07:39 -07:00
|
|
|
export async function removeTeamOwner(id: string): Promise<Team> {
|
|
|
|
|
return await updateTeam(id, { ownerId: null });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function renameTeam(id: string, name: string): Promise<Team> {
|
|
|
|
|
return await updateTeam(id, { name });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function deleteTeam(id: string): Promise<void> {
|
|
|
|
|
const db = database();
|
|
|
|
|
await db.delete(schema.teams).where(eq(schema.teams.id, id));
|
|
|
|
|
}
|