Skip to content

Commit

Permalink
fix: adapt delete functions to categories and testimonials
Browse files Browse the repository at this point in the history
  • Loading branch information
ewan-escience committed Nov 1, 2024
1 parent 09ee8a5 commit 114578e
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
2 changes: 2 additions & 0 deletions database/106-project-views.sql
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ BEGIN
RAISE EXCEPTION USING MESSAGE = 'You are not allowed to delete this project';
END IF;

DELETE FROM category_for_project WHERE category_for_project.project_id = delete_project.id;
DELETE FROM impact_for_project WHERE impact_for_project.project = delete_project.id;
DELETE FROM invite_maintainer_for_project WHERE invite_maintainer_for_project.project = delete_project.id;
DELETE FROM keyword_for_project WHERE keyword_for_project.project = delete_project.id;
Expand All @@ -36,6 +37,7 @@ BEGIN
DELETE FROM research_domain_for_project WHERE research_domain_for_project.project = delete_project.id;
DELETE FROM software_for_project WHERE software_for_project.project = delete_project.id;
DELETE FROM team_member WHERE team_member.project = delete_project.id;
DELETE FROM testimonial_for_project WHERE testimonial_for_project.project = delete_project.id;
DELETE FROM url_for_project WHERE url_for_project.project = delete_project.id;

DELETE FROM project WHERE project.id = delete_project.id;
Expand Down
35 changes: 35 additions & 0 deletions database/109-category-functions.sql
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,38 @@ DELETE FROM category_for_project
category_for_project.project_id = delete_organisation_categories_from_project.project_id AND
category.organisation = delete_organisation_categories_from_project.organisation_id;
$$;

CREATE FUNCTION delete_category_node(category_id UUID)
RETURNS VOID
LANGUAGE plpgsql
SECURITY DEFINER
VOLATILE
AS
$$
DECLARE child_id UUID;
DECLARE child_ids UUID[];
BEGIN
IF category_id IS NULL THEN
RAISE EXCEPTION USING MESSAGE = 'Please provide the ID of the category to delete';
END IF;

IF
(SELECT rolsuper FROM pg_roles WHERE rolname = SESSION_USER) IS DISTINCT FROM TRUE
AND
(SELECT CURRENT_SETTING('request.jwt.claims', FALSE)::json->>'role') IS DISTINCT FROM 'rsd_admin'
THEN
RAISE EXCEPTION USING MESSAGE = 'You are not allowed to delete this category';
END IF;

child_ids := (SELECT COALESCE((SELECT ARRAY_AGG(category.id) FROM category WHERE category.parent = delete_category_node.category_id), '{}'));

FOREACH child_id IN ARRAY child_ids LOOP
PERFORM delete_category_node(child_id);
END LOOP;

DELETE FROM category_for_software WHERE category_for_software.category_id = delete_category_node.category_id;
DELETE FROM category_for_project WHERE category_for_project.category_id = delete_category_node.category_id;

DELETE FROM category WHERE category.id = delete_category_node.category_id;
END;
$$;
10 changes: 9 additions & 1 deletion database/113-organisation-views.sql
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ AS
$$
DECLARE child_id UUID;
DECLARE child_ids UUID[];
DECLARE category_id UUID;
DECLARE category_ids UUID[];
BEGIN
IF id IS NULL THEN
RAISE EXCEPTION USING MESSAGE = 'Please provide the ID of the organisation to delete';
Expand All @@ -26,12 +28,18 @@ BEGIN
RAISE EXCEPTION USING MESSAGE = 'You are not allowed to delete this organisation';
END IF;

child_ids := ARRAY_REMOVE(ARRAY_AGG((SELECT organisation.id FROM organisation WHERE organisation.parent = delete_organisation.id)), NULL);
child_ids := (SELECT COALESCE((SELECT ARRAY_AGG(organisation.id) FROM organisation WHERE organisation.parent = delete_organisation.id), '{}'));

FOREACH child_id IN ARRAY child_ids LOOP
PERFORM delete_organisation(child_id);
END LOOP;

category_ids := (SELECT COALESCE((SELECT ARRAY_AGG(category.id) FROM category WHERE category.organisation = delete_organisation.id), '{}'));

FOREACH category_id IN ARRAY category_ids LOOP
PERFORM delete_category_node(category_id);
END LOOP;

DELETE FROM invite_maintainer_for_organisation WHERE invite_maintainer_for_organisation.organisation = delete_organisation.id;
DELETE FROM maintainer_for_organisation WHERE maintainer_for_organisation.organisation = delete_organisation.id;
DELETE FROM project_for_organisation WHERE project_for_organisation.organisation = delete_organisation.id;
Expand Down

0 comments on commit 114578e

Please sign in to comment.