Skip to content

New Components - _1crm #12843

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 7 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 77 additions & 5 deletions components/_1crm/_1crm.app.mjs
Original file line number Diff line number Diff line change
@@ -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,
});
},
},
};
};
106 changes: 106 additions & 0 deletions components/_1crm/actions/common/base.mjs
Original file line number Diff line number Diff line change
@@ -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;
},
};
19 changes: 19 additions & 0 deletions components/_1crm/actions/create-contact/create-contact.mjs
Original file line number Diff line number Diff line change
@@ -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}`;
},
},
};
19 changes: 19 additions & 0 deletions components/_1crm/actions/create-lead/create-lead.mjs
Original file line number Diff line number Diff line change
@@ -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}`;
},
},
};
34 changes: 34 additions & 0 deletions components/_1crm/actions/update-contact/update-contact.mjs
Original file line number Diff line number Diff line change
@@ -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}`;
},
},
};
36 changes: 36 additions & 0 deletions components/_1crm/actions/update-lead/update-lead.mjs
Original file line number Diff line number Diff line change
@@ -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`;
},
},
};
1 change: 1 addition & 0 deletions components/_1crm/common/constants.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const LIMIT = 100;
7 changes: 5 additions & 2 deletions components/_1crm/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pipedream/_1crm",
"version": "0.0.1",
"version": "0.1.0",
"description": "Pipedream 1CRM Components",
"main": "_1crm.app.mjs",
"keywords": [
Expand All @@ -11,5 +11,8 @@
"author": "Pipedream <support@pipedream.com> (https://pipedream.com/)",
"publishConfig": {
"access": "public"
},
"dependencies": {
"@pipedream/platform": "^3.0.0"
}
}
}
Loading
Loading