Added organisation graph endpoint and tests
This commit is contained in:
parent
7ed2636978
commit
3ae8a43df4
5 changed files with 214 additions and 0 deletions
|
@ -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');
|
||||
|
|
21
lib/Pear/LocalLoop/Controller/Api/V1/Organisation.pm
Normal file
21
lib/Pear/LocalLoop/Controller/Api/V1/Organisation.pm
Normal file
|
@ -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;
|
87
lib/Pear/LocalLoop/Controller/Api/V1/Organisation/Graphs.pm
Normal file
87
lib/Pear/LocalLoop/Controller/Api/V1/Organisation/Graphs.pm
Normal file
|
@ -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;
|
|
@ -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 ) = @_;
|
||||
|
||||
|
|
90
t/api/v1/organisation/graphs.t
Normal file
90
t/api/v1/organisation/graphs.t
Normal file
|
@ -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;
|
Reference in a new issue