2020-10-10 07:10:28 +13:00
|
|
|
const rowController = require("../../api/controllers/row")
|
2020-09-24 00:34:11 +12:00
|
|
|
const automationUtils = require("../automationUtils")
|
2020-09-23 23:29:20 +12:00
|
|
|
|
2021-09-07 04:53:02 +12:00
|
|
|
exports.definition = {
|
2020-09-26 02:01:48 +12:00
|
|
|
name: "Update Row",
|
2020-10-10 07:10:28 +13:00
|
|
|
tagline: "Update a {{inputs.enriched.table.name}} row",
|
2021-09-08 01:32:43 +12:00
|
|
|
icon: "Refresh",
|
2020-09-26 02:01:48 +12:00
|
|
|
description: "Update a row in your database",
|
2020-09-23 23:29:20 +12:00
|
|
|
type: "ACTION",
|
2021-09-14 23:40:19 +12:00
|
|
|
internal: true,
|
2020-10-10 07:10:28 +13:00
|
|
|
stepId: "UPDATE_ROW",
|
2020-09-23 23:29:20 +12:00
|
|
|
inputs: {},
|
|
|
|
schema: {
|
|
|
|
inputs: {
|
|
|
|
properties: {
|
2020-10-10 07:10:28 +13:00
|
|
|
row: {
|
2020-09-23 23:29:20 +12:00
|
|
|
type: "object",
|
2020-10-10 07:10:28 +13:00
|
|
|
customType: "row",
|
2020-09-26 02:01:48 +12:00
|
|
|
title: "Table",
|
2020-09-23 23:29:20 +12:00
|
|
|
},
|
2020-10-10 07:10:28 +13:00
|
|
|
rowId: {
|
2020-09-23 23:29:20 +12:00
|
|
|
type: "string",
|
2020-09-26 02:01:48 +12:00
|
|
|
title: "Row ID",
|
2020-09-23 23:29:20 +12:00
|
|
|
},
|
|
|
|
},
|
2020-10-10 07:10:28 +13:00
|
|
|
required: ["row", "rowId"],
|
2020-09-23 23:29:20 +12:00
|
|
|
},
|
|
|
|
outputs: {
|
|
|
|
properties: {
|
2020-10-10 07:10:28 +13:00
|
|
|
row: {
|
2020-09-23 23:29:20 +12:00
|
|
|
type: "object",
|
2020-10-10 07:10:28 +13:00
|
|
|
customType: "row",
|
|
|
|
description: "The updated row",
|
2020-09-23 23:29:20 +12:00
|
|
|
},
|
|
|
|
response: {
|
|
|
|
type: "object",
|
|
|
|
description: "The response from the table",
|
|
|
|
},
|
|
|
|
success: {
|
|
|
|
type: "boolean",
|
|
|
|
description: "Whether the action was successful",
|
|
|
|
},
|
|
|
|
id: {
|
|
|
|
type: "string",
|
2020-10-10 07:10:28 +13:00
|
|
|
description: "The identifier of the updated row",
|
2020-09-23 23:29:20 +12:00
|
|
|
},
|
|
|
|
revision: {
|
|
|
|
type: "string",
|
2020-10-10 07:10:28 +13:00
|
|
|
description: "The revision of the updated row",
|
2020-09-23 23:29:20 +12:00
|
|
|
},
|
|
|
|
},
|
|
|
|
required: ["success", "id", "revision"],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-09-07 04:53:02 +12:00
|
|
|
exports.run = async function ({ inputs, appId, emitter }) {
|
2020-10-10 07:10:28 +13:00
|
|
|
if (inputs.rowId == null || inputs.row == null) {
|
2021-03-13 04:32:16 +13:00
|
|
|
return {
|
|
|
|
success: false,
|
|
|
|
response: {
|
|
|
|
message: "Invalid inputs",
|
|
|
|
},
|
|
|
|
}
|
2020-09-23 23:29:20 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
// clear any falsy properties so that they aren't updated
|
2020-10-10 07:10:28 +13:00
|
|
|
for (let propKey of Object.keys(inputs.row)) {
|
|
|
|
if (!inputs.row[propKey] || inputs.row[propKey] === "") {
|
|
|
|
delete inputs.row[propKey]
|
2020-09-23 23:29:20 +12:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-10 07:10:28 +13:00
|
|
|
// have to clean up the row, remove the table from it
|
2020-09-23 23:29:20 +12:00
|
|
|
const ctx = {
|
|
|
|
params: {
|
2021-03-13 04:32:16 +13:00
|
|
|
rowId: inputs.rowId,
|
2020-09-23 23:29:20 +12:00
|
|
|
},
|
|
|
|
request: {
|
2021-07-17 06:13:44 +12:00
|
|
|
body: {
|
|
|
|
...inputs.row,
|
|
|
|
_id: inputs.rowId,
|
|
|
|
},
|
2020-09-23 23:29:20 +12:00
|
|
|
},
|
2021-04-10 04:33:21 +12:00
|
|
|
appId,
|
2020-12-08 06:23:53 +13:00
|
|
|
eventEmitter: emitter,
|
2020-09-23 23:29:20 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2021-03-13 04:32:16 +13:00
|
|
|
inputs.row = await automationUtils.cleanUpRowById(
|
|
|
|
appId,
|
|
|
|
inputs.rowId,
|
|
|
|
inputs.row
|
|
|
|
)
|
2020-10-10 07:10:28 +13:00
|
|
|
await rowController.patch(ctx)
|
2020-09-23 23:29:20 +12:00
|
|
|
return {
|
2020-10-10 07:10:28 +13:00
|
|
|
row: ctx.body,
|
2020-09-23 23:29:20 +12:00
|
|
|
response: ctx.message,
|
|
|
|
id: ctx.body._id,
|
|
|
|
revision: ctx.body._rev,
|
|
|
|
success: ctx.status === 200,
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
return {
|
|
|
|
success: false,
|
|
|
|
response: err,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|