Skip to content

Commit 8c0269a

Browse files
luancazarinelcaresia
authored andcommitted
[ACTION] Loops.so - Add to Mailing List #14614 (#14651)
Actions - Create Contact - Update Contact
1 parent 7ec4d71 commit 8c0269a

File tree

12 files changed

+118
-32
lines changed

12 files changed

+118
-32
lines changed

components/loops_so/actions/common/common-create-update.mjs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
/* eslint-disable no-unused-vars */
2+
import pickBy from "lodash.pickby";
3+
import { parseObject } from "../../common/utils.mjs";
14
import loops from "../../loops_so.app.mjs";
25

36
export default {
@@ -31,17 +34,61 @@ export default {
3134
"lastName",
3235
],
3336
},
37+
source: {
38+
propDefinition: [
39+
loops,
40+
"source",
41+
],
42+
},
43+
subscribed: {
44+
propDefinition: [
45+
loops,
46+
"subscribed",
47+
],
48+
},
3449
userGroup: {
3550
propDefinition: [
3651
loops,
3752
"userGroup",
3853
],
3954
},
55+
userId: {
56+
propDefinition: [
57+
loops,
58+
"userId",
59+
],
60+
},
61+
mailingLists: {
62+
propDefinition: [
63+
loops,
64+
"mailingLists",
65+
],
66+
},
4067
customFields: {
4168
propDefinition: [
4269
loops,
4370
"customFields",
4471
],
4572
},
4673
},
74+
methods: {
75+
prepareData() {
76+
const {
77+
loops,
78+
customFields,
79+
mailingLists,
80+
...data
81+
} = this;
82+
83+
const mailingListObject = {};
84+
for (const item of (parseObject(mailingLists) || [])) {
85+
mailingListObject[item] = true;
86+
}
87+
88+
return pickBy({
89+
mailingLists: mailingListObject,
90+
...data,
91+
});
92+
},
93+
},
4794
};

components/loops_so/actions/create-contact/create-contact.mjs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,11 @@ export default {
55
key: "loops_so-create-contact",
66
name: "Create Contact",
77
description: "Creates a new contact. [See the Documentation](https://loops.so/docs/add-users/api-reference#add)",
8-
version: "0.1.1",
8+
version: "0.2.0",
99
type: "action",
1010
async run({ $ }) {
11-
const { // eslint-disable-next-line no-unused-vars
12-
loops, email, firstName, lastName, userGroup, customFields, ...data
13-
} = this;
14-
const response = await loops.createContact({
15-
data: {
16-
email,
17-
firstName,
18-
lastName,
19-
userGroup,
20-
...data,
21-
},
11+
const response = await this.loops.createContact({
12+
data: this.prepareData(),
2213
$,
2314
});
2415

components/loops_so/actions/delete-contact/delete-contact.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export default {
55
key: "loops_so-delete-contact",
66
name: "Delete Contact",
77
description: "Delete an existing contact. [See the documentation](https://loops.so/docs/api-reference/delete-contact)",
8-
version: "0.0.1",
8+
version: "0.0.2",
99
type: "action",
1010
props: {
1111
loops,

components/loops_so/actions/find-contact/find-contact.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export default {
44
key: "loops_so-find-contact",
55
name: "Find Contact",
66
description: "Search for a contact by email address. [See the Documentation](https://loops.so/docs/add-users/api-reference#find)",
7-
version: "0.0.3",
7+
version: "0.0.4",
88
type: "action",
99
props: {
1010
loops,

components/loops_so/actions/list-custom-fields/list-custom-fields.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export default {
44
key: "loops_so-list-custom-fields",
55
name: "List Custom Fields",
66
description: "List your account's custom contact properties. [See the documentation](https://loops.so/docs/api-reference/list-custom-fields)",
7-
version: "0.0.1",
7+
version: "0.0.2",
88
type: "action",
99
props: {
1010
loops,

components/loops_so/actions/list-mailing-lists/list-mailing-lists.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export default {
44
key: "loops_so-list-mailing-lists",
55
name: "List Mailing Lists",
66
description: "List your account's mailing lists. [See the documentation](https://loops.so/docs/api-reference/list-mailing-lists)",
7-
version: "0.0.1",
7+
version: "0.0.2",
88
type: "action",
99
props: {
1010
loops,

components/loops_so/actions/send-event/send-event.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export default {
44
key: "loops_so-send-event",
55
name: "Send Event",
66
description: "Send an event to an email address. [See the Documentation](https://loops.so/docs/add-users/api-reference#send)",
7-
version: "0.0.3",
7+
version: "0.0.4",
88
type: "action",
99
props: {
1010
loops,

components/loops_so/actions/send-transactional-email/send-transactional-email.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export default {
44
key: "loops_so-send-transactional-email",
55
name: "Send Transactional Email",
66
description: "Send a transactional email. [See the Documentation](https://loops.so/docs/transactional/guide#send-your-email)",
7-
version: "0.0.3",
7+
version: "0.0.4",
88
type: "action",
99
props: {
1010
loops,

components/loops_so/actions/update-contact/update-contact.mjs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,15 @@
11
import common from "../common/common-create-update.mjs";
2-
import pickBy from "lodash.pickby";
32

43
export default {
54
...common,
65
key: "loops_so-update-contact",
76
name: "Update Contact",
87
description: "Updates an existing contact by email. If email not found, a new contact will be created. [See the Documentation](https://loops.so/docs/add-users/api-reference#update)",
9-
version: "0.1.1",
8+
version: "0.2.0",
109
type: "action",
1110
async run({ $ }) {
12-
const { // eslint-disable-next-line no-unused-vars
13-
loops, email, firstName, lastName, userGroup, customFields, ...data
14-
} = this;
15-
const response = await loops.updateContact({
16-
data: pickBy({
17-
email,
18-
firstName,
19-
lastName,
20-
userGroup,
21-
...data,
22-
}),
11+
const response = await this.loops.updateContact({
12+
data: this.prepareData(),
2313
$,
2414
});
2515

components/loops_so/common/utils.mjs

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+
};

0 commit comments

Comments
 (0)