From 5b745f666bb402fc1de5d170b06be3f4e1d94a66 Mon Sep 17 00:00:00 2001 From: Finn Date: Wed, 13 Dec 2017 13:33:29 +0000 Subject: [PATCH] made customer graphs work with passing test --- .../Controller/Api/V1/Customer/Graphs.pm | 166 +++++++++--------- t/api/v1/customer/graphs.t | 79 ++++++++- 2 files changed, 163 insertions(+), 82 deletions(-) diff --git a/lib/Pear/LocalLoop/Controller/Api/V1/Customer/Graphs.pm b/lib/Pear/LocalLoop/Controller/Api/V1/Customer/Graphs.pm index b669dc0..cfd97dd 100644 --- a/lib/Pear/LocalLoop/Controller/Api/V1/Customer/Graphs.pm +++ b/lib/Pear/LocalLoop/Controller/Api/V1/Customer/Graphs.pm @@ -16,14 +16,10 @@ sub index { my $validation = $c->validation; $validation->input( $c->stash->{api_json} ); $validation->required('graph')->in( qw/ - total_today - avg_spend_today total_last_week avg_spend_last_week total_last_month avg_spend_last_month - total_user - avg_spend_user / ); return $c->api_validation_error if $validation->has_error; @@ -45,6 +41,9 @@ sub index { return $c->$graph_sub; } + +# Replace with code for doughnut +=pod sub graph_customers_range { my $c = shift; @@ -78,22 +77,12 @@ sub graph_customers_range { } ); } +=cut -sub graph_customers_last_7_days { - my $c = shift; +sub graph_total_last_week { return shift->_purchases_total_duration( 7 ) } +sub graph_total_last_month { return shift->_purchases_total_duration( 30 ) } - my $duration = DateTime::Duration->new( days => 7 ); - return $c->_customers_last_duration( $duration ); -} - -sub graph_customers_last_30_days { - my $c = shift; - - my $duration = DateTime::Duration->new( days => 30 ); - return $c->_customers_last_duration( $duration ); -} - -sub _customers_last_duration { +sub _purchases_total_duration { my ( $c, $duration ) = @_; my $entity = $c->stash->{api_user}->entity; @@ -102,69 +91,6 @@ sub _customers_last_duration { my ( $start, $end ) = $c->_get_start_end_duration( $duration ); - while ( $start < $end ) { - my $next_end = $start->clone->add( days => 1 ); - my $transactions = $entity->sales - ->search_between( $start, $next_end ) - ->count; - push @{ $data->{ labels } }, $start->day_name; - push @{ $data->{ data } }, $transactions; - $start->add( days => 1 ); - } - - return $c->render( - json => { - success => Mojo::JSON->true, - graph => $data, - } - ); -} - -sub graph_sales_last_7_days { return shift->_sales_last_duration( 7 ) } -sub graph_sales_last_30_days { return shift->_sales_last_duration( 30 ) } - -sub _sales_last_duration { - my ( $c, $day_duration ) = @_; - - my $duration = DateTime::Duration->new( days => $day_duration ); - my $entity = $c->stash->{api_user}->entity; - - my $data = { labels => [], data => [] }; - - my ( $start, $end ) = $c->_get_start_end_duration( $duration ); - - while ( $start < $end ) { - my $next_end = $start->clone->add( days => 1 ); - my $transactions = $entity->sales - ->search_between( $start, $next_end ) - ->get_column('value') - ->sum || 0 + 0; - push @{ $data->{ labels } }, $start->day_name; - push @{ $data->{ data } }, $transactions / 100000; - $start->add( days => 1 ); - } - - return $c->render( - json => { - success => Mojo::JSON->true, - graph => $data, - } - ); -} - -sub graph_purchases_last_7_days { return shift->_purchases_last_duration( 7 ) } -sub graph_purchases_last_30_days { return shift->_purchases_last_duration( 30 ) } - -sub _purchases_last_duration { - my ( $c, $day_duration ) = @_; - - my $duration = DateTime::Duration->new( days => $day_duration ); - my $entity = $c->stash->{api_user}->entity; - - my $data = { labels => [], data => [] }; - - my ( $start, $end ) = $c->_get_start_end_duration( $duration ); - while ( $start < $end ) { my $next_end = $start->clone->add( days => 1 ); my $transactions = $entity->purchases @@ -184,6 +110,69 @@ sub _purchases_last_duration { ); } +sub graph_avg_spend_last_week { return shift->_purchases_avg_spend_duration( 7 ) } +sub graph_avg_spend_last_month { return shift->_purchases_avg_spend_duration( 30 ) } + +sub _purchases_avg_spend_duration { + my ( $c, $day_duration ) = @_; + + my $duration = DateTime::Duration->new( days => $day_duration ); + my $entity = $c->stash->{api_user}->entity; + + my $data = { labels => [], data => [] }; + + my ( $start, $end ) = $c->_get_start_end_duration( $duration ); + + my $dtf = $c->schema->storage->datetime_parser; + my $driver = $c->schema->storage->dbh->{Driver}->{Name}; + my $transaction_rs = $c->schema->resultset('ViewQuantisedTransaction' . $driver)->search( + { + purchase_time => { + -between => [ + $dtf->format_datetime($start), + $dtf->format_datetime($end), + ], + }, + }, + { + columns => [ + { + quantised => 'quantised_days', + count => \"COUNT(*)", + sum_value => $c->pg_or_sqlite( + 'SUM("me"."value")', + 'SUM("me"."value")', + ), + average_value => $c->pg_or_sqlite( + 'AVG("me"."value")', + 'AVG("me"."value")', + ), + } + ], + group_by => 'quantised_days', + order_by => { '-asc' => 'quantised_days' }, + } + ); + +my $data = { + labels => [], + data => [], +}; + + for ( $transaction_rs->all ) { + my $quantised = $c->db_datetime_parser->parse_datetime($_->get_column('quantised')); + push @{ $data->{ labels } }, $quantised->day_name; + push @{ $data->{ data } }, ($_->get_column('average_value') || 0) / 100000; + } + + return $c->render( + json => { + success => Mojo::JSON->true, + graph => $data, + } + ); +} + sub _get_start_end_duration { my ( $c, $duration ) = @_; my $end = DateTime->today; @@ -191,4 +180,19 @@ sub _get_start_end_duration { return ( $start, $end ); } +sub pg_or_sqlite { + my ( $c, $pg_sql, $sqlite_sql ) = @_; + + my $driver = $c->schema->storage->dbh->{Driver}->{Name}; + + if ( $driver eq 'Pg' ) { + return \$pg_sql; + } elsif ( $driver eq 'SQLite' ) { + return \$sqlite_sql; + } else { + $c->app->log->warn('Unknown Driver Used'); + return undef; + } +} + 1; diff --git a/t/api/v1/customer/graphs.t b/t/api/v1/customer/graphs.t index 6f9a3d9..0e20778 100644 --- a/t/api/v1/customer/graphs.t +++ b/t/api/v1/customer/graphs.t @@ -1,5 +1,82 @@ use Mojo::Base -strict; -use Test::More skip_all => 'Test needs making!'; +use FindBin qw/ $Bin /; + +use Test::More; +use Mojo::JSON; +use Test::Pear::LocalLoop; +use DateTime; + +my $framework = Test::Pear::LocalLoop->new( + etc_dir => "$Bin/../../../etc", +); +$framework->install_fixtures('users'); + +my $t = $framework->framework; +my $schema = $t->app->schema; + +my $start = DateTime->today->subtract( hours => 12 ); + +# create 30 days worth of data +for my $count ( 0 .. 29 ) { + my $trans_day = $start->clone->subtract( days => $count ); + + create_random_transaction( 'test1@example.com', $trans_day ); + if ( $count % 2 ) { + create_random_transaction( 'test1@example.com', $trans_day ); + } + if ( $count % 3 ) { + create_random_transaction( 'test1@example.com', $trans_day ); + } + if ( $count % 4 ) { + create_random_transaction( 'test1@example.com', $trans_day ); + } +} + +my $session_key = $framework->login({ + email => 'test1@example.com', + password => 'abc123', +}); + +$t->post_ok('/api/v1/customer/graphs' => json => { + session_key => $session_key, + graph => 'avg_spend_last_week', + }) + ->status_is(200)->or($framework->dump_error) + ->json_is('/graph', { + labels => [ map { $start->clone->subtract( days => $_ )->day_name } reverse ( 0 .. 6 ) ], + data => [ 10, 10, 10, 10, 10, 10, 10 ], + }); + +$framework->logout( $session_key ); + +$session_key = $framework->login({ + email => 'org@example.com', + password => 'abc123', +}); + +$t->post_ok('/api/v1/customer/graphs' => json => { + session_key => $session_key, + graph => 'avg_spend_last_week', + }) + ->status_is(403) + ->json_is('/success', Mojo::JSON->false) + ->json_is('/error', 'user_not_cust'); + + +sub create_random_transaction { + my $buyer = shift; + my $time = shift; + + my $buyer_result = $schema->resultset('User')->find({ email => $buyer })->entity; + my $seller_result = $schema->resultset('Organisation')->find({ name => 'Test Org' })->entity; + $schema->resultset('Transaction')->create({ + buyer => $buyer_result, + seller => $seller_result, + value => 10 * 100000, + proof_image => 'a', + purchase_time => $time, + }); +} done_testing;