* Add overnight pause feature for draft timers Protects players from having their pick timer expire while asleep. Admins configure a nightly window (league-wide or per-user timezone); the timer freezes during that window while autodraft can still fire. Fixes #66 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * Fix TS error: guard getUserDisplayName against undefined user Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
96 lines
2.7 KiB
TypeScript
96 lines
2.7 KiB
TypeScript
import { eq, inArray, and } from "drizzle-orm";
|
|
import { database } from "~/database/context";
|
|
import * as schema from "~/database/schema";
|
|
|
|
export type User = typeof schema.users.$inferSelect;
|
|
export type NewUser = typeof schema.users.$inferInsert;
|
|
|
|
export function getUserDisplayName(
|
|
user: Pick<User, "username" | "displayName">
|
|
): string | null {
|
|
return user.username ?? user.displayName ?? null;
|
|
}
|
|
|
|
export async function createUser(data: NewUser): Promise<User> {
|
|
const db = database();
|
|
const [user] = await db.insert(schema.users).values(data).returning();
|
|
return user;
|
|
}
|
|
|
|
export async function findUserById(id: string): Promise<User | undefined> {
|
|
const db = database();
|
|
return await db.query.users.findFirst({
|
|
where: eq(schema.users.id, id),
|
|
});
|
|
}
|
|
|
|
export async function findUsersByIds(ids: string[]): Promise<User[]> {
|
|
if (ids.length === 0) return [];
|
|
const db = database();
|
|
return await db
|
|
.select()
|
|
.from(schema.users)
|
|
.where(inArray(schema.users.id, ids));
|
|
}
|
|
|
|
export async function updateUser(
|
|
id: string,
|
|
data: Partial<NewUser>
|
|
): Promise<User> {
|
|
const db = database();
|
|
const [user] = await db
|
|
.update(schema.users)
|
|
.set({ ...data, updatedAt: new Date() })
|
|
.where(eq(schema.users.id, id))
|
|
.returning();
|
|
return user;
|
|
}
|
|
|
|
export async function deleteUser(id: string): Promise<void> {
|
|
const db = database();
|
|
await db.delete(schema.users).where(eq(schema.users.id, id));
|
|
}
|
|
|
|
export async function findAdmins(): Promise<User[]> {
|
|
const db = database();
|
|
return await db.query.users.findMany({
|
|
where: eq(schema.users.isAdmin, true),
|
|
orderBy: (users, { asc }) => [asc(users.displayName)],
|
|
});
|
|
}
|
|
|
|
export async function isUserAdmin(userId: string): Promise<boolean> {
|
|
const user = await findUserById(userId);
|
|
return user?.isAdmin ?? false;
|
|
}
|
|
|
|
export async function setUserAdmin(userId: string, isAdmin: boolean): Promise<User> {
|
|
return await updateUser(userId, { isAdmin });
|
|
}
|
|
|
|
export async function findAllUsers(): Promise<User[]> {
|
|
const db = database();
|
|
return await db.query.users.findMany({
|
|
orderBy: (users, { asc }) => [asc(users.displayName)],
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Returns true if the user currently owns a team in a draft-status season.
|
|
* Used to prevent timezone changes mid-draft.
|
|
*/
|
|
export async function isUserInActiveDraft(userId: string): Promise<boolean> {
|
|
const db = database();
|
|
const result = await db
|
|
.select({ id: schema.teams.id })
|
|
.from(schema.teams)
|
|
.innerJoin(schema.seasons, eq(schema.teams.seasonId, schema.seasons.id))
|
|
.where(
|
|
and(
|
|
eq(schema.teams.ownerId, userId),
|
|
eq(schema.seasons.status, "draft")
|
|
)
|
|
)
|
|
.limit(1);
|
|
return result.length > 0;
|
|
}
|