forked from uktrade/help-desk-service
-
Notifications
You must be signed in to change notification settings - Fork 0
/
interfaces.py
176 lines (134 loc) · 4.75 KB
/
interfaces.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
import datetime
from abc import ABC, abstractmethod
from dataclasses import dataclass
from enum import Enum
from typing import Dict, List, Optional
class Priority(Enum):
URGENT = "urgent"
HIGH = "high"
NORMAL = "normal"
LOW = "low"
class TicketType(Enum):
QUESTION = "question"
INCIDENT = "incident"
PROBLEM = "problem"
TASK = "task"
class Status(Enum):
CLOSED = "closed"
NEW = "new"
PENDING = "pending"
OPEN = "open"
@dataclass
class HelpDeskGroup:
name: str
created_at: Optional[datetime.datetime] = None
deleted: Optional[bool] = None
id: Optional[int] = None
updated_at: Optional[datetime.datetime] = None
url: Optional[str] = None
@dataclass
class HelpDeskUser:
id: Optional[int] = None
full_name: Optional[str] = None
email: Optional[str] = None
groups: Optional[List[HelpDeskGroup]] = None
@dataclass
class HelpDeskComment:
body: str
author_id: Optional[int] = None
public: bool = True
@dataclass
class HelpDeskCustomField:
id: int
value: str
@dataclass
class HelpDeskTicket:
subject: str
id: Optional[int] = None
description: Optional[str] = None
user: Optional[HelpDeskUser] = None
group_id: Optional[int] = None
external_id: Optional[int] = None
assignee_id: Optional[int] = None
comment: Optional[HelpDeskComment] = None
tags: Optional[List[str]] = None
custom_fields: Optional[List[HelpDeskCustomField]] = None
recipient_email: Optional[str] = None
responder: Optional[str] = None
created_at: Optional[datetime.datetime] = None
updated_at: Optional[datetime.datetime] = None
due_at: Optional[datetime.datetime] = None
status: Optional[Status] = None
priority: Optional[Priority] = None
ticket_type: Optional[TicketType] = None
class HelpDeskException(Exception):
pass
class HelpDeskTicketNotFoundException(Exception):
pass
class HelpDeskBase(ABC):
@abstractmethod
def get_or_create_user(self, user: HelpDeskUser) -> HelpDeskUser:
raise NotImplementedError
@abstractmethod
def create_ticket(self, ticket: HelpDeskTicket) -> HelpDeskTicket:
raise NotImplementedError
@abstractmethod
def get_ticket(self, ticket_id: int) -> HelpDeskTicket:
raise NotImplementedError
@abstractmethod
def close_ticket(self, ticket_id: int) -> HelpDeskTicket:
raise NotImplementedError
@abstractmethod
def add_comment(self, ticket_id: int, comment: HelpDeskComment) -> HelpDeskTicket:
raise NotImplementedError
@abstractmethod
def update_ticket(self, ticket: HelpDeskTicket) -> HelpDeskTicket:
raise NotImplementedError
class HelpDeskStubbed(HelpDeskBase):
def __init__(self, *args, **kwargs) -> None:
self.id = 1
self._next_ticket_id = 2
self._tickets: Dict[int, HelpDeskTicket] = {}
self._users: Dict[int, HelpDeskUser] = {}
self._next_user_id = 1
def get_or_create_user(self, user: HelpDeskUser) -> HelpDeskUser:
if user.id:
user_id = user.id
else:
user_id = self._next_user_id
self._next_user_id += 1
if not self._users[user_id]:
self._users[user_id] = user
return self._users[user_id]
def create_ticket(self, ticket: HelpDeskTicket) -> HelpDeskTicket:
ticket.created_at = datetime.datetime.now()
self._tickets[self._next_ticket_id] = ticket
ticket.id = self._next_ticket_id
self._next_ticket_id += 1
return ticket
def get_ticket(self, ticket_id: int) -> HelpDeskTicket:
if self._tickets.get(ticket_id):
return self._tickets.get(ticket_id)
else:
raise HelpDeskTicketNotFoundException
def add_comment(self, ticket_id: int, comment: HelpDeskComment) -> HelpDeskTicket:
if self._tickets.get(ticket_id):
self._tickets[ticket_id].comment = comment
self._tickets[ticket_id].updated_at = datetime.datetime.now()
return self._tickets[ticket_id]
else:
raise HelpDeskTicketNotFoundException
def close_ticket(self, ticket_id: int) -> HelpDeskTicket:
if self._tickets.get(ticket_id):
self._tickets[ticket_id].status = Status.CLOSED
self._tickets[ticket_id].updated_at = datetime.datetime.now()
return self._tickets[ticket_id]
else:
raise HelpDeskTicketNotFoundException
def update_ticket(self, ticket: HelpDeskTicket) -> HelpDeskTicket:
if self._tickets.get(ticket.id):
self._tickets[ticket.id] = ticket
self._tickets[ticket.id].updated_at = datetime.datetime.now()
return self._tickets[ticket.id]
else:
raise HelpDeskTicketNotFoundException