From adfb256e87adda5336f65e862cf3182ac000a829 Mon Sep 17 00:00:00 2001 From: Ahmed-Ghanam Date: Fri, 29 Nov 2024 10:55:49 +0100 Subject: [PATCH] Save and retrieve the customized subject and body to and from the database --- .../getemailsstatusnewupdatestatus.sql | 64 +++++++++++------ .../getsmsstatusnewupdatestatus.sql | 20 ++++-- .../insertemailnotification.sql | 66 ++++++++++-------- .../insertsmsnotification.sql | 69 ++++++++++--------- 4 files changed, 134 insertions(+), 85 deletions(-) diff --git a/src/Altinn.Notifications.Persistence/Migration/FunctionsAndProcedures/getemailsstatusnewupdatestatus.sql b/src/Altinn.Notifications.Persistence/Migration/FunctionsAndProcedures/getemailsstatusnewupdatestatus.sql index 93856b6e..fcd56eb6 100644 --- a/src/Altinn.Notifications.Persistence/Migration/FunctionsAndProcedures/getemailsstatusnewupdatestatus.sql +++ b/src/Altinn.Notifications.Persistence/Migration/FunctionsAndProcedures/getemailsstatusnewupdatestatus.sql @@ -5,24 +5,48 @@ AS $BODY$ DECLARE latest_email_timeout TIMESTAMP WITH TIME ZONE; BEGIN - SELECT emaillimittimeout INTO latest_email_timeout FROM notifications.resourcelimitlog WHERE id = (SELECT MAX(id) FROM notifications.resourcelimitlog); - IF latest_email_timeout IS NOT NULL THEN - IF latest_email_timeout >= NOW() THEN - RETURN QUERY SELECT NULL::uuid AS alternateid, NULL::text AS subject, NULL::text AS body, NULL::text AS fromaddress, NULL::text AS toaddress, NULL::text AS contenttype WHERE FALSE; - RETURN; - ELSE - UPDATE notifications.resourcelimitlog SET emaillimittimeout = NULL WHERE id = (SELECT MAX(id) FROM notifications.resourcelimitlog); - END IF; - END IF; - - RETURN query - WITH updated AS ( - UPDATE notifications.emailnotifications - SET result = 'Sending', resulttime = now() - WHERE result = 'New' - RETURNING notifications.emailnotifications.alternateid, _orderid, notifications.emailnotifications.toaddress) - SELECT u.alternateid, et.subject, et.body, et.fromaddress, u.toaddress, et.contenttype - FROM updated u, notifications.emailtexts et - WHERE u._orderid = et._orderid; + SELECT emaillimittimeout + INTO latest_email_timeout + FROM notifications.resourcelimitlog + WHERE id = (SELECT MAX(id) FROM notifications.resourcelimitlog); + + -- Check if the latest email timeout is set and valid + IF latest_email_timeout IS NOT NULL THEN + IF latest_email_timeout >= NOW() THEN + RETURN QUERY + SELECT NULL::uuid AS alternateid, + NULL::text AS subject, + NULL::text AS body, + NULL::text AS fromaddress, + NULL::text AS toaddress, + NULL::text AS contenttype + WHERE FALSE; + RETURN; + ELSE + UPDATE notifications.resourcelimitlog + SET emaillimittimeout = NULL + WHERE id = (SELECT MAX(id) FROM notifications.resourcelimitlog); + END IF; + END IF; + + RETURN QUERY + WITH updated AS ( + UPDATE notifications.emailnotifications + SET result = 'Sending', resulttime = now() + WHERE result = 'New' + RETURNING notifications.emailnotifications.alternateid, + _orderid, + notifications.emailnotifications.toaddress, + notifications.emailnotifications.customizedsubject, + notifications.emailnotifications.customizedbody + ) + SELECT u.alternateid, + CASE WHEN u.customizedsubject IS NOT NULL AND u.customizedsubject <> '' THEN u.customizedsubject ELSE et.subject END AS subject, + CASE WHEN u.customizedbody IS NOT NULL AND u.customizedbody <> '' THEN u.customizedbody ELSE et.body END AS body, + et.fromaddress, + u.toaddress, + et.contenttype + FROM updated u + JOIN notifications.emailtexts et ON u._orderid = et._orderid; END; -$BODY$; \ No newline at end of file +$BODY$; diff --git a/src/Altinn.Notifications.Persistence/Migration/FunctionsAndProcedures/getsmsstatusnewupdatestatus.sql b/src/Altinn.Notifications.Persistence/Migration/FunctionsAndProcedures/getsmsstatusnewupdatestatus.sql index 91dafe24..cc6709af 100644 --- a/src/Altinn.Notifications.Persistence/Migration/FunctionsAndProcedures/getsmsstatusnewupdatestatus.sql +++ b/src/Altinn.Notifications.Persistence/Migration/FunctionsAndProcedures/getsmsstatusnewupdatestatus.sql @@ -3,15 +3,21 @@ CREATE OR REPLACE FUNCTION notifications.getsms_statusnew_updatestatus() LANGUAGE 'plpgsql' AS $BODY$ BEGIN - - RETURN query + RETURN QUERY WITH updated AS ( UPDATE notifications.smsnotifications SET result = 'Sending', resulttime = now() WHERE result = 'New' - RETURNING notifications.smsnotifications.alternateid, _orderid, notifications.smsnotifications.mobilenumber) - SELECT u.alternateid, st.sendernumber, u.mobilenumber, st.body - FROM updated u, notifications.smstexts st - WHERE u._orderid = st._orderid; + RETURNING notifications.smsnotifications.alternateid, + _orderid, + notifications.smsnotifications.mobilenumber, + notifications.smsnotifications.customizedbody + ) + SELECT u.alternateid, + st.sendernumber, + u.mobilenumber, + CASE WHEN u.customizedbody IS NOT NULL AND u.customizedbody <> '' THEN u.customizedbody ELSE st.body END AS body + FROM updated u + JOIN notifications.smstexts st ON u._orderid = st._orderid; END; -$BODY$; \ No newline at end of file +$BODY$; diff --git a/src/Altinn.Notifications.Persistence/Migration/FunctionsAndProcedures/insertemailnotification.sql b/src/Altinn.Notifications.Persistence/Migration/FunctionsAndProcedures/insertemailnotification.sql index 3e377d3a..775ff06c 100644 --- a/src/Altinn.Notifications.Persistence/Migration/FunctionsAndProcedures/insertemailnotification.sql +++ b/src/Altinn.Notifications.Persistence/Migration/FunctionsAndProcedures/insertemailnotification.sql @@ -1,35 +1,47 @@ CREATE OR REPLACE PROCEDURE notifications.insertemailnotification( -_orderid uuid, -_alternateid uuid, -_recipientorgno TEXT, -_recipientnin TEXT, -_toaddress TEXT, -_result text, -_resulttime timestamptz, -_expirytime timestamptz) + _orderid uuid, + _alternateid uuid, + _recipientorgno TEXT, + _recipientnin TEXT, + _toaddress TEXT, + _customizedbody TEXT, + _customizedsubject TEXT, + _result TEXT, + _resulttime timestamptz, + _expirytime timestamptz +) LANGUAGE 'plpgsql' AS $BODY$ DECLARE -__orderid BIGINT := (SELECT _id from notifications.orders - where alternateid = _orderid); + __orderid BIGINT; BEGIN + SELECT _id INTO __orderid + FROM notifications.orders + WHERE alternateid = _orderid; -INSERT INTO notifications.emailnotifications( -_orderid, -alternateid, -recipientorgno, -recipientnin, -toaddress, result, -resulttime, -expirytime) -VALUES ( -__orderid, -_alternateid, -_recipientorgno, -_recipientnin, -_toaddress, -_result::emailnotificationresulttype, -_resulttime, -_expirytime); + INSERT INTO notifications.emailnotifications( + _orderid, + alternateid, + recipientorgno, + recipientnin, + toaddress, + customizedbody, + customizedsubject, + result, + resulttime, + expirytime + ) + VALUES ( + __orderid, + _alternateid, + _recipientorgno, + _recipientnin, + _toaddress, + _customizedbody, + _customizedsubject, + _result::emailnotificationresulttype, + _resulttime, + _expirytime + ); END; $BODY$; \ No newline at end of file diff --git a/src/Altinn.Notifications.Persistence/Migration/FunctionsAndProcedures/insertsmsnotification.sql b/src/Altinn.Notifications.Persistence/Migration/FunctionsAndProcedures/insertsmsnotification.sql index 5f60838c..f41ebdb3 100644 --- a/src/Altinn.Notifications.Persistence/Migration/FunctionsAndProcedures/insertsmsnotification.sql +++ b/src/Altinn.Notifications.Persistence/Migration/FunctionsAndProcedures/insertsmsnotification.sql @@ -1,40 +1,47 @@ CREATE OR REPLACE PROCEDURE notifications.insertsmsnotification( -_orderid uuid, -_alternateid uuid, -_recipientorgno TEXT, -_recipientnin TEXT, -_mobilenumber TEXT, -_result text, -_smscount integer, -_resulttime timestamptz, -_expirytime timestamptz + _orderid uuid, + _alternateid uuid, + _recipientorgno TEXT, + _recipientnin TEXT, + _mobilenumber TEXT, + _customizedbody TEXT, + _result TEXT, + _smscount integer, + _resulttime timestamptz, + _expirytime timestamptz ) LANGUAGE 'plpgsql' AS $BODY$ DECLARE -__orderid BIGINT := (SELECT _id from notifications.orders - where alternateid = _orderid); + __orderid BIGINT; BEGIN + SELECT _id INTO __orderid + FROM notifications.orders + WHERE alternateid = _orderid; -INSERT INTO notifications.smsnotifications( -_orderid, -alternateid, -recipientorgno, -recipientnin, -mobilenumber, -result, -smscount, -resulttime, -expirytime) -VALUES ( -__orderid, -_alternateid, -_recipientorgno, -_recipientnin, -_mobilenumber, -_result::smsnotificationresulttype, -_smscount, -_resulttime, -_expirytime); + INSERT INTO notifications.smsnotifications( + _orderid, + alternateid, + recipientorgno, + recipientnin, + mobilenumber, + customizedbody, + result, + smscount, + resulttime, + expirytime + ) + VALUES ( + __orderid, + _alternateid, + _recipientorgno, + _recipientnin, + _mobilenumber, + _customizedbody, + _result::smsnotificationresulttype, + _smscount, + _resulttime, + _expirytime + ); END; $BODY$; \ No newline at end of file