* fix(invoices): roll back the header row when a recurring-schedule item replace fails
PATCH /api/invoices/recurring/[id] and the update_recurring_schedule commit
executor wrote the schedule header first, then replaced the items. An item
insert failure restored the items snapshot but left the header update
committed, so a combined edit half-applied: a new day_of_month or
default_dimensions stayed while the line edit was undone.
Both write paths now go through one shared helper,
lib/invoices/apply-recurring-schedule-update.ts, which snapshots the header
before writing it (only for a combined edit, the only case with something to
undo) and compensates it on any items failure. The rollback update is filtered
on the updated_at stamp our own write produced, so a concurrent writer (the
hourly cron, a second edit) wins instead of being clobbered from a stale
snapshot: audit finding C2 in lib/invoices/voucher-matching.ts.
A compensation that itself fails is no longer swallowed. The helper reports
itemsRestored / headerRestored, logs the unrecoverable rows and the intended
restore payload, and both call sites then return the new
INVOICE_RECURRING_UPDATE_PARTIAL registry entry, which tells the user in
Swedish that the schedule may be half-saved and to check fields and items
before retrying. A clean rollback keeps the PG-mapped error so a CHECK
violation still surfaces its specific message.
Also in the rewritten block:
- the items DELETE error is checked, so a failed delete no longer proceeds to
an insert that would duplicate every line;
- the 404 existence check moved above every write, so a PATCH with items for a
missing or cross-tenant id writes nothing;
- the items snapshot uses select('*') with id/created_at stripped on restore
(same idiom as replaceInvoiceItems), so a column added later is carried
through instead of silently dropped;
- NewRecurringScheduleDialog unwraps the nested { error: { message } } envelope
the route returns, which otherwise reached the toast as "[object Object]".
The cron's no-empty-items invariant holds on every failure path: the items are
either untouched, restored, or the failure is reported explicitly.
Fixes #1275
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
* fix(invoices): never write when the compensating snapshot is unavailable
Follow-up on the recurring-schedule rollback: the helper still performed two
writes it already knew it could not compensate.
- The header snapshot read now checks its error and a missing row, and the
header UPDATE is skipped entirely when either holds, so no header change is
committed that we already know can never be rolled back.
- An unreadable item snapshot now aborts BEFORE the delete (rolling the header
back) instead of deleting first and reporting itemsRestored: false, so the
cron invariant "a schedule always has items" holds on every failure path.
- That header read now runs whenever items are replaced and is scoped by
company_id, so it doubles as the ownership proof the schedule_id-only item
delete/insert lacks (the commit executor runs with RLS off). Stated in the
JSDoc as well.
- The item snapshot is paginated via fetchAllRows: a schedule with more than
1000 lines could otherwise restore partially while reporting a clean
rollback.
- The executor now returns errorCode INVOICE_RECURRING_UPDATE_PARTIAL,
surfaced as CommitResult.code and persisted as result_data.error_code, so a
staged-op caller can detect the partial state without substring-matching the
Swedish sentence.
- Route: details keys are camelCase throughout, and an item failure is logged
once, with the repair context kept on the partial path only.
Tests: the unreadable-snapshot branches are exercised (including the
previously unused itemsSnapshotError harness hook), and the test that pinned
"header written with no possibility of rollback" now asserts that nothing is
written at all.
Fixes #1275
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
262 lines
9.2 KiB
TypeScript
262 lines
9.2 KiB
TypeScript
import { NextResponse } from 'next/server'
|
|
import { ensureInitialized } from '@/lib/init'
|
|
import { withRouteContext } from '@/lib/api/with-route-context'
|
|
import { errorResponse, errorResponseFromCode } from '@/lib/errors/get-structured-error'
|
|
import { UpdateRecurringScheduleSchema } from '@/lib/api/schemas'
|
|
import { applyRecurringScheduleUpdate } from '@/lib/invoices/apply-recurring-schedule-update'
|
|
import {
|
|
computeInitialRunDate,
|
|
computeNextRunDate,
|
|
getStockholmDateHour,
|
|
} from '@/lib/invoices/recurring-schedule-service'
|
|
|
|
ensureInitialized()
|
|
|
|
export const GET = withRouteContext(
|
|
'recurring_invoice.get',
|
|
async (_request, ctx, { params }: { params: Promise<{ id: string }> }) => {
|
|
const { id } = await params
|
|
const { supabase, companyId, log } = ctx
|
|
const { data, error } = await supabase
|
|
.from('recurring_invoice_schedules')
|
|
.select('*, customer:customers(*), items:recurring_invoice_schedule_items(*)')
|
|
.eq('id', id)
|
|
.eq('company_id', companyId)
|
|
.single()
|
|
|
|
if (error || !data) {
|
|
log.warn('recurring schedule not found', { scheduleId: id })
|
|
return NextResponse.json(
|
|
{ error: 'Schedule not found', type: 'not_found' },
|
|
{ status: 404 },
|
|
)
|
|
}
|
|
return NextResponse.json({ data })
|
|
},
|
|
)
|
|
|
|
export const PATCH = withRouteContext(
|
|
'recurring_invoice.update',
|
|
async (request, ctx, { params }: { params: Promise<{ id: string }> }) => {
|
|
const { id } = await params
|
|
const { supabase, companyId, log, requestId } = ctx
|
|
|
|
let rawBody: unknown
|
|
try {
|
|
rawBody = await request.json()
|
|
} catch {
|
|
return NextResponse.json(
|
|
{ error: 'Invalid JSON in request body', type: 'validation_error' },
|
|
{ status: 400 },
|
|
)
|
|
}
|
|
|
|
const parsed = UpdateRecurringScheduleSchema.safeParse(rawBody)
|
|
if (!parsed.success) {
|
|
return NextResponse.json(
|
|
{
|
|
error: 'Validation failed',
|
|
type: 'validation_error',
|
|
errors: parsed.error.issues.map((i) => ({
|
|
field: i.path.join('.'),
|
|
message: i.message,
|
|
code: i.code,
|
|
})),
|
|
},
|
|
{ status: 400 },
|
|
)
|
|
}
|
|
const input = parsed.data
|
|
const { items, ...scheduleFields } = input
|
|
|
|
// Only forward fields the user actually supplied.
|
|
const updateRow: Record<string, unknown> = {}
|
|
for (const [k, v] of Object.entries(scheduleFields)) {
|
|
if (v !== undefined) updateRow[k] = v
|
|
}
|
|
|
|
// Turning auto_send on (or moving the schedule to another customer while
|
|
// it is on) requires the customer to have an email address; otherwise
|
|
// every cron run degrades to a draft + warning. Mirrors the create
|
|
// route's guard.
|
|
if (input.auto_send === true || input.customer_id !== undefined) {
|
|
const { data: current } = await supabase
|
|
.from('recurring_invoice_schedules')
|
|
.select('auto_send, customer_id')
|
|
.eq('id', id)
|
|
.eq('company_id', companyId)
|
|
.single()
|
|
|
|
if (!current) {
|
|
return NextResponse.json(
|
|
{ error: 'Schedule not found', type: 'not_found' },
|
|
{ status: 404 },
|
|
)
|
|
}
|
|
|
|
const effectiveAutoSend = input.auto_send ?? current.auto_send
|
|
if (effectiveAutoSend) {
|
|
const { data: customer } = await supabase
|
|
.from('customers')
|
|
.select('email')
|
|
.eq('id', input.customer_id ?? current.customer_id)
|
|
.eq('company_id', companyId)
|
|
.maybeSingle()
|
|
|
|
if (!customer?.email) {
|
|
return NextResponse.json(
|
|
{
|
|
error: 'Customer has no email address: automatic sending requires one',
|
|
type: 'validation_error',
|
|
},
|
|
{ status: 400 },
|
|
)
|
|
}
|
|
}
|
|
}
|
|
|
|
// Recompute next_run_date when either the schedule is being reactivated
|
|
// (from a stale date) or its day-of-month actually changed via an edit.
|
|
if (input.status === 'active' || input.day_of_month !== undefined) {
|
|
const { data: existing } = await supabase
|
|
.from('recurring_invoice_schedules')
|
|
.select('next_run_date, day_of_month')
|
|
.eq('id', id)
|
|
.eq('company_id', companyId)
|
|
.single()
|
|
|
|
if (!existing) {
|
|
return NextResponse.json(
|
|
{ error: 'Schedule not found', type: 'not_found' },
|
|
{ status: 404 },
|
|
)
|
|
}
|
|
|
|
const reactivating = input.status === 'active'
|
|
const dayChanged =
|
|
input.day_of_month !== undefined && input.day_of_month !== existing.day_of_month
|
|
const effectiveDay = input.day_of_month ?? existing.day_of_month
|
|
const { date: todayStockholm } = getStockholmDateHour(new Date())
|
|
const stockholmToday = new Date(`${todayStockholm}T00:00:00Z`)
|
|
|
|
// Recompute to the next STRICTLY-future occurrence (never today, so an
|
|
// edit or reactivation can't trigger a same-hour surprise send; today's
|
|
// invoice is the explicit run-now action instead) when either:
|
|
// - reactivating a schedule whose date already passed (e.g. the safety
|
|
// pause when the send-hour cron shipped), or
|
|
// - the day-of-month changed, so "Nästa körning" follows the new day.
|
|
// Editing other fields (name, items, time) leaves next_run_date alone,
|
|
// so an unrelated edit never skips an imminent send.
|
|
const staleOnReactivate = reactivating && existing.next_run_date <= todayStockholm
|
|
if (staleOnReactivate || dayChanged) {
|
|
const rolled = computeInitialRunDate(stockholmToday, effectiveDay)
|
|
updateRow.next_run_date =
|
|
rolled === todayStockholm
|
|
? computeNextRunDate(stockholmToday, effectiveDay)
|
|
: rolled
|
|
}
|
|
|
|
// A conscious reactivation invalidates any lingering warning (the
|
|
// safety-pause note, or a stale failure from months ago).
|
|
if (reactivating) {
|
|
updateRow.last_run_warning = null
|
|
}
|
|
}
|
|
|
|
// Existence check BEFORE any write: with items in the payload the request
|
|
// writes two tables, so a 404 must not leave a header update behind.
|
|
if (items) {
|
|
const { data: existing } = await supabase
|
|
.from('recurring_invoice_schedules')
|
|
.select('id')
|
|
.eq('id', id)
|
|
.eq('company_id', companyId)
|
|
.single()
|
|
|
|
if (!existing) {
|
|
return NextResponse.json(
|
|
{ error: 'Schedule not found', type: 'not_found' },
|
|
{ status: 404 },
|
|
)
|
|
}
|
|
}
|
|
|
|
// Items are replaced wholesale. Cheaper than diffing for a small list and
|
|
// matches how the UI form sends the full list back on every save. Both
|
|
// writes are compensated on failure inside the shared helper.
|
|
const result = await applyRecurringScheduleUpdate(supabase, {
|
|
scheduleId: id,
|
|
companyId,
|
|
fields: updateRow,
|
|
items,
|
|
log,
|
|
})
|
|
|
|
if (!result.ok) {
|
|
if (result.stage === 'header') {
|
|
log.error('failed to update recurring schedule', result.error)
|
|
return errorResponse(result.error, log, { requestId })
|
|
}
|
|
if (!result.itemsRestored || !result.headerRestored) {
|
|
// A compensation did not apply, so the schedule may be half-saved: say
|
|
// so instead of reporting a clean failure. Logged here with the repair
|
|
// context (errorResponseFromCode only records the code itself), which
|
|
// the clean-rollback path below does not need.
|
|
log.error('recurring schedule update left a partial state', result.error, {
|
|
scheduleId: id,
|
|
stage: result.stage,
|
|
itemsRestored: result.itemsRestored,
|
|
headerRestored: result.headerRestored,
|
|
})
|
|
return errorResponseFromCode('INVOICE_RECURRING_UPDATE_PARTIAL', log, {
|
|
requestId,
|
|
// camelCase throughout, matching the pgCode key errorResponse itself
|
|
// merges into details for Postgres failures.
|
|
details: {
|
|
pgCode: result.error.code,
|
|
stage: result.stage,
|
|
itemsRestored: result.itemsRestored,
|
|
headerRestored: result.headerRestored,
|
|
},
|
|
})
|
|
}
|
|
// Clean rollback: keep the PG-mapped error so a CHECK violation still
|
|
// surfaces its specific Swedish message. errorResponse logs it, so no
|
|
// second log line here.
|
|
return errorResponse(result.error, log, { requestId })
|
|
}
|
|
|
|
const { data: complete } = await supabase
|
|
.from('recurring_invoice_schedules')
|
|
.select('*, customer:customers(*), items:recurring_invoice_schedule_items(*)')
|
|
.eq('id', id)
|
|
.eq('company_id', companyId)
|
|
.single()
|
|
|
|
return NextResponse.json({ data: complete })
|
|
},
|
|
{ requireWrite: true },
|
|
)
|
|
|
|
export const DELETE = withRouteContext(
|
|
'recurring_invoice.delete',
|
|
async (_request, ctx, { params }: { params: Promise<{ id: string }> }) => {
|
|
const { id } = await params
|
|
const { supabase, companyId, log, requestId } = ctx
|
|
|
|
// Items cascade-delete via FK ON DELETE CASCADE.
|
|
const { error } = await supabase
|
|
.from('recurring_invoice_schedules')
|
|
.delete()
|
|
.eq('id', id)
|
|
.eq('company_id', companyId)
|
|
|
|
if (error) {
|
|
log.error('failed to delete recurring schedule', error)
|
|
return errorResponse(error, log, { requestId })
|
|
}
|
|
return NextResponse.json({ success: true })
|
|
},
|
|
{ requireWrite: true },
|
|
)
|