| 1 | package App::SD::CLI::Command::Ticket::Update; | ||
| 2 | use Any::Moose; | ||
| 3 | use Params::Validate qw/validate/; | ||
| 4 | |||
| 5 | extends 'Prophet::CLI::Command::Update'; | ||
| 6 | with 'App::SD::CLI::Model::Ticket'; | ||
| 7 | with 'App::SD::CLI::Command'; | ||
| 8 | with 'Prophet::CLI::TextEditorCommand'; | ||
| 9 | |||
| 10 | sub ARG_TRANSLATIONS { shift->SUPER::ARG_TRANSLATIONS(), a => 'all-props' }; | ||
| 11 | |||
| 12 | sub usage_msg { | ||
| 13 | my $self = shift; | ||
| 14 | my $cmd = $self->cli->get_script_name; | ||
| 15 | my @primary_commands = @{ $self->context->primary_commands }; | ||
| 16 | |||
| 17 | # if primary commands was only length 1, the type was not specified | ||
| 18 | # and we should indicate that a type is expected | ||
| 19 | push @primary_commands, '<record-type>' if @primary_commands <= 1; | ||
| 20 | |||
| 21 | my $type_and_subcmd = join( q{ }, @primary_commands ); | ||
| 22 | |||
| 23 | return <<"END_USAGE"; | ||
| 24 | usage: ${cmd}${type_and_subcmd} <record-id> --edit [--all-props] | ||
| 25 | ${cmd}${type_and_subcmd} <record-id> -- status=open | ||
| 26 | END_USAGE | ||
| 27 | } | ||
| 28 | |||
| 29 | # use an editor to edit if no props are specified on the commandline, | ||
| 30 | # allowing the creation of a new comment in the process | ||
| 31 | override run => sub { | ||
| 32 | my $self = shift; | ||
| 33 | |||
| 34 | $self->print_usage if $self->has_arg('h'); | ||
| 35 | |||
| 36 | $self->require_uuid; | ||
| 37 | my $record = $self->_load_record; | ||
| 38 | |||
| 39 | return super() if ($self->context->prop_names && !$self->has_arg('edit')); | ||
| 40 | my $template_to_edit = $self->create_record_template($record); | ||
| 41 | |||
| 42 | my $done = 0; | ||
| 43 | |||
| 44 | while (!$done) { | ||
| 45 | $done = $self->try_to_edit( template => \$template_to_edit, record => $record); | ||
| 46 | } | ||
| 47 | |||
| 48 | }; | ||
| 49 | |||
| 50 | sub process_template { | ||
| 51 | my $self = shift; | ||
| 52 | my %args = validate( @_, { template => 1, edited => 1, record => 1 } ); | ||
| 53 | |||
| 54 | my $record = $args{record}; | ||
| 55 | my $updated = $args{edited}; | ||
| 56 | my ( $props_ref, $comment ) = $self->parse_record_template($updated); | ||
| 57 | |||
| 58 | no warnings 'uninitialized'; | ||
| 59 | |||
| 60 | # if a formerly existing prop was removed from the output, delete it | ||
| 61 | # (deleting is currently the equivalent of setting to '', and | ||
| 62 | # we want to do this all in one changeset) | ||
| 63 | for my $prop ( keys %{ $record->get_props } ) { | ||
| 64 | next if ( grep { $_ eq $prop } $record->immutable_props ); | ||
| 65 | $props_ref->{$prop} = '' | ||
| 66 | if (!exists $props_ref->{$prop} && | ||
| 67 | # only delete props if they were actually presented | ||
| 68 | # for editing in the first place | ||
| 69 | grep { $_ eq $prop } $record->props_to_show( { | ||
| 70 | update => 1, | ||
| 71 | verbose => $self->has_arg('all-props'), | ||
| 72 | } ) | ||
| 73 | ); | ||
| 74 | } | ||
| 75 | |||
| 76 | # don't add props that didn't change to the changeset | ||
| 77 | for my $prop ( keys %$props_ref ) { | ||
| 78 | delete $props_ref->{$prop} | ||
| 79 | if $props_ref->{$prop} eq $record->prop($prop); | ||
| 80 | } | ||
| 81 | |||
| 82 | # set the new props | ||
| 83 | if ( keys %$props_ref ) { | ||
| 84 | my $error; | ||
| 85 | local $@; | ||
| 86 | eval { $record->set_props( props => $props_ref ) } | ||
| 87 | or $error = $@ || "Something went wrong!"; | ||
| 88 | |||
| 89 | return $self->handle_template_errors( | ||
| 90 | error => $error, | ||
| 91 | template_ref => $args{template}, | ||
| 92 | bad_template => $updated | ||
| 93 | ) if ($error); | ||
| 94 | |||
| 95 | print 'Updated ticket ' . $record->luid . ' (' . $record->uuid . ")\n"; | ||
| 96 | } else { | ||
| 97 | print "No changes in properties.\n"; | ||
| 98 | } | ||
| 99 | |||
| 100 | $self->add_comment( content => $comment, uuid => $record->uuid ) if $comment; | ||
| 101 | return 1; | ||
| 102 | } | ||
| 103 | |||
| 104 | __PACKAGE__->meta->make_immutable; | ||
| 105 | no Any::Moose; | ||
| 106 | |||
| 107 | 1; |