diff --git a/lib/Pear/LocalLoop/Controller/Api/V1/Organisation/Graphs.pm b/lib/Pear/LocalLoop/Controller/Api/V1/Organisation/Graphs.pm index 5b4eab5..2fb474a 100644 --- a/lib/Pear/LocalLoop/Controller/Api/V1/Organisation/Graphs.pm +++ b/lib/Pear/LocalLoop/Controller/Api/V1/Organisation/Graphs.pm @@ -22,6 +22,7 @@ sub index { sales_last_30_days purchases_last_7_days purchases_last_30_days + customers_range / ); return $c->api_validation_error if $validation->has_error; @@ -43,6 +44,40 @@ sub index { return $c->$graph_sub; } +sub graph_customers_range { + my $c = shift; + + my $validation = $c->validation; + $validation->input( $c->stash->{api_json} ); + $validation->required('start')->is_iso_date; + $validation->required('end')->is_iso_date; + + return $c->api_validation_error if $validation->has_error; + + my $entity = $c->stash->{api_user}->entity; + + my $data = { labels => [], data => [] }; + my $start = $c->parse_iso_date( $validation->param('start') ); + my $end = $c->parse_iso_date( $validation->param('end') ); + + while ( $start <= $end ) { + my $next_end = $start->clone->add( days => 1 ); + my $transactions = $entity->sales + ->search_between( $start, $next_end ) + ->count; + push @{ $data->{ labels } }, $c->format_iso_date( $start ); + push @{ $data->{ data } }, $transactions; + $start->add( days => 1 ); + } + + return $c->render( + json => { + success => Mojo::JSON->true, + graph => $data, + } + ); +} + sub graph_customers_last_7_days { my $c = shift; diff --git a/t/api/v1/organisation/graphs.t b/t/api/v1/organisation/graphs.t index 45531a6..d373aa1 100644 --- a/t/api/v1/organisation/graphs.t +++ b/t/api/v1/organisation/graphs.t @@ -78,6 +78,18 @@ $t->post_ok('/api/v1/organisation/graphs' => json => { data => [ 40, 20, 30, 30, 40, 10, 40, 30, 30, 20, 40, 20, 40, 20, 30, 30, 40, 10, 40, 30, 30, 20, 40, 20, 40, 20, 30, 30, 40, 10 ], }); +$t->post_ok('/api/v1/organisation/graphs' => json => { + session_key => $session_key, + graph => 'customers_range', + start => $start->clone->subtract( days => 8 )->ymd, + end => $start->clone->ymd, + }) + ->status_is(200)->or($framework->dump_error) + ->json_is('/graph', { + labels => [ map { $start->clone->subtract( days => $_ )->ymd } reverse ( 0 .. 8 ) ], + data => [ 2, 4, 2, 4, 2, 3, 3, 4, 1 ], + }); + $framework->logout( $session_key ); $session_key = $framework->login({