|
| 1 | +import freshdesk from "../../freshdesk.app.mjs"; |
| 2 | +import { removeNullEntries } from "../../common/utils.mjs"; |
| 3 | + |
| 4 | +export default { |
| 5 | + key: "freshdesk-update-ticket", |
| 6 | + name: "Update a Ticket", |
| 7 | + description: "Update status, priority, subject, description, agent, group, etc. [See the documentation](https://developers.freshdesk.com/api/#update_ticket).", |
| 8 | + version: "0.0.1", |
| 9 | + type: "action", |
| 10 | + props: { |
| 11 | + freshdesk, |
| 12 | + ticketId: { |
| 13 | + propDefinition: [ |
| 14 | + freshdesk, |
| 15 | + "ticketId", |
| 16 | + ], |
| 17 | + }, |
| 18 | + status: { |
| 19 | + propDefinition: [ |
| 20 | + freshdesk, |
| 21 | + "ticketStatus", |
| 22 | + ], |
| 23 | + optional: true, |
| 24 | + }, |
| 25 | + priority: { |
| 26 | + propDefinition: [ |
| 27 | + freshdesk, |
| 28 | + "ticketPriority", |
| 29 | + ], |
| 30 | + optional: true, |
| 31 | + }, |
| 32 | + subject: { |
| 33 | + type: "string", |
| 34 | + label: "Subject", |
| 35 | + description: "Ticket subject", |
| 36 | + optional: true, |
| 37 | + }, |
| 38 | + description: { |
| 39 | + type: "string", |
| 40 | + label: "Description", |
| 41 | + description: "Detailed ticket description (HTML allowed)", |
| 42 | + optional: true, |
| 43 | + }, |
| 44 | + group_id: { |
| 45 | + propDefinition: [ |
| 46 | + freshdesk, |
| 47 | + "groupId", |
| 48 | + ], |
| 49 | + }, |
| 50 | + responder_id: { |
| 51 | + propDefinition: [ |
| 52 | + freshdesk, |
| 53 | + "agentId", |
| 54 | + ], |
| 55 | + }, |
| 56 | + email: { |
| 57 | + type: "string", |
| 58 | + label: "Requester Email (replaces requester)", |
| 59 | + description: "Updates the requester. If no contact with this email exists, a new one will be created and assigned to the ticket.", |
| 60 | + optional: true, |
| 61 | + }, |
| 62 | + phone: { |
| 63 | + type: "string", |
| 64 | + label: "Requester Phone (replaces requester)", |
| 65 | + description: "If no contact with this phone number exists, a new one will be created. If used without email, 'name' is required.", |
| 66 | + optional: true, |
| 67 | + }, |
| 68 | + name: { |
| 69 | + type: "string", |
| 70 | + label: "Requester Name (required with phone if no email)", |
| 71 | + description: "Used when creating a contact with phone but no email.", |
| 72 | + optional: true, |
| 73 | + }, |
| 74 | + type: { |
| 75 | + type: "string", |
| 76 | + label: "Type", |
| 77 | + description: "Type of ticket (must match one of the allowed values)", |
| 78 | + optional: true, |
| 79 | + options: [ |
| 80 | + "Question", |
| 81 | + "Incident", |
| 82 | + "Problem", |
| 83 | + "Feature Request", |
| 84 | + "Refund", |
| 85 | + ], |
| 86 | + }, |
| 87 | + custom_fields: { |
| 88 | + type: "object", |
| 89 | + label: "Custom Fields", |
| 90 | + description: "Custom fields as key-value pairs (make sure types match your config)", |
| 91 | + optional: true, |
| 92 | + }, |
| 93 | + }, |
| 94 | + async run({ $ }) { |
| 95 | + const { |
| 96 | + freshdesk, |
| 97 | + ticketId, |
| 98 | + ...fields |
| 99 | + } = this; |
| 100 | + |
| 101 | + const data = removeNullEntries(fields); |
| 102 | + |
| 103 | + const ticketName = await freshdesk.getTicketName(ticketId); |
| 104 | + |
| 105 | + if (!Object.keys(data).length) { |
| 106 | + throw new Error("Please provide at least one field to update."); |
| 107 | + } |
| 108 | + |
| 109 | + if (data.custom_fields) freshdesk.parseIfJSONString(data.custom_fields); |
| 110 | + |
| 111 | + const response = await freshdesk._makeRequest({ |
| 112 | + $, |
| 113 | + method: "PUT", |
| 114 | + url: `/tickets/${ticketId}`, |
| 115 | + data, |
| 116 | + }); |
| 117 | + |
| 118 | + $.export("$summary", `Ticket "${ticketName}" (ID: ${this.ticketId}) updated successfully`); |
| 119 | + return response; |
| 120 | + }, |
| 121 | +}; |
| 122 | + |
0 commit comments