Update to include update record internal action option, seems very useful rather than having to delete and re-create records every time.
This commit is contained in:
parent
8afbb40f36
commit
7f107fcf0a
|
@ -3,18 +3,18 @@ const validateJs = require("validate.js")
|
|||
const newid = require("../../db/newid")
|
||||
|
||||
function emitEvent(eventType, ctx, record) {
|
||||
// add syntactic sugar for mustache later
|
||||
if (record._id) {
|
||||
record.id = record._id
|
||||
}
|
||||
if (record._rev) {
|
||||
record.revision = record._rev
|
||||
}
|
||||
ctx.eventEmitter &&
|
||||
ctx.eventEmitter.emit(eventType, {
|
||||
let event = {
|
||||
record,
|
||||
instanceId: ctx.user.instanceId,
|
||||
})
|
||||
}
|
||||
// add syntactic sugar for mustache later
|
||||
if (record._id) {
|
||||
event.id = record._id
|
||||
}
|
||||
if (record._rev) {
|
||||
event.revision = record._rev
|
||||
}
|
||||
ctx.eventEmitter && ctx.eventEmitter.emit(eventType, event)
|
||||
}
|
||||
|
||||
validateJs.extend(validateJs.validators.datetime, {
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
const sendEmail = require("./steps/sendEmail")
|
||||
const saveRecord = require("./steps/saveRecord")
|
||||
const updateRecord = require("./steps/updateRecord")
|
||||
const deleteRecord = require("./steps/deleteRecord")
|
||||
const createUser = require("./steps/createUser")
|
||||
const environment = require("../environment")
|
||||
|
@ -17,12 +18,14 @@ const AUTOMATION_MANIFEST = "manifest.json"
|
|||
const BUILTIN_ACTIONS = {
|
||||
SEND_EMAIL: sendEmail.run,
|
||||
SAVE_RECORD: saveRecord.run,
|
||||
UPDATE_RECORD: updateRecord.run,
|
||||
DELETE_RECORD: deleteRecord.run,
|
||||
CREATE_USER: createUser.run,
|
||||
}
|
||||
const BUILTIN_DEFINITIONS = {
|
||||
SEND_EMAIL: sendEmail.definition,
|
||||
SAVE_RECORD: saveRecord.definition,
|
||||
UPDATE_RECORD: updateRecord.definition,
|
||||
DELETE_RECORD: deleteRecord.definition,
|
||||
CREATE_USER: createUser.definition,
|
||||
}
|
||||
|
|
|
@ -0,0 +1,94 @@
|
|||
const recordController = require("../../api/controllers/record")
|
||||
|
||||
module.exports.definition = {
|
||||
name: "Update Record",
|
||||
tagline: "Update a {{inputs.enriched.model.name}} record",
|
||||
icon: "ri-refresh-fill",
|
||||
description: "Update a record to your database",
|
||||
type: "ACTION",
|
||||
stepId: "UPDATE_RECORD",
|
||||
inputs: {},
|
||||
schema: {
|
||||
inputs: {
|
||||
properties: {
|
||||
record: {
|
||||
type: "object",
|
||||
customType: "record",
|
||||
title: "Record",
|
||||
},
|
||||
recordId: {
|
||||
type: "string",
|
||||
title: "Record ID",
|
||||
},
|
||||
},
|
||||
required: ["record", "recordId"],
|
||||
},
|
||||
outputs: {
|
||||
properties: {
|
||||
record: {
|
||||
type: "object",
|
||||
customType: "record",
|
||||
description: "The updated record",
|
||||
},
|
||||
response: {
|
||||
type: "object",
|
||||
description: "The response from the table",
|
||||
},
|
||||
success: {
|
||||
type: "boolean",
|
||||
description: "Whether the action was successful",
|
||||
},
|
||||
id: {
|
||||
type: "string",
|
||||
description: "The identifier of the updated record",
|
||||
},
|
||||
revision: {
|
||||
type: "string",
|
||||
description: "The revision of the updated record",
|
||||
},
|
||||
},
|
||||
required: ["success", "id", "revision"],
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
module.exports.run = async function({ inputs, instanceId }) {
|
||||
if (inputs.recordId == null || inputs.record == null) {
|
||||
return
|
||||
}
|
||||
|
||||
// clear any falsy properties so that they aren't updated
|
||||
for (let propKey of Object.keys(inputs.record)) {
|
||||
if (!inputs.record[propKey] || inputs.record[propKey] === "") {
|
||||
delete inputs.record[propKey]
|
||||
}
|
||||
}
|
||||
|
||||
// have to clean up the record, remove the model from it
|
||||
const ctx = {
|
||||
params: {
|
||||
id: inputs.recordId,
|
||||
},
|
||||
request: {
|
||||
body: inputs.record,
|
||||
},
|
||||
user: { instanceId },
|
||||
}
|
||||
|
||||
try {
|
||||
await recordController.patch(ctx)
|
||||
return {
|
||||
record: ctx.body,
|
||||
response: ctx.message,
|
||||
id: ctx.body._id,
|
||||
revision: ctx.body._rev,
|
||||
success: ctx.status === 200,
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
return {
|
||||
success: false,
|
||||
response: err,
|
||||
}
|
||||
}
|
||||
}
|
|
@ -109,6 +109,7 @@ class Orchestrator {
|
|||
step.inputs = recurseMustache(step.inputs, this._context)
|
||||
step.inputs = cleanInputValue(step.inputs, step.schema.inputs)
|
||||
// instanceId is always passed
|
||||
try {
|
||||
const outputs = await stepFn({
|
||||
inputs: step.inputs,
|
||||
instanceId: this._instanceId,
|
||||
|
@ -117,6 +118,9 @@ class Orchestrator {
|
|||
break
|
||||
}
|
||||
this._context.steps.push(outputs)
|
||||
} catch (err) {
|
||||
console.error(`Automation error - ${step.stepId} - ${err}`)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -36,8 +36,16 @@ const BUILTIN_DEFINITIONS = {
|
|||
customType: "record",
|
||||
description: "The new record that was saved",
|
||||
},
|
||||
id: {
|
||||
type: "string",
|
||||
description: "Record ID - can be used for updating",
|
||||
},
|
||||
required: ["record"],
|
||||
revision: {
|
||||
type: "string",
|
||||
description: "Revision of record",
|
||||
},
|
||||
},
|
||||
required: ["record", "id"],
|
||||
},
|
||||
},
|
||||
type: "TRIGGER",
|
||||
|
@ -69,7 +77,7 @@ const BUILTIN_DEFINITIONS = {
|
|||
description: "The record that was deleted",
|
||||
},
|
||||
},
|
||||
required: ["record"],
|
||||
required: ["record", "id"],
|
||||
},
|
||||
},
|
||||
type: "TRIGGER",
|
||||
|
|
Loading…
Reference in New Issue