Skip to content

Commit

Permalink
Merge pull request #3 from Readify/tickets
Browse files Browse the repository at this point in the history
Added a few ticket and user functions
  • Loading branch information
RobFaie authored Feb 1, 2020
2 parents bbaa959 + 38d7f98 commit 2671d74
Show file tree
Hide file tree
Showing 5 changed files with 613 additions and 0 deletions.
62 changes: 62 additions & 0 deletions functions/Merge-Ticket.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@

function Merge-Ticket {

[OutputType([PSCustomObject])]
[CMDletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'High')]
Param (

# Unique Id of the ticket or tickets to merge into target ticket
[Parameter(Mandatory = $true)]
[ValidateRange(1, [Int64]::MaxValue)]
[Int64[]]
$SourceTicketId,

# Unique Id of the ticket to merge source tickets into.
[Parameter(Mandatory = $true)]
[ValidateRange(1, [Int64]::MaxValue)]
[Int64]
$TargetTicketId,

# Private comment to leave on the source tickets.
[Parameter(Mandatory = $false)]
[ValidateNotNullOrEmpty()]
[String]
$SourceTicketComment,

# Private comment to leave on the target ticket.
[Parameter(Mandatory = $false)]
[ValidateNotNullOrEmpty()]
[String]
$TargetTicketComment,

# Zendesk Connection Context from `Get-ZendeskConnection`
[Parameter(Mandatory = $false)]
[PSTypeName('ZendeskContext')]
[PSCustomObject]
$Context = $null
)

if (-not $PSBoundParameters.ContainsKey('SourceTicketComment')) {
$SourceTicketComment = "This request was closed and merged into request #$TargetTicketId"
}

if (-not $PSBoundParameters.ContainsKey('TargetTicketComment')) {
if ($SourceTicketId.Count -gt 1) {
$ids = $SourceTicketId -join ' #'
$TargetTicketComment = "Requests #$ids were closed and merged into this request."
} else {
$TargetTicketComment = "Request #$SourceTicketId was closed and merged into this request."
}
}

$path = "/api/v2/tickets/$TargetTicketId/merge.json"
$body = @{
ids = $SourceTicketId
target_comment = $TargetTicketComment
source_comment = $SourceTicketComment
}

$result = Invoke-Method -Context $Context -Method 'Post' -Path $path -Body $body -Verbose:$VerbosePreference
$result

}
217 changes: 217 additions & 0 deletions functions/New-Ticket.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@

function New-Ticket {

[OutputType([PSCustomObject])]
[CMDletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'High')]
Param (

# The subject of the ticket
[Parameter(Mandatory = $false)]
[String]
$Subject,

# The dynamic content placeholder
[Parameter(Mandatory = $false)]
[String]
$RawSubject,

# An object that adds a comment to the ticket.
[Parameter(Mandatory = $true)]
[PSCustomObject]
$Comment,

# The numeric ID of the user asking for support through the ticket
[Parameter(Mandatory = $false)]
[ValidateRange(1, [Int64]::MaxValue)]
[Int64]
$RequesterId,

# The user who submitted the ticket.
[Parameter(Mandatory = $false)]
[ValidateRange(1, [Int64]::MaxValue)]
[Int64]
$SubmitterId,

# The numeric ID of the agent to assign the ticket to
[Parameter(Mandatory = $false)]
[ValidateRange(1, [Int64]::MaxValue)]
[Int64]
$AssigneeId,

# The email address of the agent to assign the ticket to
[Parameter(Mandatory = $false)]
[ValidatePattern('@')]
[String]
$AssigneeEmail,

# The original recipient e-mail address of the ticket
[Parameter(Mandatory = $false)]
[ValidatePattern('@')]
[String]
$RecipientEmail,

# The numeric ID of the group to assign the ticket to
[Parameter(Mandatory = $false)]
[ValidateRange(1, [Int64]::MaxValue)]
[Int64]
$GroupId,

# The numeric ID of the organization to assign the ticket to.
[Parameter(Mandatory = $false)]
[ValidateRange(1, [Int64]::MaxValue)]
[Int64]
$OrganizationId,

# An array of the numeric IDs of agents or end-users to CC.
[Parameter(Mandatory = $false)]
[ValidateRange(1, [Int64]::MaxValue)]
[Int64]
$CollaboratorId,

# An array of numeric IDs, emails, or objects containing name and email properties.
[Parameter(Mandatory = $false)]
[Object[]]
$Collaborator,

# An array of objects that represent agent followers to add or delete from the ticket.
[Parameter(Mandatory = $false)]
[ValidateRange(1, [Int64]::MaxValue)]
[Int64[]]
$FollowerId,

# An array of objects that represent agent or end users email CCs to add or delete from the ticket.
[Parameter(Mandatory = $false)]
[Object[]]
$EmailCc,

# Allowed values are problem, incident, question, or task
[Parameter(Mandatory = $false)]
[ValidateSet('problem', 'incident', 'question', 'task')]
[String]
$Type,

# Allowed values are urgent, high, normal, or low
[Parameter(Mandatory = $false)]
[ValidateSet('urgent', 'high', 'normal', 'low')]
[String]
$Priority,

# Allowed values are open, pending, hold, solved or closed
[Parameter(Mandatory = $false)]
[ValidateSet('new', 'open', 'pending', 'hold', 'solved', 'closed')]
$Status,

# An array of tags to add to the ticket.
[Parameter(Mandatory = $false)]
[ValidateNotNullOrEmpty()]
[String[]]
$Tags,

# An ID to link tickets to local records
[Parameter(Mandatory = $false)]
[String]
$ExternalId,

# The topic this ticket originated from, if any
[Parameter(Mandatory = $false)]
[ValidateRange(1, [Int64]::MaxValue)]
[Int64]
$ForumTopicId,

# The id of a closed ticket when creating a follow-up ticket.
[Parameter(Mandatory = $false)]
[ValidateRange(1, [Int64]::MaxValue)]
[Int64]
$FollowupId,

# The id of the ticket form to render for the ticket
[Parameter(Mandatory = $false)]
[ValidateRange(1, [Int64]::MaxValue)]
[Int64]
$FormId,

# The id of the brand this ticket is associated with
[Parameter(Mandatory = $false)]
[ValidateRange(1, [Int64]::MaxValue)]
[Int64]
$BrandId,

# For tickets of type "incident", the numeric ID of the problem the incident is linked to, if any
[Parameter(Mandatory = $false)]
[ValidateRange(1, [Int64]::MaxValue)]
[Int64]
$ProblemId,

# For tickets of type "task", the due date of the task. Accepts the ISO 8601 date format (yyyy-mm-dd)
[Parameter(Mandatory = $false)]
[DateTime]
$DueAt,

# An array of the custom field objects consisting of ids and values.
[Parameter(Mandatory = $false)]
[PSCustomObject[]]
$CustomFields,

# An array of macro IDs to be recorded in the ticket audit
[Parameter(Mandatory = $false)]
[ValidateRange(1, [Int64]::MaxValue)]
[Int64[]]
$MacroId,

# Zendesk Connection Context from `Get-ZendeskConnection`
[Parameter(Mandatory = $false)]
[PSTypeName('ZendeskContext')]
[PSCustomObject]
$Context = $null
)

