Skip to content

Commit 1e09207

Browse files
committed
Enhance Encharge component with new actions and sources
- Added actions for adding/updating a person, archiving a person, and removing tags. - Introduced new sources for detecting new and updated persons, as well as when tags are removed. - Updated prop definitions to include user ID and tags for better integration. - Implemented utility functions for parsing objects and managing API requests. - Bumped version to 0.1.0 and added dependencies for improved functionality.
1 parent 29b168a commit 1e09207

File tree

14 files changed

+530
-5
lines changed

14 files changed

+530
-5
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import { parseObject } from "../../common/utils.mjs";
2+
import app from "../../encharge.app.mjs";
3+
4+
export default {
5+
key: "encharge-add-or-update-person",
6+
name: "Add or Update Person",
7+
description: "Add or update a person in Encharge. [See the documentation](https://app-encharge-resources.s3.amazonaws.com/redoc.html#/people/createupdatepeople)",
8+
version: "0.0.1",
9+
annotations: {
10+
destructiveHint: false,
11+
openWorldHint: true,
12+
readOnlyHint: false,
13+
},
14+
type: "action",
15+
props: {
16+
app,
17+
userId: {
18+
propDefinition: [
19+
app,
20+
"userId",
21+
],
22+
optional: true,
23+
},
24+
firstName: {
25+
type: "string",
26+
label: "First Name",
27+
description: "The first name of the person.",
28+
optional: true,
29+
},
30+
lastName: {
31+
type: "string",
32+
label: "Last Name",
33+
description: "The last name of the person.",
34+
optional: true,
35+
},
36+
email: {
37+
type: "string",
38+
label: "Email",
39+
description: "The email of the person.",
40+
optional: true,
41+
},
42+
additionalFields: {
43+
type: "object",
44+
label: "Additional Fields",
45+
description: "Additional fields to include in the request body.",
46+
optional: true,
47+
},
48+
},
49+
async run({ $ }) {
50+
const parsedAdditionalFields = parseObject(this.additionalFields) || {};
51+
const data = [
52+
{
53+
firstName: this.firstName,
54+
lastName: this.lastName,
55+
email: this.email,
56+
phone: this.phone,
57+
id: this.userId,
58+
additionalFields: this.additionalFields,
59+
...parsedAdditionalFields,
60+
},
61+
];
62+
63+
const response = await this.app.addOrUpdatePerson({
64+
$,
65+
data,
66+
});
67+
68+
$.export("$summary", `Successfully ${this.userId
69+
? "updated"
70+
: "added"} person with email ${this.email}`);
71+
return response;
72+
},
73+
};
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import app from "../../encharge.app.mjs";
2+
3+
export default {
4+
key: "encharge-archive-person",
5+
name: "Archive Person",
6+
description: "Archive a person in Encharge. [See the documentation](https://app-encharge-resources.s3.amazonaws.com/redoc.html#/people/archivepeople)",
7+
version: "0.0.1",
8+
annotations: {
9+
destructiveHint: true,
10+
openWorldHint: true,
11+
readOnlyHint: false,
12+
},
13+
type: "action",
14+
props: {
15+
app,
16+
userId: {
17+
propDefinition: [
18+
app,
19+
"userId",
20+
],
21+
description: "The user ID of the person to archive.",
22+
optional: true,
23+
},
24+
email: {
25+
type: "string",
26+
label: "Email",
27+
description: "The email of the person to archive.",
28+
optional: true,
29+
},
30+
force: {
31+
type: "boolean",
32+
label: "Force",
33+
description: "If set to `true`, will delete the person's data. This is useful for GDPR-compliant removal of user data.",
34+
default: false,
35+
},
36+
},
37+
async run({ $ }) {
38+
if (this.userId && this.email) {
39+
throw new Error("You must provide either a user ID or an email, not both.");
40+
}
41+
42+
const response = await this.app.archivePerson({
43+
$,
44+
params: {
45+
people: [
46+
{
47+
id: this.userId,
48+
email: this.email,
49+
},
50+
],
51+
force: this.force,
52+
},
53+
});
54+
$.export("$summary", `Successfully archived person with ${this.userId
55+
? `ID ${this.userId}`
56+
: `email ${this.email}`}`);
57+
return response;
58+
},
59+
};
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { parseObject } from "../../common/utils.mjs";
2+
import app from "../../encharge.app.mjs";
3+
4+
export default {
5+
key: "encharge-remove-tags",
6+
name: "Remove Tags",
7+
description: "Remove tag(s) from existing user. [See the documentation](https://app-encharge-resources.s3.amazonaws.com/#/tags/removetag)",
8+
version: "0.0.1",
9+
annotations: {
10+
destructiveHint: true,
11+
openWorldHint: true,
12+
readOnlyHint: false,
13+
},
14+
type: "action",
15+
props: {
16+
app,
17+
userId: {
18+
propDefinition: [
19+
app,
20+
"userId",
21+
],
22+
description: "UserID of the person.",
23+
},
24+
tags: {
25+
propDefinition: [
26+
app,
27+
"tags",
28+
({ userId }) => ({
29+
userId,
30+
}),
31+
],
32+
},
33+
},
34+
async run({ $ }) {
35+
const response = await this.app.removeTag({
36+
$,
37+
data: {
38+
tag: parseObject(this.tags).join(","),
39+
id: this.userId,
40+
},
41+
});
42+
$.export("$summary", `Successfully removed ${this.tags.length} tag${this.tags.length > 1
43+
? "s"
44+
: ""} from person with ID ${this.userId}`);
45+
return response;
46+
},
47+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const LIMIT = 100;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
export const parseObject = (obj) => {
2+
if (!obj) return undefined;
3+
4+
if (Array.isArray(obj)) {
5+
return obj.map((item) => {
6+
if (typeof item === "string") {
7+
try {
8+
return JSON.parse(item);
9+
} catch (e) {
10+
return item;
11+
}
12+
}
13+
return item;
14+
});
15+
}
16+
if (typeof obj === "string") {
17+
try {
18+
return JSON.parse(obj);
19+
} catch (e) {
20+
return obj;
21+
}
22+
}
23+
return obj;
24+
};
Lines changed: 110 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,117 @@
1+
import { axios } from "@pipedream/platform";
2+
import { LIMIT } from "./common/constants.mjs";
3+
14
export default {
25
type: "app",
36
app: "encharge",
4-
propDefinitions: {},
7+
propDefinitions: {
8+
userId: {
9+
type: "string",
10+
label: "User ID",
11+
description: "The user ID of the person to update.",
12+
async options({ page }) {
13+
const { people } = await this.listPeople({
14+
params: {
15+
limit: LIMIT,
16+
offset: LIMIT * page,
17+
},
18+
});
19+
20+
return people.map(({
21+
id: value, name, email,
22+
}) => ({
23+
label: `${name}${email
24+
? ` (${email})`
25+
: ""}`,
26+
value,
27+
}));
28+
},
29+
},
30+
tags: {
31+
type: "string[]",
32+
label: "Tags",
33+
description: "Tags to remove from the person.",
34+
async options({ userId }) {
35+
const {
36+
users: [
37+
{ tags },
38+
],
39+
} = await this.getPerson({
40+
params: {
41+
people: [
42+
{
43+
id: userId,
44+
},
45+
],
46+
},
47+
});
48+
return tags && tags.split(",") || [];
49+
},
50+
},
51+
},
552
methods: {
6-
// this.$auth contains connected account data
7-
authKeys() {
8-
console.log(Object.keys(this.$auth));
53+
_apiUrl() {
54+
return "https://api.encharge.io/v1";
55+
},
56+
_getHeaders() {
57+
return {
58+
"Authorization": `Bearer ${this.$auth.oauth_access_token}`,
59+
};
60+
},
61+
_makeRequest({
62+
$ = this, path, ...opts
63+
}) {
64+
return axios($, {
65+
url: `${this._apiUrl()}/${path}`,
66+
headers: this._getHeaders(),
67+
...opts,
68+
});
69+
},
70+
getPerson(args = {}) {
71+
return this._makeRequest({
72+
path: "people",
73+
...args,
74+
});
75+
},
76+
listPeople(args = {}) {
77+
return this._makeRequest({
78+
path: "people/all",
79+
...args,
80+
});
81+
},
82+
addOrUpdatePerson(args = {}) {
83+
return this._makeRequest({
84+
method: "POST",
85+
path: "people",
86+
...args,
87+
});
88+
},
89+
archivePerson(args = {}) {
90+
return this._makeRequest({
91+
method: "DELETE",
92+
path: "people",
93+
...args,
94+
});
95+
},
96+
removeTag(args = {}) {
97+
return this._makeRequest({
98+
method: "DELETE",
99+
path: "tags",
100+
...args,
101+
});
102+
},
103+
createHook(args = {}) {
104+
return this._makeRequest({
105+
method: "POST",
106+
path: "event-subscriptions",
107+
...args,
108+
});
109+
},
110+
deleteHook(hookId) {
111+
return this._makeRequest({
112+
method: "DELETE",
113+
path: `event-subscriptions/${hookId}`,
114+
});
9115
},
10116
},
11117
};

components/encharge/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pipedream/encharge",
3-
"version": "0.0.2",
3+
"version": "0.1.0",
44
"description": "Pipedream Encharge Components",
55
"main": "encharge.app.mjs",
66
"keywords": [
@@ -11,5 +11,8 @@
1111
"author": "Pipedream <support@pipedream.com> (https://pipedream.com/)",
1212
"publishConfig": {
1313
"access": "public"
14+
},
15+
"dependencies": {
16+
"@pipedream/platform": "^3.1.1"
1417
}
1518
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import encharge from "../../encharge.app.mjs";
2+
3+
export default {
4+
props: {
5+
encharge,
6+
db: "$.service.db",
7+
http: "$.interface.http",
8+
},
9+
methods: {
10+
_setWebhookId(id) {
11+
this.db.set("webhookId", id);
12+
},
13+
_getWebhookId() {
14+
return this.db.get("webhookId");
15+
},
16+
},
17+
hooks: {
18+
async activate() {
19+
const { subscription } = await this.encharge.createHook({
20+
data: {
21+
url: this.http.endpoint,
22+
eventType: this.getEvent(),
23+
},
24+
});
25+
this._setWebhookId(subscription.id);
26+
},
27+
async deactivate() {
28+
const webhookId = this._getWebhookId();
29+
await this.encharge.deleteHook(webhookId);
30+
},
31+
},
32+
async run({ body }) {
33+
this.$emit(body, this.generateMeta(body));
34+
},
35+
};

0 commit comments

Comments
 (0)