[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[taler-merchant] 05/14: adding inventory realted webhooks adding
From: |
gnunet |
Subject: |
[taler-merchant] 05/14: adding inventory realted webhooks adding |
Date: |
Sun, 08 Dec 2024 14:18:14 +0100 |
This is an automated email from the git hooks/post-receive script.
grothoff pushed a commit to branch master
in repository merchant.
commit b4a24cbc1e54d035572eb7ff910d361d7b638c07
Author: bohdan-potuzhnyi <bohdan.potuzhnyi@gmail.com>
AuthorDate: Thu Nov 21 19:00:37 2024 +0100
adding inventory realted webhooks adding
---
src/backenddb/merchant-0013.sql | 155 ++++++++++++++++++++++++++++++++++++++++
1 file changed, 155 insertions(+)
diff --git a/src/backenddb/merchant-0013.sql b/src/backenddb/merchant-0013.sql
index 1c21e3c5..719849bd 100644
--- a/src/backenddb/merchant-0013.sql
+++ b/src/backenddb/merchant-0013.sql
@@ -171,4 +171,159 @@ ON merchant_categories
FOR EACH ROW
EXECUTE FUNCTION handle_category_changes();
+-- Function to handle inventory changes and notify webhooks
+CREATE OR REPLACE FUNCTION handle_inventory_changes()
+ RETURNS TRIGGER AS $$
+DECLARE
+ my_merchant_serial BIGINT;
+ resolved_body TEXT;
+ webhook RECORD; -- To iterate over all matching webhooks
+BEGIN
+ -- Fetch the merchant_serial directly from the NEW or OLD row
+ my_merchant_serial := COALESCE(OLD.merchant_serial, NEW.merchant_serial);
+
+ -- INSERT case: Notify webhooks for inventory addition
+ IF TG_OP = 'INSERT' THEN
+ FOR webhook IN
+ SELECT * FROM merchant_webhook
+ WHERE event_type = 'inventory_added'
+ AND merchant_serial = my_merchant_serial
+ LOOP
+ -- Resolve placeholders for the current webhook
+ resolved_body := webhook.body_template;
+ resolved_body := replace_placeholder(resolved_body, 'webhook_type',
'inventory_added');
+ resolved_body := replace_placeholder(resolved_body, 'product_serial',
NEW.product_serial::TEXT);
+ resolved_body := replace_placeholder(resolved_body, 'product_id',
NEW.product_id);
+ resolved_body := replace_placeholder(resolved_body, 'description',
NEW.description);
+ resolved_body := replace_placeholder(resolved_body,
'description_i18n', encode(NEW.description_i18n, 'escape'));
+ resolved_body := replace_placeholder(resolved_body, 'unit', NEW.unit);
+ resolved_body := replace_placeholder(resolved_body, 'image',
encode(NEW.image, 'escape'));
+ resolved_body := replace_placeholder(resolved_body, 'taxes',
encode(NEW.taxes, 'escape'));
+ resolved_body := replace_placeholder(resolved_body, 'price',
NEW.price::TEXT);
+ resolved_body := replace_placeholder(resolved_body, 'total_stock',
NEW.total_stock::TEXT);
+ resolved_body := replace_placeholder(resolved_body, 'total_sold',
NEW.total_sold::TEXT);
+ resolved_body := replace_placeholder(resolved_body, 'total_lost',
NEW.total_lost::TEXT);
+ resolved_body := replace_placeholder(resolved_body, 'address',
encode(NEW.address, 'escape'));
+ resolved_body := replace_placeholder(resolved_body, 'next_restock',
NEW.next_restock::TEXT);
+ resolved_body := replace_placeholder(resolved_body, 'minimum_age',
NEW.minimum_age::TEXT);
+
+ -- Insert into pending webhooks for this webhook
+ INSERT INTO merchant_pending_webhooks
+ (merchant_serial, webhook_serial, url, http_method, body)
+ VALUES
+ (webhook.merchant_serial,
+ webhook.webhook_serial,
+ webhook.url,
+ webhook.http_method,
+ resolved_body);
+ END LOOP;
+
+ -- Notify the webhook service
+ NOTIFY XXJWF6C1DCS1255RJH7GQ1EK16J8DMRSQ6K9EDKNKCP7HRVWAJPKG;
+ END IF;
+
+ -- UPDATE case: Notify webhooks for inventory update
+ IF TG_OP = 'UPDATE' THEN
+ FOR webhook IN
+ SELECT * FROM merchant_webhook
+ WHERE event_type = 'inventory_updated'
+ AND merchant_serial = my_merchant_serial
+ LOOP
+ -- Resolve placeholders for the current webhook
+ resolved_body := webhook.body_template;
+ resolved_body := replace_placeholder(resolved_body, 'webhook_type',
'inventory_updated');
+ resolved_body := replace_placeholder(resolved_body, 'product_serial',
NEW.product_serial::TEXT);
+ resolved_body := replace_placeholder(resolved_body, 'product_id',
NEW.product_id);
+ resolved_body := replace_placeholder(resolved_body, 'old_description',
OLD.description);
+ resolved_body := replace_placeholder(resolved_body, 'description',
NEW.description);
+ resolved_body := replace_placeholder(resolved_body,
'old_description_i18n', encode(OLD.description_i18n, 'escape'));
+ resolved_body := replace_placeholder(resolved_body,
'description_i18n', encode(NEW.description_i18n, 'escape'));
+ resolved_body := replace_placeholder(resolved_body, 'old_unit',
OLD.unit);
+ resolved_body := replace_placeholder(resolved_body, 'unit', NEW.unit);
+ resolved_body := replace_placeholder(resolved_body, 'old_image',
encode(OLD.image, 'escape'));
+ resolved_body := replace_placeholder(resolved_body, 'image',
encode(NEW.image, 'escape'));
+ resolved_body := replace_placeholder(resolved_body, 'old_taxes',
encode(OLD.taxes, 'escape'));
+ resolved_body := replace_placeholder(resolved_body, 'taxes',
encode(NEW.taxes, 'escape'));
+ resolved_body := replace_placeholder(resolved_body, 'old_price',
OLD.price::TEXT);
+ resolved_body := replace_placeholder(resolved_body, 'price',
NEW.price::TEXT);
+ resolved_body := replace_placeholder(resolved_body, 'old_total_stock',
OLD.total_stock::TEXT);
+ resolved_body := replace_placeholder(resolved_body, 'total_stock',
NEW.total_stock::TEXT);
+ resolved_body := replace_placeholder(resolved_body, 'old_total_sold',
OLD.total_sold::TEXT);
+ resolved_body := replace_placeholder(resolved_body, 'total_sold',
NEW.total_sold::TEXT);
+ resolved_body := replace_placeholder(resolved_body, 'old_total_lost',
OLD.total_lost::TEXT);
+ resolved_body := replace_placeholder(resolved_body, 'total_lost',
NEW.total_lost::TEXT);
+ resolved_body := replace_placeholder(resolved_body, 'old_address',
encode(OLD.address, 'escape'));
+ resolved_body := replace_placeholder(resolved_body, 'address',
encode(NEW.address, 'escape'));
+ resolved_body := replace_placeholder(resolved_body,
'old_next_restock', OLD.next_restock::TEXT);
+ resolved_body := replace_placeholder(resolved_body, 'next_restock',
NEW.next_restock::TEXT);
+ resolved_body := replace_placeholder(resolved_body, 'old_minimum_age',
OLD.minimum_age::TEXT);
+ resolved_body := replace_placeholder(resolved_body, 'minimum_age',
NEW.minimum_age::TEXT);
+
+ -- Insert into pending webhooks for this webhook
+ INSERT INTO merchant_pending_webhooks
+ (merchant_serial, webhook_serial, url, http_method, body)
+ VALUES
+ (webhook.merchant_serial,
+ webhook.webhook_serial,
+ webhook.url,
+ webhook.http_method,
+ resolved_body);
+ END LOOP;
+
+ -- Notify the webhook service
+ NOTIFY XXJWF6C1DCS1255RJH7GQ1EK16J8DMRSQ6K9EDKNKCP7HRVWAJPKG;
+ END IF;
+
+ -- DELETE case: Notify webhooks for inventory deletion
+ IF TG_OP = 'DELETE' THEN
+ FOR webhook IN
+ SELECT * FROM merchant_webhook
+ WHERE event_type = 'inventory_deleted'
+ AND merchant_serial = my_merchant_serial
+ LOOP
+ -- Resolve placeholders for the current webhook
+ resolved_body := webhook.body_template;
+ resolved_body := replace_placeholder(resolved_body, 'webhook_type',
'inventory_deleted');
+ resolved_body := replace_placeholder(resolved_body, 'product_serial',
OLD.product_serial::TEXT);
+ resolved_body := replace_placeholder(resolved_body, 'product_id',
OLD.product_id);
+ resolved_body := replace_placeholder(resolved_body, 'description',
OLD.description);
+ resolved_body := replace_placeholder(resolved_body,
'description_i18n', encode(OLD.description_i18n, 'escape'));
+ resolved_body := replace_placeholder(resolved_body, 'unit', OLD.unit);
+ resolved_body := replace_placeholder(resolved_body, 'image',
encode(OLD.image, 'escape'));
+ resolved_body := replace_placeholder(resolved_body, 'taxes',
encode(OLD.taxes, 'escape'));
+ resolved_body := replace_placeholder(resolved_body, 'price',
OLD.price::TEXT);
+ resolved_body := replace_placeholder(resolved_body, 'total_stock',
OLD.total_stock::TEXT);
+ resolved_body := replace_placeholder(resolved_body, 'total_sold',
OLD.total_sold::TEXT);
+ resolved_body := replace_placeholder(resolved_body, 'total_lost',
OLD.total_lost::TEXT);
+ resolved_body := replace_placeholder(resolved_body, 'address',
encode(OLD.address, 'escape'));
+ resolved_body := replace_placeholder(resolved_body, 'next_restock',
OLD.next_restock::TEXT);
+ resolved_body := replace_placeholder(resolved_body, 'minimum_age',
OLD.minimum_age::TEXT);
+
+ -- Insert into pending webhooks for this webhook
+ INSERT INTO merchant_pending_webhooks
+ (merchant_serial, webhook_serial, url, http_method, body)
+ VALUES
+ (webhook.merchant_serial,
+ webhook.webhook_serial,
+ webhook.url,
+ webhook.http_method,
+ resolved_body);
+ END LOOP;
+
+ -- Notify the webhook service
+ NOTIFY XXJWF6C1DCS1255RJH7GQ1EK16J8DMRSQ6K9EDKNKCP7HRVWAJPKG;
+ END IF;
+
+ RETURN NULL;
+END;
+$$ LANGUAGE plpgsql;
+
+-- Trigger to invoke the trigger function
+CREATE TRIGGER trigger_inventory_changes
+ AFTER INSERT OR UPDATE OR DELETE
+ ON merchant_inventory
+ FOR EACH ROW
+EXECUTE FUNCTION handle_inventory_changes();
+
+
COMMIT;
--
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.
- [taler-merchant] branch master updated (9438e749 -> a39cc48c), gnunet, 2024/12/08
- [taler-merchant] 03/14: small update of the categories webhook calls, gnunet, 2024/12/08
- [taler-merchant] 04/14: update of the templating, gnunet, 2024/12/08
- [taler-merchant] 02/14: adding webhook for the categories, gnunet, 2024/12/08
- [taler-merchant] 01/14: adding sql calls for the order_settled webhook, gnunet, 2024/12/08
- [taler-merchant] 06/14: few updates of the webhook trigger, gnunet, 2024/12/08
- [taler-merchant] 08/14: adding webhook for the categories, gnunet, 2024/12/08
- [taler-merchant] 05/14: adding inventory realted webhooks adding,
gnunet <=
- [taler-merchant] 07/14: small update of the styling, gnunet, 2024/12/08
- [taler-merchant] 10/14: tests added, gnunet, 2024/12/08
- [taler-merchant] 11/14: few design fixes, gnunet, 2024/12/08
- [taler-merchant] 12/14: small fix, gnunet, 2024/12/08
- [taler-merchant] 14/14: add check to test for openssl, gnunet, 2024/12/08
- [taler-merchant] 09/14: start test, gnunet, 2024/12/08
- [taler-merchant] 13/14: -fix logging, gnunet, 2024/12/08