commit 75c8023ca740511425fc34b5b75bd1f91b1df22e
parent 7a672056dc8a7aa094b394e20275eaeed39d0974
Author: bohdan-potuzhnyi <bohdan.potuzhnyi@gmail.com>
Date: Thu, 21 Nov 2024 09:38:34 +0100
adding webhook for the categories
Diffstat:
1 file changed, 113 insertions(+), 0 deletions(-)
diff --git a/src/backenddb/merchant-0013.sql b/src/backenddb/merchant-0013.sql
@@ -0,0 +1,113 @@
+--
+-- This file is part of TALER
+-- Copyright (C) 2024 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/>
+--
+
+-- @file merchant-0013.sql
+-- @brief Creating trigger for the category change webhook
+-- @author Bohdan Potuzhnyi
+-- @author Vlada Svirsh
+
+
+BEGIN;
+
+-- Check patch versioning is in place.
+SELECT _v.register_patch('merchant-0013', NULL, NULL);
+
+SET search_path TO merchant;
+
+-- Trigger function to handle pending webhooks for category changes
+CREATE OR REPLACE FUNCTION handle_category_changes()
+RETURNS TRIGGER AS $$
+DECLARE
+ my_merchant_serial BIGINT;
+BEGIN
+ -- Fetch the merchant_serial directly from the NEW or OLD row
+ my_merchant_serial := COALESCE(OLD.merchant_serial, NEW.merchant_serial);
+
+ -- Check if there are webhooks listening for this event
+ PERFORM 1
+ FROM merchant_webhook mw
+ WHERE mw.event_type IN ('category_added', 'category_updated', 'category_deleted')
+ AND mw.merchant_serial = my_merchant_serial;
+
+ IF FOUND THEN
+ -- INSERT case: Add a webhook for category addition
+ IF TG_OP = 'INSERT' THEN
+ INSERT INTO merchant_pending_webhooks
+ (merchant_serial, webhook_serial, url, http_method, body)
+ SELECT mw.merchant_serial,
+ mw.webhook_serial,
+ mw.url,
+ mw.http_method,
+ json_build_object(
+ 'webhook_type', 'category_added',
+ 'category_serial', NEW.category_serial,
+ 'category_name', NEW.category_name
+ )::TEXT
+ FROM merchant_webhook mw
+ WHERE mw.event_type = 'category_added'
+ AND mw.merchant_serial = my_merchant_serial;
+ END IF;
+
+ -- UPDATE case: Add a webhook for category update
+ IF TG_OP = 'UPDATE' THEN
+ INSERT INTO merchant_pending_webhooks
+ (merchant_serial, webhook_serial, url, http_method, body)
+ SELECT mw.merchant_serial,
+ mw.webhook_serial,
+ mw.url,
+ mw.http_method,
+ json_build_object(
+ 'webhook_type', 'category_updated',
+ 'category_serial', NEW.category_serial,
+ 'old_category_name', OLD.category_name,
+ 'new_category_name', NEW.category_name
+ )::TEXT
+ FROM merchant_webhook mw
+ WHERE mw.event_type = 'category_updated'
+ AND mw.merchant_serial = my_merchant_serial;
+ END IF;
+
+ -- DELETE case: Add a webhook for category deletion
+ IF TG_OP = 'DELETE' THEN
+ INSERT INTO merchant_pending_webhooks
+ (merchant_serial, webhook_serial, url, http_method, body)
+ SELECT mw.merchant_serial,
+ mw.webhook_serial,
+ mw.url,
+ mw.http_method,
+ json_build_object(
+ 'webhook_type', 'category_deleted',
+ 'category_serial', OLD.category_serial,
+ 'category_name', OLD.category_name
+ )::TEXT
+ FROM merchant_webhook mw
+ WHERE mw.event_type = 'category_deleted'
+ AND mw.merchant_serial = my_merchant_serial;
+ END IF;
+ END IF;
+
+ RETURN NULL; -- Triggers that fire AFTER must return NULL
+END;
+$$ LANGUAGE plpgsql;
+
+-- Trigger to invoke the trigger function
+CREATE TRIGGER trigger_category_changes
+AFTER INSERT OR UPDATE OR DELETE
+ON merchant_categories
+FOR EACH ROW
+EXECUTE FUNCTION handle_category_changes();
+
+COMMIT;