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>
149 lines
5.2 KiB
TypeScript
149 lines
5.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 { CreateCustomerSchema } from '@/lib/api/schemas'
|
|
import { validateVatNumber } from '@/lib/vat/vies-client'
|
|
import { withRouteContext } from '@/lib/api/with-route-context'
|
|
import { errorResponse, errorResponseFromCode } from '@/lib/errors/get-structured-error'
|
|
import type { Customer } from '@/types'
|
|
import { encryptCustomerPersonalNumber, maskCustomerRow } from '@/lib/customers/protect-personal-number'
|
|
import { fetchAllRows } from '@/lib/supabase/fetch-all'
|
|
import { resolveDefaultPaymentTerms } from '@/lib/customers/default-payment-terms'
|
|
import { getErrorMessage as getUserErrorMessage } from '@/lib/errors/get-error-message'
|
|
|
|
ensureInitialized()
|
|
|
|
export const GET = withRouteContext(
|
|
'customer.list',
|
|
async (_request, ctx) => {
|
|
const { supabase, companyId, log, requestId } = ctx
|
|
|
|
// Paginated: PostgREST caps an unranged select at 1000 rows, which would
|
|
// hand the roster page a silently truncated customer list. Ordered on the
|
|
// PK because paging is only stable under a unique total order; the
|
|
// name sort callers expect is re-applied below.
|
|
//
|
|
// Archived rows (soft-deleted via the v1 API) stay in the table for BFL
|
|
// retention but are not part of the roster: same canonical
|
|
// `archived_at IS NULL` filter as the v1 list route.
|
|
let rows: Customer[]
|
|
try {
|
|
rows = await fetchAllRows<Customer>(
|
|
({ from, to }) =>
|
|
supabase
|
|
.from('customers')
|
|
.select('*')
|
|
.eq('company_id', companyId)
|
|
.is('archived_at', null)
|
|
.order('id', { ascending: true })
|
|
.range(from, to),
|
|
{ dedupeBy: (row) => row.id },
|
|
)
|
|
} catch (error) {
|
|
log.error('customer list failed', error as Error)
|
|
return errorResponse(error, log, { requestId })
|
|
}
|
|
|
|
rows.sort((a, b) => (a.name ?? '').localeCompare(b.name ?? '', 'sv'))
|
|
|
|
return NextResponse.json({ data: rows.map(maskCustomerRow) })
|
|
},
|
|
)
|
|
|
|
export const POST = withRouteContext(
|
|
'customer.create',
|
|
async (request, ctx) => {
|
|
const { user, supabase, companyId, log, requestId } = ctx
|
|
|
|
const result = await validateBody(request, CreateCustomerSchema, {
|
|
log,
|
|
operation: 'customer.create',
|
|
})
|
|
if (!result.success) return result.response
|
|
const body = result.data
|
|
|
|
// Unset payment terms follow the company's own default, not a hardcoded 30.
|
|
const defaultPaymentTerms = await resolveDefaultPaymentTerms(
|
|
supabase,
|
|
companyId!,
|
|
body.default_payment_terms,
|
|
)
|
|
|
|
const { data, error } = await supabase
|
|
.from('customers')
|
|
.insert({
|
|
user_id: user.id,
|
|
company_id: companyId,
|
|
name: body.name,
|
|
customer_type: body.customer_type,
|
|
customer_number: body.customer_number || null,
|
|
contact_person: body.contact_person ?? null,
|
|
email: body.email,
|
|
phone: body.phone,
|
|
invoice_email_cc_addresses: body.invoice_email_cc_addresses ?? null,
|
|
invoice_email_bcc_addresses: body.invoice_email_bcc_addresses ?? null,
|
|
address_line1: body.address_line1,
|
|
address_line2: body.address_line2,
|
|
postal_code: body.postal_code,
|
|
city: body.city,
|
|
country: body.country || 'Sweden',
|
|
org_number: body.org_number,
|
|
vat_number: body.vat_number,
|
|
personal_number: encryptCustomerPersonalNumber(body.personal_number),
|
|
language: body.language || 'sv',
|
|
default_payment_terms: defaultPaymentTerms,
|
|
notes: body.notes,
|
|
})
|
|
.select()
|
|
.single()
|
|
|
|
if (error) {
|
|
if (error.code === '23505') {
|
|
return errorResponseFromCode('CUSTOMER_DUPLICATE_ORG_NUMBER', log, {
|
|
requestId,
|
|
details: { orgNumber: body.org_number },
|
|
})
|
|
}
|
|
log.error('customer insert failed', error)
|
|
return errorResponseFromCode('CUSTOMER_CREATE_FAILED', log, {
|
|
requestId,
|
|
details: { reason: getUserErrorMessage(error) },
|
|
})
|
|
}
|
|
|
|
// Auto-validate VAT number for EU business customers (non-blocking).
|
|
if (body.customer_type === 'eu_business' && body.vat_number) {
|
|
try {
|
|
const vatResult = await validateVatNumber(body.vat_number)
|
|
if (vatResult.valid) {
|
|
await supabase
|
|
.from('customers')
|
|
.update({
|
|
vat_number_validated: true,
|
|
vat_number_validated_at: new Date().toISOString(),
|
|
})
|
|
.eq('id', data.id)
|
|
.eq('company_id', companyId)
|
|
|
|
data.vat_number_validated = true
|
|
data.vat_number_validated_at = new Date().toISOString()
|
|
}
|
|
} catch (err) {
|
|
log.warn('auto-VIES validation failed on customer create', err as Error, {
|
|
customerId: data.id,
|
|
})
|
|
}
|
|
}
|
|
|
|
const safeCustomer = maskCustomerRow(data)
|
|
await eventBus.emit({
|
|
type: 'customer.created',
|
|
payload: { customer: safeCustomer as Customer, companyId: companyId!, userId: user.id },
|
|
})
|
|
|
|
return NextResponse.json({ data: safeCustomer })
|
|
},
|
|
{ requireWrite: true },
|
|
)
|