--
-- This file is part of TALER
-- Copyright (C) 2026 Taler Systems SA
--
-- TALER is free software; you can redistribute it and/or modify it under the
-- terms of the GNU General Public License as published by the Free Software
-- Foundation; either version 3, or (at your option) any later version.
--
-- TALER is distributed in the hope that it will be useful, but WITHOUT ANY
-- WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-- A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License along with
-- TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
--

-- Everything in one big transaction
BEGIN;

SELECT _v.register_patch('auditor-triggers-0002');

-- The AML helper listens to this trigger.  The channel is the one
-- GNUNET_PQ_get_event_notify_channel() derives from a
-- TALER_DBEVENT_EXCHANGE_AUDITOR_WAKE_HELPER_AML (1236) event header.
CREATE OR REPLACE FUNCTION auditor_wake_aml_helper_trigger()
    RETURNS trigger
    LANGUAGE plpgsql
AS $$
BEGIN
    NOTIFY XYRBWY61ENX0AQVQ2M8X2MSZMTADQPK18VM1TAW7VWFFHD85CGXNG;
RETURN NEW;
END $$;
COMMENT ON FUNCTION auditor_wake_aml_helper_trigger()
    IS 'Call auditor_call_db_notify on new entry';


-- A status change of an AML officer; the helper checks the offline master
-- key's signature on it.  aml_staff is append-only, so every change is an
-- INSERT and this fires for all of them.
CREATE OR REPLACE TRIGGER auditor_exchange_notify_helper_aml0
    AFTER INSERT ON exchange.aml_staff
EXECUTE FUNCTION auditor_wake_aml_helper_trigger();


-- An AML decision; the helper checks the officer's signature on it and
-- that the officer was eligible to make it.
CREATE OR REPLACE TRIGGER auditor_exchange_notify_helper_aml1
    AFTER INSERT ON exchange.aml_history
EXECUTE FUNCTION auditor_wake_aml_helper_trigger();

COMMIT;
