fix: check automation usage in all rundowns before allowing delete

This commit is contained in:
Carlos Valente
2025-09-21 20:31:24 +02:00
committed by Alex Christoffer Rasmussen
parent cbd7bce106
commit bcabc50ad1
6 changed files with 178 additions and 75 deletions
@@ -2,9 +2,8 @@ import type {
Automation,
AutomationDTO,
AutomationSettings,
EntryId,
NormalisedAutomation,
Rundown,
ProjectRundowns,
Trigger,
TriggerDTO,
} from 'ontime-types';
@@ -136,15 +135,15 @@ export async function editAutomation(id: string, newAutomation: AutomationDTO):
/**
* Deletes a automation given its ID
*/
export async function deleteAutomation(rundown: Rundown, timedEventOrder: EntryId[], id: string): Promise<void> {
export async function deleteAutomation(projectRundowns: ProjectRundowns, automationId: string): Promise<void> {
const automations = getAutomations();
// ignore request if automation does not exist
if (!Object.hasOwn(automations, id)) {
if (!Object.hasOwn(automations, automationId)) {
return;
}
// prevent deleting a automation that is in use in triggers
const triggers = getAutomationTriggers().filter((trigger) => trigger.automationId === id);
const triggers = getAutomationTriggers().filter((trigger) => trigger.automationId === automationId);
if (triggers.length) {
throw new Error(
`Unable to delete automation used in trigger ${triggers[0].title}${triggers.length > 1 ? ` and ${triggers.length - 1} more` : ''}`,
@@ -152,12 +151,12 @@ export async function deleteAutomation(rundown: Rundown, timedEventOrder: EntryI
}
// prevent deleting a automation that is in use in events
const isInUse = isAutomationUsed(rundown, timedEventOrder, id);
const isInUse = isAutomationUsed(projectRundowns, automationId);
if (isInUse) {
throw new Error(`Unable to delete automation used in event with ID ${isInUse}`);
throw new Error(`Unable to delete automation used in rundown: ${isInUse[0]}, in event with ID: ${isInUse[1]}`);
}
delete automations[id];
delete automations[automationId];
await saveChanges({ automations });
}