Merge branch 'development' into TBSliver/Import-Fixes

This commit is contained in:
Tom Bloor 2017-11-28 17:52:56 +00:00 committed by GitHub
commit f6f50d4932
23 changed files with 2743 additions and 44 deletions

View file

@ -158,6 +158,7 @@ sub startup {
my $api_v1_supplier = $api_v1->under('/supplier');
$api_v1_supplier->post('/location')->to('api-v1-supplier-location#index');
$api_v1_supplier->post('/location/lis')->to('api-v1-supplier-location#lis_load');
my $api_v1_org = $api_v1->under('/organisation')->to('api-v1-organisation#auth');

View file

@ -85,9 +85,15 @@ sub valid_read {
order_by => { -desc => 'submitted_at' },
},
);
my $associations = $valid_org->entity->associations;
my $assoc = {
lis => defined $associations ? $associations->lis : 0,
};
$c->stash(
valid_org => $valid_org,
transactions => $transactions,
associations => $assoc,
);
}
@ -102,6 +108,7 @@ sub valid_edit {
$validation->required('postcode')->postcode;
$validation->optional('pending');
$validation->optional('is_local');
$validation->optional('is_lis');
if ( $validation->has_error ) {
$c->flash( error => 'The validation has failed' );
@ -121,9 +128,12 @@ sub valid_edit {
pending => defined $validation->param('pending') ? 0 : 1,
is_local => $validation->param('is_local'),
});
$valid_org->entity->update_or_create_related( 'associations', {
lis => $validation->param('is_lis'),
});
} );
} finally {
if ( @_ ) {
if ( @_ ) {use Devel::Dwarn; Dwarn \@_;
$c->flash( error => 'Something went wrong Updating the Organisation' );
} else {
$c->flash( success => 'Updated Organisation' );
@ -143,7 +153,7 @@ sub merge_list {
$c->redirect_to( '/admin/organisations/' . $org_id );
return;
}
my $org_rs = $c->result_set->search(
{
id => { '!=' => $org_id },

View file

@ -79,6 +79,9 @@ sub index {
'organisation.name',
'organisation.latitude',
'organisation.longitude',
'organisation.street_name',
'organisation.town',
'organisation.postcode',
],
group_by => [ qw/ organisation.id / ],
},
@ -86,11 +89,14 @@ sub index {
$org_rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
my $suppliers = [ map {
my $suppliers = [ map {
{
latitude => $_->{organisation}->{latitude} * 1,
longitude => $_->{organisation}->{longitude} * 1,
name => $_->{organisation}->{name},
street_name => $_->{organisation}->{street_name},
town => $_->{organisation}->{town},
postcode => $_->{organisation}->{postcode},
}
} $org_rs->all ];
@ -106,4 +112,82 @@ sub index {
);
}
sub lis_load {
my $c = shift;
return if $c->validation_error('index');
my $json = $c->stash->{api_json};
# Extra custom error, because its funny
if ( $json->{north_east}->{latitude} < $json->{south_west}->{latitude} ) {
return $c->render(
json => {
success => Mojo::JSON->false,
errors => [ 'upside_down' ],
},
status => 400,
);
}
my $entity = $c->stash->{api_user}->entity;
my $entity_type_object = $entity->type_object;
my $orgs_lis = $c->schema->resultset('EntityAssociation')->search(
{
'lis' => 1,
},
);
# need: organisations only, with name, latitude, and longitude
my $org_rs = $orgs_lis->search_related('entity',
{
'entity.type' => 'organisation',
'organisation.latitude' => { -between => [
$json->{south_west}->{latitude},
$json->{north_east}->{latitude},
] },
'organisation.longitude' => { -between => [
$json->{south_west}->{longitude},
$json->{north_east}->{longitude},
] },
},
{
join => [ qw/ organisation / ],
columns => [
'organisation.name',
'organisation.latitude',
'organisation.longitude',
'organisation.street_name',
'organisation.town',
'organisation.postcode',
],
group_by => [ qw/ organisation.id / ],
},
);
$org_rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
my $locations = [ map {
{
latitude => $_->{organisation}->{latitude} * 1,
longitude => $_->{organisation}->{longitude} * 1,
name => $_->{organisation}->{name},
street_name => $_->{organisation}->{street_name},
town => $_->{organisation}->{town},
postcode => $_->{organisation}->{postcode},
}
} $org_rs->all ];
$c->render(
json => {
success => Mojo::JSON->true,
locations => $locations,
self => {
latitude => $entity_type_object->latitude,
longitude => $entity_type_object->longitude,
}
},
);
}
1;

View file

@ -6,7 +6,7 @@ use warnings;
use base 'DBIx::Class::Schema';
our $VERSION = 16;
our $VERSION = 17;
__PACKAGE__->load_namespaces;

View file

@ -37,6 +37,11 @@ __PACKAGE__->might_have(
"Pear::LocalLoop::Schema::Result::User" => "entity_id",
);
__PACKAGE__->might_have(
"associations",
"Pear::LocalLoop::Schema::Result::EntityAssociation" => "entity_id",
);
__PACKAGE__->has_many(
"purchases",
"Pear::LocalLoop::Schema::Result::Transaction",

View file

@ -0,0 +1,34 @@
package Pear::LocalLoop::Schema::Result::EntityAssociation;
use strict;
use warnings;
use base 'DBIx::Class::Core';
__PACKAGE__->table("entity_association");
__PACKAGE__->add_columns(
"id" => {
data_type => "integer",
is_auto_increment => 1,
is_nullable => 0,
},
"entity_id" => {
data_type => 'integer',
is_nullable => 0,
is_foreign_key => 1,
},
"lis" => {
data_type => 'boolean',
default => undef,
is_nullable => 1,
},
);
__PACKAGE__->set_primary_key("id");
__PACKAGE__->belongs_to(
"entity",
"Pear::LocalLoop::Schema::Result::Entity",
"entity_id",
);