diff --git a/src/main/resources/sql/functions/follow/unfollow.sql b/src/main/resources/sql/functions/follow/unfollow.sql index 63fc934..14c4853 100644 --- a/src/main/resources/sql/functions/follow/unfollow.sql +++ b/src/main/resources/sql/functions/follow/unfollow.sql @@ -1,12 +1,39 @@ create or replace function unfollow(reference regclass, _target_id uuid, _created_by_id uuid) returns void language plpgsql as $$ +declare + _targets_ids uuid[]; begin - delete - from follow f - where f.created_by_id = _created_by_id - and f.target_id = _target_id - and f.target_reference = reference; + if reference = 'article'::regclass then + select array_agg(a2.id) into _targets_ids + from article a1 + join article a2 using (version_id) + where a1.id = _target_id; + + delete + from follow f + where f.created_by_id = _created_by_id + and f.target_id = any(_targets_ids) + and f.target_reference = reference; + elseif reference = 'constitution'::regclass then + select array_agg(c2.id) into _targets_ids + from constitution c1 + join constitution c2 using (version_id) + where c1.id = _target_id; + delete + from follow f + where f.created_by_id = _created_by_id + and f.target_id = any(_targets_ids) + and f.target_reference = reference; + elseif reference = 'citizen'::regclass then + delete + from follow f + where f.created_by_id = _created_by_id + and f.target_id = _target_id + and f.target_reference = reference; + else + raise exception '% no implemented for follow', reference::text; + end if; end; $$; diff --git a/src/test/sql/follow.sql b/src/test/sql/follow.sql index ca364ab..deec2e4 100644 --- a/src/test/sql/follow.sql +++ b/src/test/sql/follow.sql @@ -28,6 +28,7 @@ declare created_article json := '{"version_id":"933b6a1b-50c9-42b6-989f-c02a57814ef9", "title": "Love the world", "anonymous": false, "content": "bla bal bla", "tags": ["love", "test"], "draft":false}'; first_article_id uuid; + first_article_updated_id uuid; begin -- insert user for context select insert_user(created_user) into created_user; @@ -61,6 +62,17 @@ begin perform follow('article'::regclass, first_article_id, _citizen_id); assert (select following = true from find_follow(first_article_id, _citizen_id)), 'find_follow must return the following'; assert (select following = false from find_follow(first_article_id, _citizen_id2)), 'find_follow must not return the following if not followinf'; + assert (select count(*) = 1 from follow), 'must have 1 following'; + + -- add new version for article, then unfollow the new one + select upsert_article(created_article) into created_article; + first_article_updated_id = (created_article->>'id')::uuid; + assert first_article_id != first_article_updated_id; + + perform unfollow('article'::regclass, first_article_id, _citizen_id); +-- perform unfollow('article'::regclass, first_article_updated_id, _citizen_id); + assert (select count(*) = 0 from follow), 'follow must be deleted after unfollow, event if article is on other version'; + -- delete follow and context delete from follow;