Skip to content

Commit 7fd4531

Browse files
Normalize endpoint url of planning unit filtering api
1 parent e444935 commit 7fd4531

File tree

2 files changed

+94
-2
lines changed

2 files changed

+94
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
--! Previous: sha1:ba0eb22ac8ff0f3a3c4e707b53ba8ce27807505a
2+
--! Hash: sha1:88ea156673a7d82b63e484ab0d633bddc174a5e0
3+
4+
-- Enter migration here
5+
CREATE OR REPLACE FUNCTION public.filter_state_to_search_string(filters jsonb, sketch_class_id integer) RETURNS text
6+
LANGUAGE plpgsql
7+
AS $$
8+
DECLARE
9+
state jsonb := '{}';
10+
filter jsonb;
11+
attr text;
12+
result text;
13+
attribute_name text;
14+
filter_server_location text;
15+
filter_version int;
16+
final_url text;
17+
BEGIN
18+
-- Retrieve the filter_api_server_location and filter_api_version from sketch_classes table
19+
SELECT sketch_classes.filter_api_server_location, sketch_classes.filter_api_version
20+
INTO filter_server_location, filter_version
21+
FROM sketch_classes
22+
WHERE id = sketch_class_id;
23+
24+
-- If filter_api_server_location is NULL, return NULL
25+
IF filter_server_location IS NULL THEN
26+
RETURN NULL;
27+
END IF;
28+
29+
filter_server_location := regexp_replace(filter_server_location, '/$', '', '');
30+
31+
-- Loop through each attribute in the input JSONB object
32+
FOR attr, filter IN
33+
SELECT key, value FROM jsonb_each(filters)
34+
LOOP
35+
-- Only process if "selected" is true
36+
IF filter->>'selected' = 'true' THEN
37+
if filter->>'attribute' is not null then
38+
attribute_name := filter->>'attribute';
39+
else
40+
-- Look up the attribute name from form_elements for the current attr ID
41+
SELECT component_settings->>'attribute'
42+
INTO attribute_name
43+
FROM form_elements
44+
WHERE id = attr::int; -- Cast attr to integer to match form_elements ID
45+
end if;
46+
47+
48+
-- Default to the original key if no match is found
49+
IF attribute_name IS NULL THEN
50+
attribute_name := attr;
51+
END IF;
52+
53+
-- Check for numberState
54+
IF filter ? 'numberState' THEN
55+
state := state || jsonb_build_object(attribute_name, jsonb_build_object(
56+
'min', filter->'numberState'->'min',
57+
'max', filter->'numberState'->'max'
58+
));
59+
60+
-- Check for stringState
61+
ELSIF filter ? 'stringState' THEN
62+
state := state || jsonb_build_object(attribute_name, jsonb_build_object(
63+
'choices', filter->'stringState'
64+
));
65+
66+
-- Check for booleanState
67+
ELSIF filter ? 'booleanState' THEN
68+
state := state || jsonb_build_object(attribute_name, jsonb_build_object(
69+
'bool', COALESCE(filter->'booleanState', 'false')::boolean
70+
));
71+
END IF;
72+
END IF;
73+
END LOOP;
74+
75+
-- If no keys were added, return an empty string; otherwise, encode the JSON
76+
IF state = '{}'::jsonb THEN
77+
RETURN filter_server_location || '/v' || filter_version || '/mvt/{z}/{x}/{y}.pbf';
78+
ELSE
79+
-- Convert the JSONB object to a text string without formatting
80+
result := state::text;
81+
82+
-- URL encode the resulting JSON text using the updated url_encode function
83+
result := url_encode(result);
84+
85+
-- Construct the final URL
86+
final_url := filter_server_location || '/v' || filter_version || '/mvt/{z}/{x}/{y}.pbf?filter=' || result;
87+
RETURN final_url;
88+
END IF;
89+
END;
90+
$$;

packages/api/schema.sql

+4-2
Original file line numberDiff line numberDiff line change
@@ -9270,7 +9270,7 @@ CREATE FUNCTION public.fail_data_upload(id uuid, msg text) RETURNS public.data_u
92709270

92719271
CREATE FUNCTION public.filter_state_to_search_string(filters jsonb, sketch_class_id integer) RETURNS text
92729272
LANGUAGE plpgsql
9273-
AS $$
9273+
AS $_$
92749274
DECLARE
92759275
state jsonb := '{}';
92769276
filter jsonb;
@@ -9292,6 +9292,8 @@ BEGIN
92929292
RETURN NULL;
92939293
END IF;
92949294

9295+
filter_server_location := regexp_replace(filter_server_location, '/$', '', '');
9296+
92959297
-- Loop through each attribute in the input JSONB object
92969298
FOR attr, filter IN
92979299
SELECT key, value FROM jsonb_each(filters)
@@ -9351,7 +9353,7 @@ BEGIN
93519353
RETURN final_url;
93529354
END IF;
93539355
END;
9354-
$$;
9356+
$_$;
93559357

93569358

93579359
--

0 commit comments

Comments
 (0)