2022-03-10 10:16:22 +13:00
|
|
|
import { save } from "../../api/controllers/row"
|
|
|
|
import { cleanUpRow, getError } from "../automationUtils"
|
|
|
|
import { buildCtx } from "./utils"
|
2020-09-17 01:00:04 +12:00
|
|
|
|
2022-03-10 10:16:22 +13:00
|
|
|
export const 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",
|
2021-09-15 22:51:49 +12:00
|
|
|
icon: "TableRowAddBottom",
|
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",
|
2021-09-14 23:40:19 +12:00
|
|
|
internal: true,
|
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",
|
2021-09-08 04:31:54 +12:00
|
|
|
description: "Whether the row creation was successful",
|
2020-09-17 01:00:04 +12:00
|
|
|
},
|
|
|
|
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"],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-03-10 10:16:22 +13:00
|
|
|
export async function run({ inputs, appId, emitter }: any) {
|
2020-10-10 07:10:28 +13:00
|
|
|
if (inputs.row == null || inputs.row.tableId == null) {
|
2021-03-12 07:29:48 +13:00
|
|
|
return {
|
|
|
|
success: false,
|
|
|
|
response: {
|
|
|
|
message: "Invalid inputs",
|
|
|
|
},
|
|
|
|
}
|
2020-09-17 01:00:04 +12:00
|
|
|
}
|
2020-10-10 07:10:28 +13:00
|
|
|
// have to clean up the row, remove the table from it
|
2022-03-10 10:16:22 +13:00
|
|
|
const ctx: any = buildCtx(appId, emitter, {
|
2021-09-21 06:21:04 +12:00
|
|
|
body: inputs.row,
|
2020-09-17 01:00:04 +12:00
|
|
|
params: {
|
2020-10-10 07:10:28 +13:00
|
|
|
tableId: inputs.row.tableId,
|
2020-09-17 01:00:04 +12:00
|
|
|
},
|
2021-09-21 06:21:04 +12:00
|
|
|
})
|
2020-09-17 01:00:04 +12:00
|
|
|
|
|
|
|
try {
|
2022-03-10 10:16:22 +13:00
|
|
|
inputs.row = await cleanUpRow(inputs.row.tableId, inputs.row)
|
2022-03-22 13:23:22 +13:00
|
|
|
await 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) {
|
|
|
|
return {
|
|
|
|
success: false,
|
2022-03-10 10:16:22 +13:00
|
|
|
response: getError(err),
|
2020-09-17 01:00:04 +12:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|