UserOperation_contact_setting_change — finish implementation.

This commit is contained in:
Bradley M. Kuhn 2022-11-10 19:10:28 -08:00
parent 42c27893f7
commit 549df60002

View file

@ -339,6 +339,9 @@ END;
-- -------------- VIEW: UserOperation_contact_setting_change ---------------------
-- For this one, note that request_type(id) of 12 is hard-coded since that's known to be the "contact-setting" one.
-- … it would obviously be better not to hard code it, but looking it up in every query would make it harder to read.
DROP VIEW IF EXISTS UserQuery_contact_settings;
CREATE VIEW UserQuery_contact_settings AS
SELECT request_configuration.description as setting_requested, request_type.type as contact_setting
@ -367,13 +370,14 @@ DELETE FROM _temp_request_configuration_change;
INSERT INTO _temp_request_configuration_change(new_description) VALUES(NEW.setting_requested);
UPDATE _temp_request_configuration_change set new_request_configuration_id = (select rc.id
from request_configuration rc, request_type rt
where rc.description = NEW.setting_requested
and rt.id = rc.request_type_id
and rt.type = OLD.contact_setting)
UPDATE _temp_request_configuration_change
SET new_request_configuration_id =
(SELECT rc.id FROM request_configuration rc, request_type rt
WHERE rc.description = NEW.setting_requested
AND rt.id = rc.request_type_id
AND rt.id = 12)
WHERE new_description = NEW.setting_requested;
UPDATE _temp_request_configuration_change set old_request_configuration_id =
(SELECT rc.id
FROM request_configuration rc, request_type rt
@ -385,15 +389,29 @@ UPDATE _temp_request_configuration_change set old_request_configuration_id =
SELECT CASE
WHEN ( (SELECT new_request_configuration_id from _temp_request_configuration_change) IS NULL )
THEN RAISE(FAIL, "Invalid contact setting; see UserLookup_contact_settings to find what settings are permitted here!")
THEN RAISE(FAIL, "Invalid contact setting; see UserQuery_contact_settings to find what settings are permitted here!")
END;
INSERT OR IGNORE INTO request(donor_id, request_type_id, request_configuration_id, date_requested, notes)
VALUES(NEW.donor_id, 12,
(SELECT new_request_configuration_id from _temp_request_configuration_change),
date("now"), NEW.note);
-- The following UPDATE will effect 0 rows if OLD.request_id is NULL and this is *by design*.
-- The reason why is we have a UNIQUE index on request(donor_id, request_type_id) and as such, we want
-- to just UPDATE an existing request_type_id of 12 (aka "contact-setting") on this trigger if one is present;
-- otherwise we want to INSERT it. This is why we did the INSERT OR IGNORE above first; if that fails, then this
-- UPDATE will succeed.
UPDATE request
set request_configuration_id =
SET request_configuration_id =
(select new_request_configuration_id from _temp_request_configuration_change),
date_requested = date("now")
where id = OLD.request_id
and request_configuration_id =
WHERE id = OLD.request_id
AND OLD.request_id is NOT NULL
AND OLD.donor_id = donor_id
AND request_type_id = 12
AND request_configuration_id =
(select old_request_configuration_id from _temp_request_configuration_change);
DELETE FROM _temp_request_configuration_change;