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")
|
const newid = require("../../db/newid")
|
||||||
|
|
||||||
function emitEvent(eventType, ctx, record) {
|
function emitEvent(eventType, ctx, record) {
|
||||||
|
let event = {
|
||||||
|
record,
|
||||||
|
instanceId: ctx.user.instanceId,
|
||||||
|
}
|
||||||
// add syntactic sugar for mustache later
|
// add syntactic sugar for mustache later
|
||||||
if (record._id) {
|
if (record._id) {
|
||||||
record.id = record._id
|
event.id = record._id
|
||||||
}
|
}
|
||||||
if (record._rev) {
|
if (record._rev) {
|
||||||
record.revision = record._rev
|
event.revision = record._rev
|
||||||
}
|
}
|
||||||
ctx.eventEmitter &&
|
ctx.eventEmitter && ctx.eventEmitter.emit(eventType, event)
|
||||||
ctx.eventEmitter.emit(eventType, {
|
|
||||||
record,
|
|
||||||
instanceId: ctx.user.instanceId,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
validateJs.extend(validateJs.validators.datetime, {
|
validateJs.extend(validateJs.validators.datetime, {
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
const sendEmail = require("./steps/sendEmail")
|
const sendEmail = require("./steps/sendEmail")
|
||||||
const saveRecord = require("./steps/saveRecord")
|
const saveRecord = require("./steps/saveRecord")
|
||||||
|
const updateRecord = require("./steps/updateRecord")
|
||||||
const deleteRecord = require("./steps/deleteRecord")
|
const deleteRecord = require("./steps/deleteRecord")
|
||||||
const createUser = require("./steps/createUser")
|
const createUser = require("./steps/createUser")
|
||||||
const environment = require("../environment")
|
const environment = require("../environment")
|
||||||
|
@ -17,12 +18,14 @@ const AUTOMATION_MANIFEST = "manifest.json"
|
||||||
const BUILTIN_ACTIONS = {
|
const BUILTIN_ACTIONS = {
|
||||||
SEND_EMAIL: sendEmail.run,
|
SEND_EMAIL: sendEmail.run,
|
||||||
SAVE_RECORD: saveRecord.run,
|
SAVE_RECORD: saveRecord.run,
|
||||||
|
UPDATE_RECORD: updateRecord.run,
|
||||||
DELETE_RECORD: deleteRecord.run,
|
DELETE_RECORD: deleteRecord.run,
|
||||||
CREATE_USER: createUser.run,
|
CREATE_USER: createUser.run,
|
||||||
}
|
}
|
||||||
const BUILTIN_DEFINITIONS = {
|
const BUILTIN_DEFINITIONS = {
|
||||||
SEND_EMAIL: sendEmail.definition,
|
SEND_EMAIL: sendEmail.definition,
|
||||||
SAVE_RECORD: saveRecord.definition,
|
SAVE_RECORD: saveRecord.definition,
|
||||||
|
UPDATE_RECORD: updateRecord.definition,
|
||||||
DELETE_RECORD: deleteRecord.definition,
|
DELETE_RECORD: deleteRecord.definition,
|
||||||
CREATE_USER: createUser.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,14 +109,18 @@ class Orchestrator {
|
||||||
step.inputs = recurseMustache(step.inputs, this._context)
|
step.inputs = recurseMustache(step.inputs, this._context)
|
||||||
step.inputs = cleanInputValue(step.inputs, step.schema.inputs)
|
step.inputs = cleanInputValue(step.inputs, step.schema.inputs)
|
||||||
// instanceId is always passed
|
// instanceId is always passed
|
||||||
const outputs = await stepFn({
|
try {
|
||||||
inputs: step.inputs,
|
const outputs = await stepFn({
|
||||||
instanceId: this._instanceId,
|
inputs: step.inputs,
|
||||||
})
|
instanceId: this._instanceId,
|
||||||
if (step.stepId === FILTER_STEP_ID && !outputs.success) {
|
})
|
||||||
break
|
if (step.stepId === FILTER_STEP_ID && !outputs.success) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
this._context.steps.push(outputs)
|
||||||
|
} catch (err) {
|
||||||
|
console.error(`Automation error - ${step.stepId} - ${err}`)
|
||||||
}
|
}
|
||||||
this._context.steps.push(outputs)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,8 +36,16 @@ const BUILTIN_DEFINITIONS = {
|
||||||
customType: "record",
|
customType: "record",
|
||||||
description: "The new record that was saved",
|
description: "The new record that was saved",
|
||||||
},
|
},
|
||||||
|
id: {
|
||||||
|
type: "string",
|
||||||
|
description: "Record ID - can be used for updating",
|
||||||
|
},
|
||||||
|
revision: {
|
||||||
|
type: "string",
|
||||||
|
description: "Revision of record",
|
||||||
|
},
|
||||||
},
|
},
|
||||||
required: ["record"],
|
required: ["record", "id"],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
type: "TRIGGER",
|
type: "TRIGGER",
|
||||||
|
@ -69,7 +77,7 @@ const BUILTIN_DEFINITIONS = {
|
||||||
description: "The record that was deleted",
|
description: "The record that was deleted",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
required: ["record"],
|
required: ["record", "id"],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
type: "TRIGGER",
|
type: "TRIGGER",
|
||||||
|
|
Loading…
Reference in New Issue