🐛 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
+38 -20
View File
@@ -1,33 +1,51 @@
import { useDb } from '~~/server/database/client'
import { expenseParticipants, expenses } from '~~/server/database/schema'
import { useDb } from '~~/server/database/client';
import { expenseParticipants, expenses } from '~~/server/database/schema';
export default defineEventHandler(async (event) => {
const slug = getRouterParam(event, 'slug')!
await getPotOrThrow(slug)
const slug = getRouterParam(event, 'slug')!;
await getPotOrThrow(slug);
const body = await readBody(event)
const fromId = Number(body?.fromId)
const toId = Number(body?.toId)
const amountCents = Number(body?.amountCents)
const body = await readBody(event);
const fromId = Number(body?.fromId);
const toId = Number(body?.toId);
const amountCents = Number(body?.amountCents);
if (!Number.isInteger(amountCents) || amountCents <= 0) {
throw createError({ statusCode: 400, statusMessage: 'amountCents must be a positive integer' })
throw createError({
statusCode: 400,
statusMessage: 'amountCents must be a positive integer',
});
}
if (fromId === toId) {
throw createError({ statusCode: 400, statusMessage: 'fromId and toId must differ' })
throw createError({
statusCode: 400,
statusMessage: 'fromId and toId must differ',
});
}
const potParticipants = await getParticipants(slug)
const validIds = new Set(potParticipants.map(p => p.id))
const potParticipants = await getParticipants(slug);
const validIds = new Set(potParticipants.map((p) => p.id));
if (!validIds.has(fromId) || !validIds.has(toId)) {
throw createError({ statusCode: 400, statusMessage: 'fromId and toId must reference participants of this Pot' })
throw createError({
statusCode: 400,
statusMessage: 'fromId and toId must reference participants of this Pot',
});
}
const db = useDb()
const [payment] = await db.insert(expenses)
.values({ potSlug: slug, type: 'payment', description: 'Settlement', amountCents, payerId: fromId })
.returning()
await db.insert(expenseParticipants).values({ expenseId: payment!.id, participantId: toId })
const db = useDb(event);
const [payment] = await db
.insert(expenses)
.values({
potSlug: slug,
type: 'payment',
description: 'Settlement',
amountCents,
payerId: fromId,
})
.returning();
await db
.insert(expenseParticipants)
.values({ expenseId: payment!.id, participantId: toId });
return { ...payment, participantIds: [toId] }
})
return { ...payment, participantIds: [toId] };
});