Skip to content

Commit 0a7fe7c

Browse files
committed
Refactor Pingback actions to simplify data handling and improve code consistency. Updated Add, Create, Remove, and Update Subscriber actions to use a unified data structure. Introduced parseCustomFields utility for better custom fields management.
1 parent 5000101 commit 0a7fe7c

File tree

5 files changed

+48
-39
lines changed

5 files changed

+48
-39
lines changed

components/pingback/actions/add-subscriber-to-segmentation-lists/add-subscriber-to-segmentation-lists.mjs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import { defineAction } from "@pipedream/types";
21
import { parseObject } from "../../common/utils.mjs";
32
import pingback from "../../pingback.app.mjs";
43

5-
export default defineAction({
4+
export default {
65
name: "Add Subscriber To Segmentation Lists",
76
description: "Add a subscriber to segmentation lists by email [See the documentation](https://developer.pingback.com/docs/api/add-subscriber-to-segment)",
87
key: "pingback-add-subscriber-to-segmentation-lists",
@@ -35,4 +34,4 @@ export default defineAction({
3534
$.export("$summary", `Subscriber ${this.email} added to segmentation list(s) successfully`);
3635
return response;
3736
},
38-
});
37+
};

components/pingback/actions/create-subscriber/create-subscriber.mjs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import { parseObject } from "../../common/utils.mjs";
1+
import {
2+
parseCustomFields,
3+
parseObject,
4+
} from "../../common/utils.mjs";
25
import pingback from "../../pingback.app.mjs";
36

47
export default {
@@ -47,22 +50,19 @@ export default {
4750
},
4851
},
4952
async run({ $ }) {
53+
const data = {
54+
email: this.email,
55+
phone: this.phone,
56+
name: this.name,
57+
status: this.status,
58+
segmentationLists: parseObject(this.segmentationLists),
59+
};
60+
61+
data.customFields = parseCustomFields(this.customFields);
62+
5063
const response = await this.pingback.createSubscriber({
5164
$,
52-
data: {
53-
email: this.email,
54-
phone: this.phone,
55-
name: this.name,
56-
status: this.status,
57-
customFields: Object.entries(parseObject(this.customFields))?.map(([
58-
key,
59-
value,
60-
]) => ({
61-
label: key,
62-
value,
63-
})),
64-
segmentationLists: parseObject(this.segmentationLists),
65-
},
65+
data,
6666
});
6767

6868
$.export("$summary", `Subscriber created successfully with ID: ${response.data.data.id}`);

components/pingback/actions/remove-subscriber-from-segmentation-list/remove-subscriber-from-segmentation-list.mjs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import { defineAction } from "@pipedream/types";
21
import pingback from "../../pingback.app.mjs";
32

4-
export default defineAction({
3+
export default {
54
name: "Remove Subscriber From Segmentation List",
65
description: "Remove a subscriber from a segmentation list by email [See the documentation](https://developer.pingback.com/docs/api/remove-subscriber-from-segment)",
76
key: "pingback-remove-subscriber-from-segmentation-list",
@@ -18,9 +17,11 @@ export default defineAction({
1817
segmentationListId: {
1918
propDefinition: [
2019
pingback,
21-
"segmentationListId",
20+
"segmentationLists",
2221
],
23-
type: "string",
22+
type: "string[]",
23+
label: "Segmentation Lists",
24+
description: "Segmentation list ID to remove the subscriber from. You can get the ID by clicking audience and lists at Pingback dashboard.",
2425
},
2526
},
2627
async run({ $ }) {
@@ -33,4 +34,4 @@ export default defineAction({
3334
$.export("$summary", `Subscriber ${this.email} removed from segmentation list ${this.segmentationListId} successfully`);
3435
return response;
3536
},
36-
});
37+
};

components/pingback/actions/update-subscriber/update-subscriber.mjs

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import { defineAction } from "@pipedream/types";
2-
import { parseObject } from "../../common/utils.mjs";
1+
import { parseCustomFields } from "../../common/utils.mjs";
32
import pingback from "../../pingback.app.mjs";
43

5-
export default defineAction({
4+
export default {
65
name: "Update Subscriber",
76
description: "Update a specific subscriber by email [See the documentation](https://developer.pingback.com/docs/api/update-subscriber)",
87
key: "pingback-update-subscriber",
@@ -42,24 +41,21 @@ export default defineAction({
4241
},
4342
},
4443
async run({ $ }) {
44+
const data = {
45+
phone: this.phone,
46+
name: this.name,
47+
status: this.status,
48+
};
49+
50+
data.customFields = parseCustomFields(this.customFields);
51+
4552
const response = await this.pingback.updateSubscriber({
4653
$,
4754
email: this.email,
48-
data: {
49-
phone: this.phone,
50-
name: this.name,
51-
status: this.status,
52-
customFields: Object.entries(parseObject(this.customFields))?.map(([
53-
key,
54-
value,
55-
]) => ({
56-
label: key,
57-
value,
58-
})),
59-
},
55+
data,
6056
});
6157

6258
$.export("$summary", `Subscriber updated successfully with ID: ${response.data.data.id}`);
6359
return response;
6460
},
65-
});
61+
};

components/pingback/common/utils.mjs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,16 @@ export const parseObject = (obj) => {
2222
}
2323
return obj;
2424
};
25+
26+
export const parseCustomFields = (customFields) => {
27+
const parsedCustomFields = Object.entries(parseObject(customFields) || {});
28+
if (parsedCustomFields.length) {
29+
return parsedCustomFields.map(([
30+
key,
31+
value,
32+
]) => ({
33+
label: key,
34+
value,
35+
}));
36+
}
37+
};

0 commit comments

Comments
 (0)