Merge pull request #53 from Pear-Trading/TBSliver/Org-Snippets
Added org snippets endpoint
This commit is contained in:
commit
c6679e1261
3 changed files with 144 additions and 0 deletions
|
@ -161,6 +161,7 @@ sub startup {
|
||||||
my $api_v1_org = $api_v1->under('/organisation')->to('api-v1-organisation#auth');
|
my $api_v1_org = $api_v1->under('/organisation')->to('api-v1-organisation#auth');
|
||||||
|
|
||||||
$api_v1_org->post('/graphs')->to('api-v1-organisation-graphs#index');
|
$api_v1_org->post('/graphs')->to('api-v1-organisation-graphs#index');
|
||||||
|
$api_v1_org->post('/snippets')->to('api-v1-organisation-snippets#index');
|
||||||
|
|
||||||
my $admin_routes = $r->under('/admin')->to('admin#under');
|
my $admin_routes = $r->under('/admin')->to('admin#under');
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,61 @@
|
||||||
|
package Pear::LocalLoop::Controller::Api::V1::Organisation::Snippets;
|
||||||
|
use Mojo::Base 'Mojolicious::Controller';
|
||||||
|
|
||||||
|
sub index {
|
||||||
|
my $c = shift;
|
||||||
|
|
||||||
|
my $entity = $c->stash->{api_user}->entity;
|
||||||
|
my $data = {
|
||||||
|
this_month_sales_count => 0,
|
||||||
|
this_month_sales_total => 0,
|
||||||
|
this_month_purchases_count => 0,
|
||||||
|
this_month_purchases_total => 0,
|
||||||
|
this_week_sales_count => 0,
|
||||||
|
this_week_sales_total => 0,
|
||||||
|
this_week_purchases_count => 0,
|
||||||
|
this_week_purchases_total => 0,
|
||||||
|
today_sales_count => 0,
|
||||||
|
today_sales_total => 0,
|
||||||
|
today_purchases_count => 0,
|
||||||
|
today_purchases_total => 0,
|
||||||
|
};
|
||||||
|
|
||||||
|
my $now = DateTime->now;
|
||||||
|
my $today = DateTime->today;
|
||||||
|
my $week_ago = $today->clone->subtract( days => 7 );
|
||||||
|
my $month_ago = $today->clone->subtract( days => 30 );
|
||||||
|
|
||||||
|
my $today_sales = $entity->sales->search_between( $today, $now );
|
||||||
|
$data->{ today_sales_count } = $today_sales->count;
|
||||||
|
$data->{ today_sales_total } = $today_sales->get_column('value')->sum || 0;
|
||||||
|
|
||||||
|
my $week_sales = $entity->sales->search_between( $week_ago, $today );
|
||||||
|
$data->{ this_week_sales_count } = $week_sales->count;
|
||||||
|
$data->{ this_week_sales_total } = $week_sales->get_column('value')->sum || 0;
|
||||||
|
|
||||||
|
my $month_sales = $entity->sales->search_between( $month_ago, $today );
|
||||||
|
$data->{ this_month_sales_count } = $month_sales->count;
|
||||||
|
$data->{ this_month_sales_total } = $month_sales->get_column('value')->sum || 0;
|
||||||
|
|
||||||
|
my $today_purchases = $entity->purchases->search_between( $today, $now );
|
||||||
|
$data->{ today_purchases_count } = $today_purchases->count;
|
||||||
|
$data->{ today_purchases_total } = $today_purchases->get_column('value')->sum || 0;
|
||||||
|
|
||||||
|
my $week_purchases = $entity->purchases->search_between( $week_ago, $today );
|
||||||
|
$data->{ this_week_purchases_count } = $week_purchases->count;
|
||||||
|
$data->{ this_week_purchases_total } = $week_purchases->get_column('value')->sum || 0;
|
||||||
|
|
||||||
|
my $month_purchases = $entity->purchases->search_between( $month_ago, $today );
|
||||||
|
$data->{ this_month_purchases_count } = $month_purchases->count;
|
||||||
|
$data->{ this_month_purchases_total } = $month_purchases->get_column('value')->sum || 0;
|
||||||
|
|
||||||
|
return $c->render(
|
||||||
|
json => {
|
||||||
|
success => Mojo::JSON->true,
|
||||||
|
snippets => $data,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
1;
|
82
t/api/v1/organisation/snippets.t
Normal file
82
t/api/v1/organisation/snippets.t
Normal file
|
@ -0,0 +1,82 @@
|
||||||
|
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/snippets' => json => {
|
||||||
|
session_key => $session_key,
|
||||||
|
})
|
||||||
|
->status_is(200)->or($framework->dump_error)
|
||||||
|
->json_is('/snippets', {
|
||||||
|
this_month_sales_count => 87,
|
||||||
|
this_month_sales_total => 870,
|
||||||
|
this_week_sales_count => 19,
|
||||||
|
this_week_sales_total => 190,
|
||||||
|
today_sales_count => 0,
|
||||||
|
today_sales_total => 0,
|
||||||
|
this_month_purchases_count => 0,
|
||||||
|
this_month_purchases_total => 0,
|
||||||
|
this_week_purchases_count => 0,
|
||||||
|
this_week_purchases_total => 0,
|
||||||
|
today_purchases_count => 0,
|
||||||
|
today_purchases_total => 0,
|
||||||
|
});
|
||||||
|
|
||||||
|
$framework->logout( $session_key );
|
||||||
|
|
||||||
|
$session_key = $framework->login({
|
||||||
|
email => 'test1@example.com',
|
||||||
|
password => 'abc123',
|
||||||
|
});
|
||||||
|
|
||||||
|
sub create_random_transaction {
|
||||||
|
my $buyer = shift;
|
||||||
|
my $time = shift;
|
||||||
|
|
||||||
|
my $buyer_result = $schema->resultset('User')->find({ email => $buyer })->entity;
|
||||||
|
my $seller_result = $schema->resultset('Organisation')->find({ name => 'Test Org' })->entity;
|
||||||
|
$schema->resultset('Transaction')->create({
|
||||||
|
buyer => $buyer_result,
|
||||||
|
seller => $seller_result,
|
||||||
|
value => 10,
|
||||||
|
proof_image => 'a',
|
||||||
|
purchase_time => $time,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
done_testing;
|
Reference in a new issue