From e6ce9cc81a9f2a8223cf43a603f4351ce0f37de9 Mon Sep 17 00:00:00 2001 From: Finn Date: Mon, 18 Dec 2017 12:56:45 +0000 Subject: [PATCH 1/2] added support for mobile app back --- lib/Pear/LocalLoop.pm | 1 + lib/Pear/LocalLoop/Controller/Api/Stats.pm | 46 ++++++++++++++++++++++ t/api/stats.t | 2 +- 3 files changed, 48 insertions(+), 1 deletion(-) diff --git a/lib/Pear/LocalLoop.pm b/lib/Pear/LocalLoop.pm index bd5680b..6ab7fa8 100644 --- a/lib/Pear/LocalLoop.pm +++ b/lib/Pear/LocalLoop.pm @@ -148,6 +148,7 @@ sub startup { $api->post('/user/account')->to('api-user#post_account_update'); $api->post('/user-history')->to('api-user#post_user_history'); $api->post('/stats')->to('api-stats#post_index'); + $api->post('/stats/customer')->to('api-stats#post_customer'); $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'); diff --git a/lib/Pear/LocalLoop/Controller/Api/Stats.pm b/lib/Pear/LocalLoop/Controller/Api/Stats.pm index 4e9b4ff..e2b98e2 100644 --- a/lib/Pear/LocalLoop/Controller/Api/Stats.pm +++ b/lib/Pear/LocalLoop/Controller/Api/Stats.pm @@ -15,6 +15,52 @@ has error_messages => sub { sub post_index { my $c = shift; + my $user = $c->stash->{api_user}->entity; + + my $today_rs = $user->purchases->today_rs; + my $today_sum = $today_rs->get_column('value')->sum || 0; + my $today_count = $today_rs->count; + + my $week_rs = $user->purchases->week_rs; + my $week_sum = $week_rs->get_column('value')->sum || 0; + my $week_count = $week_rs->count; + + my $month_rs = $user->purchases->month_rs; + my $month_sum = $month_rs->get_column('value')->sum || 0; + my $month_count = $month_rs->count; + + my $user_rs = $user->purchases; + my $user_sum = $user_rs->get_column('value')->sum || 0; + my $user_count = $user_rs->count; + + my $global_rs = $c->schema->resultset('Transaction'); + my $global_sum = $global_rs->get_column('value')->sum || 0; + my $global_count = $global_rs->count; + + my $leaderboard_rs = $c->schema->resultset('Leaderboard'); + my $monthly_board = $leaderboard_rs->get_latest( 'monthly_total' ); + my $monthly_values = $monthly_board->values; + my $current_user_position = $monthly_values ? $monthly_values->find({ entity_id => $user->id }) : undef; + + return $c->render( json => { + success => Mojo::JSON->true, + today_sum => $today_sum / 100000, + today_count => $today_count, + week_sum => $week_sum / 100000, + week_count => $week_count, + month_sum => $month_sum / 100000, + month_count => $month_count, + user_sum => $user_sum / 100000, + user_count => $user_count, + global_sum => $global_sum / 100000, + global_count => $global_count, + user_position => defined $current_user_position ? $current_user_position->position : 0, + }); +} + +sub post_customer { + my $c = shift; + my $entity = $c->stash->{api_user}->entity; my $duration = DateTime::Duration->new( weeks => 7 ); diff --git a/t/api/stats.t b/t/api/stats.t index cad04ff..620f5d3 100644 --- a/t/api/stats.t +++ b/t/api/stats.t @@ -40,7 +40,7 @@ my $session_key = $framework->login({ #TODO be able to define start and end below in request -$t->post_ok('/api/stats' => json => { +$t->post_ok('/api/stats/customer' => json => { session_key => $session_key, }) ->status_is(200)->or($framework->dump_error) From 6f085ff54122d1fd6413f4b4d368a5caaa8ba2a8 Mon Sep 17 00:00:00 2001 From: Finn Date: Mon, 18 Dec 2017 14:05:51 +0000 Subject: [PATCH 2/2] Fixed stats test --- cpanfile | 7 +++++-- t/api/stats.t | 8 +++++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/cpanfile b/cpanfile index 1916f82..5ef4b1f 100644 --- a/cpanfile +++ b/cpanfile @@ -6,7 +6,6 @@ requires 'Mojo::JSON'; requires 'Email::Valid'; requires 'Geo::UK::Postcode::Regex' => '0.017'; requires 'Authen::Passphrase::BlowfishCrypt'; -requires 'Time::Fake'; requires 'Scalar::Util'; requires 'DBIx::Class'; requires 'DBIx::Class::PassphraseColumn'; @@ -25,6 +24,11 @@ requires 'GIS::Distance'; requires 'Text::CSV'; requires 'Try::Tiny'; +on 'test' => sub { + requires 'Test::More'; + requires 'Test::MockTime'; +}; + feature 'schema-graph', 'Draw diagrams of Schema' => sub { requires 'GraphViz'; requires 'SQL::Translator'; @@ -38,4 +42,3 @@ feature 'postgres', 'PostgreSQL Support' => sub { feature 'codepoint-open', 'Code Point Open manipulation' => sub { requires 'Geo::UK::Postcode::CodePointOpen'; }; - diff --git a/t/api/stats.t b/t/api/stats.t index 620f5d3..4fe849b 100644 --- a/t/api/stats.t +++ b/t/api/stats.t @@ -1,5 +1,9 @@ use Mojo::Base -strict; +BEGIN { + use Test::MockTime qw/ set_absolute_time /; +} + use FindBin qw/ $Bin /; use Test::More; @@ -15,6 +19,8 @@ $framework->install_fixtures('users'); my $t = $framework->framework; my $schema = $t->app->schema; +set_absolute_time('2017-01-01T00:00:00Z'); + my $start = DateTime->today->subtract( hours => 12 ); # create 40 days worth of data @@ -45,7 +51,7 @@ $t->post_ok('/api/stats/customer' => json => { }) ->status_is(200)->or($framework->dump_error) ->json_is('/weeks', { - purchases => [ 8, 21, 19, 22, 20, 20, 8 ], + purchases => [ 2, 21, 20, 21, 19, 22, 13 ], }) ->json_is('/sectors', { sectors => ['A'],