Skip to content

Commit

Permalink
Merge pull request #70 from Pear-Trading/Release-v0.9.5
Browse files Browse the repository at this point in the history
Update for 0.9.5
  • Loading branch information
TBSliver authored Nov 14, 2017
2 parents 8f699c2 + af312ff commit 18d2237
Show file tree
Hide file tree
Showing 14 changed files with 405 additions and 11 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,19 @@

# Next Release

# v0.9.5

* Added leaderboard api for web-app with pagination
* Location is now updated on registration. Customers location is truncated to 2
decimal places based on their postcode.
* Location is also updated on changing a users postcode
* Distance is now calculated when a transaction is submitted

## Bug Fixes

* Updated Geo::UK::Postcode::Regex dependency to latest version. Fixes postcode
validation errors

# v0.9.4

* **Admin Feature:** Report of transaction data graphs
Expand Down
2 changes: 1 addition & 1 deletion cpanfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ requires 'Data::UUID';
requires 'Devel::Dwarn';
requires 'Mojo::JSON';
requires 'Email::Valid';
requires 'Geo::UK::Postcode::Regex';
requires 'Geo::UK::Postcode::Regex' => '0.017';
requires 'Authen::Passphrase::BlowfishCrypt';
requires 'Time::Fake';
requires 'Scalar::Util';
Expand Down
2 changes: 2 additions & 0 deletions lib/Pear/LocalLoop.pm
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ sub startup {
$self->plugin('Pear::LocalLoop::Plugin::BootstrapPagination', { bootstrap4 => 1 } );
$self->plugin('Pear::LocalLoop::Plugin::Validators');
$self->plugin('Pear::LocalLoop::Plugin::Datetime');
$self->plugin('Pear::LocalLoop::Plugin::Postcodes');
$self->plugin('Pear::LocalLoop::Plugin::TemplateHelpers');

$self->plugin('Authentication' => {
Expand Down Expand Up @@ -147,6 +148,7 @@ sub startup {
$api->post('/user-history')->to('api-user#post_user_history');
$api->post('/stats')->to('api-stats#post_index');
$api->post('/stats/leaderboard')->to('api-stats#post_leaderboards');
$api->post('/stats/leaderboard/paged')->to('api-stats#post_leaderboards_paged');
$api->post('/outgoing-transactions')->to('api-transactions#post_transaction_list_purchases');


Expand Down
9 changes: 9 additions & 0 deletions lib/Pear/LocalLoop/Controller/Api/Register.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down Expand Up @@ -80,6 +82,11 @@ sub post_register {

return $c->api_validation_error if $validation->has_error;

my $location = $c->get_location_from_postcode(
$validation->param('postcode'),
$usertype,
);

if ($usertype eq 'customer'){

$c->schema->txn_do( sub {
Expand All @@ -94,6 +101,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'),
Expand All @@ -118,6 +126,7 @@ sub post_register {
town => $validation->param('town'),
sector => $validation->param('sector'),
postcode => $validation->param('postcode'),
( defined $location ? ( %$location ) : () ),
},
user => {
email => $validation->param('email'),
Expand Down
65 changes: 65 additions & 0 deletions lib/Pear/LocalLoop/Controller/Api/Stats.pm
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,69 @@ sub post_leaderboards {
});
}

sub post_leaderboards_paged {
my $c = shift;

my $validation = $c->validation;
$validation->input( $c->stash->{api_json} );

my $leaderboard_rs = $c->schema->resultset('Leaderboard');

$validation->required('type')->in_resultset( 'type', $leaderboard_rs );
$validation->optional('page')->number;

return $c->api_validation_error if $validation->has_error;

my $page = 1;

my $today_board = $leaderboard_rs->get_latest( $validation->param('type') );

if ( !defined $validation->param('page') || $validation->param('page') < 1 ) {
my $user_position = $today_board->values->find({ entity_id => $c->stash->{api_user}->entity->id });
$page = int(defined $user_position ? $user_position->{position} : 0 / 10) + 1;
} else {
$page = $validation->param('page');
}

my $today_values = $today_board->values->search(
{},
{
page => $page,
rows => 10,
order_by => { -asc => 'me.position' },
columns => [
qw/
me.value
me.trend
me.position
/,
{ display_name => 'customer.display_name' },
],
join => { entity => 'customer' },
},
);
$today_values->result_class( 'DBIx::Class::ResultClass::HashRefInflator' );

my @leaderboard_array = $today_values->all;

if ( $validation->param('type') =~ /total$/ ) {
@leaderboard_array = (map {
{
%$_,
value => $_->{value} / 100000,
}
} @leaderboard_array);
}

my $current_user_position = $today_values->find({ entity_id => $c->stash->{api_user}->entity->id });

return $c->render( json => {
success => Mojo::JSON->true,
leaderboard => [ @leaderboard_array ],
user_position => defined $current_user_position ? $current_user_position->{position} : 0,
page => $page,
count => $today_values->pager->total_entries,
});
}

1;
2 changes: 2 additions & 0 deletions lib/Pear/LocalLoop/Controller/Api/Upload.pm
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ sub post_upload {
my $purchase_time = $c->parse_iso_datetime($validation->param('purchase_time') || '');
$purchase_time ||= DateTime->now();
my $file = defined $upload ? $c->store_file_from_upload( $upload ) : undef;
my $distance = $c->get_distance_from_coords( $user->entity->type_object, $organisation );

my $new_transaction = $organisation->entity->create_related(
'sales',
Expand All @@ -181,6 +182,7 @@ sub post_upload {
value => $transaction_value * 100000,
( defined $file ? ( proof_image => $file ) : () ),
purchase_time => $c->format_db_datetime($purchase_time),
distance => $distance,
}
);

Expand Down
33 changes: 25 additions & 8 deletions lib/Pear/LocalLoop/Controller/Api/User.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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(
Expand Down Expand Up @@ -135,13 +145,19 @@ sub post_account_update {

return $c->api_validation_error if $validation->has_error;

my $location = $c->get_location_from_postcode(
$validation->param('postcode'),
$user->type,
);

if ( $user->type eq 'customer' ){

$c->schema->txn_do( sub {
$user->entity->customer->update({
full_name => $validation->param('full_name'),
display_name => $validation->param('display_name'),
postcode => $validation->param('postcode'),
( defined $location ? ( %$location ) : ( latitude => undef, longitude => undef ) ),
});
$user->update({
email => $validation->param('email'),
Expand All @@ -159,6 +175,7 @@ sub post_account_update {
town => $validation->param('town'),
sector => $validation->param('sector'),
postcode => $validation->param('postcode'),
( defined $location ? ( %$location ) : ( latitude => undef, longitude => undef ) ),
});
$user->update({
email => $validation->param('email'),
Expand Down
58 changes: 58 additions & 0 deletions lib/Pear/LocalLoop/Plugin/Postcodes.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package Pear::LocalLoop::Plugin::Postcodes;
use Mojo::Base 'Mojolicious::Plugin';

use Geo::UK::Postcode::Regex;
use GIS::Distance;

sub register {
my ( $plugin, $app, $conf ) = @_;

$app->helper( get_location_from_postcode => sub {
my ( $c, $postcode, $usertype ) = @_;
my $postcode_obj = Geo::UK::Postcode::Regex->parse( $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
),
};
}
}
return $location;
});

$app->helper( get_distance_from_coords => sub {
my ( $c, $buyer, $seller ) = @_;

my $gis = GIS::Distance->new();

my $buyer_lat = $buyer->latitude;
my $buyer_long = $buyer->longitude;
my $seller_lat = $seller->latitude;
my $seller_long = $seller->longitude;

if ( $buyer_lat && $buyer_long
&& $seller_lat && $seller_long ) {
return int( $gis->distance( $buyer_lat, $buyer_long => $seller_lat, $seller_long )->meters );
}
return;
});
}

1;
2 changes: 2 additions & 0 deletions lib/Test/Pear/LocalLoop.pm
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,8 @@ sub install_fixtures {
{
entities => 'entities_id_seq',
organisations => 'organisations_id_seq',
users => 'users_id_seq',
customers => 'customers_id_seq',
}
);
}
Expand Down
4 changes: 2 additions & 2 deletions t/admin/reports/transactions.t
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ my $expected_hours = {};
sub increment_day {
my ( $value, $day, $distance ) = @_;
$value *= 100000;
$distance //= 0;
$distance //= 845;
$expected_days->{$day} = {
quantised => $day,
sum_value => ($expected_days->{$day}->{sum_value} || 0) + $value,
Expand All @@ -52,7 +52,7 @@ sub increment_day {
sub increment_hour {
my ( $value, $day, $distance ) = @_;
$value *= 100000;
$distance //= 0;
$distance //= 845;
$expected_hours->{$day} = {
quantised => $day,
sum_value => ($expected_hours->{$day}->{sum_value} || 0) + $value,
Expand Down
Loading

0 comments on commit 18d2237

Please sign in to comment.