Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision a03a52eb

Von Tamino Steinert vor 11 Monaten hinzugefügt

  • ID a03a52eb41062404a7bb16472ef47e020511c24f
  • Vorgänger e453ae7c
  • Nachfolger 6aa3c3ea

Email Background Jobs: validiere Daten-Feld

Unterschiede anzeigen:

SL/BackgroundJob/CleanUpEmailSubfolders.pm
9 9

  
10 10
sub clean_up_record_subfolders {
11 11
  my ($self) = @_;
12
  my $imap_client = SL::IMAPClient->new();
12
  my $imap_client = SL::IMAPClient->new(%{$::lx_office_conf{imap_client}});
13 13

  
14 14
  my $open_sales_orders = SL::DB::Manager::Order->get_all(
15 15
    query => [
......
18 18
    ],
19 19
  );
20 20

  
21
  $imap_client->clean_up_record_subfolders($open_sales_orders);
21
  $imap_client->clean_up_record_subfolders(active_records => $open_sales_orders);
22 22
}
23 23

  
24 24
sub run {
SL/BackgroundJob/ImportRecordEmails.pm
11 11
use SL::Helper::EmailProcessing;
12 12
use SL::Presenter::Tag qw(link_tag);
13 13

  
14
use Params::Validate qw(:all);
14 15
use List::MoreUtils qw(any);
15 16

  
16 17
sub sync_record_email_folder {
17 18
  my ($self, $config) = @_;
18 19

  
19
  my $imap_client = SL::IMAPClient->new(%$config);
20
  return "IMAP client is disabled." unless $imap_client;
20
  my %imap_config;
21
  foreach my $key (qw(enabled hostname port ssl username password base_folder)) {
22
    if (defined $config->{$key}) {
23
      $imap_config{$key} = $config->{$key};
24
    }
25
  }
26

  
27
  my $imap_client = SL::IMAPClient->new(%imap_config);
21 28

  
22 29
  my $email_import = $imap_client->update_emails_from_folder(
23
    $config->{folder},
24
    {
25
      email_journal => {
26
        status => 'imported',
27
        record_type => $config->{record_type},
28
      },
30
    folder => $config->{folder},
31
    email_journal_params => {
32
      record_type => $config->{record_type},
29 33
    }
30 34
  );
31 35
  return "No emails to import." unless $email_import;
......
57 61
          $result .= "Error while processing email journal $email_journal_id attachments with $function_name: $@";
58 62
        };
59 63
      }
60
      if ($created_records) {
64
      if ($created_records  && $config->{processed_imap_flag}) {
61 65
        $imap_client->set_flag_for_email(
62
          $email_journal, $config->{processed_imap_flag});
63
      } else {
66
          email_journal => $email_journal,
67
          flag          => $config->{processed_imap_flag},
68
        );
69
      } elsif ($config->{not_processed_imap_flag}) {
64 70
        $imap_client->set_flag_for_email(
65
          $email_journal, $config->{not_processed_imap_flag});
71
          email_journal => $email_journal,
72
          flag          => $config->{not_processed_imap_flag},
73
        );
66 74
      }
67 75
    }
68 76
    $result .= "Processed attachments with "
......
105 113
  my ($self, $job_obj) = @_;
106 114
  $self->{job_obj} = $job_obj;
107 115

  
108
  my $data = $job_obj->data_as_hash;
116
  my %spec = (
117
      folder => {
118
        type => SCALAR,
119
        optional => 1,
120
      },
121
      record_type => {
122
        optional => 1,
123
        default  => 'catch_all',
124
        callbacks => {
125
          'valid record type' => sub {
126
            my $valid_record_types = SL::DB::EmailJournal->meta->{columns}->{record_type}->{check_in};
127
            unless (any {$_[0] eq $_} @$valid_record_types) {
128
              die "record_type '$_[0]' is not valid. Possible values:\n- " . join("\n- ", @$valid_record_types);
129
            }
130
          },
131
        },
132
      },
133
      process_imported_emails => {
134
        type => SCALAR | ARRAYREF,
135
        optional => 1,
136
        callbacks => {
137
          'function is implemented' => sub {
138
            foreach my $function_name (ref $_[0] eq 'ARRAY' ? @{$_[0]} : ($_[0])) {
139
              !!SL::Helper::EmailProcessing->can_function($function_name) or
140
                die "Function '$function_name' not implemented in SL::Helper::EmailProcessing";
141
            }
142
            1;
143
          }
144
        }
145
      },
146
      processed_imap_flag => {
147
        type => SCALAR,
148
        optional => 1,
149
      },
150
      not_processed_imap_flag => {
151
        type => SCALAR,
152
        optional => 1,
153
      },
154
      email_import_ids_to_delete => {
155
        type => ARRAYREF,
156
        optional => 1,
157
      },
158
      # email config
159
      enabled     => { type => BOOLEAN, optional => 1, },
160
      hostname    => { type => SCALAR,  optional => 1, },
161
      port        => { type => SCALAR,  optional => 1, },
162
      ssl         => { type => BOOLEAN, optional => 1, },
163
      username    => { type => SCALAR,  optional => 1, },
164
      password    => { type => SCALAR,  optional => 1, },
165
      base_folder => { type => SCALAR,  optional => 1, },
166
  );
