* feat(api): implement commit functionality for journal entries * fix(extensions): make ExtensionSettings.clear() a real delete so disconnect flows work The 2026-03-30 multi-tenant refactor dropped all RLS policies on extension_data and recreated only SELECT/INSERT/UPDATE. Combined with `value jsonb NOT NULL`, every extension that called `settings.set(key, null)` to clear stored state (cloud-backup disconnect, skatteverket OAuth/AGI cleanup, arcim-migration consent reset) silently failed — the upsert hit the NOT NULL constraint and the error was swallowed, leaving users stuck with stale connection rows. Adds an `extension_data_delete` RLS policy, a `clear(key)` method backed by a real DELETE, switches the four affected handlers, and makes `set()` throw on Supabase error so this class of silent failure can't recur. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * feat(journal-entries): add draft saving functionality to journal entry form * feat: add periodisk sammanställning report generation and CSV export - Implemented period date helpers in `period-dates.ts` for calculating start and end dates based on period type (monthly, quarterly, yearly). - Created `periodisk-sammanstallning.ts` to generate the periodisk sammanställning report, including data fetching, validation, and warning handling. - Developed CSV serializer in `periodisk-sammanstallning-csv.ts` for exporting the report in SKV574008 format. - Added new columns to `company_settings` for storing periodisk sammanställning settings and tax contact information via migration. - Introduced a new migration to add a `paid_with_private_funds` flag to `supplier_invoices` for tracking out-of-pocket expenses. - Updated journal entries to include the new source type for privately paid supplier invoices. * feat(migrations): add paid_with_private_funds flag to supplier_invoices and expand journal_entries.source_type CHECK * fix(ai_requests): drop existing policies and trigger before creating new ones * fix(migrations): ensure extension_data has a proper DELETE policy for ExtensionSettings.clear() * fix(supplier-invoices): update error handling for invalid input in POST request * fix: correct capitalization in project title * fix(migrations): resolve duplicate version 20260513120000 Two migrations shared the same timestamp prefix, causing schema_migrations_pkey collision on Supabase preview branches. Bump extension_data_delete_policy to 20260513120001. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
107 lines
3.8 KiB
TypeScript
107 lines
3.8 KiB
TypeScript
import {
|
|
generatePeriodiskSammanstallning,
|
|
type PsPeriodType,
|
|
} from '@/lib/reports/periodisk-sammanstallning'
|
|
import {
|
|
buildPeriodiskSammanstallningCsv,
|
|
PsCsvBuildError,
|
|
} from '@/lib/reports/periodisk-sammanstallning-csv'
|
|
import { withRouteContext } from '@/lib/api/with-route-context'
|
|
import { errorResponseFromCode } from '@/lib/errors/get-structured-error'
|
|
|
|
/**
|
|
* GET /api/reports/periodisk-sammanstallning/csv
|
|
*
|
|
* Returns the SKV574008-formatted CSV file for upload to Skatteverket.
|
|
* Refuses (400) if the report has any blocking warnings.
|
|
*/
|
|
export const GET = withRouteContext(
|
|
'report.periodisk_sammanstallning.csv',
|
|
async (request, ctx) => {
|
|
const { supabase, companyId, log, requestId } = ctx
|
|
|
|
const { searchParams } = new URL(request.url)
|
|
const periodType = searchParams.get('periodType') as PsPeriodType | null
|
|
const yearStr = searchParams.get('year')
|
|
const periodStr = searchParams.get('period')
|
|
|
|
if (!periodType || !yearStr || !periodStr) {
|
|
return errorResponseFromCode('PS_REPORT_MISSING_PARAMS', log, { requestId })
|
|
}
|
|
if (periodType !== 'monthly' && periodType !== 'quarterly') {
|
|
return errorResponseFromCode('PS_REPORT_INVALID_PERIOD_TYPE', log, {
|
|
requestId, details: { received: periodType },
|
|
})
|
|
}
|
|
const year = parseInt(yearStr, 10)
|
|
const period = parseInt(periodStr, 10)
|
|
if (isNaN(year) || year < 2000 || year > 2100) {
|
|
return errorResponseFromCode('PS_REPORT_INVALID_YEAR', log, { requestId })
|
|
}
|
|
if (isNaN(period)) {
|
|
return errorResponseFromCode('PS_REPORT_INVALID_PERIOD', log, { requestId })
|
|
}
|
|
if (periodType === 'monthly' && (period < 1 || period > 12)) {
|
|
return errorResponseFromCode('PS_REPORT_INVALID_PERIOD', log, { requestId })
|
|
}
|
|
if (periodType === 'quarterly' && (period < 1 || period > 4)) {
|
|
return errorResponseFromCode('PS_REPORT_INVALID_PERIOD', log, { requestId })
|
|
}
|
|
|
|
const { data: settings } = await supabase
|
|
.from('company_settings')
|
|
.select('org_number, tax_contact_name, tax_contact_phone, tax_contact_email')
|
|
.eq('company_id', companyId)
|
|
.single()
|
|
|
|
if (!settings?.org_number
|
|
|| !settings?.tax_contact_name
|
|
|| !settings?.tax_contact_phone
|
|
|| !settings?.tax_contact_email) {
|
|
return errorResponseFromCode('PS_REPORT_MISSING_FILER_INFO', log, { requestId })
|
|
}
|
|
|
|
try {
|
|
const report = await generatePeriodiskSammanstallning(
|
|
supabase, companyId, periodType, year, period,
|
|
)
|
|
|
|
const csv = buildPeriodiskSammanstallningCsv(report, {
|
|
organizationNumber: settings.org_number,
|
|
contactName: settings.tax_contact_name,
|
|
contactPhone: settings.tax_contact_phone,
|
|
contactEmail: settings.tax_contact_email,
|
|
})
|
|
|
|
return new Response(csv.content as unknown as BodyInit, {
|
|
status: 200,
|
|
headers: {
|
|
'Content-Type': csv.mimeType,
|
|
'Content-Disposition': `attachment; filename="${csv.filename}"`,
|
|
'X-Request-Id': requestId,
|
|
},
|
|
})
|
|
} catch (err) {
|
|
if (err instanceof PsCsvBuildError) {
|
|
if (err.reason === 'BLOCKING_WARNINGS') {
|
|
return errorResponseFromCode('PS_REPORT_CSV_BLOCKED_BY_ERRORS', log, {
|
|
requestId, details: { message: err.message },
|
|
})
|
|
}
|
|
if (err.reason === 'MISSING_FILER_INFO') {
|
|
return errorResponseFromCode('PS_REPORT_MISSING_FILER_INFO', log, {
|
|
requestId, details: { message: err.message },
|
|
})
|
|
}
|
|
}
|
|
log.error('periodisk sammanställning CSV failed', err as Error, {
|
|
periodType, year, period,
|
|
})
|
|
return errorResponseFromCode('PS_REPORT_GENERATION_FAILED', log, {
|
|
requestId,
|
|
details: { reason: err instanceof Error ? err.message : 'unknown' },
|
|
})
|
|
}
|
|
},
|
|
)
|