-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathItopAPIController.php
More file actions
303 lines (265 loc) · 10.1 KB
/
ItopAPIController.php
File metadata and controls
303 lines (265 loc) · 10.1 KB
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
<?php
/**
* Nextcloud - iTop
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the COPYING file.
*
* @author Integration Bot
* @copyright Integration Bot 2025
*/
namespace OCA\Itop\Controller;
use OCA\Itop\AppInfo\Application;
use OCA\Itop\Service\ItopAPIService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
use OCP\IConfig;
use OCP\IL10N;
use OCP\IRequest;
use Psr\Log\LoggerInterface;
class ItopAPIController extends Controller {
public function __construct(
string $appName,
IRequest $request,
private ItopAPIService $itopAPIService,
private IConfig $config,
private IL10N $l10n,
private LoggerInterface $logger,
private ?string $userId
) {
parent::__construct($appName, $request);
}
/**
* Get user created tickets
*
* @NoAdminRequired
*
* @param int $limit
* @return DataResponse
*/
public function getTickets(int $limit = 10): DataResponse {
if ($this->userId === null) {
return new DataResponse(['error' => 'Unauthorized'], Http::STATUS_UNAUTHORIZED);
}
try {
$tickets = $this->itopAPIService->getUserCreatedTickets($this->userId, null, $limit);
return new DataResponse($tickets);
} catch (\Exception $e) {
$this->logger->error('Error getting iTop tickets: ' . $e->getMessage(), ['app' => Application::APP_ID]);
return new DataResponse(['error' => $e->getMessage()], Http::STATUS_INTERNAL_SERVER_ERROR);
}
}
/**
* Get count of user created tickets
*
* @NoAdminRequired
*
* @return DataResponse
*/
public function getTicketsCount(): DataResponse {
if ($this->userId === null) {
return new DataResponse(['error' => 'Unauthorized'], Http::STATUS_UNAUTHORIZED);
}
try {
$ticketCount = $this->itopAPIService->getUserCreatedTicketsCount($this->userId);
return new DataResponse($ticketCount);
} catch (\Exception $e) {
$this->logger->error('Error getting iTop ticket count: ' . $e->getMessage(), ['app' => Application::APP_ID]);
return new DataResponse(['error' => $e->getMessage()], Http::STATUS_INTERNAL_SERVER_ERROR);
}
}
/**
* Get specific ticket details
*
* @NoAdminRequired
*
* @param int $ticketId
* @return DataResponse
*/
public function getTicket(int $ticketId): DataResponse {
if ($this->userId === null) {
return new DataResponse(['error' => 'Unauthorized'], Http::STATUS_UNAUTHORIZED);
}
try {
$ticket = $this->itopAPIService->getTicketInfo($this->userId, $ticketId);
return new DataResponse($ticket);
} catch (\Exception $e) {
$this->logger->error('Error getting iTop ticket: ' . $e->getMessage(), ['app' => Application::APP_ID]);
return new DataResponse(['error' => $e->getMessage()], Http::STATUS_INTERNAL_SERVER_ERROR);
}
}
/**
* Search iTop objects
*
* @NoAdminRequired
*
* @param string $term
* @param int $offset
* @param int $limit
* @return DataResponse
*/
public function search(string $term, int $offset = 0, int $limit = 10): DataResponse {
if ($this->userId === null) {
return new DataResponse(['error' => 'Unauthorized'], Http::STATUS_UNAUTHORIZED);
}
try {
$results = $this->itopAPIService->search($this->userId, $term, $offset, $limit);
return new DataResponse($results);
} catch (\Exception $e) {
$this->logger->error('Error searching iTop: ' . $e->getMessage(), ['app' => Application::APP_ID]);
return new DataResponse(['error' => $e->getMessage()], Http::STATUS_INTERNAL_SERVER_ERROR);
}
}
/**
* Get Configuration Items
*
* @NoAdminRequired
*
* @param int $limit
* @return DataResponse
*/
public function getCIs(int $limit = 10): DataResponse {
if ($this->userId === null) {
return new DataResponse(['error' => 'Unauthorized'], Http::STATUS_UNAUTHORIZED);
}
try {
$results = $this->itopAPIService->search($this->userId, '', 0, $limit);
// Filter to only CIs
$cis = array_filter($results, function($item) {
return $item['type'] === 'FunctionalCI';
});
return new DataResponse(array_values($cis));
} catch (\Exception $e) {
$this->logger->error('Error getting iTop CIs: ' . $e->getMessage(), ['app' => Application::APP_ID]);
return new DataResponse(['error' => $e->getMessage()], Http::STATUS_INTERNAL_SERVER_ERROR);
}
}
/**
* Get avatar/image - placeholder for future implementation
*
* @NoAdminRequired
*
* @return DataResponse
*/
public function getAvatar(): DataResponse {
// Placeholder - iTop doesn't have user avatars like Zammad
// This could be extended to return organization logos or CI images
return new DataResponse(['message' => 'Avatar functionality not implemented for iTop'], Http::STATUS_NOT_IMPLEMENTED);
}
/**
* Get dashboard data for current user
*
* @NoAdminRequired
*
* @return DataResponse
*/
public function getDashboardData(): DataResponse {
$this->logger->info('getDashboardData called for user: ' . ($this->userId ?? 'null'), ['app' => Application::APP_ID]);
if ($this->userId === null) {
$this->logger->warning('getDashboardData: No user ID', ['app' => Application::APP_ID]);
return new DataResponse(['error' => 'Unauthorized'], Http::STATUS_UNAUTHORIZED);
}
try {
$this->logger->debug('Fetching ticket counts...', ['app' => Application::APP_ID]);
// Ticket counts (UserRequest + Incident)
$counts = $this->itopAPIService->getUserCreatedTicketsCount($this->userId);
$this->logger->debug('Counts: ' . json_encode($counts), ['app' => Application::APP_ID]);
$this->logger->debug('Fetching tickets by status...', ['app' => Application::APP_ID]);
// Status breakdown and grouped tickets
$byStatus = $this->itopAPIService->getUserTicketsByStatus($this->userId);
$this->logger->debug('ByStatus: ' . json_encode($byStatus), ['app' => Application::APP_ID]);
// Get iTop URL for "View All" button
$itopUrl = $this->itopAPIService->getItopUrl($this->userId);
$this->logger->debug('iTop URL: ' . $itopUrl, ['app' => Application::APP_ID]);
// Get admin-configured display name (e.g., "ServicePoint" instead of "iTop")
$displayName = $this->config->getAppValue(Application::APP_ID, 'user_facing_name', 'iTop');
$response = [
'counts' => $counts,
'stats' => [
'by_status' => $byStatus['by_status'] ?? [],
],
'tickets' => $byStatus['tickets'] ?? [],
'recent_cis' => [],
'itop_url' => $itopUrl,
'display_name' => $displayName
];
$this->logger->info('getDashboardData success, returning data', ['app' => Application::APP_ID]);
return new DataResponse($response);
} catch (\Exception $e) {
$this->logger->error('Error getting iTop dashboard data: ' . $e->getMessage(), [
'app' => Application::APP_ID,
'exception' => $e->getTraceAsString()
]);
return new DataResponse(['error' => $e->getMessage()], Http::STATUS_INTERNAL_SERVER_ERROR);
}
}
/**
* Get agent dashboard data (agent-specific metrics and queues)
*
* @NoAdminRequired
*
* @return DataResponse
*/
public function getAgentDashboardData(): DataResponse {
$this->logger->info('getAgentDashboardData called for user: ' . ($this->userId ?? 'null'), ['app' => Application::APP_ID]);
if ($this->userId === null) {
$this->logger->warning('getAgentDashboardData: No user ID', ['app' => Application::APP_ID]);
return new DataResponse(['error' => 'Unauthorized'], Http::STATUS_UNAUTHORIZED);
}
try {
$this->logger->debug('Fetching my assigned tickets...', ['app' => Application::APP_ID]);
// Get tickets assigned to me
$myTickets = $this->itopAPIService->getMyAssignedTickets($this->userId, 20);
$this->logger->debug('Fetching team tickets...', ['app' => Application::APP_ID]);
// Get tickets assigned to my teams
$teamTickets = $this->itopAPIService->getTeamAssignedTickets($this->userId, 20);
$this->logger->debug('Fetching upcoming changes...', ['app' => Application::APP_ID]);
// Get upcoming changes
$upcomingChanges = $this->itopAPIService->getUpcomingChanges($this->userId, 10);
$this->logger->debug('Fetching SLA warning counts...', ['app' => Application::APP_ID]);
// Get SLA warning counts (approaching deadline within 24h)
$slaWarningCounts = $this->itopAPIService->getSLAWarningCounts($this->userId);
$this->logger->debug('Fetching SLA breach counts...', ['app' => Application::APP_ID]);
// Get SLA breach counts (already escalated)
$slaBreachCounts = $this->itopAPIService->getSLABreachCounts($this->userId);
// Get iTop URL for links
$itopUrl = $this->itopAPIService->getItopUrl($this->userId);
// Get admin-configured display name
$displayName = $this->config->getAppValue(Application::APP_ID, 'user_facing_name', 'iTop');
// Calculate type-specific counts for detailed breakdown
$myIncidents = count(array_filter($myTickets, fn($t) => $t['type'] === 'Incident'));
$myRequests = count(array_filter($myTickets, fn($t) => $t['type'] === 'UserRequest'));
$teamIncidents = count(array_filter($teamTickets, fn($t) => $t['type'] === 'Incident'));
$teamRequests = count(array_filter($teamTickets, fn($t) => $t['type'] === 'UserRequest'));
$response = [
'myTickets' => $myTickets,
'teamTickets' => $teamTickets,
'upcomingChanges' => $upcomingChanges,
'counts' => [
'my_tickets' => count($myTickets),
'my_incidents' => $myIncidents,
'my_requests' => $myRequests,
'team_tickets' => count($teamTickets),
'team_incidents' => $teamIncidents,
'team_requests' => $teamRequests,
'sla_warning_tto' => $slaWarningCounts['tto'],
'sla_warning_ttr' => $slaWarningCounts['ttr'],
'sla_breaches_tto' => $slaBreachCounts['tto'],
'sla_breaches_ttr' => $slaBreachCounts['ttr'],
'upcoming_changes' => count($upcomingChanges)
],
'itop_url' => $itopUrl,
'display_name' => $displayName
];
$this->logger->info('getAgentDashboardData success, returning data', ['app' => Application::APP_ID]);
return new DataResponse($response);
} catch (\Exception $e) {
$this->logger->error('Error getting iTop agent dashboard data: ' . $e->getMessage(), [
'app' => Application::APP_ID,
'exception' => $e->getTraceAsString()
]);
return new DataResponse(['error' => $e->getMessage()], Http::STATUS_INTERNAL_SERVER_ERROR);
}
}
}