From 1a669336935c4187dd738ce016632b37b1e3355f Mon Sep 17 00:00:00 2001 From: Finn Date: Mon, 8 Jul 2019 18:12:35 +0100 Subject: [PATCH] Added properly working imports with minions and status --- lib/Pear/LocalLoop.pm | 1 + .../LocalLoop/Controller/Admin/ImportFrom.pm | 48 ++++++++----------- lib/Pear/LocalLoop/Import/LCCCsv/Suppliers.pm | 4 +- .../LocalLoop/Import/LCCCsv/Transactions.pm | 5 +- lib/Pear/LocalLoop/Import/Role/CSV.pm | 2 +- .../Plugin/Minion/Job/csv_supplier_import.pm | 33 +++---------- .../Minion/Job/csv_transaction_import.pm | 15 ++++++ 7 files changed, 49 insertions(+), 59 deletions(-) create mode 100644 lib/Pear/LocalLoop/Plugin/Minion/Job/csv_transaction_import.pm diff --git a/lib/Pear/LocalLoop.pm b/lib/Pear/LocalLoop.pm index e6248e6..a6fd54f 100644 --- a/lib/Pear/LocalLoop.pm +++ b/lib/Pear/LocalLoop.pm @@ -26,6 +26,7 @@ sub startup { $self->plugin('Config', { default => { storage_path => tempdir, + upload_path => $self->home->child('upload'), sessionTimeSeconds => 60 * 60 * 24 * 7, sessionTokenJsonName => 'session_key', sessionExpiresJsonName => 'sessionExpires', diff --git a/lib/Pear/LocalLoop/Controller/Admin/ImportFrom.pm b/lib/Pear/LocalLoop/Controller/Admin/ImportFrom.pm index 29d16cf..3b8e923 100644 --- a/lib/Pear/LocalLoop/Controller/Admin/ImportFrom.pm +++ b/lib/Pear/LocalLoop/Controller/Admin/ImportFrom.pm @@ -2,9 +2,7 @@ package Pear::LocalLoop::Controller::Admin::ImportFrom; use Mojo::Base 'Mojolicious::Controller'; use Moo; use Try::Tiny; - -use Pear::LocalLoop::Import::LCCCsv::Suppliers; -use Pear::LocalLoop::Import::LCCCsv::Transactions; +use Mojo::File qw/ path /; sub index { my $c = shift; @@ -26,20 +24,17 @@ sub post_suppliers { return $c->redirect_to( '/admin/import_from' ); } - my $filename; + my $file = $c->param('suppliers_csv'); - $c->minion->enqueue('csv_supplier_import' => $filename ); + my $filename = path($c->app->config->{upload_path}, time.'suppliers.csv' ); - # my $csv_import = Pear::LocalLoop::Import::LCCCsv::Suppliers->new( - # csv_string => $c->param('suppliers_csv')->slurp, - # schema => $c->app->schema - # )->import_csv; - # - # my $job_id = $c->minion->enqueue('csv_supplier_import' => [$csv_import] ); + $file->move_to($filename); + my $job_id = $c->minion->enqueue('csv_supplier_import' => [$filename] ); + my $job_url = $c->url_for("/admin/minionjobs?id=$job_id")->to_abs; - $c->flash( success => "CSV imported" ); + $c->flash(success => "CSV import started, see status of minion job at: $job_url"); return $c->redirect_to( '/admin/import_from' ); } @@ -56,22 +51,19 @@ sub post_transactions { $c->flash( error => "CSV file size is too large" ); return $c->redirect_to( '/admin/import_from' ); } - my $csv_error; - try { - Pear::LocalLoop::Import::LCCCsv::Transactions->new( - csv_string => $c->param('transactions_csv')->slurp, - schema => $c->app->schema - )->import_csv; - } catch { - $csv_error = $_; - }; - if ( $csv_error ) { - $c->flash( error => $csv_error ); - return $c->redirect_to( '/admin/import_from' ); - } else { - $c->flash( success => "CSV imported" ); - return $c->redirect_to( '/admin/import_from' ); - } + + my $file = $c->param('transactions_csv'); + + my $filename = path($c->app->config->{upload_path}, time.'transactions.csv' ); + + $file->move_to($filename); + + my $job_id = $c->minion->enqueue('csv_transaction_import' => [$filename] ); + + my $job_url = $c->url_for("/admin/minionjobs?id=$job_id")->to_abs; + + $c->flash(success => "CSV import started, see status of minion job at: $job_url"); + return $c->redirect_to( '/admin/import_from' ); } 1; diff --git a/lib/Pear/LocalLoop/Import/LCCCsv/Suppliers.pm b/lib/Pear/LocalLoop/Import/LCCCsv/Suppliers.pm index 1e7e80c..6c2541e 100644 --- a/lib/Pear/LocalLoop/Import/LCCCsv/Suppliers.pm +++ b/lib/Pear/LocalLoop/Import/LCCCsv/Suppliers.pm @@ -15,7 +15,9 @@ sub import_csv { my $rows = $self->csv_data; - return $rows; + foreach my $row ( @{$rows} ) { + $self->_row_to_result($row); + } } sub _row_to_result { diff --git a/lib/Pear/LocalLoop/Import/LCCCsv/Transactions.pm b/lib/Pear/LocalLoop/Import/LCCCsv/Transactions.pm index 89e574c..77d7e92 100644 --- a/lib/Pear/LocalLoop/Import/LCCCsv/Transactions.pm +++ b/lib/Pear/LocalLoop/Import/LCCCsv/Transactions.pm @@ -19,11 +19,10 @@ sub import_csv { my ($self) = @_; my $rows = $self->csv_data; - my $lcc_org = $self->schema->resultset('Organisation')->find({ name => "Lancashire County Council" }); + foreach my $row ( @{$rows} ) { - $self->_row_to_result($row, $lcc_org); + $self->_row_to_result($row); } - return 1; } sub _row_to_result { diff --git a/lib/Pear/LocalLoop/Import/Role/CSV.pm b/lib/Pear/LocalLoop/Import/Role/CSV.pm index b4315cb..110252f 100644 --- a/lib/Pear/LocalLoop/Import/Role/CSV.pm +++ b/lib/Pear/LocalLoop/Import/Role/CSV.pm @@ -29,7 +29,7 @@ has _csv_filehandle => ( my $self = shift; my $fh; if ( $self->has_csv_file ) { - open $fh, '<', \${$self->csv_file}; + open $fh, '<', $self->csv_file; } elsif ( $self->has_csv_string ) { my $string = $self->csv_string; open $fh, '<', \$string; diff --git a/lib/Pear/LocalLoop/Plugin/Minion/Job/csv_supplier_import.pm b/lib/Pear/LocalLoop/Plugin/Minion/Job/csv_supplier_import.pm index a103e92..7c0b5fc 100644 --- a/lib/Pear/LocalLoop/Plugin/Minion/Job/csv_supplier_import.pm +++ b/lib/Pear/LocalLoop/Plugin/Minion/Job/csv_supplier_import.pm @@ -1,34 +1,15 @@ package Pear::LocalLoop::Plugin::Minion::Job::csv_supplier_import; use Mojo::Base 'Pear::LocalLoop::Plugin::Minion::Job'; -use Devel::Dwarn; + +use Pear::LocalLoop::Import::LCCCsv::Suppliers; sub run { - my ( $self, $rows ) = @_; + my ( $self, $filename ) = @_; - foreach my $row ( @{$rows} ) { - $self->_row_to_result($row); - } -} - -sub _row_to_result { - my ( $self, $row ) = @_; - # Dwarn $row->{supplier_id}; - my $addr2 = $row->{post_town}; - - my $address = ( defined $addr2 ? ( $row->{"address line 2"} . ' ' . $addr2) : $row->{"address line 2"} ); - - $self->external_result->find_or_create_related('organisations', { - external_id => $row->{supplier_id}, - organisation => { - name => $row->{name}, - street_name => $row->{"address line 1"}, - town => $address, - postcode => $row->{post_code}, - country => $row->{country_code}, - entity => { type => 'organisation' }, - } - }); - $self->app->log->debug('Imported the CSV fully!'); + my $csv_import = Pear::LocalLoop::Import::LCCCsv::Suppliers->new( + csv_file => $filename, + schema => $self->app->schema + )->import_csv; } 1; diff --git a/lib/Pear/LocalLoop/Plugin/Minion/Job/csv_transaction_import.pm b/lib/Pear/LocalLoop/Plugin/Minion/Job/csv_transaction_import.pm new file mode 100644 index 0000000..4d6db82 --- /dev/null +++ b/lib/Pear/LocalLoop/Plugin/Minion/Job/csv_transaction_import.pm @@ -0,0 +1,15 @@ +package Pear::LocalLoop::Plugin::Minion::Job::csv_transaction_import; +use Mojo::Base 'Pear::LocalLoop::Plugin::Minion::Job'; + +use Pear::LocalLoop::Import::LCCCsv::Transactions; + +sub run { + my ( $self, $filename ) = @_; + + my $csv_import = Pear::LocalLoop::Import::LCCCsv::Transactions->new( + csv_file => $filename, + schema => $self->app->schema + )->import_csv; +} + +1;