$path = "/api/v2/tickets.json"

$body = @{
ticket = @{}
}

$map = @{
external_id = 'ExternalId'
type = 'Type'
subject = 'Subject'
raw_subject = 'RawSubject'
comment = 'Comment'
priority = 'Priority'
status = 'Status'
recipient = 'RecipientEmail'
requester_id = 'RequesterId'
submitter_id = 'SubmitterId'
assignee_id = 'AssigneeId'
assignee = 'AssigneeEmail'
organization_id = 'OrganizationId'
collaborator_ids = 'CollaboratorId'
collaborators = 'Collaborator'
email_cc_ids = 'EmailCc'
follower_ids = 'FollowerId'
forum_topic_id = 'ForumTopicId'
problem_id = 'ProblemId'
due_at = 'DueAt'
tags = 'Tags'
custom_fields = 'CustomFields'
via_followup_source_id = 'FollowupId'
macro_ids = 'MacroId'
ticket_form_id = 'FormId'
brand_id = 'BrandId'
group_id = 'GroupId'
}

foreach ($item in $map.GetEnumerator()) {
$property = $item.key
$parameter = $item.value
if ($PSBoundParameters.ContainsKey($parameter)) {
$body.ticket[$property] = $PSBoundParameters.$parameter
}
}

if ($PSCmdlet.ShouldProcess($Subject, "Create Zendesk Ticket")) {
$result = Invoke-Method -Context $Context -Method 'Post' -Path $path -Body $body -Verbose:$VerbosePreference
$result
}

}
36 changes: 36 additions & 0 deletions functions/Set-GroupMembershipAsDefault.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

function Set-GroupMembershipAsDefault {

[OutputType([PSCustomObject])]
[CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'High')]
Param (

# Unique Id of User to set default group membership for
[Parameter(Mandatory = $true)]
[ValidateRange(1, [Int64]::MaxValue)]
[ValidateNotNullOrEmpty()]
[Int64[]]
$UserId,

# Unique Id of group membership to make default
[Parameter(Mandatory = $true)]
[ValidateRange(1, [Int64]::MaxValue)]
[ValidateNotNullOrEmpty()]
[Int64[]]
$MembershipId,

# Zendesk Connection Context from `Get-ZendeskConnection`
[Parameter(Mandatory = $false)]
[PSTypeName('ZendeskContext')]
[PSCustomObject]
$Context = $null
)

$path = "/api/v2/users/$UserId/group_memberships/$MembershipId/make_default.json"

if ($PSCmdlet.ShouldProcess($UserId, "Set default group: $MembershipId")) {
$result = Invoke-Method -Context $Context -Method 'Put' -Path $path -Verbose:$VerbosePreference
$result
}

}
36 changes: 36 additions & 0 deletions functions/Set-OrganizationMembershipAsDefault.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

function Set-OrganizationMembershipAsDefault {

[OutputType([PSCustomObject])]
[CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'High')]
Param (

# Unique Id of user to set default organization for
[Parameter(Mandatory = $true)]
[ValidateRange(1, [Int64]::MaxValue)]
[ValidateNotNullOrEmpty()]
[Int64[]]
$UserId,

# Unique Id of organization membership to set as default
[Parameter(Mandatory = $true)]
[ValidateRange(1, [Int64]::MaxValue)]
[ValidateNotNullOrEmpty()]
[Int64[]]
$MembershipId,

# Zendesk Connection Context from `Get-ZendeskConnection`
[Parameter(Mandatory = $false)]
[PSTypeName('ZendeskContext')]
[PSCustomObject]
$Context = $null
)

$path = "/api/v2/users/$UserId/organization_memberships/$MembershipId/make_default.json"

if ($PSCmdlet.ShouldProcess($UserId, "Set default Organization Membership: $MembershipId")) {
$result = Invoke-Method -Context $Context -Method 'Put' -Path $path -Verbose:$VerbosePreference
$result
}

}
Loading

0 comments on commit 2671d74

Please sign in to comment.