🐛 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
+48 -21
View File
@@ -1,35 +1,62 @@
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 description = typeof body?.description === 'string' ? body.description.trim() : ''
const amountCents = Number(body?.amountCents)
const payerId = Number(body?.payerId)
const participantIds: number[] = Array.isArray(body?.participantIds) ? body.participantIds.map(Number) : []
const body = await readBody(event);
const description =
typeof body?.description === 'string' ? body.description.trim() : '';
const amountCents = Number(body?.amountCents);
const payerId = Number(body?.payerId);
const participantIds: number[] = Array.isArray(body?.participantIds)
? body.participantIds.map(Number)
: [];
if (!description) {
throw createError({ statusCode: 400, statusMessage: 'description is required' })
throw createError({
statusCode: 400,
statusMessage: 'description is required',
});
}
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 (participantIds.length === 0) {
throw createError({ statusCode: 400, statusMessage: 'at least one participant must be selected' })
throw createError({
statusCode: 400,
statusMessage: 'at least one participant must be selected',
});
}
const potParticipants = await getParticipants(slug)
const validIds = new Set(potParticipants.map(p => p.id))
if (!validIds.has(payerId) || participantIds.some(id => !validIds.has(id))) {
throw createError({ statusCode: 400, statusMessage: 'payerId and participantIds must reference participants of this Pot' })
const potParticipants = await getParticipants(slug);
const validIds = new Set(potParticipants.map((p) => p.id));
if (
!validIds.has(payerId) ||
participantIds.some((id) => !validIds.has(id))
) {
throw createError({
statusCode: 400,
statusMessage:
'payerId and participantIds must reference participants of this Pot',
});
}
const db = useDb()
const [expense] = await db.insert(expenses).values({ potSlug: slug, description, amountCents, payerId }).returning()
await db.insert(expenseParticipants).values(participantIds.map(participantId => ({ expenseId: expense!.id, participantId })))
const db = useDb(event);
const [expense] = await db
.insert(expenses)
.values({ potSlug: slug, description, amountCents, payerId })
.returning();
await db.insert(expenseParticipants).values(
participantIds.map((participantId) => ({
expenseId: expense!.id,
participantId,
})),
);
return { ...expense, participantIds }
})
return { ...expense, participantIds };
});