From f50c5e685c67cadff08e60d3a0b1bf90401cee34 Mon Sep 17 00:00:00 2001 From: Finn Date: Wed, 13 Dec 2017 14:29:17 +0000 Subject: [PATCH] working graphs on frontend --- .../LocalLoop/Controller/Api/V1/Customer/Graphs.pm | 14 +++++--------- t/api/v1/customer/graphs.t | 4 +++- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/lib/Pear/LocalLoop/Controller/Api/V1/Customer/Graphs.pm b/lib/Pear/LocalLoop/Controller/Api/V1/Customer/Graphs.pm index cfd97dd..e8814cd 100644 --- a/lib/Pear/LocalLoop/Controller/Api/V1/Customer/Graphs.pm +++ b/lib/Pear/LocalLoop/Controller/Api/V1/Customer/Graphs.pm @@ -83,8 +83,9 @@ sub graph_total_last_week { return shift->_purchases_total_duration( 7 ) } sub graph_total_last_month { return shift->_purchases_total_duration( 30 ) } sub _purchases_total_duration { - my ( $c, $duration ) = @_; + my ( $c, $day_duration ) = @_; + my $duration = DateTime::Duration->new( days => $day_duration ); my $entity = $c->stash->{api_user}->entity; my $data = { labels => [], data => [] }; @@ -96,8 +97,8 @@ sub _purchases_total_duration { my $transactions = $entity->purchases ->search_between( $start, $next_end ) ->get_column('value') - ->sum || 0 + 0; - push @{ $data->{ labels } }, $start->day_name; + ->sum || 0 * 1; + push @{ $data->{ labels } }, $c->format_iso_datetime( $start ); push @{ $data->{ data } }, $transactions / 100000; $start->add( days => 1 ); } @@ -154,14 +155,9 @@ sub _purchases_avg_spend_duration { } ); -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->{ labels } }, $c->format_iso_datetime( $quantised ); push @{ $data->{ data } }, ($_->get_column('average_value') || 0) / 100000; } diff --git a/t/api/v1/customer/graphs.t b/t/api/v1/customer/graphs.t index 0e20778..da9ec64 100644 --- a/t/api/v1/customer/graphs.t +++ b/t/api/v1/customer/graphs.t @@ -44,7 +44,9 @@ $t->post_ok('/api/v1/customer/graphs' => json => { }) ->status_is(200)->or($framework->dump_error) ->json_is('/graph', { - labels => [ map { $start->clone->subtract( days => $_ )->day_name } reverse ( 0 .. 6 ) ], + labels => [ map { $t->app->format_iso_datetime( + $start->clone->subtract( days => $_ )->subtract( hours => 12 ) + ) } reverse ( 0 .. 6 ) ], data => [ 10, 10, 10, 10, 10, 10, 10 ], });