Skip to content

Commit

Permalink
New Components - acymailing (#12377)
Browse files Browse the repository at this point in the history
* acymailing init

* [Components] acymailing #12376
Polling Sources
- New Confirmed User
- New Unsubscribed User
- New Subscribed User

Actions
 - Add Update User
 - Email User
 - Subscribe User

* pnpm update

* Fix listIds prop on sources

---------

Co-authored-by: Leo Vu <[email protected]>
  • Loading branch information
luancazarine and vunguyenhung authored Jun 17, 2024
1 parent 1a109ca commit 366936d
Show file tree
Hide file tree
Showing 12 changed files with 532 additions and 8 deletions.
77 changes: 77 additions & 0 deletions components/acymailing/actions/add-update-user/add-update-user.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import acymailing from "../../acymailing.app.mjs";
import { parseObject } from "../../common/utils.mjs";

export default {
key: "acymailing-add-update-user",
name: "Add or Update User",
description: "Creates a new user or updates an existing user in AcyMailing. If the user exists, will update the user's data with provided information. [See the documentation](https://docs.acymailing.com/v/rest-api/users#create-or-update-a-user)",
version: "0.0.1",
type: "action",
props: {
acymailing,
email: {
type: "string",
label: "Email",
description: "The email address is used when updating an existing user.",
},
name: {
type: "string",
label: "Name",
description: "Any character should be available.",
optional: true,
},
active: {
type: "boolean",
label: "Active",
description: "Defaults to true.",
optional: true,
},
confirmed: {
type: "boolean",
label: "Confirmed",
description: "The confirmation is related to the \"Require confirmation\" option in the configuration, tab \"Subscription\".",
optional: true,
},
cmsId: {
type: "integer",
label: "CMS Id",
description: "The cms_id must match the ID of the corresponding Joomla/WordPress user.",
optional: true,
},
customFields: {
type: "object",
label: "Custom Fields",
description: "An object of field Ids and values.",
optional: true,
},
triggers: {
type: "boolean",
label: "Triggers",
description: "Defaults to true. Defines if the saving of the user triggers automated tasks like follow-up campaigns and automations.",
optional: true,
},
sendConf: {
type: "boolean",
label: "Send Conf",
description: "Defaults to true. Defines if the confirmation email should be sent when a new user is created.",
optional: true,
},
},
async run({ $ }) {
const response = await this.acymailing.createUserOrUpdate({
$,
data: {
email: this.email,
name: this.name,
active: this.active,
confirmed: this.confirmed,
cmsId: this.cmsId,
customFields: parseObject(this.customFields),
triggers: this.triggers,
sendConf: this.sendConf,
},
});
$.export("$summary", `Successfully added or updated user with email with Id: ${response.userId}`);
return response;
},
};
55 changes: 55 additions & 0 deletions components/acymailing/actions/email-user/email-user.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import acymailing from "../../acymailing.app.mjs";
import { parseObject } from "../../common/utils.mjs";

export default {
key: "acymailing-email-user",
name: "Email User",
description: "Sends an email to a single AcyMailing user. The user must exist in the AcyMailing database. [See the documentation](https://docs.acymailing.com/v/rest-api/emails#send-an-email-to-a-user)",
version: "0.0.1",
type: "action",
props: {
acymailing,
email: {
type: "string",
label: "Email",
description: "The email address of the receiver.",
},
autoAddUser: {
type: "boolean",
label: "Auto Add User",
description: "Defaults to false. If the email address doesn't match an existing AcyMailing user, one will be automatically created if this option is set to true.",
optional: true,
},
emailId: {
type: "integer",
label: "Email Id",
description: "The mail ID to send. This is not a campaign ID but the mail ID of the table xxx_acym_mail in the database, or the mail_id of a campaign.",
},
trackEmail: {
type: "boolean",
label: "Track Email",
description: "Defaults to true. If true, the open/click statistics will be collected for this email.",
optional: true,
},
params: {
type: "object",
label: "Params",
description: "An object of shortcodes and values to replace in the body of the sent email. Example: { \"shortcode1\": \"value 1\" }. If the body of the sent email contains the text \"{shortcode1}\", it will be replaced by \"value 1\" in the sent version.",
optional: true,
},
},
async run({ $ }) {
const response = await this.acymailing.sendEmailToUser({
$,
data: {
email: this.email,
autoAddUser: this.autoAddUser,
emailId: this.emailId,
trackEmail: this.trackEmail,
params: parseObject(this.params),
},
});
$.export("$summary", `Email successfully sent to ${this.email}`);
return response;
},
};
51 changes: 51 additions & 0 deletions components/acymailing/actions/subscribe-user/subscribe-user.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import acymailing from "../../acymailing.app.mjs";
import { parseObject } from "../../common/utils.mjs";

export default {
key: "acymailing-subscribe-user",
name: "Subscribe User to Lists",
description: "Subscribes a user to one or more specified lists in AcyMailing. [See the documentation](https://docs.acymailing.com/v/rest-api/subscription#subscribe-users-to-lists)",
version: "0.0.1",
type: "action",
props: {
acymailing,
emails: {
propDefinition: [
acymailing,
"emails",
],
},
listIds: {
propDefinition: [
acymailing,
"listIds",
],
},
sendWelcomeEmail: {
type: "boolean",
label: "Send Welcome Email",
description: "Defaults to true. If true, the welcome emails will be sent if the lists have one.",
optional: true,
},
trigger: {
type: "boolean",
label: "Trigger",
description: "Defaults to true. If you want to trigger or not the automation or follow-up when subscribing the user.",
optional: true,
},
},
async run({ $ }) {
const response = await this.acymailing.subscribeUserToLists({
$,
data: {
emails: parseObject(this.emails),
listIds: parseObject(this.listIds),
sendWelcomeEmail: this.sendWelcomeEmail,
trigger: this.trigger,
},
});

$.export("$summary", `Successfully subscribed ${this.emails.length} users to lists ${this.listIds.length} lists`);
return response;
},
};
145 changes: 140 additions & 5 deletions components/acymailing/acymailing.app.mjs
Original file line number Diff line number Diff line change
@@ -1,11 +1,146 @@
import { axios } from "@pipedream/platform";
import { LIMIT } from "./common/constants.mjs";

export default {
type: "app",
app: "acymailing",
propDefinitions: {},
propDefinitions: {
listIds: {
type: "integer[]",
label: "List Ids",
description: "Array of list IDs.",
async options({ page }) {
const lists = await this.listLists({
params: {
limit: LIMIT,
offset: LIMIT * page,
},
});

return lists.map(({
id: value, name: label,
}) => ({
label,
value,
}));
},
},
emails: {
type: "string[]",
label: "Emails",
description: "The email addresses of users to subscribe to the lists. These must match already existing AcyMailing users.",
async options({ page }) {
const data = await this.listUsers({
params: {
limit: LIMIT,
offset: LIMIT * page,
},
});

return data.map(({ email }) => email);
},
},
},
methods: {
// this.$auth contains connected account data
authKeys() {
console.log(Object.keys(this.$auth));
_baseUrl() {
return `${this.$auth.url}`;
},
_headers() {
return {
"Api-Key": `${this.$auth.api_key}`,
"Content-Type": "application/json",
};
},
_params(params) {
return {
page: "acymailing_front",
option: "com_acym",
ctrl: "api",
...params,
};
},
_makeRequest({
$ = this, params, task, ...opts
}) {
return axios($, {
url: `${this._baseUrl()}`,
params: this._params({
...params,
task,
}),
headers: this._headers(),
...opts,
});
},
listUsers(opts = {}) {
return this._makeRequest({
task: "getUsers",
...opts,
});
},
listLists(opts = {}) {
return this._makeRequest({
task: "getLists",
...opts,
});
},
listSubscribersFromLists(opts = {}) {
return this._makeRequest({
task: "getSubscribersFromLists",
...opts,
});
},
listUnsubscribedUsersFromLists(opts = {}) {
return this._makeRequest({
task: "getUnsubscribedUsersFromLists",
...opts,
});
},
createUserOrUpdate(opts = {}) {
return this._makeRequest({
method: "POST",
task: "createOrUpdateUser",
...opts,
});
},
sendEmailToUser(opts = {}) {
return this._makeRequest({
method: "POST",
task: "sendEmailToSingleUser",
...opts,
});
},
subscribeUserToLists(opts = {}) {
return this._makeRequest({
method: "POST",
task: "subscribeUsers",
...opts,
});
},
async *paginate({
fn, params = {}, ...opts
}) {
let hasMore = false;
let page = 0;

do {
params.limit = LIMIT;
params.offset = LIMIT * page;
page++;

const data = await fn({
params,
...opts,
});

for (const d of data) {
yield d;
}

hasMore = data.length;

} while (hasMore);
},
},
};

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

Loading

0 comments on commit 366936d

Please sign in to comment.