Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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,74 @@
import { parseObject } from "../../common/utils.mjs";
import app from "../../encharge.app.mjs";

export default {
key: "encharge-add-or-update-person",
name: "Add or Update Person",
description: "Add or update a person in Encharge. [See the documentation](https://app-encharge-resources.s3.amazonaws.com/redoc.html#/people/createupdatepeople)",
version: "0.0.1",
annotations: {
destructiveHint: false,
openWorldHint: true,
readOnlyHint: false,
},
type: "action",
props: {
app,
userId: {
propDefinition: [
app,
"userId",
],
optional: true,
},
firstName: {
type: "string",
label: "First Name",
description: "The first name of the person.",
optional: true,
},
lastName: {
type: "string",
label: "Last Name",
description: "The last name of the person.",
optional: true,
},
email: {
type: "string",
label: "Email",
description: "The email of the person.",
optional: true,
},
additionalFields: {
type: "object",
label: "Additional Fields",
description: "Additional fields to include in the request body.",
optional: true,
},
},
async run({ $ }) {
const parsedAdditionalFields = parseObject(this.additionalFields) || {};
const data = [
{
firstName: this.firstName,
lastName: this.lastName,
email: this.email,
phone: this.phone,
id: this.userId,
...parsedAdditionalFields,
},
];

const response = await this.app.addOrUpdatePerson({
$,
data,
});

$.export("$summary", `Successfully ${this.userId
? "updated"
: "added"} person${this.email
? ` with email ${this.email}`
: ""}`);
return response;
},
};
62 changes: 62 additions & 0 deletions components/encharge/actions/archive-person/archive-person.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import app from "../../encharge.app.mjs";

export default {
key: "encharge-archive-person",
name: "Archive Person",
description: "Archive a person in Encharge. [See the documentation](https://app-encharge-resources.s3.amazonaws.com/redoc.html#/people/archivepeople)",
version: "0.0.1",
annotations: {
destructiveHint: true,
openWorldHint: true,
readOnlyHint: false,
},
type: "action",
props: {
app,
userId: {
propDefinition: [
app,
"userId",
],
description: "The user ID of the person to archive.",
optional: true,
},
email: {
type: "string",
label: "Email",
description: "The email of the person to archive.",
optional: true,
},
force: {
type: "boolean",
label: "Force",
description: "If set to `true`, will delete the person's data. This is useful for GDPR-compliant removal of user data.",
default: false,
},
},
async run({ $ }) {
if (!this.userId && !this.email) {
throw new Error("You must provide either a user ID or an email.");
}
if (this.userId && this.email) {
throw new Error("You must provide either a user ID or an email, not both.");
}

const response = await this.app.archivePerson({
$,
params: {
people: [
{
id: this.userId,
email: this.email,
},
],
force: this.force,
},
});
$.export("$summary", `Successfully archived person with ${this.userId
? `ID ${this.userId}`
: `email ${this.email}`}`);
return response;
},
};
53 changes: 53 additions & 0 deletions components/encharge/actions/remove-tags/remove-tags.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { parseObject } from "../../common/utils.mjs";
import app from "../../encharge.app.mjs";

export default {
key: "encharge-remove-tags",
name: "Remove Tags",
description: "Remove tag(s) from existing user. [See the documentation](https://app-encharge-resources.s3.amazonaws.com/#/tags/removetag)",
version: "0.0.1",
annotations: {
destructiveHint: true,
openWorldHint: true,
readOnlyHint: false,
},
type: "action",
props: {
app,
userId: {
propDefinition: [
app,
"userId",
],
description: "UserID of the person.",
},
tags: {
propDefinition: [
app,
"tags",
({ userId }) => ({
userId,
}),
],
},
},
async run({ $ }) {
const tags = parseObject(this.tags);
const tagsArray = Array.isArray(tags)
? tags
: [
tags,
];
const response = await this.app.removeTag({
$,
data: {
tag: tagsArray.join(","),
id: this.userId,
},
});
$.export("$summary", `Successfully removed ${tagsArray.length} tag${tagsArray.length > 1
? "s"
: ""} from person with ID ${this.userId}`);
return response;
},
};
1 change: 1 addition & 0 deletions components/encharge/common/constants.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const LIMIT = 100;
24 changes: 24 additions & 0 deletions components/encharge/common/utils.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
export const parseObject = (obj) => {
if (!obj) return undefined;

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;
};
116 changes: 112 additions & 4 deletions components/encharge/encharge.app.mjs
Original file line number Diff line number Diff line change
@@ -1,11 +1,119 @@
import { axios } from "@pipedream/platform";
import { LIMIT } from "./common/constants.mjs";

