Files
accounted/app/api/deadlines/route.ts
T
Mattsson e11f70b347 Bug/gh issues fiz (#1103)
* refactor: optimize page loading and data fetching

* fix: resolve recurring production runtime errors

* feat: add MCP company and customer updates

* fix: handle year-end tax adjustments

* feat: harden annual report compliance

* fix: expand invoice logo and font support

* fix: sanitize API route error responses

* fix: sanitize user-facing error messages

* feat: persist onboarding and tax assessment notices

* fix: reduce cloud backup audit churn

* feat: refine invoice editor layout

* fix: show saved tax adjustments in INK2

* fix: complete annual report API mappings

* docs: record operational safeguards and decisions

* fix: harden annual report review findings

* fix: adjust column span for description based on VAT registration

* New css class name
2026-07-21 23:00:15 +02:00

100 lines
2.7 KiB
TypeScript

import { NextResponse } from 'next/server'
import { withRouteContext } from '@/lib/api/with-route-context'
import { validateBody } from '@/lib/api/validate'
import { CreateDeadlineSchema } from '@/lib/api/schemas'
import { getErrorMessage as getUserErrorMessage } from '@/lib/errors/get-error-message'
/**
* GET /api/deadlines
* List deadlines for the authenticated user
* Query params:
* - status: 'all' | 'pending' | 'completed' (default: 'all')
* - type: DeadlineType (optional)
* - from: ISO date string (optional)
* - to: ISO date string (optional)
*/
export const GET = withRouteContext('deadline.list', async (request, ctx) => {
const { supabase, companyId } = ctx
// Parse query params
const { searchParams } = new URL(request.url)
const status = searchParams.get('status') || 'all'
const type = searchParams.get('type')
const from = searchParams.get('from')
const to = searchParams.get('to')
// Build query. Dismissed system deadlines are an explicit opt-out and
// never listed.
let query = supabase
.from('deadlines')
.select('*, customer:customers(id, name)')
.eq('company_id', companyId)
.is('dismissed_at', null)
// Apply filters
if (status === 'pending') {
query = query.eq('is_completed', false)
} else if (status === 'completed') {
query = query.eq('is_completed', true)
}
if (type) {
query = query.eq('deadline_type', type)
}
if (from) {
query = query.gte('due_date', from)
}
if (to) {
query = query.lte('due_date', to)
}
const { data, error } = await query.order('due_date', { ascending: true })
if (error) {
return NextResponse.json({ error: getUserErrorMessage(error) }, { status: 500 })
}
return NextResponse.json({ data })
})
/**
* POST /api/deadlines
* Create a new deadline
*/
export const POST = withRouteContext(
'deadline.create',
async (request, ctx) => {
const { supabase, companyId, user } = ctx
const validation = await validateBody(request, CreateDeadlineSchema)
if (!validation.success) return validation.response
const body = validation.data
// Insert the deadline
const { data, error } = await supabase
.from('deadlines')
.insert({
user_id: user.id,
company_id: companyId,
title: body.title,
due_date: body.due_date,
due_time: body.due_time || null,
deadline_type: body.deadline_type,
priority: body.priority || 'normal',
customer_id: body.customer_id || null,
notes: body.notes || null,
})
.select('*, customer:customers(id, name)')
.single()
if (error) {
return NextResponse.json({ error: getUserErrorMessage(error) }, { status: 500 })
}
return NextResponse.json({ data })
},
{ requireWrite: true },
)