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-10-29 09:35:06 +13:00
|
|
|
const env = require("../../environment")
|
2020-10-09 05:34:41 +13:00
|
|
|
const usage = require("../../utilities/usageQuota")
|
2020-09-17 01:00:04 +12:00
|
|
|
|
|
|
|
module.exports.definition = {
|
2020-09-26 02:01:48 +12:00
|
|
|
name: "Create Row",
|
2020-10-10 06:49:23 +13:00
|
|
|
tagline: "Create a {{inputs.enriched.table.name}} row",
|
2020-10-28 01:33:25 +13:00
|
|
|
icon: "ri-save-3-line",
|
2020-09-26 02:01:48 +12:00
|
|
|
description: "Add a row to your database",
|
2020-09-17 01:00:04 +12:00
|
|
|
type: "ACTION",
|
2020-10-10 07:10:28 +13:00
|
|
|
stepId: "CREATE_ROW",
|
2020-09-17 01:00:04 +12:00
|
|
|
inputs: {},
|
|
|
|
schema: {
|
|
|
|
inputs: {
|
|
|
|
properties: {
|
2020-10-10 07:10:28 +13:00
|
|
|
row: {
|
2020-09-17 01:00:04 +12:00
|
|
|
type: "object",
|
|
|
|
properties: {
|
2020-10-10 06:49:23 +13:00
|
|
|
tableId: {
|
2020-09-17 01:00:04 +12:00
|
|
|
type: "string",
|
2020-10-10 06:49:23 +13:00
|
|
|
customType: "table",
|
2020-09-17 01:00:04 +12:00
|
|
|
},
|
|
|
|
},
|
2020-10-10 07:10:28 +13:00
|
|
|
customType: "row",
|
2020-09-18 01:04:13 +12:00
|
|
|
title: "Table",
|
2020-10-10 06:49:23 +13:00
|
|
|
required: ["tableId"],
|
2020-09-17 01:00:04 +12:00
|
|
|
},
|
|
|
|
},
|
2020-10-10 07:10:28 +13:00
|
|
|
required: ["row"],
|
2020-09-17 01:00:04 +12:00
|
|
|
},
|
|
|
|
outputs: {
|
|
|
|
properties: {
|
2020-10-10 07:10:28 +13:00
|
|
|
row: {
|
2020-09-19 04:20:19 +12:00
|
|
|
type: "object",
|
2020-10-10 07:10:28 +13:00
|
|
|
customType: "row",
|
2020-09-26 02:01:48 +12:00
|
|
|
description: "The new row",
|
2020-09-19 04:20:19 +12:00
|
|
|
},
|
2020-09-17 01:00:04 +12:00
|
|
|
response: {
|
|
|
|
type: "object",
|
|
|
|
description: "The response from the table",
|
|
|
|
},
|
|
|
|
success: {
|
|
|
|
type: "boolean",
|
|
|
|
description: "Whether the action was successful",
|
|
|
|
},
|
|
|
|
id: {
|
|
|
|
type: "string",
|
2020-09-26 02:01:48 +12:00
|
|
|
description: "The identifier of the new row",
|
2020-09-17 01:00:04 +12:00
|
|
|
},
|
|
|
|
revision: {
|
|
|
|
type: "string",
|
2020-09-26 02:01:48 +12:00
|
|
|
description: "The revision of the new row",
|
2020-09-17 01:00:04 +12:00
|
|
|
},
|
|
|
|
},
|
|
|
|
required: ["success", "id", "revision"],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-12-08 06:23:53 +13:00
|
|
|
module.exports.run = async function({ inputs, appId, apiKey, emitter }) {
|
2020-09-17 01:00:04 +12:00
|
|
|
// TODO: better logging of when actions are missed due to missing parameters
|
2020-10-10 07:10:28 +13:00
|
|
|
if (inputs.row == null || inputs.row.tableId == null) {
|
2020-09-17 01:00:04 +12:00
|
|
|
return
|
|
|
|
}
|
2020-10-10 07:10:28 +13:00
|
|
|
inputs.row = await automationUtils.cleanUpRow(
|
2020-10-29 23:28:27 +13:00
|
|
|
appId,
|
2020-10-10 07:10:28 +13:00
|
|
|
inputs.row.tableId,
|
|
|
|
inputs.row
|
2020-09-24 00:34:11 +12:00
|
|
|
)
|
2020-10-10 07:10:28 +13:00
|
|
|
// have to clean up the row, remove the table from it
|
2020-09-17 01:00:04 +12:00
|
|
|
const ctx = {
|
|
|
|
params: {
|
2020-10-10 07:10:28 +13:00
|
|
|
tableId: inputs.row.tableId,
|
2020-09-17 01:00:04 +12:00
|
|
|
},
|
|
|
|
request: {
|
2020-10-10 07:10:28 +13:00
|
|
|
body: inputs.row,
|
2020-09-17 01:00:04 +12:00
|
|
|
},
|
2020-10-29 23:28:27 +13:00
|
|
|
user: { appId },
|
2020-12-08 06:23:53 +13:00
|
|
|
eventEmitter: emitter,
|
2020-09-17 01:00:04 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2020-10-29 09:35:06 +13:00
|
|
|
if (env.CLOUD) {
|
2020-10-10 07:10:28 +13:00
|
|
|
await usage.update(apiKey, usage.Properties.ROW, 1)
|
2020-10-09 05:34:41 +13:00
|
|
|
}
|
2020-10-10 07:10:28 +13:00
|
|
|
await rowController.save(ctx)
|
2020-09-17 01:00:04 +12:00
|
|
|
return {
|
2020-10-10 07:10:28 +13:00
|
|
|
row: inputs.row,
|
2020-09-17 01:00:04 +12:00
|
|
|
response: ctx.body,
|
|
|
|
id: ctx.body._id,
|
|
|
|
revision: ctx.body._rev,
|
|
|
|
success: ctx.status === 200,
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err)
|
|
|
|
return {
|
|
|
|
success: false,
|
|
|
|
response: err,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|