Adding functionality so that when an automation trigger is deleted its test inputs will also be deleted.

This commit is contained in:
mike12345567 2021-09-13 18:03:09 +01:00
parent 8119a5f7f7
commit 0ff5fd1ef0
3 changed files with 56 additions and 17 deletions

View File

@ -7,6 +7,8 @@ const {
checkForWebhooks, checkForWebhooks,
updateTestHistory, updateTestHistory,
} = require("../../automations/utils") } = require("../../automations/utils")
const { deleteEntityMetadata } = require("../../utilities")
const { MetadataTypes } = require("../../constants")
const { setTestFlag, clearTestFlag } = require("../../utilities/redis") 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) { function cleanAutomationInputs(automation) {
if (automation == null) { if (automation == null) {
return automation return automation
@ -84,6 +99,23 @@ exports.update = async function (ctx) {
const response = await db.put(automation) const response = await db.put(automation)
automation._rev = response.rev 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.status = 200
ctx.body = { ctx.body = {
message: `Automation ${automation._id} updated successfully.`, message: `Automation ${automation._id} updated successfully.`,
@ -112,12 +144,15 @@ exports.find = async function (ctx) {
exports.destroy = async function (ctx) { exports.destroy = async function (ctx) {
const db = new CouchDB(ctx.appId) 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({ await checkForWebhooks({
appId: ctx.appId, appId: ctx.appId,
oldAuto: oldAutomation, 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) { exports.getActionList = async function (ctx) {

View File

@ -1,7 +1,7 @@
const { MetadataTypes } = require("../../constants") const { MetadataTypes } = require("../../constants")
const CouchDB = require("../../db") const CouchDB = require("../../db")
const { generateMetadataID } = require("../../db/utils") const { generateMetadataID } = require("../../db/utils")
const { saveEntityMetadata } = require("../../utilities") const { saveEntityMetadata, deleteEntityMetadata } = require("../../utilities")
exports.getTypes = async ctx => { exports.getTypes = async ctx => {
ctx.body = { ctx.body = {
@ -24,20 +24,7 @@ exports.saveMetadata = async ctx => {
exports.deleteMetadata = async ctx => { exports.deleteMetadata = async ctx => {
const { type, entityId } = ctx.params const { type, entityId } = ctx.params
const db = new CouchDB(ctx.appId) await deleteEntityMetadata(ctx.appId, type, entityId)
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)
}
ctx.body = { ctx.body = {
message: "Metadata deleted successfully", message: "Metadata deleted successfully",
} }

View File

@ -89,3 +89,20 @@ exports.saveEntityMetadata = async (appId, type, entityId, metadata) => {
return 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)
}
}