Skip to content

Commit

Permalink
Normalize endpoint url of planning unit filtering api
Browse files Browse the repository at this point in the history
  • Loading branch information
underbluewaters committed Nov 17, 2024
1 parent e444935 commit 7fd4531
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 2 deletions.
90 changes: 90 additions & 0 deletions packages/api/migrations/committed/000342.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
--! Previous: sha1:ba0eb22ac8ff0f3a3c4e707b53ba8ce27807505a
--! Hash: sha1:88ea156673a7d82b63e484ab0d633bddc174a5e0

-- Enter migration here
CREATE OR REPLACE FUNCTION public.filter_state_to_search_string(filters jsonb, sketch_class_id integer) RETURNS text
LANGUAGE plpgsql
AS $$
DECLARE
state jsonb := '{}';
filter jsonb;
attr text;
result text;
attribute_name text;
filter_server_location text;
filter_version int;
final_url text;
BEGIN
-- Retrieve the filter_api_server_location and filter_api_version from sketch_classes table
SELECT sketch_classes.filter_api_server_location, sketch_classes.filter_api_version
INTO filter_server_location, filter_version
FROM sketch_classes
WHERE id = sketch_class_id;

-- If filter_api_server_location is NULL, return NULL
IF filter_server_location IS NULL THEN
RETURN NULL;
END IF;

filter_server_location := regexp_replace(filter_server_location, '/$', '', '');

-- Loop through each attribute in the input JSONB object
FOR attr, filter IN
SELECT key, value FROM jsonb_each(filters)
LOOP
-- Only process if "selected" is true
IF filter->>'selected' = 'true' THEN
if filter->>'attribute' is not null then
attribute_name := filter->>'attribute';
else
-- Look up the attribute name from form_elements for the current attr ID
SELECT component_settings->>'attribute'
INTO attribute_name
FROM form_elements
WHERE id = attr::int; -- Cast attr to integer to match form_elements ID
end if;


-- Default to the original key if no match is found
IF attribute_name IS NULL THEN
attribute_name := attr;
END IF;

-- Check for numberState
IF filter ? 'numberState' THEN
state := state || jsonb_build_object(attribute_name, jsonb_build_object(
'min', filter->'numberState'->'min',
'max', filter->'numberState'->'max'
));

-- Check for stringState
ELSIF filter ? 'stringState' THEN
state := state || jsonb_build_object(attribute_name, jsonb_build_object(
'choices', filter->'stringState'
));

-- Check for booleanState
ELSIF filter ? 'booleanState' THEN
state := state || jsonb_build_object(attribute_name, jsonb_build_object(
'bool', COALESCE(filter->'booleanState', 'false')::boolean
));
END IF;
END IF;
END LOOP;

-- If no keys were added, return an empty string; otherwise, encode the JSON
IF state = '{}'::jsonb THEN
RETURN filter_server_location || '/v' || filter_version || '/mvt/{z}/{x}/{y}.pbf';
ELSE
-- Convert the JSONB object to a text string without formatting
result := state::text;

-- URL encode the resulting JSON text using the updated url_encode function
result := url_encode(result);

-- Construct the final URL
final_url := filter_server_location || '/v' || filter_version || '/mvt/{z}/{x}/{y}.pbf?filter=' || result;
RETURN final_url;
END IF;
END;
$$;
6 changes: 4 additions & 2 deletions packages/api/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9270,7 +9270,7 @@ CREATE FUNCTION public.fail_data_upload(id uuid, msg text) RETURNS public.data_u

CREATE FUNCTION public.filter_state_to_search_string(filters jsonb, sketch_class_id integer) RETURNS text
LANGUAGE plpgsql
AS $$
AS $_$
DECLARE
state jsonb := '{}';
filter jsonb;
Expand All @@ -9292,6 +9292,8 @@ BEGIN
RETURN NULL;
END IF;

filter_server_location := regexp_replace(filter_server_location, '/$', '', '');

-- Loop through each attribute in the input JSONB object
FOR attr, filter IN
SELECT key, value FROM jsonb_each(filters)
Expand Down Expand Up @@ -9351,7 +9353,7 @@ BEGIN
RETURN final_url;
END IF;
END;
$$;
$_$;


--
Expand Down

0 comments on commit 7fd4531

Please sign in to comment.