diff --git a/backend/openapi.yaml b/backend/openapi.yaml index bf39c24056..94a9882a4f 100644 --- a/backend/openapi.yaml +++ b/backend/openapi.yaml @@ -2547,6 +2547,23 @@ paths: schema: type: string + /w/{workspace}/schedules/delete/{path}: + delete: + summary: delete schedule + operationId: deleteSchedule + tags: + - schedule + parameters: + - $ref: "#/components/parameters/WorkspaceId" + - $ref: "#/components/parameters/Path" + responses: + "200": + description: schedule deleted + content: + text/plain: + schema: + type: string + /w/{workspace}/schedules/get/{path}: get: summary: get schedule diff --git a/backend/src/schedule.rs b/backend/src/schedule.rs index 8e32977aa1..45f7bb1e26 100644 --- a/backend/src/schedule.rs +++ b/backend/src/schedule.rs @@ -17,7 +17,7 @@ use crate::{ }; use axum::{ extract::{Extension, Path, Query}, - routing::{get, post}, + routing::{delete, get, post}, Json, Router, }; @@ -33,6 +33,7 @@ pub fn workspaced_service() -> Router { .route("/exists/*path", get(exists_schedule)) .route("/create", post(create_schedule)) .route("/update/*path", post(edit_schedule)) + .route("/delete/*path", delete(delete_schedule)) .route("/setenabled/*path", post(set_enabled)) } @@ -404,6 +405,38 @@ pub async fn set_enabled( )) } +async fn delete_schedule( + authed: Authed, + Extension(user_db): Extension, + Path((w_id, path)): Path<(String, StripPath)>, +) -> Result { + let path = path.to_path(); + let mut tx = user_db.begin(&authed).await?; + + sqlx::query!( + "DELETE FROM schedule WHERE path = $1 AND workspace_id = $2", + path, + w_id + ) + .execute(&mut tx) + .await?; + + audit_log( + &mut tx, + &authed.username, + "schedule.delete", + ActionKind::Delete, + &w_id, + Some(path), + None, + ) + .await?; + + tx.commit().await?; + + Ok(format!("schedule {} deleted", path)) +} + fn schedule_to_user(path: &str) -> String { format!("schedule-{}", path.replace('/', "-")) } diff --git a/backend/src/variables.rs b/backend/src/variables.rs index 7f21e3f413..d18ea63996 100644 --- a/backend/src/variables.rs +++ b/backend/src/variables.rs @@ -325,7 +325,6 @@ async fn create_variable( async fn delete_variable( authed: Authed, Extension(user_db): Extension, - Extension(db): Extension, Path((w_id, path)): Path<(String, StripPath)>, ) -> Result { let path = path.to_path(); @@ -336,7 +335,7 @@ async fn delete_variable( path, w_id ) - .execute(&db) + .execute(&mut tx) .await?; audit_log( &mut tx, diff --git a/frontend/src/routes/schedules.svelte b/frontend/src/routes/schedules.svelte index 49c473ca7e..06235e1713 100644 --- a/frontend/src/routes/schedules.svelte +++ b/frontend/src/routes/schedules.svelte @@ -13,7 +13,8 @@ faPlus, faShare, faToggleOff, - faToggleOn + faToggleOn, + faTrash } from '@fortawesome/free-solid-svg-icons' import { userStore, workspaceStore } from '$lib/stores' import CenteredPage from '$lib/components/CenteredPage.svelte' @@ -114,6 +115,18 @@ setScheduleEnabled(path, enabled ? false : true) } }, + { + displayName: 'Delete', + icon: faTrash, + disabled: !canWrite, + action: async () => { + await ScheduleService.deleteSchedule({ + workspace: $workspaceStore ?? '', + path + }) + loadSchedules() + } + }, { displayName: 'Edit', icon: faEdit,