From 0ff5fd1ef0aa38b053ad1fcf2adaaa8e6754f387 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Mon, 13 Sep 2021 18:03:09 +0100 Subject: [PATCH] Adding functionality so that when an automation trigger is deleted its test inputs will also be deleted. --- .../server/src/api/controllers/automation.js | 39 ++++++++++++++++++- .../server/src/api/controllers/metadata.js | 17 +------- packages/server/src/utilities/index.js | 17 ++++++++ 3 files changed, 56 insertions(+), 17 deletions(-) diff --git a/packages/server/src/api/controllers/automation.js b/packages/server/src/api/controllers/automation.js index b8f6cc7351..93e3f493f5 100644 --- a/packages/server/src/api/controllers/automation.js +++ b/packages/server/src/api/controllers/automation.js @@ -7,6 +7,8 @@ const { checkForWebhooks, updateTestHistory, } = require("../../automations/utils") +const { deleteEntityMetadata } = require("../../utilities") +const { MetadataTypes } = require("../../constants") const { setTestFlag, clearTestFlag } = require("../../utilities/redis") /************************* @@ -15,6 +17,19 @@ const { setTestFlag, clearTestFlag } = require("../../utilities/redis") * * *************************/ +async function cleanupAutomationMetadata(appId, automationId) { + await deleteEntityMetadata( + appId, + MetadataTypes.AUTOMATION_TEST_INPUT, + automationId + ) + await deleteEntityMetadata( + appId, + MetadataTypes.AUTOMATION_TEST_HISTORY, + automationId + ) +} + function cleanAutomationInputs(automation) { if (automation == null) { return automation @@ -84,6 +99,23 @@ exports.update = async function (ctx) { const response = await db.put(automation) automation._rev = response.rev + const oldAutoTrigger = + oldAutomation && oldAutomation.definition.trigger + ? oldAutomation.definition.trigger + : {} + const newAutoTrigger = + automation && automation.definition.trigger + ? automation.definition.trigger + : {} + // trigger has been updated, remove the test inputs + if (oldAutoTrigger.id !== newAutoTrigger.id) { + await deleteEntityMetadata( + ctx.appId, + MetadataTypes.AUTOMATION_TEST_INPUT, + automation._id + ) + } + ctx.status = 200 ctx.body = { message: `Automation ${automation._id} updated successfully.`, @@ -112,12 +144,15 @@ exports.find = async function (ctx) { exports.destroy = async function (ctx) { const db = new CouchDB(ctx.appId) - const oldAutomation = await db.get(ctx.params.id) + const automationId = ctx.params.id + const oldAutomation = await db.get(automationId) await checkForWebhooks({ appId: ctx.appId, oldAuto: oldAutomation, }) - ctx.body = await db.remove(ctx.params.id, ctx.params.rev) + // delete metadata first + await cleanupAutomationMetadata(ctx.appId, automationId) + ctx.body = await db.remove(automationId, ctx.params.rev) } exports.getActionList = async function (ctx) { diff --git a/packages/server/src/api/controllers/metadata.js b/packages/server/src/api/controllers/metadata.js index 7e098b6586..75236650fd 100644 --- a/packages/server/src/api/controllers/metadata.js +++ b/packages/server/src/api/controllers/metadata.js @@ -1,7 +1,7 @@ const { MetadataTypes } = require("../../constants") const CouchDB = require("../../db") const { generateMetadataID } = require("../../db/utils") -const { saveEntityMetadata } = require("../../utilities") +const { saveEntityMetadata, deleteEntityMetadata } = require("../../utilities") exports.getTypes = async ctx => { ctx.body = { @@ -24,20 +24,7 @@ exports.saveMetadata = async ctx => { exports.deleteMetadata = async ctx => { const { type, entityId } = ctx.params - const db = new CouchDB(ctx.appId) - const id = generateMetadataID(type, entityId) - let rev - try { - const metadata = await db.get(id) - if (metadata) { - rev = metadata._rev - } - } catch (err) { - // don't need to error if it doesn't exist - } - if (id && rev) { - await db.remove(id, rev) - } + await deleteEntityMetadata(ctx.appId, type, entityId) ctx.body = { message: "Metadata deleted successfully", } diff --git a/packages/server/src/utilities/index.js b/packages/server/src/utilities/index.js index 114e1bd5e6..a81f9ddcf5 100644 --- a/packages/server/src/utilities/index.js +++ b/packages/server/src/utilities/index.js @@ -89,3 +89,20 @@ exports.saveEntityMetadata = async (appId, type, entityId, metadata) => { return metadata }) } + +exports.deleteEntityMetadata = async (appId, type, entityId) => { + const db = new CouchDB(appId) + const id = generateMetadataID(type, entityId) + let rev + try { + const metadata = await db.get(id) + if (metadata) { + rev = metadata._rev + } + } catch (err) { + // don't need to error if it doesn't exist + } + if (id && rev) { + await db.remove(id, rev) + } +}