Refactor user_settings

This commit is contained in:
xbgmsharp
2022-09-24 23:32:17 +02:00
parent 3fb2534263
commit 97e739ffe9
2 changed files with 18 additions and 75 deletions

View File

@@ -127,9 +127,8 @@ begin
-- Gather email and pushover app settings -- Gather email and pushover app settings
app_settings = get_app_settings_fn(); app_settings = get_app_settings_fn();
-- Gather user settings -- Gather user settings
user_settings := get_user_settings_from_metadata_fn(metadata_rec.id::INTEGER); user_settings := get_user_settings_from_clientid_fn(metadata_rec.client_id::TEXT);
--user_settings := get_user_settings_from_clientid_fn(metadata_rec.id::INTEGER); RAISE DEBUG '-> debug monitor_offline get_user_settings_from_clientid_fn [%]', user_settings;
RAISE DEBUG '-> debug monitor_offline get_user_settings_from_metadata_fn [%]', user_settings;
-- Send notification -- Send notification
--PERFORM send_notification_fn('monitor_offline'::TEXT, metadata_rec::RECORD); --PERFORM send_notification_fn('monitor_offline'::TEXT, metadata_rec::RECORD);
PERFORM send_email_py_fn('monitor_offline'::TEXT, user_settings::JSONB, app_settings::JSONB); PERFORM send_email_py_fn('monitor_offline'::TEXT, user_settings::JSONB, app_settings::JSONB);
@@ -172,9 +171,8 @@ begin
-- Gather email and pushover app settings -- Gather email and pushover app settings
app_settings = get_app_settings_fn(); app_settings = get_app_settings_fn();
-- Gather user settings -- Gather user settings
user_settings := get_user_settings_from_metadata_fn(metadata_rec.id::INTEGER); user_settings := get_user_settings_from_clientid_fn(metadata_rec.client_id::TEXT);
--user_settings := get_user_settings_from_clientid_fn((metadata_rec.client_id::INTEGER, ); RAISE NOTICE '-> debug monitor_online get_user_settings_from_clientid_fn [%]', user_settings;
RAISE NOTICE '-> debug monitor_online get_user_settings_from_metadata_fn [%]', user_settings;
-- Send notification -- Send notification
--PERFORM send_notification_fn('monitor_online'::TEXT, metadata_rec::RECORD); --PERFORM send_notification_fn('monitor_online'::TEXT, metadata_rec::RECORD);
PERFORM send_email_py_fn('monitor_online'::TEXT, user_settings::JSONB, app_settings::JSONB); PERFORM send_email_py_fn('monitor_online'::TEXT, user_settings::JSONB, app_settings::JSONB);

View File

@@ -146,7 +146,7 @@ AS $send_email_py$
# Replace fields using input jsonb obj # Replace fields using input jsonb obj
plpy.notice('Parameters [{}] [{}]'.format(_user, app)) plpy.notice('Parameters [{}] [{}]'.format(_user, app))
if not _user or not app: if not _user or not app:
plpy.error('Error no parameters') plpy.error('Error missing parameters')
return None return None
if 'logbook_name' in _user and _user['logbook_name']: if 'logbook_name' in _user and _user['logbook_name']:
email_content = email_content.replace('__LOGBOOK_NAME__', _user['logbook_name']) email_content = email_content.replace('__LOGBOOK_NAME__', _user['logbook_name'])
@@ -403,6 +403,7 @@ CREATE OR REPLACE FUNCTION process_logbook_queue_fn(IN _id integer) RETURNS void
log_name varchar; log_name varchar;
avg_rec record; avg_rec record;
geo_rec record; geo_rec record;
log_settings jsonb;
user_settings jsonb; user_settings jsonb;
app_settings jsonb; app_settings jsonb;
geojson jsonb; geojson jsonb;
@@ -452,7 +453,11 @@ CREATE OR REPLACE FUNCTION process_logbook_queue_fn(IN _id integer) RETURNS void
-- Gather email and pushover app settings -- Gather email and pushover app settings
app_settings := get_app_settings_fn(); app_settings := get_app_settings_fn();
-- Gather user settings -- Gather user settings
user_settings := get_user_settings_from_log_fn(logbook_rec::RECORD); SELECT json_build_object('logbook_name', log_name, 'logbook_link', logbook_rec.id) into log_settings;
user_settings := get_user_settings_from_clientid_fn(logbook_rec.client_id::TEXT);
SELECT user_settings::JSONB || log_settings::JSONB into user_settings;
RAISE DEBUG '-> debug process_logbook_queue_fn get_user_settings_from_clientid_fn [%]', user_settings;
--user_settings := get_user_settings_from_log_fn(logbook_rec::RECORD);
--user_settings := '{"logbook_name": "' || log_name || '"}, "{"email": "' || account_rec.email || '", "recipient": "' || account_rec.first || '}'; --user_settings := '{"logbook_name": "' || log_name || '"}, "{"email": "' || account_rec.email || '", "recipient": "' || account_rec.first || '}';
--user_settings := '{"logbook_name": "' || log_name || '"}'; --user_settings := '{"logbook_name": "' || log_name || '"}';
-- Send notification email, pushover -- Send notification email, pushover
@@ -481,7 +486,6 @@ CREATE OR REPLACE FUNCTION process_stay_queue_fn(IN _id integer) RETURNS void AS
SELECT * INTO stay_rec SELECT * INTO stay_rec
FROM api.stays FROM api.stays
WHERE id = _id; WHERE id = _id;
-- AND client_id LIKE '%' || current_setting('vessel.mmsi', false) || '%';
-- geo reverse _lng _lat -- geo reverse _lng _lat
_name := reverse_geocode_py_fn('nominatim', stay_rec.longitude::NUMERIC, stay_rec.latitude::NUMERIC); _name := reverse_geocode_py_fn('nominatim', stay_rec.longitude::NUMERIC, stay_rec.latitude::NUMERIC);
@@ -644,7 +648,7 @@ COMMENT ON FUNCTION
-- Get user settings details from a log entry -- Get user settings details from a log entry
DROP FUNCTION IF EXISTS get_app_settings_fn; DROP FUNCTION IF EXISTS get_app_settings_fn;
CREATE OR REPLACE FUNCTION get_app_settings_fn(OUT app_settings JSON) RETURNS JSON CREATE OR REPLACE FUNCTION get_app_settings_fn(OUT app_settings JSONB) RETURNS JSONB
AS $get_app_settings$ AS $get_app_settings$
DECLARE DECLARE
BEGIN BEGIN
@@ -658,62 +662,6 @@ COMMENT ON FUNCTION
public.get_app_settings_fn public.get_app_settings_fn
IS 'get app settings details, email, pushover'; IS 'get app settings details, email, pushover';
-- Get user settings details from a log entry
DROP FUNCTION IF EXISTS get_user_settings_from_log_fn;
CREATE OR REPLACE FUNCTION get_user_settings_from_log_fn(IN logbook_rec RECORD, OUT user_settings JSON) RETURNS JSON
AS $get_user_settings_from_log$
DECLARE
BEGIN
-- If client_id is not NULL
IF logbook_rec.client_id IS NULL OR logbook_rec.client_id = '' THEN
RAISE WARNING '-> get_user_settings_from_log_fn invalid input %', logbook_rec.client_id;
END IF;
SELECT
json_build_object(
'boat' , v.name,
'recipient', a.first,
'email', v.owner_email,
'logbook_name', l.name,
'logbook_link', l.id) INTO user_settings
FROM auth.accounts a, auth.vessels v, api.metadata m, api.logbook l
WHERE lower(a.email) = lower(v.owner_email)
AND lower(v.name) = lower(m.name)
AND m.client_id = l.client_id
AND l.client_id = logbook_rec.client_id
AND l.id = logbook_rec.id;
END;
$get_user_settings_from_log$ LANGUAGE plpgsql;
-- Description
COMMENT ON FUNCTION
public.get_user_settings_from_log_fn
IS 'get user settings details from a log entry, initiate for logbook entry notification';
-- Get user settings details from a metadata entry
DROP FUNCTION IF EXISTS get_user_settings_from_metadata;
CREATE OR REPLACE FUNCTION get_user_settings_from_metadata_fn(IN meta_id INTEGER, OUT user_settings JSON) RETURNS JSON
AS $get_user_settings_from_metadata$
DECLARE
BEGIN
-- If meta_id is not NULL
IF meta_id IS NULL OR meta_id < 1 THEN
RAISE WARNING '-> get_user_settings_from_metadata_fn invalid input %', meta_id;
END IF;
SELECT json_build_object(
'boat' , v.name,
'email', v.owner_email) INTO user_settings
FROM auth.vessels v, api.metadata m
WHERE
--lower(v.name) = lower(m.name) AND
m.id = meta_id;
END;
$get_user_settings_from_metadata$ LANGUAGE plpgsql;
-- Description
COMMENT ON FUNCTION
public.get_user_settings_from_metadata_fn
IS 'get user settings details from a metadata entry, initiate for monitoring offline,online notification';
-- Get user settings details from a metadata entry -- Get user settings details from a metadata entry
DROP FUNCTION IF EXISTS send_notification_fn; DROP FUNCTION IF EXISTS send_notification_fn;
CREATE OR REPLACE FUNCTION send_notification_fn(IN email_type TEXT, IN notification_rec RECORD) RETURNS JSON CREATE OR REPLACE FUNCTION send_notification_fn(IN email_type TEXT, IN notification_rec RECORD) RETURNS JSON
@@ -744,9 +692,8 @@ COMMENT ON FUNCTION
DROP FUNCTION IF EXISTS get_user_settings_from_clientid_fn; DROP FUNCTION IF EXISTS get_user_settings_from_clientid_fn;
CREATE OR REPLACE FUNCTION get_user_settings_from_clientid_fn( CREATE OR REPLACE FUNCTION get_user_settings_from_clientid_fn(
IN clientid TEXT, IN clientid TEXT,
IN logbook_name TEXT, OUT user_settings JSONB
OUT user_settings JSON ) RETURNS JSONB
) RETURNS JSON
AS $get_user_settings_from_clientid$ AS $get_user_settings_from_clientid$
DECLARE DECLARE
BEGIN BEGIN
@@ -761,19 +708,17 @@ AS $get_user_settings_from_clientid$
'email', v.owner_email , 'email', v.owner_email ,
'settings', a.preferences, 'settings', a.preferences,
'pushover_key', a.preferences->'pushover_key', 'pushover_key', a.preferences->'pushover_key',
'badges', a.preferences->'badges', 'badges', a.preferences->'badges'
'logbook_name', logbook_name ) INTO user_settings ) INTO user_settings
FROM auth.accounts a, auth.vessels v, api.metadata m FROM auth.accounts a, auth.vessels v, api.metadata m
WHERE lower(a.email) = lower(v.owner_email) WHERE m.mmsi = v.mmsi
AND lower(v.name) = lower(m.name)
AND m.mmsi = v.mmsi
AND m.client_id = clientid; AND m.client_id = clientid;
END; END;
$get_user_settings_from_clientid$ LANGUAGE plpgsql; $get_user_settings_from_clientid$ LANGUAGE plpgsql;
-- Description -- Description
COMMENT ON FUNCTION COMMENT ON FUNCTION
public.get_user_settings_from_clientid_fn public.get_user_settings_from_clientid_fn
IS 'get user settings details from a clientid, initiate for badge entry notification'; IS 'get user settings details from a clientid, initiate for notifications';
--------------------------------------------------------------------------- ---------------------------------------------------------------------------
-- Queue handling -- Queue handling