Skip to content

Commit

Permalink
fix: delete an ESP in the backend (#219)
Browse files Browse the repository at this point in the history
* feat: delete an ESP in the backend

* fix: the deleting function is now functional

Closes: #218
  • Loading branch information
ColinRgm authored Jan 14, 2025
1 parent efa7a36 commit 7320efc
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 20 deletions.
4 changes: 2 additions & 2 deletions database/migration/deploy/delete_esp_permission.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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;
23 changes: 23 additions & 0 deletions database/migration/deploy/new_way_delete_esp_permission.sql
Original file line number Diff line number Diff line change
@@ -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;
2 changes: 1 addition & 1 deletion database/migration/revert/add_new_permission.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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;
4 changes: 2 additions & 2 deletions database/migration/revert/delete_esp_permission.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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;
10 changes: 10 additions & 0 deletions database/migration/revert/new_way_delete_esp_permission.sql
Original file line number Diff line number Diff line change
@@ -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;
1 change: 1 addition & 0 deletions database/migration/sqitch.plan
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ esp 2024-06-26T08:20:01Z dylan <dylanbossoku@dylan> # create a esp table to stov
2024-07-10 2024-07-10T08:08:27Z Nils <nils@fedora> # Give the esp permission to know it's id
add_new_permission 2024-12-17T14:46:26Z Colin <colin@JT-24-11-07-Colin> # Add a permission for the user to edit the user's datas
delete_esp_permission 2025-01-07T08:18:34Z Colin <colin@JT-24-11-07-Colin> # Add a permission for the user to delete ESP
new_way_delete_esp_permission 2025-01-07T10:54:34Z Colin <colin@JT-24-11-07-Colin> # change the function to delete an ESP
7 changes: 7 additions & 0 deletions database/migration/verify/new_way_delete_esp_permission.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- Verify climat-guardian:new_way_delete_esp_permission on pg

BEGIN;

-- XXX Add verifications here.

ROLLBACK;
27 changes: 12 additions & 15 deletions nextjs-interface/src/app/ui/dashboard/DeleteEsp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 7320efc

Please sign in to comment.