Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

New Components - twenty #11823

Merged
merged 6 commits into from
May 13, 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
import {
camelCaseToWords, parseObject,
} from "../../common/utils.mjs";
import twenty from "../../twenty.app.mjs";

export default {
key: "twenty-create-update-delete-record",
name: "Create, Update, or Delete a Record in Twenty",
description: "Create, update, or delete a single record in Twenty. This action allows for dynamic handling of records based on specified action type. [See the documentation](https://api.twenty.com/docs)",
version: "0.0.1",
type: "action",
props: {
twenty,
recordId: {
propDefinition: [
twenty,
"recordId",
],
reloadProps: true,
},
actionType: {
propDefinition: [
twenty,
"actionType",
],
reloadProps: true,
},
additionalProp: {
type: "object",
label: "Additional Prop",
description: "Any additional prop you want to fill.",
optional: true,
},
},
async additionalProps() {
const props = {};
const recordId = this.recordId;
const actionType = this.actionType;

if (recordId && this.actionType) {
if ([
"delete",
"update",
].includes(actionType)) {
props.id = {
type: "string",
label: `${camelCaseToWords(recordId)} Id`,
description: `The Id of ${recordId}`,
options: async () => {
const {
components: { schemas }, tags,
} = await this.twenty.listRecords();
const index = Object.keys(schemas).findIndex((i) => i === this.recordId);
const { data } = await this.twenty.listRecordItems(tags[index + 1].name);
const response = data[tags[index + 1].name];
return response.map((item) => ({
label: item.name || item.title || item.id,
value: item.id,
}));
},
};
}

if ([
"create",
"update",
].includes(actionType)) {

const { components: { schemas } } = await this.twenty.listRecords();
const properties = schemas[recordId].properties;
const required = schemas[recordId].required || [];

for (const [
key,
value,
] of Object.entries(properties)) {
if (
(key != "id") &&
(key != "createdAt") &&
(key != "updatedAt") &&
(value.type) &&
(value.type != "object") &&
(value.type != "array")
) {
props[key] = {
type: value["type"] === "number"
? "integer"
: value["type"],
label: camelCaseToWords(key),
description: value["description"],
optional: !(required.includes === key),
};
}
}
}
}
return props;
},
async run({ $ }) {
const {
twenty,
id,
recordId,
actionType,
additionalProp,
...data
} = this;

let response;

const {
components: { schemas }, tags,
} = await this.twenty.listRecords();
const index = Object.keys(schemas).findIndex((i) => i === this.recordId);

try {
response = await twenty.performAction({
$,
id,
actionType: this.actionType,
recordName: tags[index + 1].name,
data: {
...data,
...(additionalProp
? parseObject(additionalProp)
: {}),
},
});

$.export("$summary", `Successfully performed ${actionType} ${recordId} on record with ID: ${id || response.data[`${actionType}${recordId}`].id}`);

return response;
} catch (error) {
throw new Error(`Failed to ${actionType} record. Error: ${error.message}`);
}
},
};
31 changes: 31 additions & 0 deletions components/twenty/common/utils.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
export const camelCaseToWords = (s) => {
const result = s.replace(/([A-Z])/g, " $1");
return result.charAt(0).toUpperCase() + result.slice(1);
};

export const capitalizeFirstLetter = (string) => {
return string.charAt(0).toLowerCase() + string.slice(1);
};

export const parseObject = (obj) => {
if (Array.isArray(obj)) {
return obj.map((item) => {
if (typeof item === "string") {
try {
return JSON.parse(item);
} catch (e) {
return item;
}
}
return item;
});
}
if (typeof obj === "string") {
try {
return JSON.parse(obj);
} catch (e) {
return obj;
}
}
return obj;
};
8 changes: 6 additions & 2 deletions components/twenty/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pipedream/twenty",
"version": "0.0.1",
"version": "0.1.0",
"description": "Pipedream Twenty Components",
"main": "twenty.app.mjs",
"keywords": [
Expand All @@ -11,5 +11,9 @@
"author": "Pipedream <[email protected]> (https://pipedream.com/)",
"publishConfig": {
"access": "public"
},
"dependencies": {
"@pipedream/platform": "^1.6.5"
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import twenty from "../../twenty.app.mjs";
import sampleEmit from "./test-event.mjs";

export default {
key: "twenty-new-record-modified-instant",
name: "New Record Modified (Instant)",
description: "Emit new event when a record is created, updated, or deleted.",
version: "0.0.1",
type: "source",
dedupe: "unique",
props: {
twenty,
http: "$.interface.http",
db: "$.service.db",
},
hooks: {
async activate() {
const { data } = await this.twenty.createHook({
data: {
targetUrl: this.http.endpoint,
operation: "*.*",
},
});

this.db.set("webhookId", data.createWebhook.id);
},
async deactivate() {
const webhookId = this.db.get("webhookId");
await this.twenty.deleteHook(webhookId);
},
},
async run(event) {
const { body } = event;

const eventType = body.eventType;
const eventName = eventType.split(".")[0];

this.$emit(body, {
id: `${body.objectMetadata.id}-${body.eventDate}`,
summary: `New ${body.objectMetadata.nameSingular} ${eventName}d with Id: ${body.objectMetadata.id}.`,
ts: Date.parse(body.eventDate) || Date.now(),
});
},
sampleEmit,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
export default {
"targetUrl": "http://webhook.url",
"eventType": "*.*",
"objectMetadata": {
"id": "12345678-1234-1234-1234-123456789012",
"nameSingular": "company"
},
"workspaceId": "12345678-1234-1234-1234-123456789012",
"webhookId": "12345678-1234-1234-1234-123456789012",
"eventDate": "2024-05-07T14:16:39.810Z",
"record": {
"id": "12345678-1234-1234-1234-123456789012",
"name": "company test",
"people": {
"edges": [],
"__typename": "personConnection"
},
"address": "",
"position": 1,
"createdAt": "2024-05-07T14:16:39.350276+00:00",
"employees": null,
"favorites": {
"edges": [],
"__typename": "favoriteConnection"
},
"updatedAt": "2024-05-07T14:16:39.350276+00:00",
"__typename": "company",
"domainName": "",
"attachments": {
"edges": [],
"__typename": "attachmentConnection"
},
"accountOwner": null,
"opportunities": {
"edges": [],
"__typename": "opportunityConnection"
},
"accountOwnerId": null,
"activityTargets": {
"edges": [],
"__typename": "activityTargetConnection"
},
"timelineActivities": {
"edges": [],
"__typename": "timelineActivityConnection"
},
"idealCustomerProfile": false,
"xLink": {
"url": "",
"label": ""
},
"linkedinLink": {
"url": "",
"label": ""
},
"annualRecurringRevenue": {
"amountMicros": null,
"currencyCode": ""
}
}
}
Loading
Loading