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 - peach #12856

Merged
merged 4 commits into from
Jul 17, 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
46 changes: 46 additions & 0 deletions components/peach/actions/create-contact/create-contact.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { clearObj } from "../../common/utils.mjs";
import peach from "../../peach.app.mjs";

export default {
key: "peach-create-contact",
name: "Create Contact",
description: "Creates a contact in your account. [See the documentation](https://peach.apidocumentation.com/reference#tag/contacts/post/subscribers)",
version: "0.0.1",
type: "action",
props: {
peach,
phoneNumber: {
propDefinition: [
peach,
"phoneNumber",
],
},
contactName: {
propDefinition: [
peach,
"contactName",
],
optional: true,
},
contactEmail: {
propDefinition: [
peach,
"contactEmail",
],
optional: true,
},
},
async run({ $ }) {
const response = await this.peach.createContact({
$,
data: clearObj({
name: this.contactName,
email: this.contactEmail,
phone_number: this.phoneNumber,
}),
});

$.export("$summary", `Created the contact successfully with waId: ${this.phoneNumber}`);
return response;
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { clearObj } from "../../common/utils.mjs";
import peach from "../../peach.app.mjs";

export default {
key: "peach-send-template-message",
name: "Send Template Message",
description: "Send a predefined message to a contact within the Peach app. [See the documentation](https://peach.apidocumentation.com/reference#tag/messaging/post/transactional_messages)",
version: "0.0.1",
type: "action",
props: {
peach,
phoneNumber: {
propDefinition: [
peach,
"phoneNumber",
],
},
templateName: {
type: "string",
label: "Template Name",
description: "WhatsApp approved utility template name",
},
contactName: {
propDefinition: [
peach,
"contactName",
],
optional: true,
},
contactEmail: {
propDefinition: [
peach,
"contactEmail",
],
optional: true,
},
arguments: {
type: "object",
label: "Arguments",
description: "Arguments for the template",
optional: true,
},
},
async run({ $ }) {
const response = await this.peach.sendTransactionalMessage({
$,
data: clearObj({
template_name: this.templateName,
to: {
name: this.contactName,
email: this.contactEmail,
phone_number: this.phoneNumber,
},
arguments: this.arguments
? Object.keys(this.arguments).map((key) => ({
key,
value: this.arguments[key],
}))
: {},
}),
});

$.export("$summary", `Message sent successfully to ${this.phoneNumber}`);
return response;
},
};
16 changes: 16 additions & 0 deletions components/peach/common/utils.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export const clearObj = (obj) => {
return Object.entries(obj)
.filter(([
,
v,
]) => (v != null && v != "" && JSON.stringify(v) != "{}"))
.reduce((acc, [
k,
v,
]) => ({
...acc,
[k]: (!Array.isArray(v) && v === Object(v))
? clearObj(v)
: v,
}), {});
};
Comment on lines +1 to +16
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optimize the clearObj function to improve performance.

The use of spread syntax in the reducer (line 11) causes inefficient O(n^2) time complexity. Consider using a more efficient method to accumulate properties.

-    }), {});
+    }, Object.create(null));

This change initializes the accumulator as a plain object without a prototype, reducing overhead and avoiding the spread syntax.

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
export const clearObj = (obj) => {
return Object.entries(obj)
.filter(([
,
v,
]) => (v != null && v != "" && JSON.stringify(v) != "{}"))
.reduce((acc, [
k,
v,
]) => ({
...acc,
[k]: (!Array.isArray(v) && v === Object(v))
? clearObj(v)
: v,
}), {});
};
export const clearObj = (obj) => {
return Object.entries(obj)
.filter(([
,
v,
]) => (v != null && v != "" && JSON.stringify(v) != "{}"))
.reduce((acc, [
k,
v,
]) => ({
...acc,
[k]: (!Array.isArray(v) && v === Object(v))
? clearObj(v)
: v,
}), Object.create(null));
};
Tools
Biome

[error] 11-11: Avoid the use of spread (...) syntax on accumulators.

Spread syntax should be avoided on accumulators (like those in .reduce) because it causes a time complexity of O(n^2).
Consider methods such as .splice or .push instead.

(lint/performance/noAccumulatingSpread)

8 changes: 6 additions & 2 deletions components/peach/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pipedream/peach",
"version": "0.0.1",
"version": "0.1.0",
"description": "Pipedream Peach Components",
"main": "peach.app.mjs",
"keywords": [
Expand All @@ -11,5 +11,9 @@
"author": "Pipedream <[email protected]> (https://pipedream.com/)",
"publishConfig": {
"access": "public"
},
"dependencies": {
"@pipedream/platform": "^3.0.0"
}
}
}

56 changes: 51 additions & 5 deletions components/peach/peach.app.mjs
Original file line number Diff line number Diff line change
@@ -1,11 +1,57 @@
import { axios } from "@pipedream/platform";

export default {
type: "app",
app: "peach",
propDefinitions: {},
propDefinitions: {
phoneNumber: {
type: "string",
label: "Phone Number",
description: "The phone number of the contact to send the message to",
},
contactName: {
type: "string",
label: "Contact Name",
description: "The name of the contact",
},
contactEmail: {
type: "string",
label: "Contact Email",
description: "The email of the contact",
},
},
methods: {
// this.$auth contains connected account data
authKeys() {
console.log(Object.keys(this.$auth));
_baseUrl() {
return "https://app.trypeach.io/api/v1";
},
_headers() {
return {
"Authorization": `${this.$auth.api_token}`,
"Content-Type": "application/json",
};
},
_makeRequest({
$ = this, path, ...opts
}) {
return axios($, {
url: this._baseUrl() + path,
headers: this._headers(),
...opts,
});
},
sendTransactionalMessage(opts = {}) {
return this._makeRequest({
method: "POST",
path: "/transactional_messages",
...opts,
});
},
createContact(opts = {}) {
return this._makeRequest({
method: "POST",
path: "/contacts",
...opts,
});
},
},
};
};
5 changes: 4 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading