[Bps-public-commit] rt-extension-rest2 branch, reminder, created. 1.07-2-g42f4387

Michel Rodriguez michel at bestpractical.com
Tue Aug 6 08:21:20 EDT 2019


The branch, reminder has been created
        at  42f4387b2b56a15f76c46e61de617cbeb1eb3a12 (commit)

- Log -----------------------------------------------------------------
commit d1e0ff6f163aeacbf6fdf8ec43ef0986074a9437
Author: michel <michel at bestpractical.com>
Date:   Tue Aug 6 11:40:42 2019 +0200

    Added POST /REST/2.0/reminder to create a reminder on a ticket

diff --git a/lib/RT/Extension/REST2/Resource/Reminder.pm b/lib/RT/Extension/REST2/Resource/Reminder.pm
new file mode 100644
index 0000000..6d85707
--- /dev/null
+++ b/lib/RT/Extension/REST2/Resource/Reminder.pm
@@ -0,0 +1,140 @@
+package RT::Extension::REST2::Resource::Reminder;
+use strict;
+use warnings;
+
+use Moose;
+use namespace::autoclean;
+
+extends 'RT::Extension::REST2::Resource::Record';
+with (
+    'RT::Extension::REST2::Resource::Record::Readable',
+    'RT::Extension::REST2::Resource::Record::Hypermedia'
+        => { -alias => { hypermedia_links => '_default_hypermedia_links' } },
+    'RT::Extension::REST2::Resource::Record::Deletable',
+    'RT::Extension::REST2::Resource::Record::Writable'
+        => { -alias => { create_record => '_create_record' } },
+);
+
+sub dispatch_rules {
+    Path::Dispatcher::Rule::Regex->new(
+        regex => qr{^/reminder/?$},
+        block => sub { { record_class => 'RT::Ticket' } },
+    ),
+    Path::Dispatcher::Rule::Regex->new(
+        regex => qr{^/reminder/(\d+)/?$},
+        block => sub { { record_class => 'RT::Ticket', record_id => shift->pos(1) } },
+    )
+}
+
+sub create_record {
+    my $self = shift;
+    my $data = shift;
+
+    my $user = RT->SystemUser;
+
+    return (\400, "Could not create reminder. Ticket not set") if !$data->{Ticket};
+
+    my $ticket = RT::Ticket->new( $user );
+    $ticket->Load($data->{Ticket});
+    return ( \400, "Unable to find ticket") if !$ticket->Id;
+    warn "type: ", $ticket->Type;
+    return ( \400, "Can only set a reminder on a ticket" ) if $ticket->Type ne 'ticket'; 
+
+    my $queue = RT::Queue->new( $user );
+    $queue->Load($ticket->Queue);
+    return (\400, "Unable to find queue") if !$queue->Id;
+
+    return (\403, $self->record->loc("No permission to create tickets in the queue '[_1]'", $queue->Name))
+    unless $self->record->CurrentUser->HasRight(
+        Right  => 'CreateTicket',
+        Object => $queue,
+    ) and $queue->Disabled != 1;
+
+    my $reminders = RT::Reminders->new( $user );
+    $reminders->Ticket( $data->{Ticket} );
+
+    my( $status, $msg)= $reminders->Add(
+        Subject => $data->{Subject},
+        Owner   => $user,
+        Due     => $data->{Due},
+    );
+
+    if( ! $status ) {
+        return ( \400, $msg );
+    }
+    else {
+        return ( \200, "Reminder added" );
+    }
+
+}
+
+sub forbidden {
+    my $self = shift;
+    return 0 unless $self->record->id;
+    return !$self->record->CurrentUserHasRight('ShowTicket');
+}
+
+sub lifecycle_hypermedia_links {
+    my $self = shift;
+    my $self_link = $self->_self_link;
+    my $ticket = $self->record;
+    my @links;
+
+    # lifecycle actions
+    my $lifecycle = $ticket->LifecycleObj;
+    my $current = $ticket->Status;
+    my $hide_resolve_with_deps = RT->Config->Get('HideResolveActionsWithDependencies')
+        && $ticket->HasUnresolvedDependencies;
+
+    for my $info ( $lifecycle->Actions($current) ) {
+        my $next = $info->{'to'};
+        next unless $lifecycle->IsTransition( $current => $next );
+
+        my $check = $lifecycle->CheckRight( $current => $next );
+        next unless $ticket->CurrentUserHasRight($check);
+
+        next if $hide_resolve_with_deps
+            && $lifecycle->IsInactive($next)
+            && !$lifecycle->IsInactive($current);
+
+        my $url = $self_link->{_url};
+        $url .= '/correspond' if ($info->{update}||'') eq 'Respond';
+        $url .= '/comment' if ($info->{update}||'') eq 'Comment';
+
+        push @links, {
+            %$info,
+            label => $self->current_user->loc($info->{'label'} || ucfirst($next)),
+            ref   => 'lifecycle',
+            _url  => $url,
+        };
+    }
+
+    return @links;
+}
+
+sub hypermedia_links {
+    my $self = shift;
+    my $self_link = $self->_self_link;
+    my $links = $self->_default_hypermedia_links(@_);
+    my $ticket = $self->record;
+
+    push @$links, $self->_transaction_history_link;
+
+    push @$links, {
+            ref     => 'correspond',
+            _url    => $self_link->{_url} . '/correspond',
+    } if $ticket->CurrentUserHasRight('ReplyToTicket');
+
+    push @$links, {
+        ref     => 'comment',
+        _url    => $self_link->{_url} . '/comment',
+    } if $ticket->CurrentUserHasRight('CommentOnTicket');
+
+    push @$links, $self->lifecycle_hypermedia_links;
+
+    return $links;
+}
+
+__PACKAGE__->meta->make_immutable;
+
+1;

commit 42f4387b2b56a15f76c46e61de617cbeb1eb3a12
Author: michel <michel at bestpractical.com>
Date:   Tue Aug 6 14:19:28 2019 +0200

    Added doc for reminder creation

diff --git a/lib/RT/Extension/REST2.pm b/lib/RT/Extension/REST2.pm
index c4b61c6..a7f2023 100644
--- a/lib/RT/Extension/REST2.pm
+++ b/lib/RT/Extension/REST2.pm
@@ -356,6 +356,18 @@ Below are some examples using the endpoints above.
     GET /attachment/:id
         retrieve an attachment
 
+=head3 Reminders
+
+    POST /reminder
+        create a reminder on a ticket
+
+=head3 Reminder Examples
+
+    # create a rminder with a due date (must be in ISO format) on ticket #1
+    curl -X POST -H "Content-Type: application/json" -u 'root:password' \
+    -d '{"Ticket": 6, "Due": "2019-08-10 00:00:01", "Subject": "Due date is 2019-08-10 00:00:01" }' \
+    https://myrt.com/REST/2.0/reminder
+
 =head3 Queues
 
     GET /queues/all

-----------------------------------------------------------------------


More information about the Bps-public-commit mailing list