f338850bd0
* fix: hide API-archived customers and suppliers from lists and pickers The v1 API soft-archives customers and suppliers (archived_at, plus is_active=false on suppliers) and its own list routes hide those rows behind ?include_archived=true. No other surface filtered archived_at, so an archived counterparty stayed a normal row in the dashboard rosters, the internal /api/customers and /api/suppliers list routes, the MCP list tools and every customer/supplier picker. Apply the same canonical `archived_at IS NULL` filter on every non-v1 list and picker path: - /api/customers GET, /api/suppliers GET (feeds the customers page and the supplier-invoice form) - suppliers dashboard page (reads suppliers via browser Supabase) - InvoiceEditor and NewRecurringScheduleDialog customer pickers; an invoice or schedule being edited keeps its current customer visible (archiving does not refuse on drafts, so a draft can point at one) - deadlines page and CalendarWorkspace customer pickers - InvoicePreviewCard sample customer - gnubok_list_customers and gnubok_list_suppliers: hidden by default, optional include_archived boolean mirroring the v1 flag; rows now carry archived_at so an agent can tell them apart when opted in Detail routes and by-id lookups are untouched: an archived row still opens. The delete-vs-archive semantics are unchanged. The tools/list payload guard moves 60.7K to 60.8K: main had ~6 tokens of headroom, so even the bare boolean contract crossed. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * test(schema): raise the unresolvable-expression ceiling by 2 for the archived-customer picker filters The two .or('archived_at.is.null,id.eq.<uuid>') filters keep an edited draft's archived customer selectable. The uuid is a runtime value, so the scanner cannot resolve the expression; both columns exist and the filter is covered by the archived-counterparty tests. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> --------- Co-authored-by: Jakob Wennberg <311770904+jakobwennberg-oss@users.noreply.github.com> Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
103 lines
3.2 KiB
TypeScript
103 lines
3.2 KiB
TypeScript
import { NextResponse } from 'next/server'
|
|
import { eventBus } from '@/lib/events'
|
|
import { ensureInitialized } from '@/lib/init'
|
|
import { validateBody } from '@/lib/api/validate'
|
|
import { CreateSupplierSchema } from '@/lib/api/schemas'
|
|
import { withRouteContext } from '@/lib/api/with-route-context'
|
|
import { errorResponse, errorResponseFromCode } from '@/lib/errors/get-structured-error'
|
|
import type { Supplier } from '@/types'
|
|
import { getErrorMessage as getUserErrorMessage } from '@/lib/errors/get-error-message'
|
|
|
|
ensureInitialized()
|
|
|
|
export const GET = withRouteContext(
|
|
'supplier.list',
|
|
async (_request, ctx) => {
|
|
const { supabase, companyId, log, requestId } = ctx
|
|
|
|
// Archived rows (soft-deleted via the v1 API: archived_at + is_active=false)
|
|
// stay in the table for BFL retention but are not part of the roster.
|
|
// Same canonical "active" filter as the v1 list route.
|
|
const { data, error } = await supabase
|
|
.from('suppliers')
|
|
.select('*')
|
|
.eq('company_id', companyId)
|
|
.is('archived_at', null)
|
|
.order('name', { ascending: true })
|
|
|
|
if (error) {
|
|
log.error('supplier list failed', error)
|
|
return errorResponse(error, log, { requestId })
|
|
}
|
|
|
|
return NextResponse.json({ data })
|
|
},
|
|
)
|
|
|
|
export const POST = withRouteContext(
|
|
'supplier.create',
|
|
async (request, ctx) => {
|
|
const { user, supabase, companyId, log, requestId } = ctx
|
|
|
|
const result = await validateBody(request, CreateSupplierSchema, {
|
|
log,
|
|
operation: 'supplier.create',
|
|
})
|
|
if (!result.success) return result.response
|
|
const body = result.data
|
|
|
|
const { data, error } = await supabase
|
|
.from('suppliers')
|
|
.insert({
|
|
user_id: user.id,
|
|
company_id: companyId,
|
|
name: body.name,
|
|
supplier_type: body.supplier_type,
|
|
email: body.email,
|
|
phone: body.phone,
|
|
address_line1: body.address_line1,
|
|
address_line2: body.address_line2,
|
|
postal_code: body.postal_code,
|
|
city: body.city,
|
|
country: body.country || 'SE',
|
|
org_number: body.org_number,
|
|
vat_number: body.vat_number,
|
|
bankgiro: body.bankgiro,
|
|
plusgiro: body.plusgiro,
|
|
bank_account: body.bank_account,
|
|
iban: body.iban,
|
|
bic: body.bic,
|
|
clearing_number: body.clearing_number,
|
|
account_number: body.account_number,
|
|
default_expense_account: body.default_expense_account,
|
|
default_payment_terms: body.default_payment_terms || 30,
|
|
default_currency: body.default_currency || 'SEK',
|
|
notes: body.notes,
|
|
})
|
|
.select()
|
|
.single()
|
|
|
|
if (error) {
|
|
if (error.code === '23505') {
|
|
return errorResponseFromCode('SUPPLIER_DUPLICATE_ORG_NUMBER', log, {
|
|
requestId,
|
|
details: { orgNumber: body.org_number },
|
|
})
|
|
}
|
|
log.error('supplier insert failed', error)
|
|
return errorResponseFromCode('SUPPLIER_CREATE_FAILED', log, {
|
|
requestId,
|
|
details: { reason: getUserErrorMessage(error) },
|
|
})
|
|
}
|
|
|
|
await eventBus.emit({
|
|
type: 'supplier.created',
|
|
payload: { supplier: data as Supplier, companyId, userId: user.id },
|
|
})
|
|
|
|
return NextResponse.json({ data })
|
|
},
|
|
{ requireWrite: true },
|
|
)
|