Files
accounted/app/api/pending-operations/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

69 lines
2.7 KiB
TypeScript

import { NextResponse } from 'next/server'
import { withRouteContext } from '@/lib/api/with-route-context'
import { validateQuery } from '@/lib/api/validate'
import { PendingOperationsQuerySchema } from '@/lib/api/schemas'
import { getErrorMessage as getUserErrorMessage } from '@/lib/errors/get-error-message'
/**
* GET /api/pending-operations
*
* List pending operations for the authenticated user.
* Query params: status (default: pending), limit, offset
*/
export const GET = withRouteContext(
'pending_operation.list',
async (request, { supabase, companyId }) => {
const result = validateQuery(request, PendingOperationsQuerySchema)
if (!result.success) return result.response
const { status, limit, offset } = result.data
// Terminal tabs (Godkända/Avvisade) order by when the op was RESOLVED, not
// created: auto-expired ops are ≥30 days old by construction, so a
// created_at ordering would bury a fresh expiry sweep below a month of
// newer rejections and the "Utgick automatiskt" context would never be seen.
const orderColumn = status === 'pending' ? 'created_at' : 'resolved_at'
const listPromise = supabase
.from('pending_operations')
.select('*', { count: 'exact' })
.eq('company_id', companyId)
.eq('status', status)
.order(orderColumn, { ascending: false, nullsFirst: false })
.range(offset, offset + limit - 1)
// Return every tab count with the active list. The browser previously
// called this authenticated route four times, repeating auth and company
// resolution for one list plus three counters. The active list already
// supplies its own exact count, so only the other two lightweight head
// queries are needed here.
const statuses = ['pending', 'committed', 'rejected'] as const
const otherStatuses = statuses.filter((candidate) => candidate !== status)
const [listResult, ...otherCountResults] = await Promise.all([
listPromise,
...otherStatuses.map((candidate) =>
supabase
.from('pending_operations')
.select('id', { count: 'exact', head: true })
.eq('company_id', companyId)
.eq('status', candidate),
),
])
const { data, error, count } = listResult
if (error) {
return NextResponse.json({ error: getUserErrorMessage(error) }, { status: 500 })
}
const counts: Partial<Record<(typeof statuses)[number], number>> = {
[status]: count ?? 0,
}
otherStatuses.forEach((candidate, index) => {
const result = otherCountResults[index]
if (!result.error) counts[candidate] = result.count ?? 0
})
return NextResponse.json({ data: data ?? [], count, counts })
},
)