From 1ffabb3e944d12ffe489c3bc6ad5b76a4558a8f1 Mon Sep 17 00:00:00 2001 From: Tom Bloor Date: Wed, 13 Sep 2017 16:07:23 +0100 Subject: [PATCH] Updated all endpoints to use new transaction value multiple --- lib/Pear/LocalLoop/Controller/Api/Stats.pm | 24 +++++++++++-------- .../LocalLoop/Controller/Api/Transactions.pm | 2 +- lib/Pear/LocalLoop/Controller/Api/Upload.pm | 2 +- .../Controller/Api/V1/Organisation/Graphs.pm | 4 ++-- .../Api/V1/Organisation/Snippets.pm | 6 +++++ t/api/stats.t | 14 +++++------ t/api/v1/organisation/graphs.t | 2 +- t/api/v1/organisation/snippets.t | 2 +- 8 files changed, 33 insertions(+), 23 deletions(-) diff --git a/lib/Pear/LocalLoop/Controller/Api/Stats.pm b/lib/Pear/LocalLoop/Controller/Api/Stats.pm index 3ee2617..ddb5152 100644 --- a/lib/Pear/LocalLoop/Controller/Api/Stats.pm +++ b/lib/Pear/LocalLoop/Controller/Api/Stats.pm @@ -18,23 +18,23 @@ sub post_index { my $user = $c->stash->{api_user}->entity; my $today_rs = $user->purchases->today_rs; - my $today_sum = $today_rs->get_column('value')->sum; + 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; + 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; + 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; + 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; + my $global_sum = $global_rs->get_column('value')->sum || 0; my $global_count = $global_rs->count; my $leaderboard_rs = $c->schema->resultset('Leaderboard'); @@ -44,15 +44,15 @@ sub post_index { return $c->render( json => { success => Mojo::JSON->true, - today_sum => $today_sum || 0, + today_sum => $today_sum / 100000, today_count => $today_count, - week_sum => $week_sum || 0, + week_sum => $week_sum / 100000, week_count => $week_count, - month_sum => $month_sum || 0, + month_sum => $month_sum / 100000, month_count => $month_count, - user_sum => $user_sum || 0, + user_sum => $user_sum / 100000, user_count => $user_count, - global_sum => $global_sum || 0, + global_sum => $global_sum / 100000, global_count => $global_count, user_position => defined $current_user_position ? $current_user_position->position : 0, }); @@ -91,6 +91,10 @@ sub post_leaderboards { my @leaderboard_array = $today_values->all; + if ( $validation->param('type') =~ /total$/ ) { + map { $_->{value} / 100000 } @leaderboard_array; + } + my $current_user_position = $today_values->find({ entity_id => $c->stash->{api_user}->entity->id }); return $c->render( json => { diff --git a/lib/Pear/LocalLoop/Controller/Api/Transactions.pm b/lib/Pear/LocalLoop/Controller/Api/Transactions.pm index d205b69..79e61ad 100644 --- a/lib/Pear/LocalLoop/Controller/Api/Transactions.pm +++ b/lib/Pear/LocalLoop/Controller/Api/Transactions.pm @@ -34,7 +34,7 @@ sub post_transaction_list_purchases { my @transaction_list = ( map {{ seller => $_->seller->name, - value => $_->value, + value => $_->value / 100000, purchase_time => $_->purchase_time, }} $transactions->all ); diff --git a/lib/Pear/LocalLoop/Controller/Api/Upload.pm b/lib/Pear/LocalLoop/Controller/Api/Upload.pm index 7b037d5..e7ae0d5 100644 --- a/lib/Pear/LocalLoop/Controller/Api/Upload.pm +++ b/lib/Pear/LocalLoop/Controller/Api/Upload.pm @@ -176,7 +176,7 @@ sub post_upload { 'sales', { buyer => $user->entity, - value => $transaction_value, + value => $transaction_value * 100000, ( defined $file ? ( proof_image => $file ) : () ), purchase_time => $c->format_db_datetime($purchase_time), } diff --git a/lib/Pear/LocalLoop/Controller/Api/V1/Organisation/Graphs.pm b/lib/Pear/LocalLoop/Controller/Api/V1/Organisation/Graphs.pm index c0cdd51..d1eddaf 100644 --- a/lib/Pear/LocalLoop/Controller/Api/V1/Organisation/Graphs.pm +++ b/lib/Pear/LocalLoop/Controller/Api/V1/Organisation/Graphs.pm @@ -139,7 +139,7 @@ sub _sales_last_duration { ->get_column('value') ->sum || 0 + 0; push @{ $data->{ labels } }, $start->day_name; - push @{ $data->{ data } }, $transactions; + push @{ $data->{ data } }, $transactions / 100000; $start->add( days => 1 ); } @@ -171,7 +171,7 @@ sub _purchases_last_duration { ->get_column('value') ->sum || 0 + 0; push @{ $data->{ labels } }, $start->day_name; - push @{ $data->{ data } }, $transactions; + push @{ $data->{ data } }, $transactions / 100000; $start->add( days => 1 ); } diff --git a/lib/Pear/LocalLoop/Controller/Api/V1/Organisation/Snippets.pm b/lib/Pear/LocalLoop/Controller/Api/V1/Organisation/Snippets.pm index 6bcd55c..07cd90a 100644 --- a/lib/Pear/LocalLoop/Controller/Api/V1/Organisation/Snippets.pm +++ b/lib/Pear/LocalLoop/Controller/Api/V1/Organisation/Snippets.pm @@ -28,26 +28,32 @@ sub index { my $today_sales = $entity->sales->search_between( $today, $now ); $data->{ today_sales_count } = $today_sales->count; $data->{ today_sales_total } = $today_sales->get_column('value')->sum || 0; + $data->{ today_sales_total } /= 100000; my $week_sales = $entity->sales->search_between( $week_ago, $today ); $data->{ this_week_sales_count } = $week_sales->count; $data->{ this_week_sales_total } = $week_sales->get_column('value')->sum || 0; + $data->{ this_week_sales_total } /= 100000; my $month_sales = $entity->sales->search_between( $month_ago, $today ); $data->{ this_month_sales_count } = $month_sales->count; $data->{ this_month_sales_total } = $month_sales->get_column('value')->sum || 0; + $data->{ this_month_sales_total } /= 100000; my $today_purchases = $entity->purchases->search_between( $today, $now ); $data->{ today_purchases_count } = $today_purchases->count; $data->{ today_purchases_total } = $today_purchases->get_column('value')->sum || 0; + $data->{ today_purchases_total } /= 100000; my $week_purchases = $entity->purchases->search_between( $week_ago, $today ); $data->{ this_week_purchases_count } = $week_purchases->count; $data->{ this_week_purchases_total } = $week_purchases->get_column('value')->sum || 0; + $data->{ this_week_purchases_total } /= 100000; my $month_purchases = $entity->purchases->search_between( $month_ago, $today ); $data->{ this_month_purchases_count } = $month_purchases->count; $data->{ this_month_purchases_total } = $month_purchases->get_column('value')->sum || 0; + $data->{ this_month_purchases_total } /= 100000; return $c->render( json => { diff --git a/t/api/stats.t b/t/api/stats.t index 8e8fbc1..0484664 100644 --- a/t/api/stats.t +++ b/t/api/stats.t @@ -43,7 +43,7 @@ $t->post_ok('/api/stats' => json => { session_key => $session_key } ) for ( 1 .. 10 ) { $user_result->create_related( 'purchases', { seller_id => $org_result->id, - value => $_, + value => $_ * 100000, proof_image => 'a', }); } @@ -51,7 +51,7 @@ for ( 1 .. 10 ) { for ( 11 .. 20 ) { $user_result->create_related( 'purchases', { seller_id => $org_result->id, - value => $_, + value => $_ * 100000, proof_image => 'a', purchase_time => $dtf->format_datetime(DateTime->today()->subtract( days => 5 )), }); @@ -60,7 +60,7 @@ for ( 11 .. 20 ) { for ( 21 .. 30 ) { $user_result->create_related( 'purchases', { seller_id => $org_result->id, - value => $_, + value => $_ * 100000, proof_image => 'a', purchase_time => $dtf->format_datetime(DateTime->today()->subtract( days => 25 )), }); @@ -69,7 +69,7 @@ for ( 21 .. 30 ) { for ( 31 .. 40 ) { $user_result->create_related( 'purchases', { seller_id => $org_result->id, - value => $_, + value => $_ * 100000, proof_image => 'a', purchase_time => $dtf->format_datetime(DateTime->today()->subtract( days => 50 )), }); @@ -78,7 +78,7 @@ for ( 31 .. 40 ) { for ( 41 .. 50 ) { $org_result->create_related( 'purchases', { seller_id => $org_result->id, - value => $_, + value => $_ * 100000, proof_image => 'a', purchase_time => $dtf->format_datetime(DateTime->today()->subtract( days => 50 )), }); @@ -91,8 +91,8 @@ is $user_result->purchases->search({ $dtf->format_datetime(DateTime->today()->add( days => 1 )), ], }, -})->get_column('value')->sum, 55, 'Got correct sum'; -is $user_result->purchases->today_rs->get_column('value')->sum, 55, 'Got correct sum through rs'; +})->get_column('value')->sum, 5500000, 'Got correct sum'; +is $user_result->purchases->today_rs->get_column('value')->sum, 5500000, 'Got correct sum through rs'; $t->post_ok('/api/stats' => json => { session_key => $session_key } ) ->status_is(200) diff --git a/t/api/v1/organisation/graphs.t b/t/api/v1/organisation/graphs.t index d373aa1..57b6cbb 100644 --- a/t/api/v1/organisation/graphs.t +++ b/t/api/v1/organisation/graphs.t @@ -115,7 +115,7 @@ sub create_random_transaction { $schema->resultset('Transaction')->create({ buyer => $buyer_result, seller => $seller_result, - value => 10, + value => 10 * 100000, proof_image => 'a', purchase_time => $time, }); diff --git a/t/api/v1/organisation/snippets.t b/t/api/v1/organisation/snippets.t index b7e127a..f4ee79c 100644 --- a/t/api/v1/organisation/snippets.t +++ b/t/api/v1/organisation/snippets.t @@ -73,7 +73,7 @@ sub create_random_transaction { $schema->resultset('Transaction')->create({ buyer => $buyer_result, seller => $seller_result, - value => 10, + value => 10 * 100000, proof_image => 'a', purchase_time => $time, });