feat(invoices): implement öresavrundning logic and next invoice numbe… (#429)
* feat(invoices): implement öresavrundning logic and next invoice number preview - Added `getDisplayTotal` utility to handle rounding for SEK invoices based on company settings. - Updated `InvoicesPage` to utilize the new rounding logic when displaying totals. - Introduced `peek_next_invoice_number` function to allow previewing the next invoice number without consuming the sequence. - Modified invoice number generation to remove the year prefix and prevent truncation of numbers exceeding three digits. - Enhanced tests for invoice number generation and rounding functionality to ensure correctness. - Updated PDF template to reflect new rounding logic for totals and display appropriate values. - Adjusted company switcher to hide options in sandbox mode. - Improved error handling and logging in sandbox seeding process. * fix(skatteverket): remove unused scope labels from SCOPE_LABELS and DEFAULT_SCOPES
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
import { createClient } from '@/lib/supabase/server'
|
||||
import { NextResponse } from 'next/server'
|
||||
import { requireCompanyId } from '@/lib/company/context'
|
||||
import { getActiveCompanyId } from '@/lib/company/context'
|
||||
import { createLogger } from '@/lib/logger'
|
||||
|
||||
const log = createLogger('sandbox:seed')
|
||||
|
||||
/**
|
||||
* POST /api/sandbox/seed
|
||||
@@ -19,7 +22,30 @@ export async function POST() {
|
||||
return NextResponse.json({ error: 'Sandbox is only available for anonymous users' }, { status: 403 })
|
||||
}
|
||||
|
||||
const companyId = await requireCompanyId(supabase, user.id)
|
||||
// Anonymous users start with no company. Create one before seeding.
|
||||
// If a previous seed attempt already created a company for this user, reuse it
|
||||
// (idempotency).
|
||||
let companyId = await getActiveCompanyId(supabase, user.id)
|
||||
|
||||
if (!companyId) {
|
||||
const { data: newCompanyId, error: companyError } = await supabase.rpc(
|
||||
'create_company_with_owner',
|
||||
{
|
||||
p_name: 'Sandlådan Konsult',
|
||||
p_entity_type: 'enskild_firma',
|
||||
}
|
||||
)
|
||||
|
||||
if (companyError || !newCompanyId) {
|
||||
log.error('failed to create sandbox company', { error: companyError, userId: user.id })
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to create sandbox company' },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
|
||||
companyId = newCompanyId as string
|
||||
}
|
||||
|
||||
// Idempotency: if already seeded, return early
|
||||
const { data: existing } = await supabase
|
||||
@@ -523,7 +549,8 @@ export async function POST() {
|
||||
if (dlError) throw dlError
|
||||
|
||||
return NextResponse.json({ seeded: true })
|
||||
} catch {
|
||||
} catch (err) {
|
||||
log.error('failed to seed sandbox data', { error: err, userId: user.id, companyId })
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to seed sandbox data' },
|
||||
{ status: 500 }
|
||||
|
||||
Reference in New Issue
Block a user