diff --git a/components/servicenow/actions/create-case/create-case.mjs b/components/servicenow/actions/create-case/create-case.mjs new file mode 100644 index 0000000000000..d524c5f374699 --- /dev/null +++ b/components/servicenow/actions/create-case/create-case.mjs @@ -0,0 +1,114 @@ +import servicenow from "../../servicenow.app.mjs"; + +export default { + key: "servicenow-create-case", + name: "Create Case", + description: "Creates a new case record in ServiceNow. [See the docs here](https://www.servicenow.com/docs/bundle/zurich-api-reference/page/integrate/inbound-rest/concept/trouble-ticket-open-api.html#title_trouble-ticket-POST-ticket-tt).", + version: "0.0.1", + type: "action", + annotations: { + destructiveHint: false, + openWorldHint: true, + readOnlyHint: false, + }, + props: { + servicenow, + name: { + propDefinition: [ + servicenow, + "name", + ], + }, + description: { + propDefinition: [ + servicenow, + "description", + ], + }, + severity: { + propDefinition: [ + servicenow, + "caseSeverity", + ], + }, + status: { + propDefinition: [ + servicenow, + "status", + ], + }, + channelName: { + propDefinition: [ + servicenow, + "channelName", + ], + }, + accountId: { + propDefinition: [ + servicenow, + "accountId", + ], + }, + contactId: { + propDefinition: [ + servicenow, + "contactId", + ], + }, + workNote: { + propDefinition: [ + servicenow, + "workNote", + ], + }, + comment: { + propDefinition: [ + servicenow, + "comment", + ], + }, + }, + async run({ $ }) { + const { + name, + description, + severity, + status, + channelName, + accountId, + contactId, + workNote, + comment, + } = this; + + const channel = this.servicenow.buildChannel(channelName); + + const notes = this.servicenow.buildNotes({ + workNote, + comment, + }); + + const relatedParties = this.servicenow.buildRelatedParties({ + customer: accountId, + customer_contact: contactId, + }); + + const response = await this.servicenow.createTroubleTicket({ + $, + data: { + ticketType: "Case", + name, + description, + severity, + status, + channel, + notes, + relatedParties, + }, + }); + + $.export("$summary", "Successfully created a case."); + + return response; + }, +}; diff --git a/components/servicenow/actions/create-incident/create-incident.mjs b/components/servicenow/actions/create-incident/create-incident.mjs new file mode 100644 index 0000000000000..d3c4af6256281 --- /dev/null +++ b/components/servicenow/actions/create-incident/create-incident.mjs @@ -0,0 +1,114 @@ +import servicenow from "../../servicenow.app.mjs"; + +export default { + key: "servicenow-create-incident", + name: "Create Incident", + description: "Creates a new incident record in ServiceNow. [See the docs here](https://www.servicenow.com/docs/bundle/zurich-api-reference/page/integrate/inbound-rest/concept/trouble-ticket-open-api.html#title_trouble-ticket-POST-ticket-tt).", + version: "0.0.1", + type: "action", + annotations: { + destructiveHint: false, + openWorldHint: true, + readOnlyHint: false, + }, + props: { + servicenow, + name: { + propDefinition: [ + servicenow, + "name", + ], + }, + description: { + propDefinition: [ + servicenow, + "description", + ], + }, + severity: { + propDefinition: [ + servicenow, + "incidentSeverity", + ], + }, + status: { + propDefinition: [ + servicenow, + "status", + ], + }, + contactMethod: { + propDefinition: [ + servicenow, + "contactMethod", + ], + }, + companyId: { + propDefinition: [ + servicenow, + "companyId", + ], + }, + userId: { + propDefinition: [ + servicenow, + "userId", + ], + }, + workNote: { + propDefinition: [ + servicenow, + "workNote", + ], + }, + comment: { + propDefinition: [ + servicenow, + "comment", + ], + }, + }, + async run({ $ }) { + const { + name, + description, + severity, + status, + contactMethod, + companyId, + userId, + workNote, + comment, + } = this; + + const channel = this.servicenow.buildChannel(contactMethod); + + const relatedParties = this.servicenow.buildRelatedParties({ + customer: companyId, + customer_contact: userId, + }); + + const notes = this.servicenow.buildNotes({ + workNote, + comment, + }); + + const response = await this.servicenow.createTroubleTicket({ + $, + data: { + ticketType: "Incident", + name, + description, + severity, + status, + channel, + notes, + relatedParties, + }, + }); + + $.export("$summary", "Successfully created an incident."); + + return response; + }, +}; diff --git a/components/servicenow/common/constants.mjs b/components/servicenow/common/constants.mjs new file mode 100644 index 0000000000000..441a75d22ddb0 --- /dev/null +++ b/components/servicenow/common/constants.mjs @@ -0,0 +1,31 @@ +const DEFAULT_SEVERITY_OPTIONS = [ + { + label: "Critical", + value: "1", + }, + { + label: "High", + value: "2", + }, + { + label: "Moderate", + value: "3", + }, + { + label: "Low", + value: "4", + }, +]; + +const INCIDENT_SEVERITY_OPTIONS = [ + ...DEFAULT_SEVERITY_OPTIONS, + { + label: "Planning", + value: "5", + }, +]; + +export default { + DEFAULT_SEVERITY_OPTIONS, + INCIDENT_SEVERITY_OPTIONS, +}; diff --git a/components/servicenow/package.json b/components/servicenow/package.json index 13cbbe37ea6df..a14661bb4f43a 100644 --- a/components/servicenow/package.json +++ b/components/servicenow/package.json @@ -1,6 +1,6 @@ { "name": "@pipedream/servicenow", - "version": "0.6.0", + "version": "0.7.0", "description": "Pipedream servicenow Components", "main": "servicenow.app.mjs", "keywords": [ @@ -13,6 +13,6 @@ "access": "public" }, "dependencies": { - "@pipedream/platform": "^3.0.0" + "@pipedream/platform": "^3.1.0" } } diff --git a/components/servicenow/servicenow.app.mjs b/components/servicenow/servicenow.app.mjs index c155b03b18d29..7443fc2314a0b 100644 --- a/components/servicenow/servicenow.app.mjs +++ b/components/servicenow/servicenow.app.mjs @@ -1,11 +1,161 @@ +import { axios } from "@pipedream/platform"; +import constants from "./common/constants.mjs"; + +const { + DEFAULT_SEVERITY_OPTIONS, + INCIDENT_SEVERITY_OPTIONS, +} = constants; + export default { type: "app", app: "servicenow", - propDefinitions: {}, + propDefinitions: { + name: { + type: "string", + label: "Name", + description: "A short description of the ticket issue.", + optional: true, + }, + description: { + type: "string", + label: "Description", + description: "A detailed description of the issue.", + }, + caseSeverity: { + type: "string", + label: "Severity", + description: "The priority/severity of the case.", + options: DEFAULT_SEVERITY_OPTIONS, + }, + incidentSeverity: { + type: "string", + label: "Severity", + description: "The priority/severity of the incident.", + options: INCIDENT_SEVERITY_OPTIONS, + }, + status: { + type: "string", + label: "Status", + description: "The current status of the ticket.", + optional: true, + default: "New", + }, + channelName: { + type: "string", + label: "Channel Name", + description: "The channel (`contact_type`) that the ticket was created through.", + optional: true, + }, + contactMethod: { + type: "string", + label: "Contact Method", + description: "Name of the contact method (`contact_type`) that the ticket was created through.", + optional: true, + }, + accountId: { + type: "string", + label: "Account ID", + description: "`Sys_id` of the account related to the case.", + optional: true, + }, + contactId: { + type: "string", + label: "Contact ID", + description: "`Sys_id` of the contact related to the case.", + optional: true, + }, + companyId: { + type: "string", + label: "Company ID", + description: "`Sys_id` of the company related to the incident.", + optional: true, + }, + userId: { + type: "string", + label: "User ID", + description: "`Sys_id` of the user related to the incident.", + optional: true, + }, + workNote: { + type: "string", + label: "Work Note", + description: "Internal work note for the ticket.", + optional: true, + }, + comment: { + type: "string", + label: "Comment", + description: "Additional comment for the ticket.", + optional: true, + }, + }, methods: { - // this.$auth contains connected account data - authKeys() { - console.log(Object.keys(this.$auth)); + baseUrl() { + const { instance_name: instanceName } = this.$auth; + return `https://${instanceName}.service-now.com`; + }, + authHeaders() { + const { oauth_access_token: oauthAccessToken } = this.$auth; + return { + "Authorization": `Bearer ${oauthAccessToken}`, + "Accept": "application/json", + "Content-Type": "application/json", + }; + }, + buildChannel(name) { + if (!name) { + return; + } + return { + name, + }; + }, + buildNotes({ + workNote, + comment, + } = {}) { + const notes = []; + if (workNote) { + notes.push({ + "text": workNote, + "@type": "work_notes", + }); + } + if (comment) { + notes.push({ + "text": comment, + "@type": "comments", + }); + } + return notes.length + ? notes + : undefined; + }, + buildRelatedParties(partyTypeToId = {}) { + const entries = Object.entries(partyTypeToId) + .filter(([ + , id, + ]) => id); + if (!entries.length) { + return; + } + return entries.map(([ + type, + id, + ]) => ({ + id, + "@referredType": type, + })); + }, + async createTroubleTicket({ + $ = this, data, + } = {}) { + return axios($, { + method: "post", + url: `${this.baseUrl()}/api/sn_ind_tsm_sdwan/ticket/troubleTicket`, + headers: this.authHeaders(), + data, + }); }, }, }; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index e6e8246269558..b775583780760 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -12900,8 +12900,8 @@ importers: components/servicenow: dependencies: '@pipedream/platform': - specifier: ^3.0.0 - version: 3.0.3 + specifier: ^3.1.0 + version: 3.1.0 components/servicetitan: {} @@ -31332,22 +31332,22 @@ packages: superagent@3.8.1: resolution: {integrity: sha512-VMBFLYgFuRdfeNQSMLbxGSLfmXL/xc+OO+BZp41Za/NRDBet/BNbkRJrYzCUu0u4GU0i/ml2dtT8b9qgkw9z6Q==} engines: {node: '>= 4.0'} - deprecated: Please upgrade to v9.0.0+ as we have fixed a public vulnerability with formidable dependency. Note that v9.0.0+ requires Node.js v14.18.0+. See https://github.com/ladjs/superagent/pull/1800 for insight. This project is supported and maintained by the team at Forward Email @ https://forwardemail.net + deprecated: Please upgrade to superagent v10.2.2+, see release notes at https://github.com/forwardemail/superagent/releases/tag/v10.2.2 - maintenance is supported by Forward Email @ https://forwardemail.net superagent@4.1.0: resolution: {integrity: sha512-FT3QLMasz0YyCd4uIi5HNe+3t/onxMyEho7C3PSqmti3Twgy2rXT4fmkTz6wRL6bTF4uzPcfkUCa8u4JWHw8Ag==} engines: {node: '>= 6.0'} - deprecated: Please upgrade to v9.0.0+ as we have fixed a public vulnerability with formidable dependency. Note that v9.0.0+ requires Node.js v14.18.0+. See https://github.com/ladjs/superagent/pull/1800 for insight. This project is supported and maintained by the team at Forward Email @ https://forwardemail.net + deprecated: Please upgrade to superagent v10.2.2+, see release notes at https://github.com/forwardemail/superagent/releases/tag/v10.2.2 - maintenance is supported by Forward Email @ https://forwardemail.net superagent@5.3.1: resolution: {integrity: sha512-wjJ/MoTid2/RuGCOFtlacyGNxN9QLMgcpYLDQlWFIhhdJ93kNscFonGvrpAHSCVjRVj++DGCglocF7Aej1KHvQ==} engines: {node: '>= 7.0.0'} - deprecated: Please upgrade to v9.0.0+ as we have fixed a public vulnerability with formidable dependency. Note that v9.0.0+ requires Node.js v14.18.0+. See https://github.com/ladjs/superagent/pull/1800 for insight. This project is supported and maintained by the team at Forward Email @ https://forwardemail.net + deprecated: Please upgrade to superagent v10.2.2+, see release notes at https://github.com/forwardemail/superagent/releases/tag/v10.2.2 - maintenance is supported by Forward Email @ https://forwardemail.net superagent@7.1.6: resolution: {integrity: sha512-gZkVCQR1gy/oUXr+kxJMLDjla434KmSOKbx5iGD30Ql+AkJQ/YlPKECJy2nhqOsHLjGHzoDTXNSjhnvWhzKk7g==} engines: {node: '>=6.4.0 <13 || >=14'} - deprecated: Please upgrade to v9.0.0+ as we have fixed a public vulnerability with formidable dependency. Note that v9.0.0+ requires Node.js v14.18.0+. See https://github.com/ladjs/superagent/pull/1800 for insight. This project is supported and maintained by the team at Forward Email @ https://forwardemail.net + deprecated: Please upgrade to superagent v10.2.2+, see release notes at https://github.com/forwardemail/superagent/releases/tag/v10.2.2 - maintenance is supported by Forward Email @ https://forwardemail.net supports-color@10.0.0: resolution: {integrity: sha512-HRVVSbCCMbj7/kdWF9Q+bbckjBHLtHMEoJWlkmYzzdwhYMkjkOwubLM6t7NbWKjgKamGDrWL1++KrjUO1t9oAQ==}