From e6ce9cc81a9f2a8223cf43a603f4351ce0f37de9 Mon Sep 17 00:00:00 2001 From: Finn Date: Mon, 18 Dec 2017 12:56:45 +0000 Subject: [PATCH] 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)