🐛 add env to useDb and apply prettier
tinyedge/deploy Deployed by TinyEdge

This commit is contained in:
2026-08-07 13:28:26 +02:00
parent 67727f96ab
commit cbe1b42f6e
54 changed files with 5492 additions and 2406 deletions
+42 -28
View File
@@ -1,47 +1,61 @@
import { eq, inArray } from 'drizzle-orm'
import { useDb } from '../database/client'
import { expenseParticipants, expenses, participants, pots } from '../database/schema'
import { eq, inArray } from 'drizzle-orm';
import { useDb } from '../database/client';
import {
expenseParticipants,
expenses,
participants,
pots,
} from '../database/schema';
export interface ExpenseWithDetails {
id: number
type: string
description: string
amountCents: number
payerId: number
participantIds: number[]
createdAt: number
id: number;
type: string;
description: string;
amountCents: number;
payerId: number;
participantIds: number[];
createdAt: number;
}
export async function getPotOrThrow(slug: string) {
const db = useDb()
const [pot] = await db.select().from(pots).where(eq(pots.slug, slug))
const db = useDb(event);
const [pot] = await db.select().from(pots).where(eq(pots.slug, slug));
if (!pot) {
throw createError({ statusCode: 404, statusMessage: 'Pot not found' })
throw createError({ statusCode: 404, statusMessage: 'Pot not found' });
}
return pot
return pot;
}
export async function getParticipants(slug: string) {
const db = useDb()
return db.select().from(participants).where(eq(participants.potSlug, slug))
const db = useDb(event);
return db.select().from(participants).where(eq(participants.potSlug, slug));
}
export async function getExpensesWithParticipants(slug: string): Promise<ExpenseWithDetails[]> {
const db = useDb()
const expenseRows = await db.select().from(expenses).where(eq(expenses.potSlug, slug)).orderBy(expenses.createdAt)
if (expenseRows.length === 0) return []
export async function getExpensesWithParticipants(
slug: string,
): Promise<ExpenseWithDetails[]> {
const db = useDb(event);
const expenseRows = await db
.select()
.from(expenses)
.where(eq(expenses.potSlug, slug))
.orderBy(expenses.createdAt);
if (expenseRows.length === 0) return [];
const expenseIds = expenseRows.map(row => row.id)
const links = await db.select().from(expenseParticipants).where(inArray(expenseParticipants.expenseId, expenseIds))
const expenseIds = expenseRows.map((row) => row.id);
const links = await db
.select()
.from(expenseParticipants)
.where(inArray(expenseParticipants.expenseId, expenseIds));
const participantIdsByExpense = new Map<number, number[]>()
const participantIdsByExpense = new Map<number, number[]>();
for (const link of links) {
const list = participantIdsByExpense.get(link.expenseId) ?? []
list.push(link.participantId)
participantIdsByExpense.set(link.expenseId, list)
const list = participantIdsByExpense.get(link.expenseId) ?? [];
list.push(link.participantId);
participantIdsByExpense.set(link.expenseId, list);
}
return expenseRows.map(row => ({
return expenseRows.map((row) => ({
id: row.id,
type: row.type,
description: row.description,
@@ -49,5 +63,5 @@ export async function getExpensesWithParticipants(slug: string): Promise<Expense
payerId: row.payerId,
participantIds: participantIdsByExpense.get(row.id) ?? [],
createdAt: row.createdAt,
}))
}));
}