forked from uktrade/help-desk-service
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zendesk_manager.py
284 lines (226 loc) · 10 KB
/
zendesk_manager.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import logging
from zenpy import Zenpy
from zenpy.lib import exception
from zenpy.lib.api_objects import Comment as ZendeskComment
from zenpy.lib.api_objects import CustomField as ZendeskCustomField
from zenpy.lib.api_objects import Ticket as ZendeskTicket
from zenpy.lib.api_objects import User as ZendeskUser
from help_desk_client.interfaces import (
HelpDeskBase,
HelpDeskComment,
HelpDeskCustomField,
HelpDeskException,
HelpDeskGroup,
HelpDeskTicket,
HelpDeskTicketNotFoundException,
HelpDeskUser,
Status,
)
logger = logging.getLogger(__name__)
class ZendeskClientNotFoundException(Exception):
pass
class ZendeskManager(HelpDeskBase):
client = None
def __init__(self, **kwargs):
"""Create a new Zendesk client - pass credentials to.
:param credentials: The credentials required to create client { token , email, subdomain }.
"""
if not kwargs.get("credentials", None):
raise ZendeskClientNotFoundException("No Zendesk credentials provided")
self.client = Zenpy(
timeout=kwargs.get("credentials").get("timeout", 5),
email=kwargs.get("credentials")["email"],
token=kwargs.get("credentials")["token"],
subdomain=kwargs.get("credentials")["subdomain"],
)
def get_or_create_user(self, user: HelpDeskUser = None) -> HelpDeskUser:
"""Get or Create a new Zendesk user. /PS-IGNORE
:param HelpDeskUser
full_name: string full name for Zendesk user.
email: string email address text for the Zendesk user.
:returns: HelpDeskUser instance representing Zendesk user.
"""
if user is None:
transformed_user = self.client.users.me()
else:
transformed_user = self.__transform_help_desk_user_to_zendesk_user(user)
if transformed_user.id:
zendesk_user = self.client.users(id=transformed_user.id)
else:
zendesk_user = self.client.users.create_or_update(transformed_user)
if zendesk_user is None:
message = f"No Zendesk user found for {transformed_user}" # Error log /PS-IGNORE,
logger.debug(message)
raise HelpDeskException(message)
return self.__transform_zendesk_user_to_help_desk_user(zendesk_user)
def create_ticket(self, ticket: HelpDeskTicket) -> HelpDeskTicket:
"""Create a new Zendesk ticket in response to a new user question.
:param ticket: HelpDeskTicket with information to create Zendesk ticket.
:returns: A HelpDeskTicket instance.
"""
zendesk_audit = self.client.tickets.create(
self.__transform_help_desk_to_zendesk_ticket(ticket)
)
return self.__transform_zendesk_to_help_desk_ticket(zendesk_audit.ticket)
def get_ticket(self, ticket_id: int) -> HelpDeskTicket:
"""Recover the ticket by Zendesk ID.
:param ticket_id: The Zendesk ID of the Ticket.
:returns: A HelpDeskTicket instance.
:raises:
HelpDeskTicketNotFoundException: If no ticket is found.
"""
logger.debug(f"Look for Ticket by is Zendesk ID:<{ticket_id}>") # /PS-IGNORE
try:
return self.__transform_zendesk_to_help_desk_ticket(self.client.tickets(id=ticket_id))
except exception.RecordNotFoundException:
message = f"Could not find Zendesk ticket with ID:<{ticket_id}>" # /PS-IGNORE
logger.debug(message)
raise HelpDeskTicketNotFoundException(message)
def close_ticket(self, ticket_id: int) -> HelpDeskTicket:
"""Close a ticket in Zendesk.
:param ticket_id: The Zendesk ticket ID.
:returns: HelpDeskTicket instance.
"""
logger.debug(f"Looking for ticket with ticket_id:<{ticket_id}>")
ticket = self.get_ticket(ticket_id)
if ticket.status == Status.CLOSED:
logger.warning(f"The ticket:<{ticket.id}> has already been closed!")
else:
ticket.status = Status.CLOSED
ticket = self.update_ticket(ticket)
logger.debug(f"Closed ticket:<{ticket.id}> for ticket_id:<{ticket_id}>")
return ticket
def add_comment(self, ticket_id: int, comment: HelpDeskComment) -> HelpDeskTicket:
"""Add a comment to an existing ticket.
:param ticket_id: id of Zendesk ticket instance.
:param comment: HelpDeskComment instance.
:returns: The updated HelpDeskTicket instance.
"""
ticket = self.get_ticket(ticket_id)
ticket.comment = comment
return self.update_ticket(ticket)
def update_ticket(self, ticket: HelpDeskTicket) -> HelpDeskTicket:
"""Update an existing ticket.
:param ticket: HelpDeskTicket ticket.
:returns: The updated HelpDeskTicket instance.
:raises:
HelpDeskTicketNotFoundException: If no ticket is found.
"""
ticket_audit = self.client.tickets.update(
self.__transform_help_desk_to_zendesk_ticket(ticket)
)
if ticket_audit is None:
message = f"Could not update ticket with id {ticket.id}"
logger.error(message)
raise HelpDeskTicketNotFoundException(message)
return self.__transform_zendesk_to_help_desk_ticket(ticket_audit.ticket)
def __transform_help_desk_to_zendesk_ticket(self, ticket: HelpDeskTicket) -> ZendeskTicket:
"""Transform from HelpDeskTicket to Zendesk ticket instance.
:param ticket: HelpDeskTicket instance.
:returns: Zendesk ticket instance.
"""
custom_fields, comment = None, None
ticket_user = self.get_or_create_user(ticket.user)
if ticket.custom_fields:
custom_fields = [
ZendeskCustomField(id=custom_field.id, value=custom_field.value)
for custom_field in ticket.custom_fields
]
if ticket.comment:
comment = ZendeskComment(
body=ticket.comment.body,
author_id=ticket.comment.author_id if ticket.comment.author_id else ticket_user.id,
public=ticket.comment.public,
)
ticket = ZendeskTicket(
id=ticket.id,
status=ticket.status,
recipient=ticket.recipient_email,
subject=ticket.subject,
description=ticket.description,
submitter_id=ticket_user.id,
assignee_id=ticket.assignee_id,
requester_id=ticket_user.id,
group_id=ticket.group_id,
external_id=ticket.external_id, # /PS-IGNORE
priority=ticket.priority,
tags=ticket.tags,
custom_fields=custom_fields,
comment=comment,
)
return ticket
def __transform_zendesk_to_help_desk_ticket(self, ticket: ZendeskTicket) -> HelpDeskTicket:
"""Transform Zendesk ticket into HelpDeskTicket instance.
:param ticket: Zendesk ticket instance.
:returns: HelpDeskTicket instance.
"""
ticket_user, custom_fields, comment = None, None, None
if getattr(ticket, "requester", None):
ticket_user = HelpDeskUser(
id=ticket.requester.id,
full_name=ticket.requester.name,
email=ticket.requester.email,
)
elif getattr(ticket, "requester_id", None):
ticket_user = HelpDeskUser(id=ticket.requester_id)
if getattr(ticket, "custom_fields", None):
custom_fields = [
HelpDeskCustomField(id=custom_field["id"], value=custom_field["value"])
for custom_field in ticket.custom_fields
]
if getattr(ticket, "comment", None):
comment = HelpDeskComment(
body=ticket.comment.body,
author_id=ticket.comment.author_id,
public=ticket.comment.public,
)
help_desk_ticket = HelpDeskTicket(
id=ticket.id,
status=getattr(ticket, "status", None),
recipient_email=getattr(ticket, "recipient", None),
subject=ticket.subject,
description=ticket.description,
user=ticket_user,
created_at=getattr(ticket, "created_at", None),
updated_at=getattr(ticket, "updated_at", None),
priority=getattr(ticket, "priority", None),
due_at=getattr(ticket, "due_at", None),
assignee_id=getattr(ticket, "assignee_id", None),
group_id=getattr(ticket, "group_id", None),
external_id=getattr(ticket, "external_id", None), # /PS-IGNORE
tags=getattr(ticket, "tags", None),
custom_fields=custom_fields,
comment=comment,
)
return help_desk_ticket
def __transform_help_desk_user_to_zendesk_user(self, user: HelpDeskUser) -> ZendeskUser:
"""Transform HelpDesk user into Zendesk user.
:param user: HelpDeskUser instance.
:returns: ZendeskUser instance.
"""
if user and user.id:
return ZendeskUser(id=user.id)
elif user and user.email:
return ZendeskUser(name=user.full_name, email=user.email)
else:
# This should not be possible so raise exception
raise HelpDeskException(
"Cannot transform user to Zendesk user",
)
def __transform_zendesk_user_to_help_desk_user(self, user: ZendeskUser) -> HelpDeskUser:
"""Transform HelpDesk user into Zendesk user.
:param user: HelpDeskUser user instance.
:returns: ZendeskUser instance.
"""
groups = [
HelpDeskGroup(
created_at=zendesk_group.created_at,
deleted=zendesk_group.deleted,
id=zendesk_group.id,
name=zendesk_group.name,
updated_at=zendesk_group.updated_at,
url=zendesk_group.url,
)
for zendesk_group in list(self.client.users.groups(user))
]
return HelpDeskUser(id=user.id, full_name=user.name, email=user.email, groups=groups)