Files
accounted/app/api/transactions/[id]/refresh-exchange-rate/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

69 lines
2.2 KiB
TypeScript

import { NextResponse } from 'next/server'
import { withRouteContext } from '@/lib/api/with-route-context'
import { errorResponse, errorResponseFromCode } from '@/lib/errors/get-structured-error'
import { fetchExchangeRate } from '@/lib/currency/riksbanken'
import { guardSandbox } from '@/lib/sandbox/guard'
import type { Currency, Transaction } from '@/types'
export const POST = withRouteContext(
'transaction.refreshExchangeRate',
async (_request, ctx, { params }: { params: Promise<{ id: string }> }) => {
const { id } = await params
const { supabase, companyId, log, requestId } = ctx
const blocked = await guardSandbox(supabase, companyId)
if (blocked) return blocked
const { data: transaction, error: fetchError } = await supabase
.from('transactions')
.select('*')
.eq('id', id)
.eq('company_id', companyId)
.single<Transaction>()
if (fetchError || !transaction) {
return errorResponseFromCode('TX_CATEGORIZE_TX_NOT_FOUND', log, { requestId })
}
// No-op for SEK transactions, or when the rate is already cached.
if (
transaction.currency === 'SEK' ||
(transaction.amount_sek != null && transaction.exchange_rate != null)
) {
return NextResponse.json({ data: transaction })
}
const rate = await fetchExchangeRate(transaction.currency as Currency, new Date(transaction.date), supabase)
if (!rate) {
return errorResponseFromCode('TX_EXCHANGE_RATE_UNAVAILABLE', log, {
requestId,
details: { currency: transaction.currency, date: transaction.date },
})
}
const amountSek = Math.round(transaction.amount * rate.rate * 100) / 100
const { data: updated, error: updateError } = await supabase
.from('transactions')
.update({
amount_sek: amountSek,
exchange_rate: rate.rate,
exchange_rate_date: rate.date,
})
.eq('id', id)
.eq('company_id', companyId)
.is('journal_entry_id', null)
.select('*')
.single<Transaction>()
if (updateError || !updated) {
return errorResponse(updateError ?? new Error('Failed to persist exchange rate'), log, {
requestId,
})
}
return NextResponse.json({ data: updated })
},
{ requireWrite: true },
)