Skip to content

Commit

Permalink
Save and retrieve the customized subject and body to and from the dat…
Browse files Browse the repository at this point in the history
…abase
  • Loading branch information
Ahmed-Ghanam committed Nov 29, 2024
1 parent 1b0385d commit adfb256
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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$;
$BODY$;
Original file line number Diff line number Diff line change
Expand Up @@ -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$;
$BODY$;
Original file line number Diff line number Diff line change
@@ -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$;
Original file line number Diff line number Diff line change
@@ -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$;

0 comments on commit adfb256

Please sign in to comment.