|
| 1 | +import acumbamail from "../../acumbamail.app.mjs"; |
| 2 | +import { parseObject } from "../../common/utils.mjs"; |
| 3 | + |
| 4 | +export default { |
| 5 | + key: "acumbamail-add-or-update-subscriber", |
| 6 | + name: "Add or Update Subscriber", |
| 7 | + description: "Adds a new subscriber to a list or updates an existing subscriber. [See the documentation](https://acumbamail.com/en/apidoc/function/addSubscriber/)", |
| 8 | + version: "0.0.1", |
| 9 | + annotations: { |
| 10 | + destructiveHint: false, |
| 11 | + openWorldHint: true, |
| 12 | + readOnlyHint: false, |
| 13 | + }, |
| 14 | + type: "action", |
| 15 | + props: { |
| 16 | + acumbamail, |
| 17 | + listId: { |
| 18 | + propDefinition: [ |
| 19 | + acumbamail, |
| 20 | + "listId", |
| 21 | + ], |
| 22 | + reloadProps: true, |
| 23 | + }, |
| 24 | + doubleOptin: { |
| 25 | + type: "boolean", |
| 26 | + label: "Double Opt-in", |
| 27 | + description: "Activates sending of confirmation email when adding the subscriber", |
| 28 | + optional: true, |
| 29 | + }, |
| 30 | + updateSubscriber: { |
| 31 | + type: "boolean", |
| 32 | + label: "Update Subscriber", |
| 33 | + description: "Modifies the fields in the case of a subscriber who is already on the list", |
| 34 | + optional: true, |
| 35 | + }, |
| 36 | + mergeFields: { |
| 37 | + type: "object", |
| 38 | + label: "Merge Fields", |
| 39 | + description: "Optionally, add merge fields as a single object. This is a Dictionary that contains the merge tags of the subscriber as keys and the value that will be added to the subscriber", |
| 40 | + optional: true, |
| 41 | + }, |
| 42 | + }, |
| 43 | + async additionalProps() { |
| 44 | + const props = {}; |
| 45 | + if (!this.listId) { |
| 46 | + return props; |
| 47 | + } |
| 48 | + const fields = await this.acumbamail.getFields({ |
| 49 | + params: { |
| 50 | + list_id: this.listId, |
| 51 | + }, |
| 52 | + }); |
| 53 | + for (const key of Object.keys(fields)) { |
| 54 | + props[key] = { |
| 55 | + type: "string", |
| 56 | + label: key, |
| 57 | + optional: true, |
| 58 | + }; |
| 59 | + } |
| 60 | + return props; |
| 61 | + }, |
| 62 | + async run({ $ }) { |
| 63 | + const mergeFields = parseObject(this.mergeFields); |
| 64 | + const fields = await this.acumbamail.getFields({ |
| 65 | + params: { |
| 66 | + list_id: this.listId, |
| 67 | + }, |
| 68 | + }); |
| 69 | + for (const key of Object.keys(fields)) { |
| 70 | + if (!this[key]) continue; |
| 71 | + mergeFields[key] = this[key]; |
| 72 | + } |
| 73 | + |
| 74 | + const response = await this.acumbamail.addOrUpdateSubscriber({ |
| 75 | + $, |
| 76 | + data: { |
| 77 | + list_id: this.listId, |
| 78 | + double_optin: this.doubleOptin, |
| 79 | + update_subscriber: this.updateSubscriber, |
| 80 | + merge_fields: mergeFields, |
| 81 | + complete_json: true, |
| 82 | + }, |
| 83 | + }); |
| 84 | + |
| 85 | + $.export("$summary", "Successfully added or updated subscriber."); |
| 86 | + return response; |
| 87 | + }, |
| 88 | +}; |
0 commit comments