Added output of location on User endpoint, and setting lat/long on user
registration
This commit is contained in:
parent
f1e3756075
commit
202deb9178
6 changed files with 182 additions and 8 deletions
|
@ -2,6 +2,8 @@ package Pear::LocalLoop::Controller::Api::Register;
|
|||
use Mojo::Base 'Mojolicious::Controller';
|
||||
use DateTime;
|
||||
|
||||
use Geo::UK::Postcode::Regex;
|
||||
|
||||
has error_messages => sub {
|
||||
return {
|
||||
token => {
|
||||
|
@ -80,6 +82,34 @@ sub post_register {
|
|||
|
||||
return $c->api_validation_error if $validation->has_error;
|
||||
|
||||
my $postcode_obj = Geo::UK::Postcode::Regex->parse(
|
||||
$validation->param('postcode')
|
||||
);
|
||||
|
||||
my $location;
|
||||
|
||||
unless ( defined $postcode_obj && $postcode_obj->{non_geographical} ) {
|
||||
my $pc_result = $c->schema->resultset('GbPostcode')->find({
|
||||
incode => $postcode_obj->{incode},
|
||||
outcode => $postcode_obj->{outcode},
|
||||
});
|
||||
if ( defined $pc_result ) {
|
||||
# Force truncation here as SQLite is stupid
|
||||
$location = {
|
||||
latitude => (
|
||||
$usertype eq 'customer'
|
||||
? int($pc_result->latitude * 100 ) / 100
|
||||
: $pc_result->latitude
|
||||
),
|
||||
longitude => (
|
||||
$usertype eq 'customer'
|
||||
? int($pc_result->longitude * 100 ) / 100
|
||||
: $pc_result->longitude
|
||||
),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if ($usertype eq 'customer'){
|
||||
|
||||
$c->schema->txn_do( sub {
|
||||
|
@ -94,6 +124,7 @@ sub post_register {
|
|||
display_name => $validation->param('display_name'),
|
||||
year_of_birth => $validation->param('year_of_birth'),
|
||||
postcode => $validation->param('postcode'),
|
||||
( defined $location ? ( %$location ) : () ),
|
||||
},
|
||||
user => {
|
||||
email => $validation->param('email'),
|
||||
|
@ -118,6 +149,7 @@ sub post_register {
|
|||
town => $validation->param('town'),
|
||||
sector => $validation->param('sector'),
|
||||
postcode => $validation->param('postcode'),
|
||||
( defined $location ? ( %$location ) : () ),
|
||||
},
|
||||
user => {
|
||||
email => $validation->param('email'),
|
||||
|
|
|
@ -49,22 +49,28 @@ sub post_account {
|
|||
my $email = $user_result->email;
|
||||
|
||||
if ( $user_result->type eq 'customer' ) {
|
||||
my $full_name = $user_result->entity->customer->full_name;
|
||||
my $display_name = $user_result->entity->customer->display_name;
|
||||
my $postcode = $user_result->entity->customer->postcode;
|
||||
my $customer = $user_result->entity->customer;
|
||||
my $full_name = $customer->full_name;
|
||||
my $display_name = $customer->display_name;
|
||||
my $postcode = $customer->postcode;
|
||||
return $c->render( json => {
|
||||
success => Mojo::JSON->true,
|
||||
full_name => $full_name,
|
||||
display_name => $display_name,
|
||||
email => $email,
|
||||
postcode => $postcode,
|
||||
location => {
|
||||
latitude => (defined $customer->latitude ? $customer->latitude * 1 : undef),
|
||||
longitude => (defined $customer->longitude ? $customer->longitude * 1 : undef),
|
||||
},
|
||||
});
|
||||
} elsif ( $user_result->type eq 'organisation' ) {
|
||||
my $name = $user_result->entity->organisation->name;
|
||||
my $postcode = $user_result->entity->organisation->postcode;
|
||||
my $street_name = $user_result->entity->organisation->street_name;
|
||||
my $town = $user_result->entity->organisation->town;
|
||||
my $sector = $user_result->entity->organisation->sector;
|
||||
my $organisation = $user_result->entity->organisation;
|
||||
my $name = $organisation->name;
|
||||
my $postcode = $organisation->postcode;
|
||||
my $street_name = $organisation->street_name;
|
||||
my $town = $organisation->town;
|
||||
my $sector = $organisation->sector;
|
||||
return $c->render( json => {
|
||||
success => Mojo::JSON->true,
|
||||
town => $town,
|
||||
|
@ -73,6 +79,10 @@ sub post_account {
|
|||
street_name => $street_name,
|
||||
email => $email,
|
||||
postcode => $postcode,
|
||||
location => {
|
||||
latitude => (defined $organisation->latitude ? $organisation->latitude * 1 : undef),
|
||||
longitude => (defined $organisation->longitude ? $organisation->longitude * 1 : undef),
|
||||
},
|
||||
});
|
||||
} else {
|
||||
return $c->render(
|
||||
|
|
Reference in a new issue