Files
accounted/app/api/settings/eu-trade-signal/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

44 lines
1.6 KiB
TypeScript

import { NextResponse } from 'next/server'
import { withRouteContext } from '@/lib/api/with-route-context'
import { fetchEntryLines, type EntryLinesQuery } from '@/lib/bookkeeping/entry-lines'
/**
* Accounts whose postings indicate EU B2B sales: 3108 goods, 3308 services,
* 3107 triangulation. Same set the periodisk sammanställning report keys on.
*/
const EU_SALES_ACCOUNTS = ['3108', '3308', '3107']
/** How far back to look for EU sales postings. */
const LOOKBACK_MONTHS = 15
/**
* GET /api/settings/eu-trade-signal
*
* Derived suggestion signal for the tax settings page: companies with EU
* sales in the ledger have a statutory periodisk sammanställning obligation
* (SFL 35 kap.), but the driving settings flags are opt-in and easily left
* off. The settings UI uses this to prompt the user to confirm EU trade and
* PS registration; it never flips the flags itself.
*/
export const GET = withRouteContext(
'settings.eu_trade_signal',
async (_request, { supabase, companyId }) => {
const since = new Date()
since.setMonth(since.getMonth() - LOOKBACK_MONTHS)
const sinceStr = since.toISOString().split('T')[0]
const lines = await fetchEntryLines<{ account_number: string }>({
supabase,
lineColumns: 'account_number',
filterEntries: (q: EntryLinesQuery) =>
q
.eq('company_id', companyId)
.in('status', ['posted', 'reversed'])
.gte('entry_date', sinceStr),
filterLines: (q: EntryLinesQuery) => q.in('account_number', EU_SALES_ACCOUNTS),
})
return NextResponse.json({ data: { has_eu_sales: lines.length > 0 } })
},
)