156 lines
4.2 KiB
TypeScript
156 lines
4.2 KiB
TypeScript
import { eq, and } from "drizzle-orm";
|
|
import { database } from "~/database/context";
|
|
import * as schema from "~/database/schema";
|
|
|
|
export type ParticipantResult = typeof schema.participantResults.$inferSelect;
|
|
export type NewParticipantResult = typeof schema.participantResults.$inferInsert;
|
|
|
|
export async function createParticipantResult(
|
|
data: NewParticipantResult
|
|
): Promise<ParticipantResult> {
|
|
const db = database();
|
|
const [result] = await db
|
|
.insert(schema.participantResults)
|
|
.values(data)
|
|
.returning();
|
|
return result;
|
|
}
|
|
|
|
export async function createManyParticipantResults(
|
|
data: NewParticipantResult[]
|
|
): Promise<ParticipantResult[]> {
|
|
const db = database();
|
|
return await db
|
|
.insert(schema.participantResults)
|
|
.values(data)
|
|
.returning();
|
|
}
|
|
|
|
export async function findParticipantResultById(
|
|
id: string
|
|
): Promise<ParticipantResult | undefined> {
|
|
const db = database();
|
|
return await db.query.participantResults.findFirst({
|
|
where: eq(schema.participantResults.id, id),
|
|
with: {
|
|
participant: true,
|
|
sportsSeason: {
|
|
with: {
|
|
sport: true,
|
|
},
|
|
},
|
|
},
|
|
});
|
|
}
|
|
|
|
export async function findParticipantResultByParticipantId(
|
|
participantId: string
|
|
): Promise<ParticipantResult | undefined> {
|
|
const db = database();
|
|
return await db.query.participantResults.findFirst({
|
|
where: eq(schema.participantResults.participantId, participantId),
|
|
with: {
|
|
participant: true,
|
|
sportsSeason: {
|
|
with: {
|
|
sport: true,
|
|
},
|
|
},
|
|
},
|
|
});
|
|
}
|
|
|
|
export async function findParticipantResultsBySportsSeasonId(
|
|
sportsSeasonId: string
|
|
): Promise<ParticipantResult[]> {
|
|
const db = database();
|
|
return await db.query.participantResults.findMany({
|
|
where: eq(schema.participantResults.sportsSeasonId, sportsSeasonId),
|
|
orderBy: (results, { asc }) => [asc(results.finalPosition)],
|
|
with: {
|
|
participant: true,
|
|
},
|
|
});
|
|
}
|
|
|
|
export async function updateParticipantResult(
|
|
id: string,
|
|
data: Partial<NewParticipantResult>
|
|
): Promise<ParticipantResult> {
|
|
const db = database();
|
|
const [result] = await db
|
|
.update(schema.participantResults)
|
|
.set({ ...data, updatedAt: new Date() })
|
|
.where(eq(schema.participantResults.id, id))
|
|
.returning();
|
|
return result;
|
|
}
|
|
|
|
export async function deleteParticipantResult(id: string): Promise<void> {
|
|
const db = database();
|
|
await db.delete(schema.participantResults).where(eq(schema.participantResults.id, id));
|
|
}
|
|
|
|
export async function deleteParticipantResultsBySportsSeasonId(
|
|
sportsSeasonId: string
|
|
): Promise<void> {
|
|
const db = database();
|
|
await db
|
|
.delete(schema.participantResults)
|
|
.where(eq(schema.participantResults.sportsSeasonId, sportsSeasonId));
|
|
}
|
|
|
|
/**
|
|
* Calculate points awarded based on final position
|
|
* 1st: 80, 2nd: 50, 3rd-4th: 30, 5th-8th: 20, 9th+: 0
|
|
*/
|
|
export function calculatePointsFromPosition(position: number | null): number {
|
|
if (position === null || position < 1) return 0;
|
|
if (position === 1) return 80;
|
|
if (position === 2) return 50;
|
|
if (position >= 3 && position <= 4) return 30;
|
|
if (position >= 5 && position <= 8) return 20;
|
|
return 0;
|
|
}
|
|
|
|
/**
|
|
* Set result with automatic point calculation
|
|
*/
|
|
export async function setParticipantResult(
|
|
participantId: string,
|
|
sportsSeasonId: string,
|
|
finalPosition: number,
|
|
qualifyingPoints?: number,
|
|
notes?: string
|
|
): Promise<ParticipantResult> {
|
|
const db = database();
|
|
const pointsAwarded = calculatePointsFromPosition(finalPosition);
|
|
|
|
// Check if result already exists
|
|
const existing = await db.query.participantResults.findFirst({
|
|
where: and(
|
|
eq(schema.participantResults.participantId, participantId),
|
|
eq(schema.participantResults.sportsSeasonId, sportsSeasonId)
|
|
),
|
|
});
|
|
|
|
if (existing) {
|
|
// Update existing result
|
|
return await updateParticipantResult(existing.id, {
|
|
finalPosition,
|
|
pointsAwarded,
|
|
qualifyingPoints: qualifyingPoints?.toString(),
|
|
notes,
|
|
});
|
|
} else {
|
|
// Create new result
|
|
return await createParticipantResult({
|
|
participantId,
|
|
sportsSeasonId,
|
|
finalPosition,
|
|
pointsAwarded,
|
|
qualifyingPoints: qualifyingPoints?.toString(),
|
|
notes,
|
|
});
|
|
}
|
|
}
|