App-SD

 view release on metacpan or  search on metacpan

lib/App/SD/Replica/hm/PullEncoder.pm  view on Meta::CPAN


sub ticket_last_modified {
    my $self   = shift;
    my $ticket = shift;
    return App::SD::Util::string_to_datetime( $ticket->{modified_at} );
}

sub transcode_one_txn {
    my ( $self, $txn_wrapper, $previous_state, $ticket_final ) = (@_);

    my $txn = $txn_wrapper->{object};

    my @changesets;

    my $changeset = Prophet::ChangeSet->new(
        {   original_source_uuid => $self->sync_source->uuid,
            original_sequence_no => $txn->{'id'},
            creator => $self->sync_source->user_info( id => $txn->{'created_by'} )->{'email'},
        }
    );

    my $method = 'recode_' . $txn->{type};
    unless ( $self->can($method) ) {
        die "Unknown change type $txn->{type}.";
    }

    my $change = $self->$method( task => $ticket_final, transaction => $txn );

    $changeset->add_change( { change => $change } );

    # XXX is this tested at all? this method doesn't exist
    for my $email ( @{ $txn->{email_entries} } ) {
        if ( my $sub = $self->can( '_recode_email_' . 'blah' ) ) {
            $sub->(
                $self,
                previous_state => $previous_state,
                email          => $email,
                txn            => $txn,
                changeset      => $changeset,
            );
        }
    }

    $self->translate_props($changeset);
    return $changeset;
}

sub find_matching_tickets {
    my $self = shift;
    my %args = ();

    if ( my $props = $self->sync_source->props ) {
        while ( my ( $k, $v ) = each %$props ) { $args{$k} = $v }
    }

    unless ( keys %args ) {
        %args = (
            owner        => 'me',
            group        => 0,
            requestor    => 'me',
            not_complete => 1,
        );
    }

    my $status = $self->sync_source->hm->act( 'TaskSearch', %args );
    unless ( $status->{'success'} ) {
        die "couldn't search";
    }
    return $status->{content}{tasks};
}

# hiveminder transaction ~= prophet changeset
# hiveminder taskhistory ~= prophet change
# hiveminder taskemail ~= prophet change
#
sub find_matching_transactions {
    my $self = shift;
    my %args = validate( @_, { ticket => 1, starting_transaction => 1 } );

    my $txns = $self->sync_source->hm->search( 'TaskTransaction', task_id => $args{ticket}->{id} )
        || [];
    my @matched;
    for my $txn (@$txns) {

        # Skip things we know we don't want
        next if $txn->{'id'} < $args{'starting_transaction'};

        # Skip things we've pushed
        next if $self->sync_source->foreign_transaction_originated_locally( $txn->{'id'}, $args{ticket}->{id} );

    $txn->{history_entries}
            = $self->sync_source->hm->search( 'TaskHistory', transaction_id => $txn->{'id'} );
        $txn->{email_entries}
            = $self->sync_source->hm->search( 'TaskEmail', transaction_id => $txn->{'id'} );
        push @matched,
            {
            timestamp => App::SD::Util::string_to_datetime( $txn->{modified_at} ),
            serial    => $txn->{id},
            object    => $txn
            };
    }
    return \@matched;

}

sub add_prop_change {
    my $self = shift;
    my %args = validate( @_, { history_entry => 1, previous_state => 1, change => 1 } );

    no warnings 'uninitialized';
    my $field = qq{$args{'history_entry'}{'field'}} ||'';
    my $old   = qq{$args{'history_entry'}{'old_value'}} ||'';
    my $new   = qq{$args{'history_entry'}{'new_value'}} ||'';

    if ( $args{'previous_state'}->{$field} eq $new ) {
        $args{'previous_state'}->{$field} = $old;
    } else {
        $args{'previous_state'}->{$field} = $old;
        warn "$field: ". $args{'previous_state'}->{$field} . " != " . $new . "\n\n";
    }



( run in 0.852 second using v1.01-cache-2.11-cpan-39bf76dae61 )