diff --git a/components/zendesk/actions/set-custom-ticket-fields/set-custom-ticket-fields.mjs b/components/zendesk/actions/set-custom-ticket-fields/set-custom-ticket-fields.mjs new file mode 100644 index 0000000000000..ff04d705cfa88 --- /dev/null +++ b/components/zendesk/actions/set-custom-ticket-fields/set-custom-ticket-fields.mjs @@ -0,0 +1,80 @@ +import app from "../../zendesk.app.mjs"; +import { parseObject } from "../../common/utils.mjs"; +import { ConfigurationError } from "@pipedream/platform"; + +export default { + key: "zendesk-set-custom-ticket-fields", + name: "Set Custom Ticket Fields", + description: "Sets one or more custom field values on a ticket. [See the documentation](https://developer.zendesk.com/api-reference/ticketing/tickets/tickets/#update-ticket).", + type: "action", + version: "0.0.1", + props: { + app, + ticketId: { + propDefinition: [ + app, + "ticketId", + ], + }, + customFields: { + type: "string[]", + label: "Custom Fields", + description: "Array of custom field objects. Each item should be formatted as `{\"id\": \"field_id\", \"value\": \"field_value\"}`. Example: `{\"id\": \"23129751115165\", \"value\": \"ABCDE\"}`", + }, + customSubdomain: { + propDefinition: [ + app, + "customSubdomain", + ], + }, + }, + methods: { + updateTicket({ + ticketId, ...args + } = {}) { + return this.app.update({ + path: `/tickets/${ticketId}`, + ...args, + }); + }, + }, + async run({ $: step }) { + const { + ticketId, + customFields, + customSubdomain, + } = this; + + // Parse custom fields from string array to objects + const parsedCustomFields = parseObject(customFields); + + if (!Array.isArray(parsedCustomFields)) { + throw new ConfigurationError("Custom Fields must be an array of custom field objects"); + } + + // Validate custom fields structure + parsedCustomFields.forEach((field, index) => { + if (!field.id) { + throw new ConfigurationError(`Custom field at index ${index} is missing required "id" property`); + } + if (field.value === undefined) { + throw new ConfigurationError(`Custom field at index ${index} is missing required "value" property`); + } + }); + + const response = await this.updateTicket({ + step, + ticketId, + customSubdomain, + data: { + ticket: { + custom_fields: parsedCustomFields, + }, + }, + }); + + step.export("$summary", `Successfully updated ${parsedCustomFields.length} custom field(s) on ticket ${response.ticket.id}`); + + return response; + }, +}; diff --git a/components/zendesk/common/utils.mjs b/components/zendesk/common/utils.mjs new file mode 100644 index 0000000000000..c2b75bdc360c0 --- /dev/null +++ b/components/zendesk/common/utils.mjs @@ -0,0 +1,25 @@ +export const parseObject = (obj) => { + if (!obj) { + return {}; + } + if (typeof obj === "string") { + try { + return JSON.parse(obj); + } catch { + return obj; + } + } + if (Array.isArray(obj)) { + return obj.map(parseObject); + } + if (typeof obj === "object") { + return Object.fromEntries(Object.entries(obj).map(([ + key, + value, + ]) => [ + key, + parseObject(value), + ])); + } + return obj; +}; diff --git a/components/zendesk/package.json b/components/zendesk/package.json index 58a97676945b3..2c7d0f93f99f7 100644 --- a/components/zendesk/package.json +++ b/components/zendesk/package.json @@ -1,6 +1,6 @@ { "name": "@pipedream/zendesk", - "version": "0.9.0", + "version": "0.10.0", "description": "Pipedream Zendesk Components", "main": "zendesk.app.mjs", "keywords": [