diff --git a/components/mitra/actions/delete-data/delete-data.mjs b/components/mitra/actions/delete-data/delete-data.mjs new file mode 100644 index 0000000000000..cbb3a44d353b8 --- /dev/null +++ b/components/mitra/actions/delete-data/delete-data.mjs @@ -0,0 +1,49 @@ +import app from "../../mitra.app.mjs"; + +export default { + key: "mitra-delete-data", + name: "Delete Data", + description: "Deletes a record from a table in the Mitra database. [See the documentation]()", // Add documentation link if available + version: "0.0.1", + type: "action", + props: { + app, + tableName: { + propDefinition: [ + app, + "tableName", + ], + }, + dimensionContentId: { + type: "string", + label: "Dimension Content ID", + description: "The unique identifier of the record to delete.", + }, + }, + methods: { + deleteData({ + tableName, dimensionContentId, ...args + } = {}) { + return this.app.delete({ + tableName, + path: `/${dimensionContentId}`, + ...args, + }); + }, + }, + async run({ $ }) { + const { + deleteData, + tableName, + dimensionContentId, + } = this; + + const response = await deleteData({ + tableName, + dimensionContentId, + }); + + $.export("$summary", "Succesfully deleted record from the Mitra database."); + return response; + }, +}; diff --git a/components/mitra/actions/get-data/get-data.mjs b/components/mitra/actions/get-data/get-data.mjs new file mode 100644 index 0000000000000..d2dff9415bf3e --- /dev/null +++ b/components/mitra/actions/get-data/get-data.mjs @@ -0,0 +1,47 @@ +import mitra from "../../mitra.app.mjs"; + +export default { + key: "mitra-get-data", + name: "Get Data", + description: "Fetches data from the specified table, allowing dynamic filters via query parameters. [See the documentation](https://mitralab.io/docs/api)", + version: "0.0.1", + type: "action", + props: { + mitra, + tableName: { + propDefinition: [ + mitra, + "tableName", + ], + }, + params: { + type: "object", + label: "Query Parameters", + description: "Dynamic filters for querying records (e.g., `status`, `hours_gt`).", + }, + }, + methods: { + fetchData({ + tableName, ...args + } = {}) { + return this.app._makeRequest({ + tableName, + ...args, + }); + }, + }, + async run({ $ }) { + const { + fetchData, + tableName, + params, + } = this; + + const response = await fetchData({ + tableName, + params, + }); + $.export("$summary", "Succesfully fetched data from the database."); + return response; + }, +}; diff --git a/components/mitra/actions/insert-data/insert-data.mjs b/components/mitra/actions/insert-data/insert-data.mjs new file mode 100644 index 0000000000000..0235d097defda --- /dev/null +++ b/components/mitra/actions/insert-data/insert-data.mjs @@ -0,0 +1,51 @@ +import utils from "../../common/utils.mjs"; +import app from "../../mitra.app.mjs"; + +export default { + key: "mitra-insert-data", + name: "Insert Data", + description: "Inserts one or more records into a table in the Mitra database.", + version: "0.0.7", + type: "action", + props: { + app, + tableName: { + propDefinition: [ + app, + "tableName", + ], + }, + records: { + propDefinition: [ + app, + "records", + ], + }, + }, + methods: { + insertData({ + tableName, ...args + } = {}) { + return this.app.post({ + tableName, + ...args, + }); + }, + }, + async run({ $ }) { + const { + insertData, + tableName, + records, + } = this; + + const response = await insertData({ + $, + tableName, + data: utils.parseArray(records), + }); + + $.export("$summary", "Successfully inserted records into the Mitra database."); + return response; + }, +}; diff --git a/components/mitra/actions/update-data/update-data.mjs b/components/mitra/actions/update-data/update-data.mjs new file mode 100644 index 0000000000000..de7da27b1ef5b --- /dev/null +++ b/components/mitra/actions/update-data/update-data.mjs @@ -0,0 +1,51 @@ +import app from "../../mitra.app.mjs"; +import utils from "../../common/utils.mjs"; + +export default { + key: "mitra-update-data", + name: "Update Data", + description: "Updates one or more records in a table.", + version: "0.0.1", + type: "action", + props: { + app, + tableName: { + propDefinition: [ + app, + "tableName", + ], + }, + records: { + propDefinition: [ + app, + "records", + ], + }, + }, + methods: { + updateData({ + tableName, ...args + } = {}) { + return this.app.put({ + tableName, + ...args, + }); + }, + }, + async run({ $ }) { + const { + updateData, + tableName, + records, + } = this; + + const response = await updateData({ + $, + tableName, + data: utils.parseArray(records), + }); + + $.export("$summary", "Successfully updated records into the Mitra database."); + return response; + }, +}; diff --git a/components/mitra/common/utils.mjs b/components/mitra/common/utils.mjs new file mode 100644 index 0000000000000..e6ed4fa271055 --- /dev/null +++ b/components/mitra/common/utils.mjs @@ -0,0 +1,50 @@ +import { ConfigurationError } from "@pipedream/platform"; + +function isJson(value) { + try { + JSON.parse(value); + } catch (e) { + return false; + } + + return true; +} + +function valueToObject(value) { + if (typeof(value) === "object") { + return value; + } + + if (!isJson(value)) { + throw new ConfigurationError(`Make sure the custom expression contains a valid JSON object: \`${value}\``); + } + + return JSON.parse(value); +} + +function parseArray(value) { + try { + if (!value) { + return []; + } + + if (Array.isArray(value)) { + return value; + } + + const parsedValue = JSON.parse(value); + + if (!Array.isArray(parsedValue)) { + throw new Error("Not an array"); + } + + return parsedValue; + + } catch (e) { + throw new ConfigurationError("Make sure the custom expression contains a valid array object"); + } +} + +export default { + parseArray: (value) => parseArray(value).map(valueToObject), +}; diff --git a/components/mitra/mitra.app.mjs b/components/mitra/mitra.app.mjs index dca7dd079de66..c876a904ad3be 100644 --- a/components/mitra/mitra.app.mjs +++ b/components/mitra/mitra.app.mjs @@ -1,11 +1,66 @@ +import { axios } from "@pipedream/platform"; + export default { type: "app", app: "mitra", - propDefinitions: {}, + propDefinitions: { + tableName: { + type: "string", + label: "Table Name", + description: "The name of the table in the database in case it was not set in the app.", + optional: true, + }, + records: { + type: "string[]", + label: "Records", + description: "An array of records to insert or update, in JSON format.", + }, + }, methods: { - // this.$auth contains connected account data - authKeys() { - console.log(Object.keys(this.$auth)); + getUrl(tableName, path) { + const { + url, + table_name: defaultTableName, + } = this.$auth; + const baseUrl = `${url}/${tableName || defaultTableName}`; + return path + ? `${baseUrl}${path}` + : baseUrl; + }, + getHeaders(headers) { + return { + ...headers, + "Content-Type": "application/json", + "Authorization": `Bearer ${this.$auth.api_key}`, + }; + }, + _makeRequest({ + $ = this, path, headers, tableName, ...args + } = {}) { + return axios($, { + ...args, + debug: true, + url: this.getUrl(tableName, path), + headers: this.getHeaders(headers), + }); + }, + post(args = {}) { + return this._makeRequest({ + method: "POST", + ...args, + }); + }, + put(args = {}) { + return this._makeRequest({ + method: "PUT", + ...args, + }); + }, + delete(args = {}) { + return this._makeRequest({ + methot: "DELETE", + ...args, + }); }, }, };