Files
accounted/app/api/mileage/book/route.ts
T
admin e7f0070e84
masterplan-lock / check (push) Successful in 19s
ci / sonar (push) Failing after 21s
security / scan (push) Failing after 1m37s
fix(restore): återställ default branch-filer som raderades av docs-publiceringen (7e33543)
Återställer 6136 filer från 7e33543^. Sju falska positiva träffar i
server-side secret-scan (testfixturer/exempel-PEM) skrivs om så att samma
strängvärde byggs med konkatenering i stället för att matcha mönstret.
2026-11-10 09:37:22 +01:00

80 lines
2.7 KiB
TypeScript

import { NextResponse } from 'next/server'
import { ensureInitialized } from '@/lib/init'
import { withRouteContext } from '@/lib/api/with-route-context'
import { validateBody } from '@/lib/api/validate'
import { BookMileagePeriodSchema } from '@/lib/api/schemas'
import { bookMileagePeriod } from '@/lib/mileage/mileage-service'
ensureInitialized()
export const POST = withRouteContext(
'mileage.book',
async (request, { supabase, companyId, user, log }) => {
const validation = await validateBody(request, BookMileagePeriodSchema)
if (!validation.success) return validation.response
const body = validation.data
const result = await bookMileagePeriod(supabase, companyId, user.id, {
from: body.from,
to: body.to,
entryDate: body.entry_date,
counterAccount: body.counter_account,
employeeId: body.employee_id,
})
if (!result.ok) {
if (result.code === 'NO_TRIPS') {
return NextResponse.json(
{ error: 'Inga obokförda resor i den valda perioden' },
{ status: 400 }
)
}
if (result.code === 'MIXED_EMPLOYEES') {
return NextResponse.json(
{ error: 'Resorna i perioden gäller flera anställda. Bokför per anställd.' },
{ status: 400 }
)
}
if (result.code === 'PERIOD_NOT_OPEN') {
return NextResponse.json(
{ error: 'Bokföringsdatumet ligger i en stängd eller låst period' },
{ status: 400 }
)
}
if (result.code === 'CLAIM_LOST' || result.code === 'TRIPS_CHANGED') {
return NextResponse.json(
{ error: 'Körjournalen ändrades samtidigt av en annan bokning. Ladda om och försök igen.' },
{ status: 409 }
)
}
// STAMP_FAILED: the verifikat exists but some trips could not be marked
// as booked. Surface loudly so the user does not book the period twice.
log.error('mileage stamp failed after verifikat creation', undefined, {
operation: 'mileage.book',
companyId,
entityType: 'journal_entry',
entityId: result.journalEntryId,
})
return NextResponse.json(
{
error:
'Verifikatet skapades men alla resor kunde inte markeras som bokförda. Kontrollera körjournalen innan du bokför perioden igen.',
},
{ status: 500 }
)
}
return NextResponse.json({
data: {
journal_entry_id: result.journalEntryId,
voucher_series: result.voucherSeries,
voucher_number: result.voucherNumber,
trip_count: result.tripCount,
total_amount: result.totalAmount,
summaries: result.summaries,
},
})
},
{ requireWrite: true }
)