Merge pull request #38 from Pear-Trading/TBSliver/Org-Graphs

Initial Organisation graphs endpoints
This commit is contained in:
Tom Bloor 2017-08-29 16:55:11 +01:00 committed by GitHub
commit c7e0b675c1
26 changed files with 557 additions and 15 deletions

View file

@ -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');

View 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;

View 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;

View file

@ -5,6 +5,7 @@ use Test::More;
use File::Temp;
use Test::Mojo;
use DateTime::Format::Strptime;
use DBIx::Class::Fixtures;
has config => sub {
my $file = File::Temp->new;
@ -21,30 +22,47 @@ END
return $file;
};
has framework => sub {
has mojo => sub {
my $self = shift;
$ENV{MOJO_CONFIG} = $self->config->filename;
my $t = Test::Mojo->new('Pear::LocalLoop');
my $schema = $t->app->schema;
$schema->deploy;
$schema->resultset('Leaderboard')->populate([
[ qw/ name type / ],
[ 'Daily Total', 'daily_total' ],
[ 'Daily Count', 'daily_count' ],
[ 'Weekly Total', 'weekly_total' ],
[ 'Weekly Count', 'weekly_count' ],
[ 'Monthly Total', 'monthly_total' ],
[ 'Monthly Count', 'monthly_count' ],
[ 'All Time Total', 'all_time_total' ],
[ 'All Time Count', 'all_time_count' ],
]);
$t->app->schema->deploy;
return $t;
};
has _deployed => sub { 0 };
sub framework {
my $self = shift;
my $no_populate = shift;
my $t = $self->mojo;
my $schema = $t->app->schema;
unless ( $no_populate || $self->_deployed ) {
$schema->resultset('Leaderboard')->populate([
[ qw/ name type / ],
[ 'Daily Total', 'daily_total' ],
[ 'Daily Count', 'daily_count' ],
[ 'Weekly Total', 'weekly_total' ],
[ 'Weekly Count', 'weekly_count' ],
[ 'Monthly Total', 'monthly_total' ],
[ 'Monthly Count', 'monthly_count' ],
[ 'All Time Total', 'all_time_total' ],
[ 'All Time Count', 'all_time_count' ],
]);
}
$self->_deployed(1);
return $t;
};
has etc_dir => sub { die "etc dir not set" };
sub dump_error {
return sub {
my $self = shift;
@ -91,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 ) = @_;
@ -106,4 +134,19 @@ sub gen_upload {
};
}
sub install_fixtures {
my ( $self, $fixture_name ) = @_;
my $fixtures = DBIx::Class::Fixtures->new({
config_dir => File::Spec->catdir( $self->etc_dir, 'fixtures', 'config'),
});
my $t = $self->framework(1);
$fixtures->populate({
directory => File::Spec->catdir( $self->etc_dir, 'fixtures', 'data', $fixture_name ),
no_deploy => 1,
schema => $t->app->schema,
});
}
1;

View file

@ -0,0 +1,89 @@
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',
});
$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;

View file

@ -0,0 +1,97 @@
#! /usr/bin/env perl
use strict;
use warnings;
use DBIx::Class::Fixtures;
use FindBin qw/ $Bin /;
use lib "$Bin/../../../../lib";
use Pear::LocalLoop::Schema;
use DateTime;
my $fixtures = DBIx::Class::Fixtures->new({
config_dir => "$Bin",
});
my $schema = Pear::LocalLoop::Schema->connect('dbi:SQLite::memory:');
$schema->deploy;
$schema->resultset('Leaderboard')->populate([
[ qw/ name type / ],
[ 'Daily Total', 'daily_total' ],
[ 'Daily Count', 'daily_count' ],
[ 'Weekly Total', 'weekly_total' ],
[ 'Weekly Count', 'weekly_count' ],
[ 'Monthly Total', 'monthly_total' ],
[ 'Monthly Count', 'monthly_count' ],
[ 'All Time Total', 'all_time_total' ],
[ 'All Time Count', 'all_time_count' ],
]);
my $user1 = {
customer => {
full_name => 'Test User1',
display_name => 'Test User1',
postcode => 'LA1 1AA',
year_of_birth => 2006,
},
email => 'test1@example.com',
password => 'abc123',
};
my $user2 = {
customer => {
full_name => 'Test User2',
display_name => 'Test User2',
postcode => 'LA1 1AA',
year_of_birth => 2006,
},
email => 'test2@example.com',
password => 'abc123',
};
my $user3 = {
customer => {
full_name => 'Test User3',
display_name => 'Test User3',
postcode => 'LA1 1AA',
year_of_birth => 2006,
},
email => 'test3@example.com',
password => 'abc123',
};
my $user4 = {
customer => {
full_name => 'Test User4',
display_name => 'Test User4',
postcode => 'LA1 1AA',
year_of_birth => 2006,
},
email => 'test4@example.com',
password => 'abc123',
};
my $org = {
organisation => {
name => 'Test Org',
street_name => 'Test Street',
town => 'Lancaster',
postcode => 'LA1 1AA',
},
email => 'org@example.com',
password => 'abc123',
};
$schema->resultset('User')->create( $_ )
for ( $user1, $user2, $user3, $user4, $org );
my $data_set = 'users';
$fixtures->dump({
all => 1,
schema => $schema,
directory => "$Bin/../data/" . $data_set,
});