109 167

  
110
  my $email_import_ids_to_delete = $data->{email_import_ids_to_delete} || [];
168
  my @bj_data = $job_obj->data_as_hash;
169
  my %data = validate_with(
170
    params => \@bj_data,
171
    spec   => \%spec,
172
    called => "data filed in Background Job",
173
  );
111 174

  
112
  my $record_type = $data->{record_type};
113
  my $config = $::lx_office_conf{"record_emails_imap/record_type/$record_type"}
175
  my $record_type = $data{record_type};
176
  my $loaded_config = $::lx_office_conf{"record_emails_imap/record_type/$record_type"}
114 177
    || $::lx_office_conf{record_emails_imap}
115 178
    || {};
179

  
180
  my @loaded_config = %$loaded_config;
181
  my %config = validate_with(
182
    params => \@loaded_config,
183
    spec   => \%spec,
184
    called => "kivitendo.conf in [record_emails_imap] with type $record_type",
185
  );
116 186
  # overwrite with background job data
117
  $config->{$_} = $data->{$_} for keys %$data;
118
  $config->{record_type} ||= 'catch_all';
119

  
120
  $record_type = $config->{record_type};
121
  if ($record_type) {
122
    my $valid_record_types = SL::DB::EmailJournal->meta->{columns}->{record_type}->{check_in};
123
    unless (any {$record_type eq $_} @$valid_record_types) {
124
      die "record_type '$record_type' is not valid. Possible values:\n- " . join("\n- ", @$valid_record_types);
125
    }
126
  }
187
  $config{$_} = $data{$_} for keys %data;
127 188

  
128 189
  my @results;
129
  if (scalar $email_import_ids_to_delete) {
130
    push @results, $self->delete_email_imports($email_import_ids_to_delete);
190
  if (scalar $config{email_import_ids_to_delete}) {
191
    push @results, $self->delete_email_imports($config{email_import_ids_to_delete});
131 192
  }
132 193

  
133
  push @results, $self->sync_record_email_folder($config);
194
  push @results, $self->sync_record_email_folder(\%config);
134 195

  
135 196
  return join("\n", grep { $_ ne ''} @results);
136 197
}
SL/BackgroundJob/SyncEmailFolder.pm
5 5

  
6 6
use parent qw(SL::BackgroundJob::Base);
7 7

  
8
use Params::Validate qw(:all);
9

  
8 10
use SL::IMAPClient;
9 11
use SL::DB::Manager::EmailImport;
10 12

  
......
12 14
  my ($self) = @_;
13 15
  my $folder = $self->{job_obj}->data_as_hash->{folder};
14 16

  
15
  my $imap_client = SL::IMAPClient->new();
16
  die "Email client is disabled" unless $imap_client;
17
  my $imap_client = SL::IMAPClient->new(%{$::lx_office_conf{imap_client}});
17 18

  
18
  my $email_import = $imap_client->update_emails_from_folder($folder);
19
  my $email_import = $imap_client->update_emails_from_folder(
20
    folder => $folder
21
  );
19 22
  return unless $email_import;
20 23

  
21 24
  return "Created email import: " . $email_import->id;
......
43 46
sub run {
44 47
  my ($self, $job_obj) = @_;
45 48
  $self->{job_obj} = $job_obj;
49
  my @bj_data = $job_obj->data_as_hash;
50
  validate_with(
51
    params => \@bj_data,
52
    spec   => {
53
      folder => {
54
        type =>
55
        SCALAR, optional => 1
56
      },
57
      email_import_ids_to_delete => {
58
        type => ARRAYREF,
59
        optional => 1,
60
      }
61
    },
62
    called => "data filed in Background Job",
63
  );
46 64

  
47 65
  my @results;
48 66
  push @results, $self->delete_email_imports();

Auch abrufbar als: Unified diff