Files
accounted/supabase/migrations/20260511120000_bank_connections_pending_selection.sql
Mattsson a53a119a2e Fix/vat parent accounts (#438)
* feat(enable-banking): add support for account selection and syncing

- Updated StoredAccount interface to include an 'enabled' flag for account syncing preferences.
- Enhanced ensureFiscalPeriod function to handle overlapping fiscal periods with posted entries and opening balances.
- Added tests for fiscal period validation and account syncing logic.
- Implemented AccountPickerDialog component for user account selection.
- Created API routes for PATCH /accounts and POST /sync to manage account syncing.
- Introduced 'pending_selection' status for bank connections to allow user account selection before syncing.
- Updated database migration to support new connection status and backfill existing accounts with enabled=true.

* feat(enable-banking): implement account selection and consent event logging

---------

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-11 17:12:00 +02:00

32 lines
1.1 KiB
SQL

-- Add 'pending_selection' to bank_connections.status CHECK constraint.
--
-- New PSD2 connections start in 'pending_selection' so the user can pick
-- which accounts to actually sync before any transactions are pulled.
-- Once the user confirms their selection the connection flips to 'active'.
ALTER TABLE public.bank_connections
DROP CONSTRAINT IF EXISTS bank_connections_status_check;
ALTER TABLE public.bank_connections
ADD CONSTRAINT bank_connections_status_check
CHECK (status IN ('pending', 'pending_selection', 'active', 'expired', 'error', 'revoked'));
-- Backfill existing rows: every account gets enabled=true so current
-- connections keep syncing exactly the accounts they were syncing before.
-- Users can later prune accounts via the picker dialog.
UPDATE public.bank_connections
SET accounts_data = (
SELECT jsonb_agg(
CASE
WHEN elem ? 'enabled' THEN elem
ELSE elem || jsonb_build_object('enabled', true)
END
)
FROM jsonb_array_elements(accounts_data) AS elem
)
WHERE accounts_data IS NOT NULL
AND jsonb_typeof(accounts_data) = 'array'
AND jsonb_array_length(accounts_data) > 0;
NOTIFY pgrst, 'reload schema';