reconciliations_staging_update_v1
One function to update staging flags (booked | archived | flagged) and/or verified status with the proper cascade to related tables. Returns a JSON you can pass straight back to clients.
What it does
-
Toggle one of
booked | archived | flaggedonpublic.reconciliations_staging. -
Set/unset verified:
-
Updates
reconciliations_staging.verified -
Updates
reconciliations.verified -
Cascades to:
-
documents_v2.reconciled+documents_v2.reconciliation_id -
transactions.reconciled+transactions.reconciliation_id
-
-
-
Prevents verifying
trueif there are no linkeddocument_idandtransaction_id.
Signature
SELECT public.reconciliations_staging_update_v1(
p_reconciliation_id uuid,
p_column text DEFAULT NULL, -- 'booked' | 'archived' | 'flagged'
p_column_value boolean DEFAULT NULL,
p_verified boolean DEFAULT NULL,
p_company_id uuid DEFAULT NULL, -- optional passthrough (unused)
p_organization_id uuid DEFAULT NULL, -- optional passthrough (unused)
p_user_id uuid DEFAULT NULL, -- optional passthrough (unused)
p_publish_event boolean DEFAULT true -- optional passthrough (unused)
) RETURNS jsonb;
Quick examples
1) Toggle a staging flag
-- Set 'flagged' = true on staging
select public.reconciliations_staging_update_v1(
'00000000-0000-0000-0000-000000000000',
'flagged',
true,
null
);
Response
{
"ok": true,
"reconciliation_id": "00000000-0000-0000-0000-000000000000",
"column": "flagged",
"value": true
}
2) Verify with cascade
-- Set verified = true and cascade reconciled/reconciliation_id to linked rows
select public.reconciliations_staging_update_v1(
'00000000-0000-0000-0000-000000000000',
null,
null,
true
);
Response
{
"ok": true,
"verified": true,
"affected": {
"documents_v2": true,
"transactions": true
}
}
3) Do both in one call
-- Flag and verify together
select public.reconciliations_staging_update_v1(
'00000000-0000-0000-0000-000000000000',
'booked',
true,
true
);
Behavior details
-
Column update: only one of
booked | archived | flaggedper call. -
Verify:
-
truerequires at least one ofreconciliations.document_idorreconciliations.transaction_id. -
Cascades:
-
documents_v2:reconciled = p_verified,reconciliation_id = (p_verified ? id : null) -
transactions: same as above
-
-
Writes
verifiedto bothreconciliations_stagingandreconciliations.
-
Errors
-
{"ok": false, "error": "not_found"}— reconciliation row doesn’t exist. -
{"ok": false, "error": "invalid_column", "allowed": ["booked","archived","flagged"]}— badp_column. -
{"ok": false, "error": "missing_value"}—p_columngiven withoutp_column_value. -
{"ok": false, "error": "no_linked_records"}— verifyingtruebut no linked doc/tx. -
{"ok": false, "error": "no_op", "message": "nothing to update"}— neitherp_columnnorp_verifiedprovided. -
{"ok": false, "error": "internal", "message": "..."}— any other failure (includes staging row missing).
Notes
-
p_company_id,p_organization_id,p_user_id,p_publish_eventare present for parity/auditing; they’re not used internally. -
Designed to mirror existing REST PATCH behavior and JSON shapes.