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")
|
2021-09-21 06:21:04 +12:00
|
|
|
const { buildCtx } = require("./utils")
|
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
|
|
|
}
|
2021-10-20 05:00:54 +13:00
|
|
|
const tableId = inputs.row.tableId
|
2020-09-23 23:29:20 +12:00
|
|
|
|
2022-10-03 21:34:06 +13:00
|
|
|
// clear any undefined, null or empty string properties so that they aren't updated
|
2020-10-10 07:10:28 +13:00
|
|
|
for (let propKey of Object.keys(inputs.row)) {
|
2022-10-03 21:34:06 +13:00
|
|
|
if (inputs.row[propKey] == null || inputs.row[propKey] === "") {
|
2020-10-10 07:10:28 +13:00
|
|
|
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
|
2021-09-21 06:21:04 +12:00
|
|
|
const ctx = buildCtx(appId, emitter, {
|
|
|
|
body: {
|
|
|
|
...inputs.row,
|
|
|
|
_id: inputs.rowId,
|
|
|
|
},
|
2020-09-23 23:29:20 +12:00
|
|
|
params: {
|
2021-03-13 04:32:16 +13:00
|
|
|
rowId: inputs.rowId,
|
2021-10-20 05:00:54 +13:00
|
|
|
tableId: tableId,
|
2020-09-23 23:29:20 +12:00
|
|
|
},
|
2021-09-21 06:21:04 +12:00
|
|
|
})
|
2020-09-23 23:29:20 +12:00
|
|
|
|
|
|
|
try {
|
2021-10-20 05:00:54 +13:00
|
|
|
if (tableId) {
|
2022-01-28 07:18:31 +13:00
|
|
|
inputs.row = await automationUtils.cleanUpRow(tableId, inputs.row)
|
2021-10-20 05:00:54 +13:00
|
|
|
}
|
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,
|
2021-10-20 05:00:54 +13:00
|
|
|
response: automationUtils.getError(err),
|
2020-09-23 23:29:20 +12:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|