Wip on changing flow with the minion

This commit is contained in:
Finn 2019-07-08 16:41:59 +01:00
parent c7ef724a46
commit ff19c81074
3 changed files with 49 additions and 8 deletions

View file

@ -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' );

View file

@ -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 {

View file

@ -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;