fix(inbox): sync matched_transaction_id on MCP-staged document attach (#549)

* fix(inbox): sync matched_transaction_id on MCP-staged document attach

The REST attach route (app/api/transactions/[id]/attach-document) already
updates invoice_inbox_items.matched_transaction_id so the inbox list shows
the "Kopplad till transaktion" indicator. The MCP-staged path through
commitAttachDocumentToTransaction did not, leaving inbox rows looking
unprocessed after a /pending approval. Mirror the REST behaviour with a
best-effort UPDATE (idempotent, gated on both FK columns being NULL).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* fix(review): inspect supabase error on best-effort inbox link

The Supabase client resolves with { error } rather than rejecting on
RLS/DB failures, so the previous try/catch wrapping the inbox UPDATE
never fired for the documented failure modes — the console.error was
dead code. Switch to destructured { error } + if-block, matching the
pattern used elsewhere in commit.ts.

Applied to both the new MCP-staged path (lib/pending-operations/commit.ts)
and the pre-existing REST route (app/api/transactions/[id]/attach-document/
route.ts) it was mirrored from. Both tests now assert console.error fires
with the expected payload, so the swallow-and-log path is exercised for
real instead of passing for the wrong reason.

Addresses Greptile P1 + P2 on PR #549.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Jakob Wennberg
2026-05-21 14:00:49 +02:00
committed by GitHub
co-authored by Claude Opus 4.7
parent 39204cc0de
commit 2879f6ed17
4 changed files with 91 additions and 10 deletions
+19
View File
@@ -1059,6 +1059,25 @@ async function commitAttachDocumentToTransaction(
}
if (!postUpdate) return { error: 'Transaction not found', status: 404 }
// If the attached doc came from an invoice_inbox_items row, mark that row
// as matched so the inbox UI shows "Kopplad till transaktion". Best-effort:
// a failure must not roll back the (compliant) attach. Mirrors the REST
// route in app/api/transactions/[id]/attach-document/route.ts so MCP-staged
// and REST attaches converge on the same inbox state.
//
// The Supabase client resolves with { error } rather than rejecting on
// RLS/DB errors, so we destructure rather than try/catch.
const { error: inboxLinkErr } = await supabase
.from('invoice_inbox_items')
.update({ matched_transaction_id: txId })
.eq('document_id', documentId)
.eq('company_id', companyId)
.is('matched_transaction_id', null)
.is('created_supplier_invoice_id', null)
if (inboxLinkErr) {
console.error('[commitAttach] Failed to link inbox item:', inboxLinkErr)
}
const journalEntryId = postUpdate.journal_entry_id as string | null
if (journalEntryId) {
const { error: linkErr } = await supabase