diff --git a/components/_1crm/_1crm.app.mjs b/components/_1crm/_1crm.app.mjs index 9a5bbb46ebc7f..17f48569cf333 100644 --- a/components/_1crm/_1crm.app.mjs +++ b/components/_1crm/_1crm.app.mjs @@ -1,11 +1,83 @@ +import { axios } from "@pipedream/platform"; +import { LIMIT } from "./common/constants.mjs"; + export default { type: "app", app: "_1crm", - propDefinitions: {}, + propDefinitions: { + recordId: { + type: "string", + label: "Contact ID", + description: "ID of the contact", + async options({ + page, model, + }) { + const { records } = await this.listModuleRecords({ + module: model, + params: { + offset: LIMIT * page, + limit: LIMIT, + }, + }); + + return records.map(({ + id: value, name: label, + }) => ({ + label, + value, + })); + }, + }, + }, methods: { - // this.$auth contains connected account data - authKeys() { - console.log(Object.keys(this.$auth)); + _baseUrl() { + return `${this.$auth.url}/api.php`; + }, + _auth() { + return { + username: `${this.$auth.username}`, + password: `${this.$auth.password}`, + }; + }, + _makeRequest({ + $ = this, path, ...opts + }) { + return axios($, { + url: this._baseUrl() + path, + auth: this._auth(), + ...opts, + }); + }, + getFields({ module }) { + return this._makeRequest({ + path: `/meta/fields/${module}`, + }); + }, + listModuleRecords({ + module, ...opts + }) { + return this._makeRequest({ + path: `/data/${module}`, + ...opts, + }); + }, + createModel({ + model, ...opts + }) { + return this._makeRequest({ + method: "POST", + path: `/data/${model}`, + ...opts, + }); + }, + updateModel({ + updateId, model, ...opts + }) { + return this._makeRequest({ + method: "PATCH", + path: `/data/${model}/${updateId}`, + ...opts, + }); }, }, -}; \ No newline at end of file +}; diff --git a/components/_1crm/actions/common/base.mjs b/components/_1crm/actions/common/base.mjs new file mode 100644 index 0000000000000..46e3b29d5741f --- /dev/null +++ b/components/_1crm/actions/common/base.mjs @@ -0,0 +1,106 @@ +import _1crm from "../../_1crm.app.mjs"; + +export default { + props: { + _1crm, + checkDuplicates: { + type: "boolean", + label: "Check Duplicates", + description: "Check Duplicates Flag", + default: true, + reloadProps: true, + }, + }, + methods: { + getMethod() { + return "create"; + }, + getUpdateId() { + return ""; + }, + getType(type) { + switch (type) { + case "bool": return "boolean"; + case "int": return "integer"; + case "multienum": return "string[]"; + default: return "string"; + } + }, + filterFields(fields) { + const groups = []; + return Object.keys(fields) + .filter( (key) => !("editable" in fields[key])) + .filter( (key) => { + if (fields[key].multi_select_group && !groups.includes(fields[key].multi_select_group)) { + groups.push(fields[key].multi_select_group); + return true; + } + if (!fields[key].multi_select_group ) return true; + }) + .reduce( (res, key) => (res[key] = fields[key], res), {} ); + }, + fixValues(data) { + return Object.keys(data) + .reduce( (res, key) => (res[key] = (typeof data[key] === "boolean" + ? +data[key] + : (Array.isArray(data[key])) + ? data[key].join(",") + : data[key]), res), {} ); + }, + }, + async additionalProps() { + const method = this.getMethod(); + const props = {}; + let { fields } = await this._1crm.getFields({ + module: this.getModule(), + }); + delete fields.assigned_user; + delete fields.assigned_user_id; + + fields = this.filterFields(fields); + + for (const [ + key, + value, + ] of Object.entries(fields)) { + props[key] = { + type: this.getType(value.type), + label: value.vname, + description: value.comment, + optional: (method === "create") + ? !value.required + : true, + options: value.options, + }; + } + return props; + }, + async run({ $ }) { + const { + _1crm, + checkDuplicates, + ...data + } = this; + + const method = this.getMethod(); + const fn = (method === "create") + ? _1crm.createModel + : _1crm.updateModel; + + const response = await fn({ + $, + data: { + data: this.fixValues(data), + }, + params: { + check_duplicates: checkDuplicates, + }, + updateId: this.getUpdateId(), + model: this.getModule(), + }); + if (response.errors) throw new Error(response.errors); + + $.export("$summary", this.getSummary(response)); + return response; + }, +}; diff --git a/components/_1crm/actions/create-contact/create-contact.mjs b/components/_1crm/actions/create-contact/create-contact.mjs new file mode 100644 index 0000000000000..56d1224cabde0 --- /dev/null +++ b/components/_1crm/actions/create-contact/create-contact.mjs @@ -0,0 +1,19 @@ +import common from "../common/base.mjs"; + +export default { + ...common, + key: "_1crm-create-contact", + name: "Create Contact", + description: "Creates a new contact in the 1CRM system. [See the documentation](https://demo.1crmcloud.com/api.php#endpoint_dataList_post)", + version: "0.0.1", + type: "action", + methods: { + ...common.methods, + getModule() { + return "Contact"; + }, + getSummary({ id }) { + return `Successfully created contact with ID ${id}`; + }, + }, +}; diff --git a/components/_1crm/actions/create-lead/create-lead.mjs b/components/_1crm/actions/create-lead/create-lead.mjs new file mode 100644 index 0000000000000..a2796fce8845b --- /dev/null +++ b/components/_1crm/actions/create-lead/create-lead.mjs @@ -0,0 +1,19 @@ +import common from "../common/base.mjs"; + +export default { + ...common, + key: "_1crm-create-lead", + name: "Create Lead", + description: "Crafts a new lead in 1CRM. [See the documentation](https://demo.1crmcloud.com/api.php#endpoint_dataList_post)", + version: "0.0.1", + type: "action", + methods: { + ...common.methods, + getModule() { + return "Lead"; + }, + getSummary({ id }) { + return `Successfully created lead with ID ${id}`; + }, + }, +}; diff --git a/components/_1crm/actions/update-contact/update-contact.mjs b/components/_1crm/actions/update-contact/update-contact.mjs new file mode 100644 index 0000000000000..78d109d25ec57 --- /dev/null +++ b/components/_1crm/actions/update-contact/update-contact.mjs @@ -0,0 +1,34 @@ +import common from "../create-contact/create-contact.mjs"; + +export default { + ...common, + key: "_1crm-update-contact", + name: "Update Contact", + description: "Modifies an existing contact within the 1CRM system. [See the documentation](https://demo.1crmcloud.com/api.php#endpoint_dataRecord_patch)", + version: "0.0.1", + type: "action", + props: { + ...common.props, + contactId: { + propDefinition: [ + common.props._1crm, + "recordId", + () => ({ + model: "Contact", + }), + ], + }, + }, + methods: { + ...common.methods, + getMethod() { + return "update"; + }, + getUpdateId() { + return this.contactId; + }, + getSummary() { + return `Successfully updated contact with ID ${this.contactId}`; + }, + }, +}; diff --git a/components/_1crm/actions/update-lead/update-lead.mjs b/components/_1crm/actions/update-lead/update-lead.mjs new file mode 100644 index 0000000000000..4cbe05c2db631 --- /dev/null +++ b/components/_1crm/actions/update-lead/update-lead.mjs @@ -0,0 +1,36 @@ +import common from "../create-lead/create-lead.mjs"; + +export default { + ...common, + key: "_1crm-update-lead", + name: "Update Lead", + description: "Updates an existing lead in 1CRM. [See the documentation](https://demo.1crmcloud.com/api.php#endpoint_dataRecord_patch)", + version: "0.0.1", + type: "action", + props: { + ...common.props, + leadId: { + propDefinition: [ + common.props._1crm, + "recordId", + () => ({ + model: "Lead", + }), + ], + label: "Lead ID", + description: "ID of the lead", + }, + }, + methods: { + ...common.methods, + getMethod() { + return "update"; + }, + getUpdateId() { + return this.leadId; + }, + getSummary() { + return `Lead with ID ${this.leadId} updated successfully`; + }, + }, +}; diff --git a/components/_1crm/common/constants.mjs b/components/_1crm/common/constants.mjs new file mode 100644 index 0000000000000..ea830c15a04cb --- /dev/null +++ b/components/_1crm/common/constants.mjs @@ -0,0 +1 @@ +export const LIMIT = 100; diff --git a/components/_1crm/package.json b/components/_1crm/package.json index 5ea6528625514..8b66069d864c9 100644 --- a/components/_1crm/package.json +++ b/components/_1crm/package.json @@ -1,6 +1,6 @@ { "name": "@pipedream/_1crm", - "version": "0.0.1", + "version": "0.1.0", "description": "Pipedream 1CRM Components", "main": "_1crm.app.mjs", "keywords": [ @@ -11,5 +11,8 @@ "author": "Pipedream (https://pipedream.com/)", "publishConfig": { "access": "public" + }, + "dependencies": { + "@pipedream/platform": "^3.0.0" } -} \ No newline at end of file +} diff --git a/components/_1crm/yarn.lock b/components/_1crm/yarn.lock new file mode 100644 index 0000000000000..6f39995bded92 --- /dev/null +++ b/components/_1crm/yarn.lock @@ -0,0 +1,85 @@ +# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. +# yarn lockfile v1 + + +"@pipedream/platform@^3.0.0": + version "3.0.0" + resolved "https://registry.yarnpkg.com/@pipedream/platform/-/platform-3.0.0.tgz#f5d39315aefefb77689719eacf392340f1fe630d" + integrity sha512-qlSJBF0Gn5RloFEPr072hSf6pRSU++Zjs0j6X9ZE2SMmfg777qYR4RQ5HkTHEELEyA948xEOqkBt3bFplpfEbw== + dependencies: + axios "^1.6.5" + fp-ts "^2.0.2" + io-ts "^2.0.0" + querystring "^0.2.1" + +asynckit@^0.4.0: + version "0.4.0" + resolved "https://registry.yarnpkg.com/asynckit/-/asynckit-0.4.0.tgz#c79ed97f7f34cb8f2ba1bc9790bcc366474b4b79" + integrity sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q== + +axios@^1.6.5: + version "1.7.2" + resolved "https://registry.yarnpkg.com/axios/-/axios-1.7.2.tgz#b625db8a7051fbea61c35a3cbb3a1daa7b9c7621" + integrity sha512-2A8QhOMrbomlDuiLeK9XibIBzuHeRcqqNOHp0Cyp5EoJ1IFDh+XZH3A6BkXtv0K4gFGCI0Y4BM7B1wOEi0Rmgw== + dependencies: + follow-redirects "^1.15.6" + form-data "^4.0.0" + proxy-from-env "^1.1.0" + +combined-stream@^1.0.8: + version "1.0.8" + resolved "https://registry.yarnpkg.com/combined-stream/-/combined-stream-1.0.8.tgz#c3d45a8b34fd730631a110a8a2520682b31d5a7f" + integrity sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg== + dependencies: + delayed-stream "~1.0.0" + +delayed-stream@~1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/delayed-stream/-/delayed-stream-1.0.0.tgz#df3ae199acadfb7d440aaae0b29e2272b24ec619" + integrity sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ== + +follow-redirects@^1.15.6: + version "1.15.6" + resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.6.tgz#7f815c0cda4249c74ff09e95ef97c23b5fd0399b" + integrity sha512-wWN62YITEaOpSK584EZXJafH1AGpO8RVgElfkuXbTOrPX4fIfOyEpW/CsiNd8JdYrAoOvafRTOEnvsO++qCqFA== + +form-data@^4.0.0: + version "4.0.0" + resolved "https://registry.yarnpkg.com/form-data/-/form-data-4.0.0.tgz#93919daeaf361ee529584b9b31664dc12c9fa452" + integrity sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww== + dependencies: + asynckit "^0.4.0" + combined-stream "^1.0.8" + mime-types "^2.1.12" + +fp-ts@^2.0.2: + version "2.16.8" + resolved "https://registry.yarnpkg.com/fp-ts/-/fp-ts-2.16.8.tgz#dfa1ea1c967ac6794c43ce877aeb8ed76f5e0df7" + integrity sha512-nmDtNqmMZkOxu0M5hkrS9YA15/KPkYkILb6Axg9XBAoUoYEtzg+LFmVWqZrl9FNttsW0qIUpx9RCA9INbv+Bxw== + +io-ts@^2.0.0: + version "2.2.21" + resolved "https://registry.yarnpkg.com/io-ts/-/io-ts-2.2.21.tgz#4ef754176f7082a1099d04c7d5c4ea53267c530a" + integrity sha512-zz2Z69v9ZIC3mMLYWIeoUcwWD6f+O7yP92FMVVaXEOSZH1jnVBmET/urd/uoarD1WGBY4rCj8TAyMPzsGNzMFQ== + +mime-db@1.52.0: + version "1.52.0" + resolved "https://registry.yarnpkg.com/mime-db/-/mime-db-1.52.0.tgz#bbabcdc02859f4987301c856e3387ce5ec43bf70" + integrity sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg== + +mime-types@^2.1.12: + version "2.1.35" + resolved "https://registry.yarnpkg.com/mime-types/-/mime-types-2.1.35.tgz#381a871b62a734450660ae3deee44813f70d959a" + integrity sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw== + dependencies: + mime-db "1.52.0" + +proxy-from-env@^1.1.0: + version "1.1.0" + resolved "https://registry.yarnpkg.com/proxy-from-env/-/proxy-from-env-1.1.0.tgz#e102f16ca355424865755d2c9e8ea4f24d58c3e2" + integrity sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg== + +querystring@^0.2.1: + version "0.2.1" + resolved "https://registry.yarnpkg.com/querystring/-/querystring-0.2.1.tgz#40d77615bb09d16902a85c3e38aa8b5ed761c2dd" + integrity sha512-wkvS7mL/JMugcup3/rMitHmd9ecIGd2lhFhK9N3UUQ450h66d1r3Y9nvXzQAW1Lq+wyx61k/1pfKS5KuKiyEbg== diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index a4cb6ac9a0320..bb81829265768 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -72,7 +72,10 @@ importers: '@pipedream/platform': 1.6.0 components/_1crm: - specifiers: {} + specifiers: + '@pipedream/platform': ^3.0.0 + dependencies: + '@pipedream/platform': 3.0.0 components/_21risk: specifiers: