From 3ae8a43df46d81a111cf1fa2e69eda7a59bc4ce3 Mon Sep 17 00:00:00 2001 From: Tom Bloor Date: Tue, 29 Aug 2017 12:42:27 +0100 Subject: [PATCH] Added organisation graph endpoint and tests --- lib/Pear/LocalLoop.pm | 6 ++ .../Controller/Api/V1/Organisation.pm | 21 +++++ .../Controller/Api/V1/Organisation/Graphs.pm | 87 ++++++++++++++++++ lib/Test/Pear/LocalLoop.pm | 10 +++ t/api/v1/organisation/graphs.t | 90 +++++++++++++++++++ 5 files changed, 214 insertions(+) create mode 100644 lib/Pear/LocalLoop/Controller/Api/V1/Organisation.pm create mode 100644 lib/Pear/LocalLoop/Controller/Api/V1/Organisation/Graphs.pm create mode 100644 t/api/v1/organisation/graphs.t diff --git a/lib/Pear/LocalLoop.pm b/lib/Pear/LocalLoop.pm index 3576c48..f06b1c0 100644 --- a/lib/Pear/LocalLoop.pm +++ b/lib/Pear/LocalLoop.pm @@ -155,6 +155,12 @@ sub startup { $api->post('/stats')->to('api-stats#post_index'); $api->post('/stats/leaderboard')->to('api-stats#post_leaderboards'); + my $api_v1 = $api->under('/v1'); + + my $api_v1_org = $api_v1->under('/organisation')->to('api-v1-organisation#auth'); + + $api_v1_org->post('/graphs')->to('api-v1-organisation-graphs#index'); + my $admin_routes = $r->under('/admin')->to('admin#under'); $admin_routes->get('/home')->to('admin#home'); diff --git a/lib/Pear/LocalLoop/Controller/Api/V1/Organisation.pm b/lib/Pear/LocalLoop/Controller/Api/V1/Organisation.pm new file mode 100644 index 0000000..29a3ccb --- /dev/null +++ b/lib/Pear/LocalLoop/Controller/Api/V1/Organisation.pm @@ -0,0 +1,21 @@ +package Pear::LocalLoop::Controller::Api::V1::Organisation; +use Mojo::Base 'Mojolicious::Controller'; + +sub auth { + my $c = shift; + + return 1 if $c->stash->{api_user}->type eq 'organisation'; + + $c->render( + json => { + success => Mojo::JSON->false, + message => 'Not an Organisation', + error => 'user_not_org', + }, + status => 403, + ); + + return 0; +} + +1; diff --git a/lib/Pear/LocalLoop/Controller/Api/V1/Organisation/Graphs.pm b/lib/Pear/LocalLoop/Controller/Api/V1/Organisation/Graphs.pm new file mode 100644 index 0000000..bb807ba --- /dev/null +++ b/lib/Pear/LocalLoop/Controller/Api/V1/Organisation/Graphs.pm @@ -0,0 +1,87 @@ +package Pear::LocalLoop::Controller::Api::V1::Organisation::Graphs; +use Mojo::Base 'Mojolicious::Controller'; + +has error_messages => sub { + return { + graph => { + required => { message => 'Must request graph type', status => 400 }, + in => { message => 'Unrecognised graph type', status => 400 }, + }, + }; +}; + +sub index { + my $c = shift; + + my $validation = $c->validation; + $validation->input( $c->stash->{api_json} ); + $validation->required('graph')->in( qw/ + customers_last_7_days + customers_last_30_days + / ); + + return $c->api_validation_error if $validation->has_error; + + my $graph_sub = "graph_" . $validation->param('graph'); + + unless ( $c->can($graph_sub) ) { + # Secondary catch in case a mistake has been made + return $c->render( + json => { + success => Mojo::JSON->false, + message => $c->error_messages->{graph}->{in}->{message}, + error => 'in', + }, + status => $c->error_messages->{graph}->{in}->{status}, + ); + } + + return $c->$graph_sub; +} + +sub graph_customers_last_7_days { + my $c = shift; + + 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 { + my ( $c, $duration ) = @_; + + my $org = $c->stash->{api_user}->organisation; + + my $data = { day => [], count => [] }; + + my $start = DateTime->today; + my $end = $start->clone->subtract_duration( $duration ); + + my $dtf = $c->schema->storage->datetime_parser; + + while ( $end < $start ) { + my $moving_end = $end->clone->add( days => 1 ); + my $transactions = $c->schema->resultset('Transaction')->search({ + seller_id => $org->id, + purchase_time => { '-between' => [ $dtf->format_datetime($end), $dtf->format_datetime($moving_end) ] }, + })->count; + push @{$data->{day}}, $end->day_name; + push @{$data->{count}}, $transactions; + $end->add( days => 1 ); + } + + return $c->render( + json => { + success => Mojo::JSON->true, + graph => $data, + } + ); +} + +1; diff --git a/lib/Test/Pear/LocalLoop.pm b/lib/Test/Pear/LocalLoop.pm index 2780c1d..5fb6fae 100644 --- a/lib/Test/Pear/LocalLoop.pm +++ b/lib/Test/Pear/LocalLoop.pm @@ -109,6 +109,16 @@ sub login { return $self->framework->tx->res->json->{session_key}; } +sub logout { + my $self = shift; + my $session_key = shift; + + $self->framework->post_ok('/api/logout' => json => { session_key => $session_key }) + ->status_is(200) + ->json_is('/success', Mojo::JSON->true) + ->json_like('/message', qr/Logged Out/); +} + sub gen_upload { my ( $self, $args ) = @_; diff --git a/t/api/v1/organisation/graphs.t b/t/api/v1/organisation/graphs.t new file mode 100644 index 0000000..b9d026a --- /dev/null +++ b/t/api/v1/organisation/graphs.t @@ -0,0 +1,90 @@ +use Mojo::Base -strict; + +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( 'test2@example.com', $trans_day ); + } + if ( $count % 3 ) { + create_random_transaction( 'test3@example.com', $trans_day ); + } + if ( $count % 4 ) { + create_random_transaction( 'test4@example.com', $trans_day ); + } +} + +my $session_key = $framework->login({ + email => 'org@example.com', + password => 'abc123', +}); +use Devel::Dwarn; + +$t->post_ok('/api/v1/organisation/graphs' => json => { + session_key => $session_key, + graph => 'customers_last_7_days', + }) + ->status_is(200)->or($framework->dump_error) + ->json_is('/graph', { + day => [ map { $start->clone->subtract( days => $_ )->day_name } reverse ( 0 .. 6 ) ], + count => [ 2, 4, 2, 3, 3, 4, 1 ], + }); + +$t->post_ok('/api/v1/organisation/graphs' => json => { + session_key => $session_key, + graph => 'customers_last_30_days', + }) + ->status_is(200)->or($framework->dump_error) + ->json_is('/graph', { + day => [ map { $start->clone->subtract( days => $_ )->day_name } reverse ( 0 .. 29 ) ], + count => [ 4, 2, 3, 3, 4, 1, 4, 3, 3, 2, 4, 2, 4, 2, 3, 3, 4, 1, 4, 3, 3, 2, 4, 2, 4, 2, 3, 3, 4, 1 ], + }); + +$framework->logout( $session_key ); + +$session_key = $framework->login({ + email => 'test1@example.com', + password => 'abc123', +}); + +$t->post_ok('/api/v1/organisation/graphs' => json => { + session_key => $session_key, + graph => 'customers_last_7_days', + }) + ->status_is(403) + ->json_is('/success', Mojo::JSON->false) + ->json_is('/error', 'user_not_org'); + +sub create_random_transaction { + my $buyer = shift; + my $time = shift; + + $schema->resultset('Transaction')->create({ + buyer => { email => $buyer }, + seller => { name => 'Test Org' }, + value => ( int( rand( 10000 ) ) / 100 ), + proof_image => 'a', + purchase_time => $time, + }); +} + +done_testing;