From 22e6001362c804d445b9b430d67a25f3af145423 Mon Sep 17 00:00:00 2001 From: Finn Date: Mon, 8 Jul 2019 16:41:59 +0100 Subject: [PATCH] Wip on changing flow with the minion --- .../LocalLoop/Controller/Admin/ImportFrom.pm | 17 +++++++--- lib/Pear/LocalLoop/Import/LCCCsv/Suppliers.pm | 6 ++-- .../Plugin/Minion/Job/csv_supplier_import.pm | 34 +++++++++++++++++++ 3 files changed, 49 insertions(+), 8 deletions(-) create mode 100644 lib/Pear/LocalLoop/Plugin/Minion/Job/csv_supplier_import.pm diff --git a/lib/Pear/LocalLoop/Controller/Admin/ImportFrom.pm b/lib/Pear/LocalLoop/Controller/Admin/ImportFrom.pm index dfb710d..29d16cf 100644 --- a/lib/Pear/LocalLoop/Controller/Admin/ImportFrom.pm +++ b/lib/Pear/LocalLoop/Controller/Admin/ImportFrom.pm @@ -25,10 +25,19 @@ sub post_suppliers { $c->flash( error => "CSV file size is too large" ); return $c->redirect_to( '/admin/import_from' ); } - my $csv_import = Pear::LocalLoop::Import::LCCCsv::Suppliers->new( - csv_string => $c->param('suppliers_csv')->slurp, - schema => $c->app->schema - )->import_csv; + + my $filename; + + $c->minion->enqueue('csv_supplier_import' => $filename ); + + # 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] ); + + $c->flash( success => "CSV imported" ); return $c->redirect_to( '/admin/import_from' ); diff --git a/lib/Pear/LocalLoop/Import/LCCCsv/Suppliers.pm b/lib/Pear/LocalLoop/Import/LCCCsv/Suppliers.pm index ca87344..1e7e80c 100644 --- a/lib/Pear/LocalLoop/Import/LCCCsv/Suppliers.pm +++ b/lib/Pear/LocalLoop/Import/LCCCsv/Suppliers.pm @@ -14,10 +14,8 @@ sub import_csv { my ($self) = @_; my $rows = $self->csv_data; - foreach my $row ( @{$rows} ) { - $self->_row_to_result($row); - } - return 1; + + return $rows; } sub _row_to_result { diff --git a/lib/Pear/LocalLoop/Plugin/Minion/Job/csv_supplier_import.pm b/lib/Pear/LocalLoop/Plugin/Minion/Job/csv_supplier_import.pm new file mode 100644 index 0000000..a103e92 --- /dev/null +++ b/lib/Pear/LocalLoop/Plugin/Minion/Job/csv_supplier_import.pm @@ -0,0 +1,34 @@ +package Pear::LocalLoop::Plugin::Minion::Job::csv_supplier_import; +use Mojo::Base 'Pear::LocalLoop::Plugin::Minion::Job'; +use Devel::Dwarn; + +sub run { + my ( $self, $rows ) = @_; + + 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!'); +} + +1;