Adding functionality so that when an automation trigger is deleted its test inputs will also be deleted.
This commit is contained in:
parent
8119a5f7f7
commit
0ff5fd1ef0
|
@ -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) {
|
||||
|
|
|
@ -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",
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue