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 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
|
# --
# Copyright (C) 2021 Znuny GmbH, https://znuny.org/
# --
# This software comes with ABSOLUTELY NO WARRANTY. For details, see
# the enclosed file COPYING for license information (AGPL). If you
# did not receive this file, see http://www.gnu.org/licenses/agpl.txt.
# --
package Kernel::GenericInterface::Invoker::Ticket::Generic;
use strict;
use warnings;
use Kernel::System::ObjectManager;
use Kernel::System::VariableCheck qw(:all);
use parent qw(Kernel::System::AsynchronousExecutor);
use parent qw(Kernel::System::EventHandler);
use MIME::Base64;
our $ObjectManagerDisabled = 1;
=head1 NAME
Kernel::GenericInterface::Invoker::Ticket::Generic - Generic Interface Invoker for ticket-related requester calls
=head1 DESCRIPTION
Invoker that prepares requests and processes responses for ticket-related Generic Interface requester calls.
=head1 PUBLIC INTERFACE
=head2 new()
usually, you want to create an instance of this
by using Kernel::GenericInterface::Invoker->new();
=cut
sub new {
my ( $Type, %Param ) = @_;
my $Self = {};
bless( $Self, $Type );
if ( !$Param{DebuggerObject} ) {
return {
Success => 0,
ErrorMessage => "Got no DebuggerObject!"
};
}
$Self->{DebuggerObject} = $Param{DebuggerObject};
return $Self;
}
=head2 PrepareRequest()
prepare the invocation of the configured remote web service.
my $Result = $InvokerObject->PrepareRequest(
Data => { # data payload
TicketID => 1, # optional
ArticleID => 7, # optional
GetAllArticleAttachments => 1, # optional, 0 as default. 0|1,
},
InvokerName => 'Generic',
Webservice => { ... }, # optional
);
Returns:
$Result = {
Success => 1, # 0 or 1
ErrorMessage => '...', # in case of error
Data => {
Ticket => { ... },
Event => { ... },
},
};
=cut
sub PrepareRequest {
my ( $Self, %Param ) = @_;
my $ConfigObject = $Kernel::OM->Get('Kernel::Config');
my $TicketObject = $Kernel::OM->Get('Kernel::System::Ticket');
my $UtilObject = $Kernel::OM->Get('Kernel::System::Util');
my $InvokerName = $Param{InvokerName} // 'Generic';
my $GetAllArticleAttachments = $Param{Data}->{GetAllArticleAttachments}
|| $Param{Webservice}->{Config}->{Requester}->{Invoker}->{$InvokerName}->{GetAllArticleAttachments};
my %Ticket;
if ( $Param{Data}->{TicketID} ) {
%Ticket = $TicketObject->TicketDeepGet(
TicketID => $Param{Data}->{TicketID},
ArticleID => $Param{Data}->{ArticleID}, # optional, hence not checked
GetAllArticleAttachments => $GetAllArticleAttachments,
UserID => 1,
);
# Provide UntilTime as date/time parts
if ( $Ticket{UntilTime} ) {
my $UntilTimeDateTimeObject = $Kernel::OM->Create(
'Kernel::System::DateTime',
);
$UntilTimeDateTimeObject->Add(
Seconds => int( $Ticket{UntilTime} ),
);
$Ticket{UntilTimeDateTimeParts} = $UntilTimeDateTimeObject->Get();
}
}
# Remove configured fields.
my $OmittedFields = $ConfigObject->Get(
'GenericInterface::Invoker::Ticket::Generic::PrepareRequest::OmittedFields'
) // {};
if (
defined $OmittedFields->{$InvokerName}
&& length $OmittedFields->{$InvokerName}
)
{
my @HashKeys = split /\s*;\s*/, $OmittedFields->{$InvokerName};
$UtilObject->DataStructureRemoveElements(
Data => \%Ticket,
HashKeys => \@HashKeys,
);
# Also remove elements from given payload.
$UtilObject->DataStructureRemoveElements(
Data => $Param{Data},
HashKeys => \@HashKeys,
);
}
# Base-64 encode configured field values.
my $Base64EncodedFields = $ConfigObject->Get(
'GenericInterface::Invoker::Ticket::Generic::PrepareRequest::Base64EncodedFields'
) // {};
if (
defined $Base64EncodedFields->{$InvokerName}
&& length $Base64EncodedFields->{$InvokerName}
)
{
my @HashKeys = split /\s*;\s*/, $Base64EncodedFields->{$InvokerName};
$UtilObject->Base64DeepEncode(
Data => \%Ticket,
HashKeys => \@HashKeys,
);
# Also encode elements of given payload.
$UtilObject->Base64DeepEncode(
Data => $Param{Data},
HashKeys => \@HashKeys,
);
}
my %Data = (
Ticket => \%Ticket,
Event => $Param{Data},
);
$Self->{RequestData} = \%Data;
return {
Success => 1,
Data => \%Data,
};
}
=head2 HandleResponse()
handle response data of the configured remote web service.
my $Result = $InvokerObject->HandleResponse(
ResponseSuccess => 1, # success status of the remote web service
ResponseErrorMessage => '', # in case of web service error
Data => { # data payload
...
},
);
Returns:
$Result = {
Success => 1, # 0 or 1
ErrorMessage => '', # in case of error
Data => { # data payload after Invoker
...
},
};
=cut
sub HandleResponse {
my ( $Self, %Param ) = @_;
my $ConfigObject = $Kernel::OM->Get('Kernel::Config');
my $BackendObject = $Kernel::OM->Get('Kernel::System::DynamicField::Backend');
my $DynamicFieldObject = $Kernel::OM->Get('Kernel::System::DynamicField');
my $TicketObject = $Kernel::OM->Get('Kernel::System::Ticket');
my $ArticleObject = $Kernel::OM->Get('Kernel::System::Ticket::Article');
# Pass through response if no hash
if ( !IsHashRefWithData( $Param{Data} ) ) {
return {
Success => 1,
Data => $Param{Data},
};
}
# Set data and execute functions even in error case.
RESULT:
for my $Key ( sort keys %{ $Param{Data} } ) {
my $Success = 1;
if ( $Key =~ m{OTRS_TicketDynamicFieldSet_(.*)}xmsi ) {
my $DynamicFieldConfig = $DynamicFieldObject->DynamicFieldGet(
Name => $1,
);
next RESULT if !IsHashRefWithData($DynamicFieldConfig);
$Success = $BackendObject->ValueSet(
DynamicFieldConfig => $DynamicFieldConfig,
ObjectID => $Self->{RequestData}->{Ticket}->{TicketID},
Value => $Param{Data}->{$Key},
UserID => 1,
);
}
elsif ( $Key eq 'OTRS_TicketTitleUpdate' ) {
$Success = $TicketObject->TicketTitleUpdate(
Title => $Param{Data}->{$Key},
TicketID => $Self->{RequestData}->{Ticket}->{TicketID},
UserID => 1,
);
}
elsif ( $Key eq 'OTRS_TicketServiceSet' ) {
$Success = $TicketObject->TicketServiceSet(
Service => $Param{Data}->{$Key},
TicketID => $Self->{RequestData}->{Ticket}->{TicketID},
UserID => 1,
);
}
elsif ( $Key eq 'OTRS_TicketTypeSet' ) {
$Success = $TicketObject->TicketTypeSet(
Type => $Param{Data}->{$Key},
TicketID => $Self->{RequestData}->{Ticket}->{TicketID},
UserID => 1,
);
}
elsif ( $Key eq 'OTRS_TicketQueueSet' ) {
$Success = $TicketObject->TicketQueueSet(
Queue => $Param{Data}->{$Key},
TicketID => $Self->{RequestData}->{Ticket}->{TicketID},
UserID => 1,
);
}
elsif ( $Key eq 'OTRS_TicketSLASet' ) {
$Success = $TicketObject->TicketSLASet(
SLA => $Param{Data}->{$Key},
TicketID => $Self->{RequestData}->{Ticket}->{TicketID},
UserID => 1,
);
}
elsif ( $Key eq 'OTRS_TicketCustomerSet' ) {
my @Value = split /;/, $Param{Data}->{$Key};
$Success = $TicketObject->TicketCustomerSet(
No => $Value[0],
User => $Value[1],
TicketID => $Self->{RequestData}->{Ticket}->{TicketID},
UserID => 1,
);
}
elsif ( $Key eq 'OTRS_TicketStateSet' ) {
$Success = $TicketObject->TicketStateSet(
State => $Param{Data}->{$Key},
TicketID => $Self->{RequestData}->{Ticket}->{TicketID},
UserID => 1,
);
}
elsif ( $Key eq 'OTRS_TicketOwnerSet' ) {
$Success = $TicketObject->TicketOwnerSet(
TicketID => $Self->{RequestData}->{Ticket}->{TicketID},
NewUser => $Param{Data}->{$Key},
UserID => 1,
);
}
elsif ( $Key eq 'OTRS_TicketResponsibleSet' ) {
$Success = $TicketObject->TicketResponsibleSet(
TicketID => $Self->{RequestData}->{Ticket}->{TicketID},
NewUser => $Param{Data}->{$Key},
UserID => 1,
);
}
elsif ( $Key eq 'OTRS_TicketPrioritySet' ) {
$Success = $TicketObject->TicketPrioritySet(
TicketID => $Self->{RequestData}->{Ticket}->{TicketID},
Priority => $Param{Data}->{$Key},
UserID => 1,
);
}
elsif ( $Key eq 'OTRS_TicketHistoryAdd' ) {
NEEDED:
for my $Needed (qw(HistoryType)) {
next NEEDED if defined $Param{Data}->{$Key}->{$Needed};
return $Self->{DebuggerObject}->Error(
Summary =>
"Missing parameter '$Needed' on action '$Key' with value '$Param{Data}->{$Key}'. Failed to execute!",
);
}
$Success = $TicketObject->HistoryAdd(
Name => $Param{Data}->{$Key}->{Name} || $Param{Data}->{$Key}->{HistoryComment} || ' ',
HistoryType => $Param{Data}->{$Key}->{HistoryType} || 'AddNote',
TicketID => $Self->{RequestData}->{Ticket}->{TicketID},
CreateUserID => 1,
);
}
elsif ( $Key eq 'OTRS_TicketArticleAdd' ) {
NEEDED:
for my $Needed (qw(Subject Body)) {
next NEEDED if defined $Param{Data}->{$Key}->{$Needed};
return $Self->{DebuggerObject}->Error(
Summary =>
"Missing parameter '$Needed' on action '$Key'. Failed to execute!",
);
}
#
# Base-64-decode article attachments.
#
my $Attachments = $Param{Data}->{$Key}->{Attachment};
if ( IsHashRefWithData($Attachments) ) {
$Attachments = [$Attachments];
}
if ( !IsArrayRefWithData($Attachments) ) {
$Attachments = [];
}
ATTACHMENT:
for my $Attachment ( @{$Attachments} ) {
next ATTACHMENT if !IsHashRefWithData($Attachment);
next ATTACHMENT if !IsStringWithData( $Attachment->{Content} );
$Attachment->{Content} = MIME::Base64::decode_base64( $Attachment->{Content} );
}
$Param{Data}->{$Key}->{Attachment} = $Attachments;
$Success = $ArticleObject->ArticleCreate(
TicketID => $Self->{RequestData}->{Ticket}->{TicketID},
ChannelName => 'Internal',
IsVisibleForCustomer => 0,
SenderType => 'agent',
Charset => 'utf-8',
MimeType => 'text/plain',
From => "Generic Interface",
UserID => 1,
%{ $Param{Data}->{$Key} },
);
}
elsif ( $Key eq 'OTRS_TicketCreate' ) {
$Success = $TicketObject->TicketCreate(
%{ $Param{Data}->{$Key} },
);
}
elsif ( $Key eq 'OTRS_AsynchronousInvokerExecution' ) {
if ( !IsArrayRefWithData( $Param{Data}->{$Key} ) ) {
return $Self->{DebuggerObject}->Error(
Summary =>
"Parameter '$Key' needs to be an array with data.",
);
}
INVOKERPARAMETERS:
for my $InvokerParameters ( @{ $Param{Data}->{$Key} } ) {
if ( !IsHashRefWithData($InvokerParameters) ) {
return $Self->{DebuggerObject}->Error(
Summary =>
"Parameter '$Key' needs to be an array of hashes with data.",
);
}
my $MaximumParallelInstances = $ConfigObject->Get(
'GenericInterface::Invoker::Ticket::Generic::HandleResponse::MaximumParallelInstances'
) // 1;
my $AsyncCallOK = $Self->AsyncCall(
ObjectName => 'Kernel::GenericInterface::Requester',
FunctionName => 'Run',
FunctionParams => $InvokerParameters,
Attempts => 3,
MaximumParallelInstances => $MaximumParallelInstances,
);
next INVOKERPARAMETERS if $AsyncCallOK;
return $Self->{DebuggerObject}->Error(
Summary =>
'Error executing asynchronous call of Kernel::GenericInterface::Requester::Run().',
);
}
$Success = 1;
}
elsif ( $Key eq 'OTRS_TicketArticleCreateEvent' ) {
$Self->EventHandlerInit(
Config => 'Ticket::EventModulePost',
);
my $ArticleIDsString = $Param{Data}->{$Key} // '';
my @ArticleIDs = split /\s*,\s*/, $ArticleIDsString;
my %UniqueArticleIDs = map { $_ => 1 } grep { $_ =~ m{\A\d+\z} } @ArticleIDs;
for my $ArticleID ( sort keys %UniqueArticleIDs ) {
$Self->EventHandler(
Event => 'ArticleCreate',
Data => {
ArticleID => $ArticleID,
TicketID => $Self->{RequestData}->{Ticket}->{TicketID},
},
UserID => 1,
);
}
# Handle all queued 'Transaction' events which were collected up to this point.
$Self->EventHandlerTransaction();
$Success = 1;
}
next RESULT if $Success;
return $Self->{DebuggerObject}->Error(
Summary => "Error on response action '$Key' with value '$Param{Data}->{$Key}'. Failed to execute!",
);
}
# if there was an error in the response, forward it
if ( !$Param{ResponseSuccess} ) {
if ( !IsStringWithData( $Param{ResponseErrorMessage} ) ) {
return $Self->{DebuggerObject}->Error(
Summary => 'Got response error, but no response error message!',
);
}
return {
Success => 0,
ErrorMessage => $Param{ResponseErrorMessage},
};
}
return {
Success => 1,
Data => $Param{Data},
};
}
=head2 HandleError()
Calls HandleResponse() with ResponseSuccess = 0 and the provided Data.
=cut
sub HandleError {
my ( $Self, %Param ) = @_;
# Execute HandleResponse with error data because it might contain further tags
# to set data or execute functions (see HandleResponse above) in error case.
return $Self->HandleResponse(
ResponseSuccess => 0,
Data => $Param{Data},
);
}
1;
=head1 SEE ALSO
L<Kernel::GenericInterface::Invoker>
|