Started with basic today stats
This commit is contained in:
parent
22abc4e464
commit
86955c0d1c
5 changed files with 150 additions and 0 deletions
|
@ -147,6 +147,7 @@ sub startup {
|
||||||
$api->post('/edit')->to('api-api#post_edit');
|
$api->post('/edit')->to('api-api#post_edit');
|
||||||
$api->post('/fetchuser')->to('api-api#post_fetchuser');
|
$api->post('/fetchuser')->to('api-api#post_fetchuser');
|
||||||
$api->post('/user-history')->to('api-user#post_user_history');
|
$api->post('/user-history')->to('api-user#post_user_history');
|
||||||
|
$api->post('/stats')->to('api-stats#post_today');
|
||||||
|
|
||||||
my $api_admin = $api->under('/')->to('api-admin#auth');
|
my $api_admin = $api->under('/')->to('api-admin#auth');
|
||||||
|
|
||||||
|
|
22
lib/Pear/LocalLoop/Controller/Api/Stats.pm
Normal file
22
lib/Pear/LocalLoop/Controller/Api/Stats.pm
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
package Pear::LocalLoop::Controller::Api::Stats;
|
||||||
|
use Mojo::Base 'Mojolicious::Controller';
|
||||||
|
|
||||||
|
sub post_today {
|
||||||
|
my $c = shift;
|
||||||
|
|
||||||
|
my $user = $c->stash->{api_user};
|
||||||
|
|
||||||
|
my $today_rs = $user->transactions->today_rs;
|
||||||
|
my $today_sum = $today_rs->get_column('value')->sum;
|
||||||
|
my $today_count = $today_rs->count;
|
||||||
|
|
||||||
|
return $c->render( json => {
|
||||||
|
success => Mojo::JSON->true,
|
||||||
|
today_sum => $today_sum,
|
||||||
|
today_count => $today_count,
|
||||||
|
today_avg => $today_sum / $today_count,
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
1;
|
29
lib/Pear/LocalLoop/Schema/ResultSet/Transaction.pm
Normal file
29
lib/Pear/LocalLoop/Schema/ResultSet/Transaction.pm
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
package Pear::LocalLoop::Schema::ResultSet::Transaction;
|
||||||
|
|
||||||
|
use strict;
|
||||||
|
use warnings;
|
||||||
|
|
||||||
|
use base 'DBIx::Class::ResultSet';
|
||||||
|
|
||||||
|
use DateTime;
|
||||||
|
|
||||||
|
sub today_rs {
|
||||||
|
my ( $self ) = @_;
|
||||||
|
|
||||||
|
my $dtf = $self->result_source->schema->storage->datetime_parser;
|
||||||
|
return $self->search({
|
||||||
|
submitted_at => {
|
||||||
|
-between => [
|
||||||
|
$dtf->format_datetime(DateTime->today()),
|
||||||
|
$dtf->format_datetime(DateTime->today()->add( days => 1 )),
|
||||||
|
],
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
sub today_for_user {
|
||||||
|
my ( $self, $user ) = @_;
|
||||||
|
return $self->search({ buyer_id => $user->id })->today_rs;
|
||||||
|
}
|
||||||
|
|
||||||
|
1;
|
|
@ -65,6 +65,16 @@ sub register_customer {
|
||||||
->json_is('/success', Mojo::JSON->true)->or($self->dump_error);
|
->json_is('/success', Mojo::JSON->true)->or($self->dump_error);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sub register_organisation {
|
||||||
|
my ( $self, $args ) = @_;
|
||||||
|
|
||||||
|
$args->{usertype} = 'organisation';
|
||||||
|
|
||||||
|
$self->framework->post_ok('/api/register' => json => $args)
|
||||||
|
->status_is(200)->or($self->dump_error)
|
||||||
|
->json_is('/success', Mojo::JSON->true)->or($self->dump_error);
|
||||||
|
}
|
||||||
|
|
||||||
sub login {
|
sub login {
|
||||||
my $self = shift;
|
my $self = shift;
|
||||||
my $args = shift;
|
my $args = shift;
|
||||||
|
@ -76,4 +86,19 @@ sub login {
|
||||||
return $self->framework->tx->res->json->{session_key};
|
return $self->framework->tx->res->json->{session_key};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sub gen_upload {
|
||||||
|
my ( $self, $args ) = @_;
|
||||||
|
|
||||||
|
my $file = {
|
||||||
|
content => '',
|
||||||
|
filename => 'text.jpg',
|
||||||
|
'Content-Type' => 'image/jpeg',
|
||||||
|
};
|
||||||
|
|
||||||
|
return {
|
||||||
|
json => Mojo::JSON::encode_json($args),
|
||||||
|
file => $file,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
1;
|
1;
|
||||||
|
|
73
t/api/stats.t
Normal file
73
t/api/stats.t
Normal file
|
@ -0,0 +1,73 @@
|
||||||
|
use Mojo::Base -strict;
|
||||||
|
|
||||||
|
use Test::More;
|
||||||
|
use Mojo::JSON;
|
||||||
|
use Test::Pear::LocalLoop;
|
||||||
|
use DateTime;
|
||||||
|
|
||||||
|
my $framework = Test::Pear::LocalLoop->new;
|
||||||
|
my $t = $framework->framework;
|
||||||
|
my $schema = $t->app->schema;
|
||||||
|
|
||||||
|
my $user = {
|
||||||
|
token => 'a',
|
||||||
|
full_name => 'Test User',
|
||||||
|
display_name => 'Test User',
|
||||||
|
email => 'test@example.com',
|
||||||
|
postcode => 'LA1 1AA',
|
||||||
|
password => 'abc123',
|
||||||
|
age_range => 1,
|
||||||
|
};
|
||||||
|
|
||||||
|
my $org = {
|
||||||
|
token => 'b',
|
||||||
|
email => 'test2@example.com',
|
||||||
|
name => 'Test Org',
|
||||||
|
street_name => 'Test Street',
|
||||||
|
town => 'Lancaster',
|
||||||
|
postcode => 'LA1 1AA',
|
||||||
|
password => 'abc123',
|
||||||
|
};
|
||||||
|
|
||||||
|
$schema->resultset('AccountToken')->create({ name => $user->{token} });
|
||||||
|
$schema->resultset('AccountToken')->create({ name => $org->{token} });
|
||||||
|
|
||||||
|
$framework->register_customer($user);
|
||||||
|
$framework->register_organisation($org);
|
||||||
|
|
||||||
|
my $org_result = $schema->resultset('Organisation')->find({ name => $org->{name} });
|
||||||
|
my $user_result = $schema->resultset('User')->find({ email => $user->{email} });
|
||||||
|
|
||||||
|
for ( 1 .. 10 ) {
|
||||||
|
$user_result->create_related( 'transactions', {
|
||||||
|
seller_id => $org_result->id,
|
||||||
|
value => $_,
|
||||||
|
proof_image => 'a',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
my $dtf = $schema->storage->datetime_parser;
|
||||||
|
is $user_result->transactions->search({
|
||||||
|
submitted_at => {
|
||||||
|
-between => [
|
||||||
|
$dtf->format_datetime(DateTime->today()),
|
||||||
|
$dtf->format_datetime(DateTime->today()->add( days => 1 )),
|
||||||
|
],
|
||||||
|
},
|
||||||
|
})->get_column('value')->sum, 55, 'Got correct sum';
|
||||||
|
is $user_result->transactions->today_rs->get_column('value')->sum, 55, 'Got correct sum through rs';
|
||||||
|
is $schema->resultset('Transaction')->today_for_user($user_result)->get_column('value')->sum, 55, 'Got correct sum through rs';
|
||||||
|
|
||||||
|
my $session_key = $framework->login({
|
||||||
|
email => $user->{email},
|
||||||
|
password => $user->{password},
|
||||||
|
});
|
||||||
|
|
||||||
|
$t->post_ok('/api/stats' => json => { session_key => $session_key } )
|
||||||
|
->status_is(200)
|
||||||
|
->json_is('/success', Mojo::JSON->true)
|
||||||
|
->json_is('/today_sum', 55)
|
||||||
|
->json_is('/today_count', 10)
|
||||||
|
->json_is('/today_avg', 5.5);
|
||||||
|
|
||||||
|
done_testing;
|
Reference in a new issue