2020-09-23 14:34:11 +02:00
|
|
|
const CouchDB = require("../db")
|
|
|
|
|
2020-09-23 17:16:24 +02:00
|
|
|
/**
|
|
|
|
* When values are input to the system generally they will be of type string as this is required for mustache. This can
|
|
|
|
* generate some odd scenarios as the Schema of the automation requires a number but the builder might supply a string
|
|
|
|
* with mustache syntax to get the number from the rest of the context. To support this the server has to make sure that
|
|
|
|
* the post mustache statement can be cast into the correct type, this function does this for numbers and booleans.
|
|
|
|
*
|
|
|
|
* @param {object} inputs An object of inputs, please note this will not recurse down into any objects within, it simply
|
|
|
|
* cleanses the top level inputs, however it can be used by recursively calling it deeper into the object structures if
|
|
|
|
* the schema is known.
|
|
|
|
* @param {object} schema The defined schema of the inputs, in the form of JSON schema. The schema definition of an
|
|
|
|
* automation is the likely use case of this, however validate.js syntax can be converted closely enough to use this by
|
|
|
|
* wrapping the schema properties in a top level "properties" object.
|
|
|
|
* @returns {object} The inputs object which has had all the various types supported by this function converted to their
|
|
|
|
* primitive types.
|
|
|
|
*/
|
2020-09-23 14:34:11 +02:00
|
|
|
module.exports.cleanInputValues = (inputs, schema) => {
|
|
|
|
if (schema == null) {
|
|
|
|
return inputs
|
|
|
|
}
|
|
|
|
for (let inputKey of Object.keys(inputs)) {
|
|
|
|
let input = inputs[inputKey]
|
|
|
|
if (typeof input !== "string") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
let propSchema = schema.properties[inputKey]
|
|
|
|
if (!propSchema) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if (propSchema.type === "boolean") {
|
|
|
|
let lcInput = input.toLowerCase()
|
|
|
|
if (lcInput === "true") {
|
|
|
|
inputs[inputKey] = true
|
|
|
|
}
|
|
|
|
if (lcInput === "false") {
|
|
|
|
inputs[inputKey] = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (propSchema.type === "number") {
|
|
|
|
let floatInput = parseFloat(input)
|
|
|
|
if (!isNaN(floatInput)) {
|
|
|
|
inputs[inputKey] = floatInput
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return inputs
|
|
|
|
}
|
|
|
|
|
2020-09-23 17:16:24 +02:00
|
|
|
/**
|
2020-10-09 20:10:28 +02:00
|
|
|
* Given a row input like a save or update row we need to clean the inputs against a schema that is not part of
|
2020-10-09 19:49:23 +02:00
|
|
|
* the automation but is instead part of the Table/Table. This function will get the table schema and use it to instead
|
2020-10-09 20:10:28 +02:00
|
|
|
* perform the cleanInputValues function on the input row.
|
2020-09-23 17:16:24 +02:00
|
|
|
*
|
2020-10-29 11:28:27 +01:00
|
|
|
* @param {string} appId The instance which the Table/Table is contained under.
|
2020-10-09 19:49:23 +02:00
|
|
|
* @param {string} tableId The ID of the Table/Table which the schema is to be retrieved for.
|
2020-10-09 20:10:28 +02:00
|
|
|
* @param {object} row The input row structure which requires clean-up after having been through mustache statements.
|
|
|
|
* @returns {Promise<Object>} The cleaned up rows object, will should now have all the required primitive types.
|
2020-09-23 17:16:24 +02:00
|
|
|
*/
|
2020-10-29 11:28:27 +01:00
|
|
|
module.exports.cleanUpRow = async (appId, tableId, row) => {
|
|
|
|
const db = new CouchDB(appId)
|
2020-10-09 19:49:23 +02:00
|
|
|
const table = await db.get(tableId)
|
2020-09-23 14:34:11 +02:00
|
|
|
|
2020-10-09 20:10:28 +02:00
|
|
|
return module.exports.cleanInputValues(row, { properties: table.schema })
|
2020-09-23 14:34:11 +02:00
|
|
|
}
|
|
|
|
|
2020-09-23 17:16:24 +02:00
|
|
|
/**
|
2020-10-09 20:10:28 +02:00
|
|
|
* A utility function for the cleanUpRow, which can be used if only the row ID is known (not the table ID) to clean
|
|
|
|
* up a row after mustache statements have been replaced. This is specifically useful for the update row action.
|
2020-09-23 17:20:39 +02:00
|
|
|
*
|
2020-10-29 11:28:27 +01:00
|
|
|
* @param {string} appId The instance which the Table/Table is contained under.
|
2020-10-09 20:10:28 +02:00
|
|
|
* @param {string} rowId The ID of the row from which the tableId will be extracted, to get the Table/Table schema.
|
|
|
|
* @param {object} row The input row structure which requires clean-up after having been through mustache statements.
|
|
|
|
* @returns {Promise<Object>} The cleaned up rows object, which will now have all the required primitive types.
|
2020-09-23 17:16:24 +02:00
|
|
|
*/
|
2020-10-29 11:28:27 +01:00
|
|
|
module.exports.cleanUpRowById = async (appId, rowId, row) => {
|
|
|
|
const db = new CouchDB(appId)
|
2020-10-09 20:10:28 +02:00
|
|
|
const foundRow = await db.get(rowId)
|
2020-10-29 11:28:27 +01:00
|
|
|
return module.exports.cleanUpRow(appId, foundRow.tableId, row)
|
2020-09-23 14:34:11 +02:00
|
|
|
}
|