merge subject

Moderator: crythias

Locked
ovron
Znuny newbie
Posts: 19
Joined: 23 Apr 2015, 15:29
Znuny Version: 4.0.7

merge subject

Post by ovron »

Hello

We have a filter that merge identical tickets. He check the sender, subject and body.

Code: Select all

# --
# Kernel/System/PostMaster/Filter/MergeIdenticalTickets.pm - sub part of PostMaster.pm
# Copyright (C) 2014 Perl-Services.de, http://perl-services.de
# --
# 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::System::PostMaster::Filter::MergeIdenticalTickets;

use strict;
use warnings;

use List::Util qw(first);

our @ObjectDependencies = qw(
    Kernel::System::Ticket
    Kernel::System::Log
);

sub new {
    my ( $Type, %Param ) = @_;

    # allocate new hash for object
    my $Self = {};
    bless( $Self, $Type );

    $Self->{Debug} = $Param{Debug} || 0;

    return $Self;
}

sub Run {
    my ( $Self, %Param ) = @_;

    my $LogObject = $Kernel::OM->Get('Kernel::System::Log');

    # check needed stuff
    for my $Needed (qw(JobConfig GetParam)) {
        if ( !$Param{$Needed} ) {
            $LogObject->Log(
                Priority => 'error',
                Message => "Need $Needed!",
            );
            return;
        }
    }

    # get config options
    my %Config;
    my %Metrics;

    if ( $Param{JobConfig} && ref $Param{JobConfig} eq 'HASH' ) {
        %Config  = %{ $Param{JobConfig} };
        %Metrics = %{ $Param{JobConfig}->{Metric} || {} };
    }

    return 1 if !%Config;
    return 1 if !%Metrics;

    my %Mail = %{ $Param{GetParam} };

    my %SearchCriteria = ( StateType => 'Open' );
    if ( $Metrics{From} ) {
        $SearchCriteria{From} = $Mail{From}
    }

    if ( $Metrics{Subject} ) {
        $SearchCriteria{Subject} = $Mail{Subject}
    }

    if ( $Metrics{Body} && !$Metrics{HTMLBody} ) {
        $SearchCriteria{Body} = $Mail{Body}
    }

    my $TicketObject = $Kernel::OM->Get('Kernel::System::Ticket');
    my @TicketIDs    = $TicketObject->TicketSearch(
        %SearchCriteria,
        Result => 'ARRAY',
        UserID => 1,
    );

    return 1 if !@TicketIDs;

    my ($TicketID) = first { $_ ne $Param{TicketID} } reverse @TicketIDs;
    my ($HTMLFile) = first{ $_->{Filename} eq 'file-2' }@{ $Mail{Attachment} || [] };

    if ( $Metrics{HTMLBody} && $HTMLFile ) {
        my $Found = 0;

        POSSIBLETICKET:
        for my $PossibleTicket ( reverse @TicketIDs ) {
            next POSSIBLETICKET if $PossibleTicket eq $Param{TicketID};

            my %Article = $TicketObject->ArticleFirstArticle(
                TicketID => $PossibleTicket,
                UserID   => 1,
            );

            my %AttachmentIndex = $TicketObject->ArticleAttachmentIndex(
                ArticleID => $Article{ArticleID},
                UserID    => 1,
            );

            my ($FileID) = first { $AttachmentIndex{$_}->{Filename} eq 'file-2' }keys %AttachmentIndex;
            my %File     = $TicketObject->ArticleAttachment(
                FileID    => $FileID,
                ArticleID => $Article{ArticleID},
                UserID    => 1,
            );

            if ( $File{Content} eq $HTMLFile->{Content} ) {
                $Found++;
                $TicketID = $PossibleTicket;
                last POSSIBLETICKET;
            }
        }

        $TicketID = undef if !$Found;
    }

    if ( $TicketID ) {
        $TicketObject->TicketMerge(
            MainTicketID  => $TicketID,
            MergeTicketID => $Param{TicketID},
            UserID        => 1,
        );
    }

    return 1;
}

1;
It is possible that he check only the first 10 letter from the subject?

Thanks
ovron
RStraub
Znuny guru
Posts: 2210
Joined: 13 Mar 2014, 09:16
Znuny Version: 6.0.14
Real Name: Rolf Straub

Re: merge subject

Post by RStraub »

You could customize that module (or write a custom one) where you modify these lines:

Code: Select all

    if ( $Metrics{Subject} ) {
        $SearchCriteria{Subject} = $Mail{Subject}
    }
to something like this:

Code: Select all

    if ( $Metrics{Subject} ) {
        $SearchCriteria{Subject} = substr( $Mail{Subject}, 0, 10);
    }
Note: Untested
Currently using: OTRS 6.0.14 -- MariaDB -- Ubuntu 16 LTS
ovron
Znuny newbie
Posts: 19
Joined: 23 Apr 2015, 15:29
Znuny Version: 4.0.7

Re: merge subject

Post by ovron »

Thanks but it don't work..
RStraub
Znuny guru
Posts: 2210
Joined: 13 Mar 2014, 09:16
Znuny Version: 6.0.14
Real Name: Rolf Straub

Re: merge subject

Post by RStraub »

Maybe with a wildcard at the end?

Code: Select all

    if ( $Metrics{Subject} ) {
        $SearchCriteria{Subject} = substr( $Mail{Subject}, 0, 10) . '%';
    }
Currently using: OTRS 6.0.14 -- MariaDB -- Ubuntu 16 LTS
Locked