Skip to content

Commit 8af1783

Browse files
authored
13377 components encharge (#19148)
* 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. * pnpm update * Refactor Encharge component actions for improved error handling and output formatting - Updated the return statement in the tags processing to use a ternary operator for clarity. - Removed unused additionalFields from the add-or-update-person action. - Enhanced the summary message in the add-or-update-person action to conditionally include the email. - Added validation in the archive-person action to ensure either user ID or email is provided. - Modified the remove-tags action to handle tags as an array for consistent processing. * Remove phone field from add-or-update-person action for cleaner data handling
1 parent ed7c5ba commit 8af1783

File tree

15 files changed

+546
-6
lines changed

15 files changed

+546
-6
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+
id: this.userId,
57+
...parsedAdditionalFields,
58+
},
59+
];
60+
61+
const response = await this.app.addOrUpdatePerson({
62+
$,
63+
data,
64+
});
65+
66+
$.export("$summary", `Successfully ${this.userId
67+
? "updated"
68+
: "added"} person${this.email
69+
? ` with email ${this.email}`
70+
: ""}`);
71+
return response;
72+
},
73+
};
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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.");
40+
}
41+
if (this.userId && this.email) {
42+
throw new Error("You must provide either a user ID or an email, not both.");
43+
}
44+
45+
const response = await this.app.archivePerson({
46+
$,
47+
params: {
48+
people: [
49+
{
50+
id: this.userId,
51+
email: this.email,
52+
},
53+
],
54+
force: this.force,
55+
},
56+
});
57+
$.export("$summary", `Successfully archived person with ${this.userId
58+
? `ID ${this.userId}`
59+
: `email ${this.email}`}`);
60+
return response;
61+
},
62+
};
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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 tags = parseObject(this.tags);
36+
const tagsArray = Array.isArray(tags)
37+
? tags
38+
: [
39+
tags,
40+
];
41+
const response = await this.app.removeTag({
42+
$,
43+
data: {
44+
tag: tagsArray.join(","),
45+
id: this.userId,
46+
},
47+
});
48+
$.export("$summary", `Successfully removed ${tagsArray.length} tag${tagsArray.length > 1
49+
? "s"
50+
: ""} from person with ID ${this.userId}`);
51+
return response;
52+
},
53+
};
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: 112 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,119 @@
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
49+
? tags.split(",")
50+
: [];
51+
},
52+
},
53+
},
554
methods: {
6-
// this.$auth contains connected account data
7-
authKeys() {
8-
console.log(Object.keys(this.$auth));
55+
_apiUrl() {
56+
return "https://api.encharge.io/v1";
57+
},
58+
_getHeaders() {
59+
return {
60+
"Authorization": `Bearer ${this.$auth.oauth_access_token}`,
61+
};
62+
},
63+
_makeRequest({
64+
$ = this, path, ...opts
65+
}) {
66+
return axios($, {
67+
url: `${this._apiUrl()}/${path}`,
68+
headers: this._getHeaders(),
69+
...opts,
70+
});
71+
},
72+
getPerson(args = {}) {
73+
return this._makeRequest({
74+
path: "people",
75+
...args,
76+
});
77+
},
78+
listPeople(args = {}) {
79+
return this._makeRequest({
80+
path: "people/all",
81+
...args,
82+
});
83+
},
84+
addOrUpdatePerson(args = {}) {
85+
return this._makeRequest({
86+
method: "POST",
87+
path: "people",
88+
...args,
89+
});
90+
},
91+
archivePerson(args = {}) {
92+
return this._makeRequest({
93+
method: "DELETE",
94+
path: "people",
95+
...args,
96+
});
97+
},
98+
removeTag(args = {}) {
99+
return this._makeRequest({
100+
method: "DELETE",
101+
path: "tags",
102+
...args,
103+
});
104+
},
105+
createHook(args = {}) {
106+
return this._makeRequest({
107+
method: "POST",
108+
path: "event-subscriptions",
109+
...args,
110+
});
111+
},
112+
deleteHook(hookId) {
113+
return this._makeRequest({
114+
method: "DELETE",
115+
path: `event-subscriptions/${hookId}`,
116+
});
9117
},
10118
},
11119
};

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)