View file

@ -0,0 +1,65 @@
$VAR1 = {
'has_many' => {
'fetch' => 0
},
'belongs_to' => {
'fetch' => 0
},
'might_have' => {
'fetch' => 0
},
'sets' => [
{
'class' => 'Leaderboard',
'quantity' => 'all'
},
{
'class' => 'AccountToken',
'quantity' => 'all'
},
{
'class' => 'LeaderboardValue',
'quantity' => 'all'
},
{
'quantity' => 'all',
'class' => 'SessionToken'
},
{
'class' => 'Administrator',
'quantity' => 'all'
},
{
'class' => 'LeaderboardSet',
'quantity' => 'all'
},
{
'quantity' => 'all',
'class' => 'Feedback'
},
{
'class' => 'PendingOrganisation',
'quantity' => 'all'
},
{
'class' => 'User',
'quantity' => 'all'
},
{
'class' => 'Transaction',
'quantity' => 'all'
},
{
'quantity' => 'all',
'class' => 'Organisation'
},
{
'quantity' => 'all',
'class' => 'Customer'
},
{
'quantity' => 'all',
'class' => 'PendingTransaction'
}
]
};

View file

@ -0,0 +1 @@
1.001036

View file

@ -0,0 +1,7 @@
$HASH1 = {
display_name => 'Test User1',
full_name => 'Test User1',
id => 1,
postcode => 'LA1 1AA',
year_of_birth => 2006
};

View file

@ -0,0 +1,7 @@
$HASH1 = {
display_name => 'Test User2',
full_name => 'Test User2',
id => 2,
postcode => 'LA1 1AA',
year_of_birth => 2006
};

View file

@ -0,0 +1,7 @@
$HASH1 = {
display_name => 'Test User3',
full_name => 'Test User3',
id => 3,
postcode => 'LA1 1AA',
year_of_birth => 2006
};

View file

@ -0,0 +1,7 @@
$HASH1 = {
display_name => 'Test User4',
full_name => 'Test User4',
id => 4,
postcode => 'LA1 1AA',
year_of_birth => 2006
};

View file

@ -0,0 +1,5 @@
$HASH1 = {
id => 1,
name => 'Daily Total',
type => 'daily_total'
};

View file

@ -0,0 +1,5 @@
$HASH1 = {
id => 2,
name => 'Daily Count',
type => 'daily_count'
};

View file

@ -0,0 +1,5 @@
$HASH1 = {
id => 3,
name => 'Weekly Total',
type => 'weekly_total'
};

View file

@ -0,0 +1,5 @@
$HASH1 = {
id => 4,
name => 'Weekly Count',
type => 'weekly_count'
};

View file

@ -0,0 +1,5 @@
$HASH1 = {
id => 5,
name => 'Monthly Total',
type => 'monthly_total'
};

View file

@ -0,0 +1,5 @@
$HASH1 = {
id => 6,
name => 'Monthly Count',
type => 'monthly_count'
};

View file

@ -0,0 +1,5 @@
$HASH1 = {
id => 7,
name => 'All Time Total',
type => 'all_time_total'
};

View file

@ -0,0 +1,5 @@
$HASH1 = {
id => 8,
name => 'All Time Count',
type => 'all_time_count'
};

View file

@ -0,0 +1,10 @@
$HASH1 = {
id => 1,
name => 'Test Org',
postcode
=> 'LA1 1AA',
sector => undef,
street_name
=> 'Test Street',
town => 'Lancaster'
};

View file

@ -0,0 +1,11 @@
$HASH1 = {
customer_id
=> 1,
email => 'test1@example.com',
id => 1,
join_date
=> '2017-08-25 15:36:11',
organisation_id
=> undef,
password => '$2a$08$yCau6xDkRFZINg80iVvMh.M3JnLq2g.LJ7GMJL5KQvO45pDL.D/Rq'
};

View file

@ -0,0 +1,11 @@
$HASH1 = {
customer_id
=> 2,
email => 'test2@example.com',
id => 2,
join_date
=> '2017-08-25 15:36:11',
organisation_id
=> undef,
password => '$2a$08$BZAsbHSW8TN/jlL2DFGDoeKFRKzj2dQTBwatxb0p/maefEcWcziom'
};

View file

@ -0,0 +1,11 @@
$HASH1 = {
customer_id
=> 3,
email => 'test3@example.com',
id => 3,
join_date
=> '2017-08-25 15:36:11',
organisation_id
=> undef,
password => '$2a$08$xdkD/OA5izrOX9cvJDa4i..8T3YGmfVSo/G87wDRoGWnQmlC0gxOW'
};

View file

@ -0,0 +1,11 @@
$HASH1 = {
customer_id
=> 4,
email => 'test4@example.com',
id => 4,
join_date
=> '2017-08-25 15:36:11',
organisation_id
=> undef,
password => '$2a$08$svjdm.Syn3f062pDIN3/ROTUU7W16n0zJFm9/sm3x7pfbMLZFV.5G'
};

View file

@ -0,0 +1,11 @@
$HASH1 = {
customer_id
=> undef,
email => 'org@example.com',
id => 5,
join_date
=> '2017-08-25 15:36:11',
organisation_id
=> 1,
password => '$2a$08$3PkZF7D9FiOq8hgU7cJ6puY86Fkl34bQj6dZeJRXPU8hhJIMZtge2'
};