This repository has been archived on 2023-08-16. You can view files and clone it, but cannot push or open issues or pull requests.
Foodloop-Server/lib/Pear/LocalLoop/Plugin/Minion/Job/entity_postcode_lookup.pm

36 lines
1.1 KiB
Perl
Raw Normal View History

2019-07-12 20:30:34 +00:00
package Pear::LocalLoop::Plugin::Minion::Job::entity_postcode_lookup;
use Mojo::Base 'Pear::LocalLoop::Plugin::Minion::Job';
sub run {
2021-03-20 12:09:50 +00:00
my ( $self, $entity_id ) = @_;
2019-07-12 20:30:34 +00:00
2021-03-20 12:09:50 +00:00
my $entity_rs = $self->app->schema->resultset('Entity');
$entity_rs = $entity_rs->search( { id => $entity_id } ) if $entity_id;
2019-07-12 20:30:34 +00:00
2021-03-20 12:09:50 +00:00
while ( my $entity = $entity_rs->next ) {
my $obj = $entity->type_object;
next unless $obj;
2019-07-12 20:30:34 +00:00
2021-03-20 12:09:50 +00:00
my $postcode_obj = Geo::UK::Postcode::Regex->parse( $obj->postcode );
2019-07-12 20:30:34 +00:00
2021-03-20 12:09:50 +00:00
unless ( defined $postcode_obj && $postcode_obj->{non_geographical} ) {
my $pc_result = $self->app->schema->resultset('GbPostcode')->find(
{
incode => $postcode_obj->{incode},
outcode => $postcode_obj->{outcode},
}
);
if ( defined $pc_result ) {
$entity->update_or_create_related(
'postcode',
{
gb_postcode => $pc_result,
}
);
}
}
2019-07-12 20:30:34 +00:00
}
}
1;