brackt/app/models/team.ts
Chris Parsons d342f1ad25
Remove backfill-flag-configs script (#389)
* Remove backfill-flag-configs script

One-time migration has been run; script is no longer needed.

https://claude.ai/code/session_014RUwPfm1qc539xLxW4m9Uy

* Clean up flag config code

- FlagSvg: remove redundant getDisplayFlagConfig call; config is already a
  valid FlagConfig by type, so the validation/fallback was dead code. Also
  replace the unreachable `?? "#adf661"` fallback with a non-null assertion.
- team/user models: pre-generate ID before insert so flagConfig is persisted
  immediately on creation rather than relying on display-time generation.
- auth.server.ts: add create.after hook so BetterAuth-created users also get
  a flagConfig written to the DB on signup.

https://claude.ai/code/session_014RUwPfm1qc539xLxW4m9Uy

* Fix lint: replace non-null assertion in FlagSvg with destructure defaults

oxlint forbids the ! operator; destructuring with empty-string defaults
is equivalent since FlagConfig always has 3 colors.

https://claude.ai/code/session_014RUwPfm1qc539xLxW4m9Uy

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-05-06 20:35:02 -07:00

114 lines
3.2 KiB
TypeScript

import { eq, and, isNull } from "drizzle-orm";
import { database } from "~/database/context";
import * as schema from "~/database/schema";
import { generateFlagConfig } from "~/lib/flag-generator";
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();
const id = data.id ?? crypto.randomUUID();
const [team] = await db
.insert(schema.teams)
.values({ ...data, id, flagConfig: data.flagConfig ?? generateFlagConfig(id) })
.returning();
return team;
}
export async function createManyTeams(teams: NewTeam[]): Promise<Team[]> {
const db = database();
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();
}
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)],
});
}
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)
),
});
}
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 });
}
export async function claimTeam(
id: string,
ownerId: string,
name: string
): Promise<Team> {
return await updateTeam(id, { ownerId, name });
}
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));
}