Skip to content

Commit d294f76

Browse files
committed
api:Fix bug in contact handling in statistical_unit_hierarchy
The query failed to get the correct time segment for contact.
1 parent 47c28c5 commit d294f76

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

migrations/20240608000000_statistical_unit_tree_and_details.up.sql

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,15 @@ $$;
3232

3333
CREATE FUNCTION public.contact_hierarchy(
3434
parent_establishment_id INTEGER,
35-
parent_legal_unit_id INTEGER
35+
parent_legal_unit_id INTEGER,
36+
valid_on DATE
3637
) RETURNS JSONB LANGUAGE sql STABLE AS $$
3738
SELECT COALESCE(
3839
(SELECT jsonb_build_object('contact',to_jsonb(c.*))
3940
FROM public.contact AS c
4041
WHERE ( parent_establishment_id IS NOT NULL AND c.establishment_id = parent_establishment_id
4142
OR parent_legal_unit_id IS NOT NULL AND c.legal_unit_id = parent_legal_unit_id
42-
)),
43+
) AND c.valid_from <= valid_on AND valid_on < c.valid_until),
4344
'{}'::JSONB
4445
);
4546
$$;
@@ -71,7 +72,7 @@ CREATE FUNCTION public.establishment_hierarchy(
7172
|| CASE WHEN scope IN ('all','details') THEN (SELECT public.sector_hierarchy(es.sector_id)) ELSE '{}'::JSONB END
7273
|| CASE WHEN scope IN ('all','details') THEN (SELECT public.unit_size_hierarchy(es.unit_size_id)) ELSE '{}'::JSONB END
7374
|| CASE WHEN scope IN ('all','details') THEN (SELECT public.status_hierarchy(es.status_id)) ELSE '{}'::JSONB END
74-
|| CASE WHEN scope IN ('all','details') THEN (SELECT public.contact_hierarchy(es.id,NULL)) ELSE '{}'::JSONB END
75+
|| CASE WHEN scope IN ('all','details') THEN (SELECT public.contact_hierarchy(es.id,NULL,valid_on)) ELSE '{}'::JSONB END
7576
|| CASE WHEN scope IN ('all','details') THEN (SELECT public.data_source_hierarchy(es.data_source_id)) ELSE '{}'::JSONB END
7677
|| CASE WHEN scope IN ('all','details') THEN (SELECT public.notes_for_unit(es.id,NULL,NULL,NULL)) ELSE '{}'::JSONB END
7778
|| CASE WHEN scope IN ('all','details') THEN (SELECT public.tag_for_unit_hierarchy(es.id,NULL,NULL,NULL)) ELSE '{}'::JSONB END
@@ -111,7 +112,7 @@ CREATE FUNCTION public.legal_unit_hierarchy(
111112
|| CASE WHEN scope IN ('all','details') THEN (SELECT public.unit_size_hierarchy(lu.unit_size_id)) ELSE '{}'::JSONB END
112113
|| CASE WHEN scope IN ('all','details') THEN (SELECT public.status_hierarchy(lu.status_id)) ELSE '{}'::JSONB END
113114
|| CASE WHEN scope IN ('all','details') THEN (SELECT public.legal_form_hierarchy(lu.legal_form_id)) ELSE '{}'::JSONB END
114-
|| CASE WHEN scope IN ('all','details') THEN (SELECT public.contact_hierarchy(NULL,lu.id)) ELSE '{}'::JSONB END
115+
|| CASE WHEN scope IN ('all','details') THEN (SELECT public.contact_hierarchy(NULL,lu.id,valid_on)) ELSE '{}'::JSONB END
115116
|| CASE WHEN scope IN ('all','details') THEN (SELECT public.data_source_hierarchy(lu.data_source_id)) ELSE '{}'::JSONB END
116117
|| CASE WHEN scope IN ('all','details') THEN (SELECT public.notes_for_unit(NULL,lu.id,NULL,NULL)) ELSE '{}'::JSONB END
117118
|| CASE WHEN scope IN ('all','details') THEN (SELECT public.tag_for_unit_hierarchy(NULL,lu.id,NULL,NULL)) ELSE '{}'::JSONB END

0 commit comments

Comments
 (0)