From 7320efc4643fae61e838d5f360bdb4d87ae5b689 Mon Sep 17 00:00:00 2001 From: ColinRgm <90181373+ColinRgm@users.noreply.github.com> Date: Tue, 14 Jan 2025 11:51:52 +0100 Subject: [PATCH] fix: delete an ESP in the backend (#219) * feat: delete an ESP in the backend * fix: the deleting function is now functional Closes: #218 --- .../deploy/delete_esp_permission.sql | 4 +-- .../deploy/new_way_delete_esp_permission.sql | 23 ++++++++++++++++ .../migration/revert/add_new_permission.sql | 2 +- .../revert/delete_esp_permission.sql | 4 +-- .../revert/new_way_delete_esp_permission.sql | 10 +++++++ database/migration/sqitch.plan | 1 + .../verify/new_way_delete_esp_permission.sql | 7 +++++ .../src/app/ui/dashboard/DeleteEsp.tsx | 27 +++++++++---------- 8 files changed, 58 insertions(+), 20 deletions(-) create mode 100644 database/migration/deploy/new_way_delete_esp_permission.sql create mode 100644 database/migration/revert/new_way_delete_esp_permission.sql create mode 100644 database/migration/verify/new_way_delete_esp_permission.sql diff --git a/database/migration/deploy/delete_esp_permission.sql b/database/migration/deploy/delete_esp_permission.sql index 98abc1b..4c4e82c 100644 --- a/database/migration/deploy/delete_esp_permission.sql +++ b/database/migration/deploy/delete_esp_permission.sql @@ -2,7 +2,7 @@ BEGIN; - grant delete on api.esp to web_user; -- any user can delete ESP - grant delete on api.data to web_user; -- any user can delete ESP's data + GRANT DELETE ON api.esp TO web_user; -- any user can delete ESP + GRANT DELETE ON api.data TO web_user; -- any user can delete ESP's data COMMIT; diff --git a/database/migration/deploy/new_way_delete_esp_permission.sql b/database/migration/deploy/new_way_delete_esp_permission.sql new file mode 100644 index 0000000..f2ff86c --- /dev/null +++ b/database/migration/deploy/new_way_delete_esp_permission.sql @@ -0,0 +1,23 @@ +-- Deploy climat-guardian:new_way_delete_esp_permission to pg + +-- Function to DELETE on data and esp tables + +BEGIN; + +-- Delete the function if exists +DROP FUNCTION IF EXISTS api.delete_esp_data_and_esp; + +-- Create the function +CREATE FUNCTION api.delete_esp_data_and_esp(id_data INT) + RETURNS VOID AS $$ + +BEGIN + + -- DELETE on data and esp tables + DELETE FROM api.data WHERE api.data.esp_id = id_data; + DELETE FROM api.esp WHERE api.esp.id = id_data; + +END; +$$ LANGUAGE plpgsql; + +COMMIT; \ No newline at end of file diff --git a/database/migration/revert/add_new_permission.sql b/database/migration/revert/add_new_permission.sql index 4ea61be..fad3771 100644 --- a/database/migration/revert/add_new_permission.sql +++ b/database/migration/revert/add_new_permission.sql @@ -2,6 +2,6 @@ BEGIN; - revoke update on api.users to web_user; -- any user can edit users + revoke update on api.users from web_user; -- any user can edit users COMMIT; diff --git a/database/migration/revert/delete_esp_permission.sql b/database/migration/revert/delete_esp_permission.sql index f9ebc2e..8c726fa 100644 --- a/database/migration/revert/delete_esp_permission.sql +++ b/database/migration/revert/delete_esp_permission.sql @@ -2,7 +2,7 @@ BEGIN; - revoke delete on api.esp to web_user; -- any user can't anymore delete ESP - revoke delete on api.data to web_user; -- any user can't anymore delete ESP's data + REVOKE DELETE ON api.esp FROM web_user; -- any user can't anymore delete ESP + REVOKE DELETE ON api.data FROM web_user; -- any user can't anymore delete ESP's data COMMIT; diff --git a/database/migration/revert/new_way_delete_esp_permission.sql b/database/migration/revert/new_way_delete_esp_permission.sql new file mode 100644 index 0000000..e8fbc62 --- /dev/null +++ b/database/migration/revert/new_way_delete_esp_permission.sql @@ -0,0 +1,10 @@ +-- Revert climat-guardian:new_way_delete_esp_permission from pg + +BEGIN; + + +-- DELETE the function if it exists +DROP FUNCTION IF EXISTS api.delete_esp_data_and_esp(id); + + +COMMIT; \ No newline at end of file diff --git a/database/migration/sqitch.plan b/database/migration/sqitch.plan index 2ca570d..bede4cc 100644 --- a/database/migration/sqitch.plan +++ b/database/migration/sqitch.plan @@ -11,3 +11,4 @@ esp 2024-06-26T08:20:01Z dylan # create a esp table to stov 2024-07-10 2024-07-10T08:08:27Z Nils # Give the esp permission to know it's id add_new_permission 2024-12-17T14:46:26Z Colin # Add a permission for the user to edit the user's datas delete_esp_permission 2025-01-07T08:18:34Z Colin # Add a permission for the user to delete ESP +new_way_delete_esp_permission 2025-01-07T10:54:34Z Colin # change the function to delete an ESP diff --git a/database/migration/verify/new_way_delete_esp_permission.sql b/database/migration/verify/new_way_delete_esp_permission.sql new file mode 100644 index 0000000..b80bdd5 --- /dev/null +++ b/database/migration/verify/new_way_delete_esp_permission.sql @@ -0,0 +1,7 @@ +-- Verify climat-guardian:new_way_delete_esp_permission on pg + +BEGIN; + +-- XXX Add verifications here. + +ROLLBACK; diff --git a/nextjs-interface/src/app/ui/dashboard/DeleteEsp.tsx b/nextjs-interface/src/app/ui/dashboard/DeleteEsp.tsx index e494746..ae44082 100644 --- a/nextjs-interface/src/app/ui/dashboard/DeleteEsp.tsx +++ b/nextjs-interface/src/app/ui/dashboard/DeleteEsp.tsx @@ -4,33 +4,30 @@ import { getToken } from "@/lib/context"; export default function DeleteEsp({ id }: { id: string }) { const deleteEsp = async (id: string) => { // Get the id in the URL of the page - const urlData = `/postgrest/data?esp_id=eq.${id}`; - const url = `/postgrest/esp?id=eq.${id}`; + const url = `/postgrest/rpc/delete_esp_data_and_esp`; try { - const responseData = await fetch(urlData, { - method: "DELETE", - headers: { - "Content-Type": "application/json", - Authorization: `Bearer ${getToken()}`, - }, - }); - const response = await fetch(url, { - method: "DELETE", + method: "POST", + body: JSON.stringify({ + id_data: id, + }), headers: { - "Content-Type": "application/json", Authorization: `Bearer ${getToken()}`, + "Content-Type": "application/json", }, }); window.location.href = `/dashboard`; - if (!responseData.ok && !response.ok) { - console.error(`une erreur lors de la suppression de l'ESP`); - console.error(await responseData.json()); + if (!response.ok) { + console.error( + `Une erreur est survenue lors de la suppression de l'ESP`, + ); console.error(await response.json()); + } else { + console.log("ESP supprimé avec succés"); } } catch (error) { console.error("Error: ", error);