export default {
type: "app",
app: "encharge",
propDefinitions: {},
propDefinitions: {
userId: {
type: "string",
label: "User ID",
description: "The user ID of the person to update.",
async options({ page }) {
const { people } = await this.listPeople({
params: {
limit: LIMIT,
offset: LIMIT * page,
},
});

return people.map(({
id: value, name, email,
}) => ({
label: `${name}${email
? ` (${email})`
: ""}`,
value,
}));
},
},
tags: {
type: "string[]",
label: "Tags",
description: "Tags to remove from the person.",
async options({ userId }) {
const {
users: [
{ tags },
],
} = await this.getPerson({
params: {
people: [
{
id: userId,
},
],
},
});
return tags
? tags.split(",")
: [];
},
},
},
methods: {
// this.$auth contains connected account data
authKeys() {
console.log(Object.keys(this.$auth));
_apiUrl() {
return "https://api.encharge.io/v1";
},
_getHeaders() {
return {
"Authorization": `Bearer ${this.$auth.oauth_access_token}`,
};
},
_makeRequest({
$ = this, path, ...opts
}) {
return axios($, {
url: `${this._apiUrl()}/${path}`,
headers: this._getHeaders(),
...opts,
});
},
getPerson(args = {}) {
return this._makeRequest({
path: "people",
...args,
});
},
listPeople(args = {}) {
return this._makeRequest({
path: "people/all",
...args,
});
},
addOrUpdatePerson(args = {}) {
return this._makeRequest({
method: "POST",
path: "people",
...args,
});
},
archivePerson(args = {}) {
return this._makeRequest({
method: "DELETE",
path: "people",
...args,
});
},
removeTag(args = {}) {
return this._makeRequest({
method: "DELETE",
path: "tags",
...args,
});
},
createHook(args = {}) {
return this._makeRequest({
method: "POST",
path: "event-subscriptions",
...args,
});
},
deleteHook(hookId) {
return this._makeRequest({
method: "DELETE",
path: `event-subscriptions/${hookId}`,
});
},
},
};
5 changes: 4 additions & 1 deletion components/encharge/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pipedream/encharge",
"version": "0.0.2",
"version": "0.1.0",
"description": "Pipedream Encharge Components",
"main": "encharge.app.mjs",
"keywords": [
Expand All @@ -11,5 +11,8 @@
"author": "Pipedream <[email protected]> (https://pipedream.com/)",
"publishConfig": {
"access": "public"
},
"dependencies": {
"@pipedream/platform": "^3.1.1"
}
}
35 changes: 35 additions & 0 deletions components/encharge/sources/common/base.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import encharge from "../../encharge.app.mjs";

export default {
props: {
encharge,
db: "$.service.db",
http: "$.interface.http",
},
methods: {
_setWebhookId(id) {
this.db.set("webhookId", id);
},
_getWebhookId() {
return this.db.get("webhookId");
},
},
hooks: {
async activate() {
const { subscription } = await this.encharge.createHook({
data: {
url: this.http.endpoint,
eventType: this.getEvent(),
},
});
this._setWebhookId(subscription.id);
},
async deactivate() {
const webhookId = this._getWebhookId();
await this.encharge.deleteHook(webhookId);
},
},
async run({ body }) {
this.$emit(body, this.generateMeta(body));
},
};
Loading
Loading